一 Zabbix監控Mysql
監控Mysql,Zabbix提供了一個監控模板,全部能夠直接使用.或者使用Percona提供的監控模板.mysql
1. 使用自帶監控模板
1.1.1 編寫監控模板
#!/bin/bash # 主機地址IP MYSQL_HOST='127.0.0.1' # 端口 MYSQL_PORT='3306' # 數據鏈接 MYSQL_CONN="/usr/bin/mysqladmin -h${MYSQL_HOST} -P${MYSQL_PORT}" # 檢查參數是否正確 if [ $# -ne "1" ];then echo "arg Error!" fi # 獲取數據 case $1 in Uptime) result=`${MYSQL_CONN} status|cut -f2 -d":"|cut -f1 -d"T"` echo $result ;; Com_update) result=`${MYSQL_CONN} extended-status |grep -w "Com_update"|cut -d"|" -f3` echo $result ;; Slow_queries) result=`${MYSQL_CONN} status |cut -f5 -d":"|cut -f1 -d"O"` echo $result ;; Com_select) result=`${MYSQL_CONN} extended-status |grep -w "Com_select"|cut -d"|" -f3` echo $result ;; Com_rollback) result=`${MYSQL_CONN} extended-status |grep -w "Com_rollback"|cut -d"|" -f3` echo $result ;; Questions) result=`${MYSQL_CONN} status|cut -f4 -d":"|cut -f1 -d"S"` echo $result ;; Com_insert) result=`${MYSQL_CONN} extended-status |grep -w "Com_insert"|cut -d"|" -f3` echo $result ;; Com_delete) result=`${MYSQL_CONN} extended-status |grep -w "Com_delete"|cut -d"|" -f3` echo $result ;; Com_commit) result=`${MYSQL_CONN} extended-status |grep -w "Com_commit"|cut -d"|" -f3` echo $result ;; Bytes_sent) result=`${MYSQL_CONN} extended-status |grep -w "Bytes_sent" |cut -d"|" -f3` echo $result ;; Bytes_received) result=`${MYSQL_CONN} extended-status |grep -w "Bytes_received" |cut -d"|" -f3` echo $result ;; Com_begin) result=`${MYSQL_CONN} extended-status |grep -w "Com_begin"|cut -d"|" -f3` echo $result ;; *) echo "Usage:$0(Uptime|Com_update|Slow_queries|Com_select|Com_rollback|Questions|Com_insert|Com_delete|Com_commit|Bytes_sent|Bytes_received|C om_begin)" ;; esac
1.1.2 異常處理
若是mysql5.7以上版本執行腳本出現Waring信息,解決以下:nginx
[mnysqladmin] user=root password=xxxxx
1.1.3 添加權限以及定義監控項
# 添加權限屬主信息 chmod 755 check_mysql.sh chown zabbix.zabbix check_mysql.sh chmod +x check_mysql.sh # /etc/zabbix/zabbix_agentd.d 下建立 UserParameter_mysql.conf文件,內容以下: UserParameter=mysql.status[*],/etc/zabbix/zabbix_agentd.d/check_mysql.sh $1 UserParameter=mysql.ping,HOME=/etc /usr/local/mysql/bin/mysqladmin ping 2>/dev/null| grep -c alive UserParameter=mysql.version,/usr/local/mysql/bin/mysql -V
1.1.4 Mysql監控主機添加模板sql
二 監控Nginx
2.1.1 開啓Nginx狀態頁
location /nginx-status { stub_status on; access_log off; allow 127.0.0.1; deny all; } # 這段location是打開nginx的狀態監控頁面, 「stub_status」爲「on」表示開啓狀態監控模塊, 「access_log off」表示關閉這個頁面的訪問日誌, 接下來的「allow」表示這個狀態監控頁面容許哪些客戶端訪問,通常容許本機(127.0.0.1)除了容許訪問的客戶端外,其它都經過「deny all」禁止訪問便可。
2.1.2 檢查 & 參數說明
# curl http://127.0.0.1/nginx-status Active connections: 3 server accepts handled requests 1649812 1649812 2770589 Reading: 0 Writing: 1 Waiting: 2 # 參數說明 Active connections: 對後端發起的活動鏈接數; accepts:nginx總共處理了多少個鏈接; handled:nginx成功建立了幾回握手; requests:nginx總共處理了多少請求。 Reading:nginx讀取客戶端的header數; Writing: nginx 返回給客戶端的header數; Waiting: nginx 請求處理完成,正在等待下一請求指令的鏈接.
2.1.3 編寫Nginx監控腳本
# cat nginx_status.sh #!/bin/bash # Set Variables HOST=127.0.0.1 PORT="80" if [ $# -eq "0" ];then echo "Usage:$0(active|reading|writing|waiting|accepts|handled|requests|ping)" fi # Functions to return nginx stats function active { /usr/bin/curl "http://$HOST:$PORT/nginx-status" 2>/dev/null| grep 'Active' | awk '{print $NF}' } function reading { /usr/bin/curl "http://$HOST:$PORT/nginx-status" 2>/dev/null| grep 'Reading' | awk '{print $2}' } function writing { /usr/bin/curl "http://$HOST:$PORT/nginx-status" 2>/dev/null| grep 'Writing' | awk '{print $4}' } function waiting { /usr/bin/curl "http://$HOST:$PORT/nginx-status" 2>/dev/null| grep 'Waiting' | awk '{print $6}' } function accepts { /usr/bin/curl "http://$HOST:$PORT/nginx-status" 2>/dev/null| awk NR==3 | awk '{print $1}' } function handled { /usr/bin/curl "http://$HOST:$PORT/nginx-status" 2>/dev/null| awk NR==3 | awk '{print $2}' } function requests { /usr/bin/curl "http://$HOST:$PORT/nginx-status" 2>/dev/null| awk NR==3 | awk '{print $3}' } function ping { /sbin/pidof nginx | wc -l } # Run the requested function $1
添加執行權限,以及編寫UserParametershell
chmod 755 nginx_status.sh chmod +x nginx_status.sh chown zabbix. nginx_status.sh # 在/etc/zabbix/zabbix_agentd.d 建立UserParameter_nginx.conf 內容以下: UserParameter=nginx.status[*],/etc/zabbix/shell/nginx_status.sh $1
2.1.4 建立模板以及連接主機
<?xml version="1.0" encoding="UTF-8"?> <zabbix_export> <version>3.0</version> <date>2018-06-26T11:14:10Z</date> <groups> <group> <name>Templates</name> </group> </groups> <templates> <template> <template>Template App NGINX</template> <name>Template App NGINX</name> <description>nginx監控模板</description> <groups> <group> <name>Templates</name> </group> </groups> <applications> <application> <name>nginx</name> </application> </applications> <items> <item> <name>nginx status server accepts</name> <type>0</type> <snmp_community/> <multiplier>0</multiplier> <snmp_oid/> <key>nginx.status[accepts]</key> <delay>60</delay> <history>90</history> <trends>365</trends> <status>0</status> <value_type>3</value_type> <allowed_hosts/> <units/> <delta>1</delta> <snmpv3_contextname/> <snmpv3_securityname/> <snmpv3_securitylevel>0</snmpv3_securitylevel> <snmpv3_authprotocol>0</snmpv3_authprotocol> <snmpv3_authpassphrase/> <snmpv3_privprotocol>0</snmpv3_privprotocol> <snmpv3_privpassphrase/> <formula>1</formula> <delay_flex/> <params/> <ipmi_sensor/> <data_type>0</data_type> <authtype>0</authtype> <username/> <password/> <publickey/> <privatekey/> <port/> <description>accepts</description> <inventory_link>0</inventory_link> <applications> <application> <name>nginx</name> </application> </applications> <valuemap/> <logtimefmt/> </item> <item> <name>nginx status connections active</name> <type>0</type> <snmp_community/> <multiplier>0</multiplier> <snmp_oid/> <key>nginx.status[active]</key> <delay>60</delay> <history>90</history> <trends>365</trends> <status>0</status> <value_type>3</value_type> <allowed_hosts/> <units/> <delta>0</delta> <snmpv3_contextname/> <snmpv3_securityname/> <snmpv3_securitylevel>0</snmpv3_securitylevel> <snmpv3_authprotocol>0</snmpv3_authprotocol> <snmpv3_authpassphrase/> <snmpv3_privprotocol>0</snmpv3_privprotocol> <snmpv3_privpassphrase/> <formula>1</formula> <delay_flex/> <params/> <ipmi_sensor/> <data_type>0</data_type> <authtype>0</authtype> <username/> <password/> <publickey/> <privatekey/> <port/> <description>active</description> <inventory_link>0</inventory_link> <applications> <application> <name>nginx</name> </application> </applications> <valuemap/> <logtimefmt/> </item> <item> <name>nginx status server handled</name> <type>0</type> <snmp_community/> <multiplier>0</multiplier> <snmp_oid/> <key>nginx.status[handled]</key> <delay>60</delay> <history>90</history> <trends>365</trends> <status>0</status> <value_type>3</value_type> <allowed_hosts/> <units/> <delta>1</delta> <snmpv3_contextname/> <snmpv3_securityname/> <snmpv3_securitylevel>0</snmpv3_securitylevel> <snmpv3_authprotocol>0</snmpv3_authprotocol> <snmpv3_authpassphrase/> <snmpv3_privprotocol>0</snmpv3_privprotocol> <snmpv3_privpassphrase/> <formula>1</formula> <delay_flex/> <params/> <ipmi_sensor/> <data_type>0</data_type> <authtype>0</authtype> <username/> <password/> <publickey/> <privatekey/> <port/> <description>handled</description> <inventory_link>0</inventory_link> <applications> <application> <name>nginx</name> </application> </applications> <valuemap/> <logtimefmt/> </item> <item> <name>nginx status PING</name> <type>0</type> <snmp_community/> <multiplier>0</multiplier> <snmp_oid/> <key>nginx.status[ping]</key> <delay>60</delay> <history>30</history> <trends>365</trends> <status>0</status> <value_type>3</value_type> <allowed_hosts/> <units/> <delta>0</delta> <snmpv3_contextname/> <snmpv3_securityname/> <snmpv3_securitylevel>0</snmpv3_securitylevel> <snmpv3_authprotocol>0</snmpv3_authprotocol> <snmpv3_authpassphrase/> <snmpv3_privprotocol>0</snmpv3_privprotocol> <snmpv3_privpassphrase/> <formula>1</formula> <delay_flex/> <params/> <ipmi_sensor/> <data_type>0</data_type> <authtype>0</authtype> <username/> <password/> <publickey/> <privatekey/> <port/> <description>is live</description> <inventory_link>0</inventory_link> <applications> <application> <name>nginx</name> </application> </applications> <valuemap> <name>Service state</name> </valuemap> <logtimefmt/> </item> <item> <name>nginx status connections reading</name> <type>0</type> <snmp_community/> <multiplier>0</multiplier> <snmp_oid/> <key>nginx.status[reading]</key> <delay>60</delay> <history>90</history> <trends>365</trends> <status>0</status> <value_type>3</value_type> <allowed_hosts/> <units/> <delta>0</delta> <snmpv3_contextname/> <snmpv3_securityname/> <snmpv3_securitylevel>0</snmpv3_securitylevel> <snmpv3_authprotocol>0</snmpv3_authprotocol> <snmpv3_authpassphrase/> <snmpv3_privprotocol>0</snmpv3_privprotocol> <snmpv3_privpassphrase/> <formula>1</formula> <delay_flex/> <params/> <ipmi_sensor/> <data_type>0</data_type> <authtype>0</authtype> <username/> <password/> <publickey/> <privatekey/> <port/> <description>reading</description> <inventory_link>0</inventory_link> <applications> <application> <name>nginx</name> </application> </applications> <valuemap/> <logtimefmt/> </item> <item> <name>nginx status server requests</name> <type>0</type> <snmp_community/> <multiplier>0</multiplier> <snmp_oid/> <key>nginx.status[requests]</key> <delay>60</delay> <history>90</history> <trends>365</trends> <status>0</status> <value_type>3</value_type> <allowed_hosts/> <units/> <delta>1</delta> <snmpv3_contextname/> <snmpv3_securityname/> <snmpv3_securitylevel>0</snmpv3_securitylevel> <snmpv3_authprotocol>0</snmpv3_authprotocol> <snmpv3_authpassphrase/> <snmpv3_privprotocol>0</snmpv3_privprotocol> <snmpv3_privpassphrase/> <formula>1</formula> <delay_flex/> <params/> <ipmi_sensor/> <data_type>0</data_type> <authtype>0</authtype> <username/> <password/> <publickey/> <privatekey/> <port/> <description>requests</description> <inventory_link>0</inventory_link> <applications> <application> <name>nginx</name> </application> </applications> <valuemap/> <logtimefmt/> </item> <item> <name>nginx status connections waiting</name> <type>0</type> <snmp_community/> <multiplier>0</multiplier> <snmp_oid/> <key>nginx.status[waiting]</key> <delay>60</delay> <history>90</history> <trends>365</trends> <status>0</status> <value_type>3</value_type> <allowed_hosts/> <units/> <delta>0</delta> <snmpv3_contextname/> <snmpv3_securityname/> <snmpv3_securitylevel>0</snmpv3_securitylevel> <snmpv3_authprotocol>0</snmpv3_authprotocol> <snmpv3_authpassphrase/> <snmpv3_privprotocol>0</snmpv3_privprotocol> <snmpv3_privpassphrase/> <formula>1</formula> <delay_flex/> <params/> <ipmi_sensor/> <data_type>0</data_type> <authtype>0</authtype> <username/> <password/> <publickey/> <privatekey/> <port/> <description>waiting</description> <inventory_link>0</inventory_link> <applications> <application> <name>nginx</name> </application> </applications> <valuemap/> <logtimefmt/> </item> <item> <name>nginx status connections writing</name> <type>0</type> <snmp_community/> <multiplier>0</multiplier> <snmp_oid/> <key>nginx.status[writing]</key> <delay>60</delay> <history>90</history> <trends>365</trends> <status>0</status> <value_type>3</value_type> <allowed_hosts/> <units/> <delta>0</delta> <snmpv3_contextname/> <snmpv3_securityname/> <snmpv3_securitylevel>0</snmpv3_securitylevel> <snmpv3_authprotocol>0</snmpv3_authprotocol> <snmpv3_authpassphrase/> <snmpv3_privprotocol>0</snmpv3_privprotocol> <snmpv3_privpassphrase/> <formula>1</formula> <delay_flex/> <params/> <ipmi_sensor/> <data_type>0</data_type> <authtype>0</authtype> <username/> <password/> <publickey/> <privatekey/> <port/> <description>writing</description> <inventory_link>0</inventory_link> <applications> <application> <name>nginx</name> </application> </applications> <valuemap/> <logtimefmt/> </item> </items> <discovery_rules/> <macros/> <templates/> <screens/> </template> </templates> <triggers> <trigger> <expression>{Template App NGINX:nginx.status[ping].last()}=0</expression> <name>nginx was down!</name> <url/> <status>0</status> <priority>4</priority> <description>NGINX進程數:0,請注意</description> <type>0</type> <dependencies/> </trigger> </triggers> <graphs> <graph> <name>nginx status connections</name> <width>900</width> <height>200</height> <yaxismin>0.0000</yaxismin> <yaxismax>100.0000</yaxismax> <show_work_period>1</show_work_period> <show_triggers>1</show_triggers> <type>0</type> <show_legend>1</show_legend> <show_3d>0</show_3d> <percent_left>0.0000</percent_left> <percent_right>0.0000</percent_right> <ymin_type_1>0</ymin_type_1> <ymax_type_1>0</ymax_type_1> <ymin_item_1>0</ymin_item_1> <ymax_item_1>0</ymax_item_1> <graph_items> <graph_item> <sortorder>0</sortorder> <drawtype>0</drawtype> <color>00C800</color> <yaxisside>0</yaxisside> <calc_fnc>2</calc_fnc> <type>0</type> <item> <host>Template App NGINX</host> <key>nginx.status[active]</key> </item> </graph_item> <graph_item> <sortorder>1</sortorder> <drawtype>0</drawtype> <color>C80000</color> <yaxisside>0</yaxisside> <calc_fnc>2</calc_fnc> <type>0</type> <item> <host>Template App NGINX</host> <key>nginx.status[reading]</key> </item> </graph_item> <graph_item> <sortorder>2</sortorder> <drawtype>0</drawtype> <color>0000C8</color> <yaxisside>0</yaxisside> <calc_fnc>2</calc_fnc> <type>0</type> <item> <host>Template App NGINX</host> <key>nginx.status[waiting]</key> </item> </graph_item> <graph_item> <sortorder>3</sortorder> <drawtype>0</drawtype> <color>C800C8</color> <yaxisside>0</yaxisside> <calc_fnc>2</calc_fnc> <type>0</type> <item> <host>Template App NGINX</host> <key>nginx.status[writing]</key> </item> </graph_item> </graph_items> </graph> <graph> <name>nginx status server</name> <width>900</width> <height>200</height> <yaxismin>0.0000</yaxismin> <yaxismax>100.0000</yaxismax> <show_work_period>1</show_work_period> <show_triggers>1</show_triggers> <type>0</type> <show_legend>1</show_legend> <show_3d>0</show_3d> <percent_left>0.0000</percent_left> <percent_right>0.0000</percent_right> <ymin_type_1>0</ymin_type_1> <ymax_type_1>0</ymax_type_1> <ymin_item_1>0</ymin_item_1> <ymax_item_1>0</ymax_item_1> <graph_items> <graph_item> <sortorder>0</sortorder> <drawtype>0</drawtype> <color>00C800</color> <yaxisside>0</yaxisside> <calc_fnc>2</calc_fnc> <type>0</type> <item> <host>Template App NGINX</host> <key>nginx.status[accepts]</key> </item> </graph_item> <graph_item> <sortorder>1</sortorder> <drawtype>0</drawtype> <color>C80000</color> <yaxisside>0</yaxisside> <calc_fnc>2</calc_fnc> <type>0</type> <item> <host>Template App NGINX</host> <key>nginx.status[handled]</key> </item> </graph_item> <graph_item> <sortorder>2</sortorder> <drawtype>0</drawtype> <color>0000C8</color> <yaxisside>0</yaxisside> <calc_fnc>2</calc_fnc> <type>0</type> <item> <host>Template App NGINX</host> <key>nginx.status[requests]</key> </item> </graph_item> </graph_items> </graph> </graphs> <value_maps> <value_map> <name>Service state</name> <mappings> <mapping> <value>0</value> <newvalue>Down</newvalue> </mapping> <mapping> <value>1</value> <newvalue>Up</newvalue> </mapping> </mappings> </value_map> </value_maps> </zabbix_export>