內網有一臺mysql服務器,版本是5.7.14mysql
關於這個版本安裝,有興趣能夠參考sql
http://xiao987334176.blog.51cto.com/2202382/1783509shell
zabbix自帶有一個模板Template App MySQL,用來監控mysql的vim
可是不能直接使用,不然會由於沒有Key,致使獲取不到數據。安全
下面介紹詳細步驟。服務器
首先在mysql服務器安裝zabbix-agent,請參考ide
http://xiao987334176.blog.51cto.com/2202382/1768281測試
最下面一部分。ui
###################################################################################spa
講解一個比較重要的問題。
在shell裏面,直接運行mysql相關命令,指定密碼時,就會有一個提示
好比: mysql -u zabbix -p123456 運行以後
Warning: Using a password on the command line interface can be insecure.
這個提示在mysql 5.5以後會出現。
並且,這個提示,會被zabbix-servre捕捉到,在zabbix-server.log日誌中會出現
24123:20160826:101433.609 error reason for "110:mysql.status[Bytes_sent]" changed: Received value [mysqladmin: [Warning] Using a password on the command line interface can be insecure.8991074] is not suitable for value type [Numeric (float)]
提示參數不符合
在zabbix-server服務器上面,使用zabbix-get命令時
[root@localhost ~]# /usr/local/zabbix/bin/zabbix_get -s 192.168.1.110 -p10050 -k mysql.status[Uptime]
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
57577
###################################################################################
那麼爲了解決這個問題,必須無密碼登陸才能夠,爲了安全起見,限定只能在localhost登陸
先建立一個zabbix用戶(建立用戶,密碼不能爲空,不然報錯)
grant all PRIVILEGES on *.* to zabbix@'localhost' identified by '123456';
設置密碼爲root
update mysql.user set authentication_string=password('root') where user='zabbix' and Host = 'localhost';
刷新權限
flush privileges;
退出
exit;
測試無密碼登陸
[root@localhost opt]# mysql -u zabbix
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 930
Server version: 5.7.14 Source distribution
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> exit
Bye
發現一個很奇怪的現象,把密碼設置和root同樣,就能夠無密碼登陸了
mysql> select Host,User,authentication_string from mysql.user;
+-----------+-----------+-------------------------------------------+
| Host | User | authentication_string |
+-----------+-----------+-------------------------------------------+
| localhost | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| localhost | mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| localhost | zabbix | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
+-----------+-----------+-------------------------------------------+
3 rows in set (0.00 sec)
進入Mysql服務器的zabbix-agent目錄
cd /usr/local/zabbix-agent/
mkdir alertscripts
編輯腳本
vim checkmysql.sh
內容以下:
#!/bin/sh
#MYSQL_SOCK="/data/3306/mysqld.sock"
#MYSQL_PWD=`cat /usr/local/zabbix-agent/alertscripts/.mysqlpassword`
ARGS=1
if [ $# -ne "$ARGS" ];then
echo "Please input one arguement:"
fi
case $1 in
Uptime)
result=`mysqladmin -uzabbix status|cut -f2 -d":"|cut -f1 -d"T"`
echo $result
;;
Com_update)
result=`mysqladmin -uzabbix extended-status |grep -w "Com_update"|cut -d"|" -f3`
echo $result
;;
Slow_queries)
result=`mysqladmin -uzabbix status |cut -f5 -d":"|cut -f1 -d"O"`
echo $result
;;
Com_select)
result=`mysqladmin -uzabbix extended-status |grep -w "Com_select"|cut -d"|" -f3`
echo $result
;;
Com_rollback)
result=`mysqladmin -uzabbix extended-status |grep -w "Com_rollback"|cut -d"|" -f3`
echo $result
;;
Questions)
result=`mysqladmin -uzabbix status|cut -f4 -d":"|cut -f1 -d"S"`
echo $result
;;
Com_insert)
result=`mysqladmin -uzabbix extended-status |grep -w "Com_insert"|cut -d"|" -f3`
echo $result
;;
Com_delete)
result=`mysqladmin -uzabbix extended-status |grep -w "Com_delete"|cut -d"|" -f3`
echo $result
;;
Com_commit)
result=`mysqladmin -uzabbix extended-status |grep -w "Com_commit"|cut -d"|" -f3`
echo $result
;;
Bytes_sent)
result=`mysqladmin -uzabbix extended-status |grep -w "Bytes_sent" |cut -d"|" -f3`
echo $result
;;
Bytes_received)
result=`mysqladmin -uzabbix extended-status |grep -w "Bytes_received" |cut -d"|" -f3`
echo $result
;;
Com_begin)
result=`mysqladmin -uzabbix extended-status |grep -w "Com_begin"|cut -d"|" -f3`
echo $result
;;
*)
echo "Usage:$0(Uptime|Com_update|Slow_queries|Com_select|Com_rollback|Questions)"
;;
esac
設置權限
chmod 755 checkmysql.sh
chown zabbix:zabbix -R /usr/local/zabbix-agent/
編輯配置文件
vim /usr/local/zabbix-agent/etc/zabbix_agentd.conf
增長紫色部分,內容以下:
LogFile=/usr/local/zabbix-agent/logs/zabbix_agentd.log
###zabbix 服務端地址
Server=192.168.1.109
##agent服務監聽地址,也就是本機地址
#ListenIP=192.168.1.105
###ServerActive=192.168.1.110
##zabbix-server端主機地址(zabbix server)
Hostname=zabbix server
UserParameter=mysql.version,mysql -V
UserParameter=mysql.status[*],/usr/local/zabbix-agent/alertscripts/checkmysql.sh $1
UserParameter=mysql.ping,mysqladmin -uzabbix ping | grep -c alive
重啓zabbix_agentd
/etc/init.d/zabbix_agentd restart
在zabbix-server服務器上面檢查mysql
[root@localhost alertscripts]# /usr/local/zabbix/bin/zabbix_get -s 192.168.1.110 -p10050 -k mysql.status[Uptime]
9479
[root@localhost alertscripts]#
若是能直接返回數字,沒有多餘的字符,就說明成功了。
在zabbix-server添加一臺主機
添加模板的時候,選擇Template App MySQL和Template OS Linux
等待10分鐘,圖像就會出來了。