The results show map
Let us look at the results of FIG, automatic discovery ZABBIX second monitor Mysql add, delete, change, by monitoring the flow rate into and out automatic discovery Mysql results is as follows:
Mysql to Zabbix monitoring authority
First, create a user monitoring, usage on a relatively small authority, safer. sql follows
grant usage on *.* to 'monitor'@'127.0.0.1' identified by 'shijiangepwd';
flush privileges;
Test monitoring whether the user entered into force, the test results are as follows
mysql -umonitor -h 127.0.0.1 -pshijiangepwd -A
mysql> show global status;
+-----------------------------------------------+-------------+
| Variable_name | Value |
+-----------------------------------------------+-------------+
| Aborted_clients | 0 |
| Aborted_connects | 0 |
| Binlog_cache_disk_use | 0 |
| Binlog_cache_use | 0 |
| Binlog_stmt_cache_disk_use | 0 |
| Binlog_stmt_cache_use | 0 |
Shell scripts written in monitoring
Script uses Mysql show global status to monitor status information mysql_check.sh:
port=$1
key=$2
mysql -umonitor -pshijiangepwd -h 127.0.0.1 -P${port} -e "show global status" |grep "${key}\s" |awk '{print $2}'
Results are as follows, Zabbix monitoring only need to return a number to
[root@test ~]# sh mysql_check.sh 3306 Bytes_received
439
[root@test ~]# sh mysql_check.sh 3306 Bytes_sent
11935
Mysql monitoring ports use auto-discovery
Mysql port using the auto-discovered script mysql_discovery.py
# -*- coding: utf-8 -*-
try:
import json
except:
import simplejson as json
import commands
(status, output) = commands.getstatusoutput(""" sudo netstat -tlnp|grep mysql|awk '{print $4}'|awk -F':' '{print $2}'|sort -u """)
outputs = output.split('\n')
result = []
for one in outputs:
result.append( {'{#ONE}': one} )
print( json.dumps({'data':result},sort_keys=True,indent=4) )
The default Zabbix users can not use the netstat command, so you have to use sudo, Zabbix user needs to add Sudo privileges visudo
zabbix ALL= NOPASSWD: /bin/netstat
Defaults:zabbix !requiretty
Mysql port auto-discovery results are as follows
[root@test ~]# python mysql_discovery.py
{
"data": [
{
"{#ONE}": "3306"
}
]
}
Monitored client add custom Key Configuration
Monitored client needs to add the following configuration and then restart the Zabbix Agent
UserParameter=mysql.discovery,python /etc/zabbix/mysql_discovery.py
UserParameter=mysql.check[*],sh /etc/zabbix/mysql_check.sh $1 $2 2>/dev/null
Why should we add 2> / dev / null, this version is the prevention of high Mysql to write code directly in command there will be the following safety tips, so too filtered
mysql: [Warning] Using a password on the command line interface can be insecure.