標籤:zabbix密鑰監控-mysql-nginx-php
密鑰監控-mysql-nginx-php一、zabbix監控mysql在客戶機上配置:[[email protected] ~]# vim /usr/local/zabbix/etc/chk_mysql.sh#!/bin/bash# -------------------------------------------------------------------------------# FileName: check_mysql.sh# Revision: 1.0# Date: 2015/06/09# Author: DengYun# Email: [email protected]# Website: www.ttlsa.com# Description: # Notes: ~# -------------------------------------------------------------------------------# Copyright: 2015 (c) DengYun# License: # 使用者名稱MYSQL_USER=‘root‘# 密碼MYSQL_PWD=‘YyIrpFNhIofsd‘# 主機地址/IPMYSQL_HOST=‘127.0.0.1‘# 連接埠MYSQL_PORT=‘3306‘# 資料連線MYSQL_CONN="/usr/local/bin/mysqladmin -u${MYSQL_USER} -p${MYSQL_PWD} -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|Com_begin)" ;;Esac[[email protected] ~]# vim /usr/local/zabbix/etc/zabbix_agentd.conf在後面添加:# 擷取mysql版本UserParameter=mysql.version,mysql -V # 擷取mysql效能指標,這個是上面定義好的指令碼UserParameter=mysql.status[*],/usr/local/zabbix/etc/chk_mysql.sh $1 # 擷取mysql運行狀態UserParameter=mysql.ping,mysqladmin -uroot -pYyIrpFNhIofsd -P3306 -h127.0.0.1 ping | grep -c alive服務端取值:[[email protected] ~]# zabbix_get -s 10.44.37.221 -p10050 -k mysql.status[Com_update]297二、監控php-fpm[[email protected] ~]# vim /usr/local/php/etc/php-fpm.confpm.status_path = /status //把;好去掉,啟用監控fpm狀態[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf location ~ ^/(status|ping)$ { fastcgi_pass 127.0.0.1:9000; include fastcgi_params; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; allow 127.0.0.1; allow 10.170.236.125; deny all; }[[email protected] ~]# service nginx restart[[email protected] etc]# curl -s http://localhost/status //測試是否可以看到php-fpm的狀態,有資訊即可建立監控指令碼:[[email protected] ~]# vim /usr/local/zabbix/etc/chk_php-fpm.sh#!/bin/bash#monitor php-fpm status from zabbix#lincense:GPL#mail:[email protected]#date:2015.04.15source /etc/bashrc >/dev/null 2>&1source /etc/profile >/dev/null 2>&1LOG=/usr/local/zabbix/etc/phpfpm_status.logcurl -s http://localhost/status >$LOGpool(){ awk ‘/pool/ {print $NF}‘ $LOG}process_manager(){ awk ‘/process manager/ {print $NF}‘ $LOG}start_since(){ awk ‘/start since:/ {print $NF}‘ $LOG}accepted_conn(){ awk ‘/accepted conn:/ {print $NF}‘ $LOG}listen_queue(){ awk ‘/^(listen queue:)/ {print $NF}‘ $LOG}max_listen_queue(){ awk ‘/max listen queue:/ {print $NF}‘ $LOG}listen_queue_len(){ awk ‘/listen queue len:/ {print $NF}‘ $LOG}idle_processes(){ awk ‘/idle processes:/ {print $NF}‘ $LOG}active_processes(){ awk ‘/^(active processes:)/ {print $NF}‘ $LOG}total_processes(){ awk ‘/total processes:/ {print $NF}‘ $LOG}max_active_processes(){ awk ‘/max active processes:/ {print $NF}‘ $LOG}max_children_reached(){ awk ‘/max children reached:/ {print $NF}‘ $LOG}slow_requests(){ awk ‘/slow requests:/ {print $NF}‘ $LOG}case "$1" inpool) pool ;;process_manager) process_manager ;;start_since) start_since ;;accepted_conn) accepted_conn ;;listen_queue) listen_queue ;;max_listen_queue) max_listen_queue ;;listen_queue_len) listen_queue_len ;;idle_processes) idle_processes ;;active_processes) active_processes ;;total_processes) total_processes ;;max_active_processes) max_active_processes ;;max_children_reached) max_children_reached ;;slow_requests) slow_requests ;;*)echo "Usage: $0 {pool|process_manager|start_since|accepted_conn|listen_queue|max_listen_queue|listen_queue_len|idle_processes|active_processes|total_processes|max_active_processes|max_children_reached|slow_requests}"esac引用指令檔[[email protected] ~]# vim /usr/local/zabbix/etc/zabbix_agentd.conf#UserParameter=php-fpm[*],/usr/local/zabbix/etc/chk_php-fpm.sh "$1"UserParameter=phpfpm.status.pool,/usr/local/zabbix/etc/chk_php-fpm.sh poolUserParameter=phpfpm.status.process.manager,/usr/local/zabbix/etc/chk_php-fpm.sh process_managerUserParameter=phpfpm.status.start.since,/usr/local/zabbix/etc/chk_php-fpm.sh start_sinceUserParameter=phpfpm.status.accepted.conn,/usr/local/zabbix/etc/chk_php-fpm.sh accepted_connUserParameter=phpfpm.status.listen.queue,/usr/local/zabbix/etc/chk_php-fpm.sh listen_queueUserParameter=phpfpm.status.max.listen.queue,/usr/local/zabbix/etc/chk_php-fpm.sh max_listen_queueUserParameter=phpfpm.status.listen.queue.len,/usr/local/zabbix/etc/chk_php-fpm.sh listen_queue_lenUserParameter=phpfpm.status.idle.processes,/usr/local/zabbix/etc/chk_php-fpm.sh idle_processesUserParameter=phpfpm.status.active.processes,/usr/local/zabbix/etc/chk_php-fpm.sh active_processesUserParameter=phpfpm.status.total.processes,/usr/local/zabbix/etc/chk_php-fpm.sh total_processesUserParameter=phpfpm.status.max.active.processes,/usr/local/zabbix/etc/chk_php-fpm.sh max_active_processesUserParameter=phpfpm.status.max.children.reached,/usr/local/zabbix/etc/chk_php-fpm.sh max_children_reachedUserParameter=phpfpm.status.slow.requests,/usr/local/zabbix/etc/chk_php-fpm.sh slow_requests[[email protected] ~]# /etc/init.d/zabbix_agentd restart[[email protected] etc]# curl http://127.0.0.1/statuspool: wwwprocess manager: dynamicstart time: 27/Apr/2016:14:21:33 +0800start since: 85601accepted conn: 37078listen queue: 0max listen queue: 13listen queue len: 128idle processes: 2active processes: 1total processes: 3max active processes: 5max children reached: 4slow requests: 0下面介紹每個參數的作用:pool:php-fpm池的名稱,一般都是應該是wwwprocess manage:進程的管理方法,php-fpm支援三種管理方法,分別是static,dynamic和ondemand,一般情況下都是dynamicstart time:php-fpm啟動時候的時間,不管是restart或者reload都會更新這裡的時間start since:php-fpm自啟動起來經過的時間,預設為秒accepted conn:當前接收的串連數listen queue:在隊列中等待串連的請求個數,如果這個數字為非0,那麼最好增加進程的fpm個數max listen queue:從fpm啟動以來,在隊列中等待串連請求的最大值listen queue len:等待串連的通訊端隊列大小idle processes:閒置進程個數active processes:活動的進程個數total processes:總共的進程個數max active processes:從fpm啟動以來,活動進程的最大個數,如果這個值小於當前的max_children,可以調小此值max children reached:當pm嘗試啟動更多的進程,卻因為max_children的限制,沒有啟動更多進程的次數。如果這個值非0,那麼可以適當增加fpm的進程數slow requests:慢請求的次數,一般如果這個值未非0,那麼可能會有慢的php進程,一般一個不好的mysql查詢是最大的禍首。伺服器驗證:[[email protected] ~]# zabbix_get -s 10.44.37.221 -p 10050 -k phpfpm.status.idle.processes127 //擷取到資料說明配置沒問題了,接下來在web頁面上添加模板三、監控nginx[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf在php-pfm下增加: location /nginx-status { stub_status on; access_log on; allow 127.0.0.1; allow 10.170.236.125; deny all; }[[email protected] ~]# vim /usr/local/zabbix/etc/chk_nginx.sh#!/bin/bash# Script to fetch nginx statuses for tribily monitoring systems# Author: [email protected]# License: GPLv2# Set VariablesBKUP_DATE=`/bin/date +%Y%m%d`LOG="/data/log/zabbix/webstatus.log"HOST=127.0.0.1PORT="80"# Functions to return nginx statsfunction 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}‘}# Run the requested function$1在被監控端配置:[[email protected] ~]$ vim /usr/local/zabbix/etc/zabbix_agentd.conf在最後添加:UserParameter=nginx.accepts,/usr/local/zabbix/etc/chk_nginx.sh acceptsUserParameter=nginx.handled,/usr/local/zabbix/etc/chk_nginx.sh handledUserParameter=nginx.requests,/usr/local/zabbix/etc/chk_nginx.sh requestsUserParameter=nginx.connections.active,/usr/local/zabbix/etc/chk_nginx.sh activeUserParameter=nginx.connections.reading,/usr/local/zabbix/etc/chk_nginx.sh readingUserParameter=nginx.connections.writing,/usr/local/zabbix/etc/chk_nginx.sh writingUserParameter=nginx.connections.waiting,/usr/local/zabbix/etc/chk_nginx.sh waiting在被監控查看nginx狀態:[[email protected] etc]# curl http://127.0.0.1/nginx-statusActive connections: 1 server accepts handled requests 67561 67561 159860 Reading: 0 Writing: 1 Waiting: 0Activeconnections:對後端發起的活動串連數;server accepts 67561:nginx 總共處理了67561個串連;handled:成功建立了67561次握手;requests:總共處理了159860請求。Reading:nginx讀取用戶端的header數;Writing: nginx 返回給用戶端的header數;Waiting: nginx 請求處理完成,正在等待下一請求指令的串連。伺服器上測試[[email protected] ~]# zabbix_get -s 10.44.37.221 -p 10050 -k "nginx.accepts"3151注意:在web添加模板時,索引值(密鑰,web是通過索引值來連結監控伺服器的)要和監控端UserParameter後面的值一樣,例如nginx.accepts
本文出自 “個人部落格” 部落格,轉載請與作者聯絡!
zabbix密鑰監控-mysql-nginx-php