笺注:这是在 Zabbix使用Agent监控CentOS6/Redhat6 的基础上进行的。
被监控主机配置MySQL数据库:( 使用数据库管理员root@localhost )
[root@localhost ~]# mysql -u"root" -p"123"
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.48-log 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>
注释:
-h"localhost" 是可以省略的;
查看当前登录的数据库用户:
mysql> Select user();
+----------------+
| user() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)
mysql>
再授权数据库用户zbx_monitor@localhost(密码168),只能本地登录,对所有的库有完全控制的权限:
mysql> grant all on *.* to zbx_monitor@localhost identified by '168';
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
查看本地数据库用户zbx_monitor@localhost的权限:
mysql> show grants for zbx_monitor@localhost;
+-----------------------------------------------------------------------------------------------------------------------------+
| Grants for zbx_monitor@localhost |
+-----------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'zbx_monitor'@'localhost' IDENTIFIED BY PASSWORD '*242E46A1E8D30FE06F7CE37B55BFC25BA981D70C' |
+-----------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql>
#######
修改被监控主机的Agent配置文件:
[root@zabbix_client ~]# find / -name zabbix_agentd.conf
/etc/zabbix/zabbix_agentd.conf
[root@zabbix_client ~]#
[root@zabbix_client ~]# vi /etc/zabbix/zabbix_agentd.conf
# UnsafeUserParameters=0
修改为:(启用该功能)
UnsafeUserParameters=1
接着插入代码:
UserParameter=mysql.get_status_variables[*], mysql -h"$1" -P"$2" -sNX -e "show global status"
UserParameter=mysql.version[*], mysqladmin -s -h"$1" -P"$2" version
UserParameter=mysql.db.discovery[*], mysql -h"$1" -P"$2" -sN -e "show databases"
UserParameter=mysql.dbsize[*], mysql -h"$1" -P"$2" -sN -e "SELECT SUM(DATA_LENGTH + INDEX_LENGTH) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='$3'"
UserParameter=mysql.replication.discovery[*], mysql -h"$1" -P"$2" -sNX -e "show slave status"
UserParameter=mysql.slave_status[*], mysql -h"$1" -P"$2" -sNX -e "show slave status"
复制代码
保存好配置文件后,重启zabbix-agent:
[root@zabbix_client ~]# service zabbix-agent restart
Shutting down Zabbix agent: [确定]
Starting Zabbix agent: [确定]
被监控主机本地测试:
[root@zabbix_client ~]# mysqladmin -u"zbx_monitor" -p"168" ping
-bash: mysqladmin: command not found
[root@zabbix_client ~]#
解决方法:
[root@zabbix_client ~]# mysql -u"zbx_monitor" -p"168" -e "show variables like '%char%'"