標籤:zabbix
一、需求:
因為我們是遊戲業務,要用到mysql,一個伺服器上面跑多個mysql執行個體也是很正常。如果單獨手動一台台修改agent.conf,添加web監控,太費時費力。所以就想的要用zabbix 的自動探索來監控
二、配置Agent
1.編寫自動探索連接埠指令碼
[[email protected] ~]# cat /etc/zabbix/alertscripts/zabbix_discovery_mysql.sh #!/bin/bashmysql() { 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.測試指令碼效果
[[email protected] ~]# /etc/zabbix/alertscripts/zabbix_discovery_mysql.sh mysql{ "data":[ { "{#MYSQLPORT}":"3306"} ]}
4.配置agent.conf
[[email protected] ~]# 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
[[email protected] zabbix_agentd.conf.d]# cat check_mysql.conf UserParameter=mysql.discovery[*],/etc/zabbix/alertscripts/zabbix_discovery_mysql.sh $1UserParameter=mysql_stats[*],/usr/bin/mysql --defaults-file=/home/zabbix/.my.cnf -P $1 -e "show global status"|grep "\<$2\>"|cut -f2
5.sudo許可權
#Defaults requirettyDefaults:zabbix !requirettyzabbix 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檔案
[[email protected] zabbix]# cat /home/zabbix/.my.cnf [client]user=zabbixpassword=zabbixhost=localhost
chown zabbix.zabbix /home/zabbix/.my.cnfchmod 600 /home/zabbix/.my.cnf
8.重啟agent服務
service zabbix_agentd restart
9.測試
[[email protected] alertscripts]# /usr/local/zabbix/bin/zabbix_get -s 127.0.0.1 -k mysql_stats[3306,Uptime]3862[[email protected] alertscripts]# /usr/local/zabbix/bin/zabbix_get -s 127.0.0.1 -k mysql.discovery[mysql]{ "data":[ { "{#MYSQLPORT}":"3306"} ]}
三、配置web
添加模板
650) this.width=650;" src="http://s2.51cto.com/wyfs02/M00/89/C3/wKiom1gb8lPw46j_AABXERmpMik442.png" title="9c2ed3f8-afb2-439b-9d5d-9f7e8b095d3a.png" alt="wKiom1gb8lPw46j_AABXERmpMik442.png" />
2.建立應用
650) this.width=650;" src="http://s2.51cto.com/wyfs02/M01/89/C1/wKioL1gb8oKwiNNFAABCxlwXGvo462.png" title="37dc5ab3-322e-4f87-b942-7c172a4bcb5c.png" alt="wKioL1gb8oKwiNNFAABCxlwXGvo462.png" />
3.建立自動探索規則
650) this.width=650;" src="http://s4.51cto.com/wyfs02/M00/89/C1/wKioL1gb8qaRQkWPAABo6IY2yaU666.png" title="f39b65b6-0030-4ff5-8a7e-c304bc02ea92.png" alt="wKioL1gb8qaRQkWPAABo6IY2yaU666.png" />
4.建立監控項
650) this.width=650;" src="http://s2.51cto.com/wyfs02/M02/89/C3/wKiom1gb8sWixaC_AACbL6A-5Ic397.png" title="bdd2f2f6-0962-4906-9a09-b91075f5d641.png" alt="wKiom1gb8sWixaC_AACbL6A-5Ic397.png" />
5.建立圖形
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/89/C3/wKiom1gb8t2BzqUaAACdXx_ToXc854.png" title="77baee71-a439-4626-9331-d5c27d767f95.png" alt="wKiom1gb8t2BzqUaAACdXx_ToXc854.png" />
6.主機連結模板
7.查看監控資料
650) this.width=650;" src="http://s5.51cto.com/wyfs02/M01/89/C3/wKiom1gb8vzSVdckAAB6IawU660105.png" title="39a93ce2-c97b-49dd-8399-3ee5edb21090.png" alt="wKiom1gb8vzSVdckAAB6IawU660105.png" />
本文出自 “不拋棄!不放棄” 部落格,請務必保留此出處http://thedream.blog.51cto.com/6427769/1869233
zabbix自動探索mysql連接埠並監控