配置Zabbix监控MySQL详细教程

一、Server端安装

机器 192.168.94.78

1、创建mysql实例

机器:192.168.94.78
目录:/data/mysql6001

2、建立系统用户

[root@192.168.94.144 zabbix_agent]# groupadd  -g 201 zabbix
[root@192.168.94.144 zabbix_agent]# useradd -g zabbix -u 201 -m zabbix
[root@192.168.94.144 zabbix_agent]# passwd zabbix

3、创建zabbix数据库

[root@192.168.94.78 zabbix]# mysql6001
mysql> create database zabbix; 
Query OK, 1 row affected (0.00 sec)

mysql>  grant all on zabbix.* to zabbix@'%' identified by 'zabbix'; 
Query OK, 0 rows affected (0.00 sec)

mysql>  flush privileges;  
Query OK, 0 rows affected (0.00 sec)

4、上传zabbix包到server端机器

zabbix-2.4.4.tar.gz
解压
tar zxvf zabbix-2.4.4.tar.gz 
目录:zabbix-2.4.4

5、导入SQL

cd /data/zabbix/zabbix-2.4.4/database/mysql/
注:有顺序的
[root@192.168.94.78 mysql]# mysql6001 zabbix < schema.sql 
[root@192.168.94.78 mysql]# mysql6001 zabbix < images.sql 
[root@192.168.94.78 mysql]# mysql6001 zabbix < data.sql 

6、导入网页

安装软件包

[root@192.168.94.78 mysql]# yum -y  install curl curl-devel net-snmp net-snmp-devel perl-DBI php-gd php-xml php-bcmath
[root@192.168.94.78 mysql]#yum -y install php 
[root@192.168.94.78 mysql]#yum -y install httpd

[root@192.168.94.78 html]# mkdir  /var/www/html/zabbix
[root@192.168.94.78 html]# cp -a /data/zabbix/zabbix-2.4.4/frontends/php/*     /var/www/html/zabbix/
[root@192.168.94.78 html]# chown -R  apache.apache /var/www/html/zabbix/

7、安装zabbix服务端

[root@192.168.94.78 zabbix-2.4.4]# cd /data/zabbix/zabbix-2.4.4
[root@192.168.94.78 zabbix-2.4.4]#  ./configure --prefix=/usr/local/zabbix --enable-server --enable-agent --enable-proxy --with-mysql=/usr/local/mysql5.5/bin/mysql_config --with-net-snmp --with-libcurl

第二步:make
第三步:make install

修改 vi /etc/services,添加下面几行

zabbix-agent    10050/tcp               #zabbix agent
zabbix-agent    10050/udp               #zabbix agent
zabbix-trapper  10051/tcp               #zabbix trapper
zabbix-trapper  10051/udp               #zabbix trapper

8、修改zabbix的配置:

[root@192.168.94.78 zabbix-2.4.4]# vi /usr/local/zabbix/etc/zabbix_server.conf
[root@192.168.94.78 zabbix-2.4.4]# cat /usr/local/zabbix/etc/zabbix_server.conf|grep -v ^# | grep -v ^$
LogFile=/data/zabbix/zabbix_server.log
PidFile=/data/zabbix/zabbix_server.pid
DBHost=192.168.94.78
DBName=zabbix
DBUser=zabbix
DBPassword=zabbix
DBSocket=/tmp/mysql.sock
DBPort=6001
StartPollers=20
StartTrappers=15
 StartPingers=10
User=zabbix

[root@localhost ~]# vi /etc/init.d/zabbix_server

BASEDIR=/usr/local/zabbix   ##########修改这行

[root@localhost ~]# vi /etc/init.d/zabbix_agentd

BASEDIR=/usr/local/zabbix   ##########修改这行

9、修改httpd配置

[root@192.168.94.78 zabbix-2.4.4]# vi /etc/httpd/conf/httpd.conf 
ServerName 127.0.0.1

 DocumentRoot  "/var/www/html"
 ServerName 192.168.94.78

10、开启httpd

[root@192.168.94.78 core]# service httpd restart
Stopping httpd: [  OK  ]
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
[  OK  ]

[root@192.168.94.78 core]# lsof -i:80
COMMAND   PID   USER   FD   TYPE     DEVICE SIZE/OFF NODE NAME
httpd   23923   root    4u  IPv4 2582967064      0t0  TCP *:80(LISTEN)
httpd   23925 apache    4u  IPv4 2582967064      0t0  TCP *:80 (LISTEN)
httpd   23926 apache    4u  IPv4 2582967064      0t0  TCP *:80 (LISTEN)
httpd   23927 apache    4u  IPv4 2582967064      0t0  TCP *:80 (LISTEN)
httpd   23928 apache    4u  IPv4 2582967064      0t0  TCP *:80 (LISTEN)
httpd   23929 apache    4u  IPv4 2582967064      0t0  TCP *:80 (LISTEN)
httpd   23930 apache    4u  IPv4 2582967064      0t0  TCP *:80 (LISTEN)
httpd   23931 apache    4u  IPv4 2582967064      0t0  TCP *:80 (LISTEN)
httpd   23932 apache    4u  IPv4 2582967064      0t0  TCP *:80 (LISTEN)

11、做几个软连接

[root@localhost ~]# ln -s /usr/local/zabbix/bin/* /usr/bin/
[root@localhost ~]# ln -s /usr/local/zabbix/sbin/* /usr/sbin/

[root@localhost ~]# cd  /data/zabbix/zabbix-2.4.4/misc/init.d/fedora/core

[root@localhost ~]# cp * /etc/init.d/

12、开启zabbix server端

[root@192.168.94.78 subsys]# chown zabbix.zabbix   /var/lock/subsys/ -R
[root@192.168.94.78 subsys]# chown zabbix.zabbix   /usr/local/zabbix  -R
[root@192.168.94.78 subsys]# chown zabbix.zabbix  /data/zabbix/  -R

[root@192.168.94.78 core]# /etc/init.d/zabbix_agentd start
Starting zabbix_agentd:  zabbix_agentd [24613]: user zabbix does not exist
zabbix_agentd [24613]: cannot run as root!
[FAILED]
[root@192.168.94.78 core]# /etc/init.d/zabbix_server start
Starting zabbix_server:  zabbix_server [24821]: user zabbix does not exist
zabbix_server [24821]: cannot run as root!
[FAILED]

处理失败:
[root@192.168.94.78 home]#  vi /etc/passwd 
改成:
zabbix:x:505:498::/home/zabbix:/bin/bash
[root@192.168.94.78 home]# su - zabbix

===============================================
[zabbix_linux@bjs-dbmypacket3301 ~]$  /etc/init.d/zabbix_agentd restart
Shutting down zabbix_agentd: [  OK  ]
Starting zabbix_agentd:  [  OK  ]

[zabbix_linux@bjs-dbmypacket3301 ~]$  /etc/init.d/zabbix_server stop
Shutting down zabbix_server: [  OK  ]
[zabbix_linux@bjs-dbmypacket3301 ~]$  /etc/init.d/zabbix_server start
Starting zabbix_server:  [  OK  ]

13、页面设置zabbix

http://192.168.94.78/zabbix/setup.php

zabbix PHP mbstring.func_overload 报错

修复方法:

vi /etc/php.ini
mbstring.func_overload=off
lways_populate_raw_post_data=1
 重启httpd与zabbix
[root@192.168.94.78 html]# service httpd restart
[zabbix@bjs-dbmypacket3301 ~]$ /etc/init.d/zabbix_server restart

二、客户端安装

客户端机器

192.168.94.144
192.168.94.137

1、安装agent

[root@192.168.94.78 zabbix]# scp zabbix-2.4.4.tar.gz 192.168.94.144:/data/zabbix_agent/

[root@192.168.94.144 zabbix_agent]# groupadd  -g 201 zabbix
[root@192.168.94.144 zabbix_agent]# useradd -g zabbix -u 201 -m zabbix
[root@192.168.94.144 zabbix_agent]# passwd zabbix
Changing password for user zabbix.
New password: 
BAD PASSWORD: it is based on a dictionary word
BAD PASSWORD: is too simple
Retype new password: 
passwd: all authentication tokens updated successfully.
 [root@192.168.94.144 zabbix-2.4.4]# cd /data/zabbix_agent/zabbix-2.4.4
 [root@192.168.94.144 zabbix-2.4.4]#  ./configure --prefix=/usr/local/zabbix  --enable-agent --enable-proxy --with-mysql=/usr/local/mysql5.5/bin/mysql_config 

[root@192.168.94.144 zabbix-2.4.4]# make && make install 

2、拷贝/创建快捷

[root@192.168.94.144 zabbix-2.4.4]# pwd
/data/zabbix_agent/zabbix-2.4.4
[root@192.168.94.144 zabbix-2.4.4]# cp misc/init.d/tru64/zabbix_agentd   /etc/init.d/
[root@192.168.94.144 zabbix-2.4.4]# chmod +x /etc/init.d/zabbix_agentd
3、修改配置文件
[root@192.168.94.144 zabbix-2.4.4]# vi  /usr/local/zabbix/etc/zabbix_agentd.conf

[root@192.168.94.144 zabbix-2.4.4]# cat /usr/local/zabbix/etc/zabbix_agentd.conf | grep -v ^# | grep -v ^$
LogFile=/data/zabbix_agent/zabbix_agentd.log
UnsafeUserParameters=1                   
Include= /usr/local/zabbix/etc/zabbix_agentd.conf.d/
Server=192.168.94.78      #server端IP
ServerActive=192.168.94.78   #server端IP
Hostname=192.168.94.144     #客户端IP

4、开启zabbix 的agent

[root@192.168.94.133 zabbix-2.4.4]# chown zabbix.zabbix /data/zabbix_agent/ -R
[root@192.168.94.133 zabbix-2.4.4]# chown zabbix.zabbix /usr/local/zabbix/  -R
[root@192.168.94.133 zabbix-2.4.4]# chown zabbix.zabbix /soft/mysqlmonitor/port_6190 -R


[root@192.168.94.144 tmp]# /usr/local/zabbix/sbin/zabbix_agentd -c /usr/local/zabbix/etc/zabbix_agentd.conf
[root@192.168.94.144 tmp]# ps -ef|grep zabbix
zabbix   53114     1  0 16:14 ?        00:00:00 /usr/local/zabbix/sbin/zabbix_agentd -c /usr/local/zabbix/etc/zabbix_agentd.conf
zabbix   53115 53114  0 16:14 ?        00:00:00 /usr/local/zabbix/sbin/zabbix_agentd: collector [idle 1 sec]                    
zabbix   53116 53114  0 16:14 ?        00:00:00 /usr/local/zabbix/sbin/zabbix_agentd: listener #1 [waiting for connection]      
zabbix   53117 53114  0 16:14 ?        00:00:00 /usr/local/zabbix/sbin/zabbix_agentd: listener #2 [waiting for connection]      
zabbix   53118 53114  0 16:14 ?        00:00:00 /usr/local/zabbix/sbin/zabbix_agentd: listener #3 [waiting for connection]      
zabbix   53119 53114  0 16:14 ?        00:00:00 /usr/local/zabbix/sbin/zabbix_agentd: active checks #1 [idle 1 sec]             
root     53362 62129  0 16:14 pts/0    00:00:00 grep zabbix
[root@192.168.94.144 tmp]# lsof -i:1005COMMAND     PID   USER   FD   TYPE     DEVICE SIZE/OFF NODE NAME
zabbix_ag 53114 zabbix    4u  IPv4 2301997410      0t0  TCP *:zabbix-agent (LISTEN)
zabbix_ag 53115 zabbix    4u  IPv4 2301997410      0t0  TCP *:zabbix-agent (LISTEN)
zabbix_ag 53116 zabbix    4u  IPv4 2301997410      0t0  TCP *:zabbix-agent (LISTEN)
zabbix_ag 53117 zabbix    4u  IPv4 2301997410      0t0  TCP *:zabbix-agent (LISTEN)
zabbix_ag 53118 zabbix    4u  IPv4 2301997410      0t0  TCP *:zabbix-agent (LISTEN)
zabbix_ag 53119 zabbix    4u  IPv4 2301997410      0t0  TCP *:zabbix-agent (LISTEN)

5、界面添加host

此时可以在界面上面添加host进行监控测试了,因后面添加自定义监控,此处忽略

三、自定义监控配置

1、准备检查脚本

准备检查mysql状态的脚本

[root@192.168.94.144 ~]# cd /soft/port_6190/
[root@192.168.94.144 port_6190]# ./mysql_status.pl|grep com_writes
com_writes=0

[root@192.168.94.144 port_6190]# /soft/port_6190/mysql_status.pl |grep com_writes|awk -F '=' '{print $2}'
0

注:此处可以自定义脚本,返回key=value 即可,或者使用percona的模板,此处以自定义监控脚本为例来进行配置监控

2、修改agent的配置文件

[root@192.168.94.144 port_6190]# vi /usr/local/zabbix/etc/zabbix_agentd.conf

[root@192.168.94.144 port_6190]# cat /usr/local/zabbix/etc/zabbix_agentd.conf|grep -v '^#'|grep -v '^$'
LogFile=/data/zabbix_agent/zabbix_agentd.log
UnsafeUserParameters=1                   
Include= /usr/local/zabbix/etc/zabbix_agentd.conf.d/
Server=192.168.94.78
ServerActive=192.168.94.78
Hostname=test
 UserParameter=com_writes,/soft/port_6190/mysql_status.pl |grep com_writes|awk -F '=' '{print $2}'
 UserParameter=com_reads,/soft/port_6190/mysql_status.pl  |grep com_reads |awk -F '=' '{print $2}'
 UserParameter=com_update,/soft/port_6190/mysql_status.pl |grep com_update|awk -F '=' '{print $2}'
 UserParameter=com_insert,/soft/port_6190/mysql_status.pl |grep com_insert|awk -F '=' '{print $2}'
 UserParameter=com_delete,/soft/port_6190/mysql_status.pl |grep com_delete|awk -F '=' '{print $2}'

注:com_delet 为键值名,后面为键值的值

重启agent

/etc/init.d/zabbix_agentd stop
/usr/local/zabbix/sbin/zabbix_agentd -c /usr/local/zabbix/etc/zabbix_agentd.conf

3、server端检查

检查

[root@192.168.94.78 bin]# pwd
/usr/local/zabbix/bin

[root@192.168.94.78 bin]# ./zabbix_get -s 192.168.94.144 -k com_writes
0

4、server端进行界面配

4.1添加host

4.2 添加模版

4.3 创建应用集

4.4创建项目

4.5 创建图形

5、查看监控图

6、其他过程

标签:MySQLZabbix监控 发布于:2019-11-14 14:12:47