MySQL監控--zabbix

o煙雨瀟瀟o發表於2017-07-31
歡迎轉載,請註明出處、作者,謝謝
作者:劉春雷
歡迎評論,感謝~                                    
------------------------------------------------------                                                                     
                                                       
概要:
如果你想搭建一套mysql監控
如果你想學習zabbix
那麼此篇文章適合你
                                       MySQL監控--zabbix
                                                                                                 --劉春雷  20160807
步驟簡略:
1、Server端安裝
2、客戶端安裝
3、zabbix介面設定

一、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
<VirtualHost *:80>
 DocumentRoot  "/var/www/html"
 ServerName 192.168.94.78
</VirtualHost>

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)


10、做幾個軟連線

[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/




11.開啟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  ]


12、頁面設定zabbix






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、其他過程


 



來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28823725/viewspace-2142822/,如需轉載,請註明出處,否則將追究法律責任。

相關文章