标签:zabbix
一、需求:
因为我们是游戏业务,要用到mysql,一个服务器上面跑多个mysql实例也是很正常。如果单独手动一台台修改agent.conf,添加web监控,太费时费力。所以就想的要用zabbix 的自动发现来监控
二、配置Agent
1.编写自动发现端口脚本
[root@test ~]# cat /etc/zabbix/alertscripts/zabbix_discovery_mysql.sh #!/bin/bash mysql() { port=($(sudo netstat -tpln | awk -F "[ :]+" ‘/[m]ysql/‘ | awk -F: ‘{print $4}‘)) printf ‘{\n‘ printf ‘\t"data":[\n‘ for key in ${!port[@]} do if [[ "${#port[@]}" -gt 1 && "${key}" -ne "$((${#port[@]}-1))" ]];then socket=`ps aux|grep ${port[${key}]}|grep -v grep|awk -F ‘=‘ ‘{print $10}‘|cut -d ‘ ‘ -f 1` printf ‘\t {\n‘ printf "\t\t\t\"{#MYSQLPORT}\":\"${port[${key}]}\"},\n" else [[ "${key}" -eq "((${#port[@]}-1))" ]] socket=`ps aux|grep ${port[${key}]}|grep -v grep|awk -F ‘=‘ ‘{print $10}‘|cut -d ‘ ‘ -f 1` printf ‘\t {\n‘ printf "\t\t\t\"{#MYSQLPORT}\":\"${port[${key}]}\"}\n" fi done printf ‘\t ]\n‘ printf ‘}\n‘ } $1
2.赋权
赋执行权限 chmod 755 /etc/zabbix/alertscripts/zabbix_discovery_mysql.sh 修改属组 chown zabbix.zabbix /etc/zabbix/alertscripts/zabbix_discovery_mysql.sh
3.测试脚本效果
[root@test ~]# /etc/zabbix/alertscripts/zabbix_discovery_mysql.sh mysql { "data":[ { "{#MYSQLPORT}":"3306"} ] }
4.配置agent.conf
[root@test ~]# cat /etc/zabbix/zabbix_agentd.conf | grep Include ### Option: Include # Include= # Include=/usr/local/etc/zabbix_agentd.userparams.conf # Include=/usr/local/etc/zabbix_agentd.conf.d/ Include=/etc/zabbix/zabbix_agentd.conf.d/*.conf
[root@test zabbix_agentd.conf.d]# cat check_mysql.conf UserParameter=mysql.discovery[*],/etc/zabbix/alertscripts/zabbix_discovery_mysql.sh $1 UserParameter=mysql_stats[*],/usr/bin/mysql --defaults-file=/home/zabbix/.my.cnf -P $1 -e "show global status"|grep "\<$2\>"|cut -f2
5.sudo权限
#Defaults requiretty Defaults:zabbix !requiretty zabbix ALL=(root) NOPASSWD:/bin/netstat,/usr/bin/mysql
6.创建zabbix查询mysql用户
GRANT PROCESS ON *.* TO ‘zabbix‘@‘localhost‘ identified BY ‘zabbix‘; Flush privileges;
注意:Warning: Using a password on the command line interface can be insecure.
因为mysql5.6不能使用明文密码登录,所以这里要创建一个文件用来指定用户名密码
7.创建mysql.cnf文件
[root@test zabbix]# cat /home/zabbix/.my.cnf [client] user=zabbix password=zabbix host=localhost
chown zabbix.zabbix /home/zabbix/.my.cnf chmod 600 /home/zabbix/.my.cnf
8.重启agent服务
service zabbix_agentd restart
9.测试
[root@test alertscripts]# /usr/local/zabbix/bin/zabbix_get -s 127.0.0.1 -k mysql_stats[3306,Uptime] 3862 [root@test alertscripts]# /usr/local/zabbix/bin/zabbix_get -s 127.0.0.1 -k mysql.discovery[mysql] { "data":[ { "{#MYSQLPORT}":"3306"} ] }
三、配置web
添加模板
2.创建应用
3.创建自动发现规则
4.创建监控项
5.创建图形
6.主机链接模板
7.查看监控数据
本文出自 “不抛弃!不放弃” 博客,请务必保留此出处http://thedream.blog.51cto.com/6427769/1869233
标签:zabbix
原文地址:http://thedream.blog.51cto.com/6427769/1869233