標籤:mysql zabbix zabbix low-level discovery
當一台伺服器上MySQL有多個執行個體的時候,MySQL佔用多個不同連接埠。利用zabbix的low-level discovery可以輕鬆監控。
思路參考:http://dl528888.blog.51cto.com/2382721/1357713
下面我寫了一個簡單的指令碼,用於一鍵部署mysql監控。
一、用戶端
在被監控端運行此指令碼,前提條件已經安裝好了zabbix agent(推薦yum安裝)。指令碼已在CentOS6.2 x86_64環境下測試通過。指令碼在附件提供下載。
#!/bin/sh## Filename: autoMonitorMysql.sh# Revision: 1.0# Date: 2014/08/12# Author: Qicheng# Email:# Website: http://qicheng0211.blog.51cto.com/# Description: zabbix low level discovery監控MySQL# Notes: 在被監控用戶端運行此指令碼,前提條件已經安裝好zabbix agent#ROOT_UID=0if [ "$UID" -ne "$ROOT_UID" ];then echo "Error: 必須以root使用者運行此程式!" exit 1fiAGENT_CONF="/etc/zabbix/zabbix_agentd.conf"mkdir -p /etc/zabbix/monitor_scripts# 建立 mysql low-level discovery 指令碼cat > /etc/zabbix/monitor_scripts/mysql_port_discovery.sh << ‘EOF‘#!/bin/bash#Function: mysql low-level discovery#Script_name: mysql_port_discovery.shmysql() { port=(`sudo /bin/netstat -tpln | awk -F "[ :]+" ‘BEGIN {IGNORECASE=1; } /mysql/ && /0.0.0.0/ {print $5}‘`) max_index=$[${#port[@]}-1] printf ‘{\n‘ printf ‘\t"data":[‘ for key in `seq -s‘ ‘ 0 $max_index` do printf ‘\n\t\t{‘ printf "\"{#MYSQLPORT}\":\"${port[${key}]}\"}" if [ $key -ne $max_index ];then printf "," fi done printf ‘\n\t]\n‘ printf ‘}\n‘}$1EOFchown -R zabbix:zabbix /etc/zabbix/monitor_scriptschmod +x /etc/zabbix/monitor_scripts/mysql_port_discovery.sh# 判斷設定檔是否存在[ -f "${AGENT_CONF}" ] || { echo "File ${AGENT_CONF} does not exist.";exit 1;}include=`grep ‘^Include‘ ${AGENT_CONF} | cut -d‘=‘ -f2`# 在設定檔中添加自訂參數if [ -d "$include" ];then cat > $include/mysql_lld.conf << ‘EOF‘UserParameter=mysql_port_discovery[*],/bin/bash /etc/zabbix/monitor_scripts/mysql_port_discovery.sh $1UserParameter=mysql_status[*],mysql -h 127.0.0.1 -P $1 -uzabbix -pzabbix -N -e "show global status where Variable_name=‘$2‘" | cut -f2UserParameter=mysql_ping[*],mysqladmin -h 127.0.0.1 -P $1 -uzabbix -pzabbix ping | grep -c aliveEOFelse grep -q ‘^UserParameter=mysql_port_discovery‘ ${AGENT_CONF} || cat >> ${AGENT_CONF} << ‘EOF‘UserParameter=mysql_port_discovery[*],/bin/bash /etc/zabbix/monitor_scripts/mysql_port_discovery.sh $1UserParameter=mysql_status[*],mysql -h 127.0.0.1 -P $1 -uzabbix -pzabbix -N -e "show global status where Variable_name=‘$2‘" | cut -f2UserParameter=mysql_ping[*],mysqladmin -h 127.0.0.1 -P $1 -uzabbix -pzabbix ping | grep -c aliveEOFfi# 手動MySQL授權,添加zabbix賬戶echo "請在所有MySQL執行個體中添加zabbix賬戶,執行> GRANT PROCESS ON *.* TO ‘zabbix‘@‘127.0.0.1‘ identified BY ‘zabbix‘;flush privileges;"# 重啟agent服務[ -f ‘/etc/init.d/zabbix-agent‘ ] && /etc/init.d/zabbix-agent restart || echo "需手動重啟zabbix agent服務."# 授權zabbix使用者無密碼運行netstat命令chmod +w /etc/sudoers sed -i ‘s/^\(Defaults\s\+requiretty\)/#\1/‘ /etc/sudoersgrep -q ‘^zabbix ALL=(ALL).*netstat‘ /etc/sudoers || echo ‘zabbix ALL=(ALL) NOPASSWD: /bin/netstat‘ >> /etc/sudoerschmod 440 /etc/sudoers# 關閉SELinuxsetenforce 0sed -i ‘/^SELINUX=/c\SELINUX=disabled‘ /etc/sysconfig/selinuxsed -i ‘/^SELINUX=/c\SELINUX=disabled‘ /etc/selinux/config
指令碼執行完畢後,還需要手動MySQL授權,在所有執行個體中添加zabbix賬戶:
mysql> GRANT PROCESS ON *.* TO ‘zabbix‘@‘127.0.0.1‘ identified BY ‘zabbix‘;mysql> flush privileges;
在zabbix server端執行zabbix_get命令測試:
shell# zabbix_get -s 115.238.xxx.xxx -k ‘mysql_port_discovery[mysql]‘{ "data":[ {"{#MYSQLPORT}":"3306"} ]}shell# zabbix_get -s 115.238.xxx.xxx -k ‘mysql_status[3306,Uptime]‘1295092shell# zabbix_get -s 115.238.xxx.xxx -k ‘mysql_ping[3306]‘1
都能返回資料,說明用戶端的MySQL監控部署好了。
二、WEB端
1、匯入模板
下載附件模板檔案(Template App MySQL used LLD.xml)後,匯入到zabbix裡。
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/45/B6/wKioL1Pp4kSyoHKjAAGXWh7Zy8s473.jpg" title="import.png" alt="wKioL1Pp4kSyoHKjAAGXWh7Zy8s473.jpg" />
2、建立主機並連結模板
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/45/B5/wKiom1Pp4cShB-e7AAGzKCOtQms583.jpg" title="link.png" alt="wKiom1Pp4cShB-e7AAGzKCOtQms583.jpg" />
3、資料展示
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/45/B6/wKioL1Pp42OwPPWPAAbiWNEsyIs158.jpg" title="play.png" alt="wKioL1Pp42OwPPWPAAbiWNEsyIs158.jpg" />
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/45/B5/wKiom1Pp5c3TKkJoAAQ8Pf6e16I805.jpg" title="traffic.png" alt="wKiom1Pp5c3TKkJoAAQ8Pf6e16I805.jpg" />
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/45/B6/wKioL1Pp5yzSM7j0AAUMjs2XGA0952.jpg" title="opt.png" alt="wKioL1Pp5yzSM7j0AAUMjs2XGA0952.jpg" />
本文出自 “啟程-Linux技術部落格” 部落格,請務必保留此出處http://qicheng0211.blog.51cto.com/3958621/1539101