从zabbix 2.2开始,zabbix官方已经支持了mysql监控,但是mysql监控默认是不可用的,需要经过额外的设置才可以使用。
mysql服务器端
- 连接本机mysql,创建zabbix帐户
> GRANT USAGE ON *.* TO 'zabbix'@'localhost' IDENTIFIED BY 'monitor';
> FLUSH PRIVILEGES;
-
zabbix agent配置
# vim /etc/zabbix_agentd.conf Include=/etc/zabbix_agentd.conf.d/*.conf
-
配置zabbix agent连接mysql信息
# mkdir /etc/zabbix_agentd.conf.d # touch /etc/zabbix_agentd.conf.d/.my.cnf # vim /etc/zabbix_agentd.conf.d/.my.cnf [mysql] host=localhost user=zabbix password=monitor [mysqladmin] host=localhost user=zabbix password=monitor
-
配置zabbix agent获取mysql数据
# touch /etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf # vim /etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf ### MySQL DB Infomation UserParameter=mysql.status[*],echo "show global status where Variable_name='$1';"|mysql --defaults- file=/etc/zabbix_agentd.conf.d/.my.cnf -N|awk '{print $$2}' UserParameter=mysql.variables[*],echo "show global variables where Variable_name='$1';"|mysql --defaults- file=/etc/zabbix_agentd.conf.d/.my.cnf -N|awk '{print $$2}' UserParameter=mysql.ping,mysqladmin --defaults-file=/etc/zabbix_agentd.conf.d/.my.cnf ping|grep -c alive UserParameter=mysql.version,echo "select version();"|mysql --defaults-file=/etc/zabbix_agentd.conf.d/.my.cnf -N #### MySQL Master Information UserParameter=mysql.master.Slave_count,echo "show slave hosts;"|mysql --defaults- file=/etc/zabbix_agentd.conf.d/.my.cnf -N|wc -l UserParameter=mysql.master.Binlog_file,echo "show master status;"|mysql --defaults- file=/etc/zabbix_agentd.conf.d/.my.cnf -N|awk '{print $1}'|awk -F. '{print $1}' UserParameter=mysql.master.Binlog_number,echo "show master status;"|mysql --defaults- file=/etc/zabbix_agentd.conf.d/.my.cnf -N|awk '{print $1}'|awk -F. '{print $2}' UserParameter=mysql.master.Binlog_position,echo "show master status;"|mysql --defaults- file=/etc/zabbix_agentd.conf.d/.my.cnf -N|awk '{print $2}' UserParameter=mysql.master.Binlog_count,echo "show binary logs;"|mysql --defaults- file=/etc/zabbix_agentd.conf.d/.my.cnf -N|wc -l UserParameter=mysql.master.Binlog_total_size,echo "show binary logs;"|mysql --defaults- file=/etc/zabbix_agentd.conf.d/.my.cnf -N|awk '{sum+=$NF}END{print sum}' #### MySQL Slave Information UserParameter=mysql.slave.Seconds_Behind_Master,echo "show slave status\G"|mysql --defaults- file=/etc/zabbix_agentd.conf.d/.my.cnf|grep "Seconds_Behind_Master"|awk '{print $2}' UserParameter=mysql.slave.Slave_IO_Running,echo "show slave status\G"|mysql --defaults- file=/etc/zabbix_agentd.conf.d/.my.cnf|grep "Slave_IO_Running"|awk '{print $2}' UserParameter=mysql.slave.Slave_SQL_Running,echo "show slave status\G"|mysql --defaults- file=/etc/zabbix_agentd.conf.d/.my.cnf|grep "Slave_SQL_Running"|awk '{print $2}' UserParameter=mysql.slave.Relay_Log_Pos,echo "show slave status\G"|mysql --defaults- file=/etc/zabbix_agentd.conf.d/.my.cnf|grep "Relay_Log_Pos"|awk '{print $2}' UserParameter=mysql.slave.Exec_Master_Log_Pos,echo "show slave status\G"|mysql --defaults- file=/etc/zabbix_agentd.conf.d/.my.cnf|grep "Exec_Master_Log_Pos"|awk '{print $2}' UserParameter=mysql.slave.Read_Master_Log_Pos,echo "show slave status\G"|mysql --defaults- file=/etc/zabbix_agentd.conf.d/.my.cnf|grep "Read_Master_Log_Pos"|awk '{print $2}' #### MySQL InnoDB Information #UserParameter=mysql.innodb[*],/etc/zabbix_agentd.conf.d/mysql_innodb_status.sh $1 ####MySQL MyISAM Information
/etc/zabbix_agentd.conf.d/.my.cnf
为Zabbix Agent连接MySQL的配置文件
mysql
和mysqladmin
命令需在系统默认$PATH变量中,否则需要改为绝对路径 -
重启zabbix agent
# /etc/init.d/zabbix_agentd restart
zabbix web端
导入模板
添加模板到主机