国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Linux > 正文

linux中Zabbix監控Memcached PHP-FPM Tomcat Nginx MySQL 網站日志

2024-08-27 23:58:54
字體:
來源:轉載
供稿:網友

Zabbix是一個linux中常用的監控軟件了,我們可以使用Zabbix監控Memcached PHP-FPM Tomcat Nginx MySQL 網站日志了,下面一起來看一個例子哦.

Zabbix作為監控軟件非常的靈活,支持的數據類型非常豐富,比如數字(無正負),數字(浮點),日志,文字等,我們需要做的就是使用腳本來收集好數據,然后zabbix收集并畫圖,設置告警線,這里我們來學習使用Zabbix監控Memcached、PHP-FPM、Tomcat、Nginx、MySQL及網站日志.

Memcached監控,自定義鍵值:

UserParameter=memcached.stat[*],/data/sh/memcached-status.sh "$1"

memcached-status.sh腳本內容為:

  1. #!/bin/bash 
  2.  
  3. item=$1 
  4. ip=127.0.0.1 
  5. port=11211 
  6. (echo "stats";sleep 0.5) | telnet $ip $port 2>/dev/null | grep "STAT $item\b" | awk '{print $3}' 

導入模板,memcached zabbix模板下載

PHP-FPM監控,配置php-fpm狀態頁,打開php-fpm.conf配置文件,添加如下配置后重啟php:

pm.status_path = /fpm_status

自定義鍵值:UserParameter=php-fpm[*],/data/sh/php-fpm-status.sh "$1"

php-fpm-status.sh腳本內容:

  1. #!/bin/bash 
  2. ################################## 
  3. # Zabbix monitoring script 
  4. # php-fpm: 
  5. #  - anything available via FPM status page 
  6. ################################## 
  7. # Contact: 
  8. #  vincent.viallet@gmail.com 
  9. ################################## 
  10. # ChangeLog: 
  11. #  20100922        VV        initial creation 
  12. ################################## 
  13.  
  14. # Zabbix requested parameter 
  15. ZBX_REQ_DATA="$1" 
  16.  
  17. # FPM defaults 
  18. URL="http://localhost/fpm_status" 
  19. WGET_BIN="/usr/bin/wget" 
  20.  
  21. # Error handling: 
  22. #  - need to be displayable in Zabbix (avoid NOT_SUPPORTED) 
  23. #  - items need to be of type "float" (allow negative + float) 
  24. ERROR_NO_ACCESS_FILE="-0.9900" 
  25. ERROR_NO_ACCESS="-0.9901" 
  26. ERROR_WRONG_PARAM="-0.9902" 
  27. ERROR_DATA="-0.9903" # either can not connect /        bad host / bad port 
  28.  
  29. # save the FPM stats in a variable for future parsing 
  30. FPM_STATS=$($WGET_BIN -q $URL -O - 2> /dev/null) 
  31.  
  32. # error during retrieve 
  33. if [ $? -ne 0 -o -z "$FPM_STATS" ]; then 
  34.   echo $ERROR_DATA 
  35.   exit 1 
  36. fi 
  37.  
  38. # Extract data from FPM stats 
  39. RESULT=$(echo "$FPM_STATS" | sed -n -r "s/^$ZBX_REQ_DATA: +([0-9]+)/\1/p"
  40. if [ $? -ne 0 -o -z "$RESULT" ]; then 
  41.     echo $ERROR_WRONG_PARAM 
  42.     exit 1 
  43. fi 
  44.  
  45. echo $RESULT 
  46.  
  47. exit 0 

導入模板,php-fpm zabbix模板下載

Tomcat監控,剛開始決定監控Tomcat時,使用的是JMX,不過這貨設置太復雜了,而且對防火墻要求還挺高,需要開放幾個端口。只好使用Tomcat自帶的狀態頁來監控了。

自定義鍵值:UserParameter=tomcat.status[*],/data/sh/tomcat-status.py $1

因為需要解析到xml,所以還是決定用python實現比較方便.

  1. /data/sh/tomcat-status.py腳本內容: 
  2. #!/usr/bin/python 
  3. import urllib2 
  4. import xml.dom.minidom 
  5. import sys 
  6.  
  7. url = 'http://127.0.0.1:8080/manager/status?XML=true' 
  8. username = 'username' 
  9. password = 'password' 
  10.  
  11. passman = urllib2.HTTPPasswordMgrWithDefaultRealm() 
  12. passman.add_password(None, url, username, password) 
  13. authhandler = urllib2.HTTPBasicAuthHandler(passman) 
  14. opener = urllib2.build_opener(authhandler) 
  15. urllib2.install_opener(opener) 
  16. pagehandle = urllib2.urlopen(url) 
  17. xmlData = pagehandle.read() 
  18. doc = xml.dom.minidom.parseString(xmlData)  
  19.  
  20. item = sys.argv[1] 
  21.  
  22. if item == "memory.free"
  23. print  doc.getElementsByTagName("memory")[0].getAttribute("free"
  24. elif item == "memory.total"
  25. print  doc.getElementsByTagName("memory")[0].getAttribute("total"
  26. elif item == "memory.max"
  27. print  doc.getElementsByTagName("memory")[0].getAttribute("max"
  28. elif item == "threadInfo.maxThreads"
  29. print  doc.getElementsByTagName("threadInfo")[0].getAttribute("maxThreads"
  30. elif item == "threadInfo.currentThreadCount"
  31. print  doc.getElementsByTagName("threadInfo")[0].getAttribute("currentThreadCount"
  32. elif item == "threadInfo.currentThreadsBusy"
  33. print  doc.getElementsByTagName("threadInfo")[0].getAttribute("currentThreadsBusy"
  34. elif item == "requestInfo.maxTime"
  35. print  doc.getElementsByTagName("requestInfo")[0].getAttribute("maxTime"
  36. elif item == "requestInfo.processingTime"
  37. print  doc.getElementsByTagName("requestInfo")[0].getAttribute("processingTime"
  38. elif item == "requestInfo.requestCount"
  39. print  doc.getElementsByTagName("requestInfo")[0].getAttribute("requestCount"
  40. elif item == "requestInfo.errorCount"
  41. print  doc.getElementsByTagName("requestInfo")[0].getAttribute("errorCount"
  42. elif item == "requestInfo.bytesReceived"
  43. print  doc.getElementsByTagName("requestInfo")[0].getAttribute("bytesReceived"
  44. elif item == "requestInfo.bytesSent"
  45. print  doc.getElementsByTagName("requestInfo")[0].getAttribute("bytesSent")  //Vevb.com 
  46. else
  47. print "unsupport item." 

這個腳本是監控Tomcat7的,Tomcat6沒有試過,應該區別在狀態頁的url以及管理頁面的用戶密碼設置上,以上腳本可運行需要在tomcat-users.xml里添加用戶,至少權限為manager-status.

導入模板:tomcat zabbix模板下載

Nginx監控,配置Nginx狀態頁,在nginx配置文件server{}中加入:

  1. location /nginx_status { 
  2.     stub_status on; 
  3.     access_log off; 

自定義鍵值:UserParameter=nginx[*],/data/sh/nginx-status.sh "$1"

nginx-status.sh腳本內容:

  1. #!/bin/bash 
  2. ################################## 
  3. # Zabbix monitoring script 
  4. # nginx: 
  5. #  - anything available via nginx stub-status module 
  6. ################################## 
  7. # Contact: 
  8. #  vincent.viallet@gmail.com 
  9. ################################## 
  10. # ChangeLog: 
  11. #  20100922        VV        initial creation 
  12. ################################## 
  13.  
  14. # Zabbix requested parameter 
  15. ZBX_REQ_DATA="$1" 
  16. ZBX_REQ_DATA_URL="$2" 
  17.  
  18. # Nginx defaults 
  19. URL="http://127.0.0.1/nginx_status" 
  20. WGET_BIN="/usr/bin/wget" 
  21.  
  22. # Error handling: 
  23. #  - need to be displayable in Zabbix (avoid NOT_SUPPORTED) 
  24. #  - items need to be of type "float" (allow negative + float) 
  25. ERROR_NO_ACCESS_FILE="-0.9900" 
  26. ERROR_NO_ACCESS="-0.9901" 
  27. ERROR_WRONG_PARAM="-0.9902" 
  28. ERROR_DATA="-0.9903" # either can not connect /        bad host / bad port 
  29.  
  30. # save the nginx stats in a variable for future parsing 
  31. NGINX_STATS=$($WGET_BIN -q $URL -O - 2> /dev/null) 
  32.  
  33. # error during retrieve 
  34. if [ $? -ne 0 -o -z "$NGINX_STATS" ]; then 
  35.   echo $ERROR_DATA 
  36.   exit 1 
  37. fi 
  38.  
  39. # Extract data from nginx stats 
  40. case $ZBX_REQ_DATA in 
  41.   active_connections)   echo "$NGINX_STATS" | head -1             | cut -f3 -d' ';; 
  42.   accepted_connections) echo "$NGINX_STATS" | grep -Ev '[a-zA-Z]' | cut -f2 -d' ';; 
  43.   handled_connections)  echo "$NGINX_STATS" | grep -Ev '[a-zA-Z]' | cut -f3 -d' ';; 
  44.   handled_requests)     echo "$NGINX_STATS" | grep -Ev '[a-zA-Z]' | cut -f4 -d' ';;   //Vevb.com 
  45.   reading)              echo "$NGINX_STATS" | tail -1             | cut -f2 -d' ';; 
  46.   writing)              echo "$NGINX_STATS" | tail -1             | cut -f4 -d' ';; 
  47.   waiting)              echo "$NGINX_STATS" | tail -1             | cut -f6 -d' ';; 
  48.   *) echo $ERROR_WRONG_PARAMexit 1;; 
  49. esac 
  50.  
  51. exit 0 

導入模板,nginx zabbix模板下載

MySQL監控:MySQL的監控,zabbix是默認支持的,已經有現成的模板,現成的鍵值,我們需要做的只是在/var/lib/zabbix里新建一個.my.cnf文件,內容如下:

  1. [client] 
  2. host=127.0.0.1 
  3. port=1036 
  4. user=root 
  5. password=root 

網站日志監控,配置日志格式,我們假設你用的web服務器是Nginx,我們添加一個日志格式,如下:

  1. log_format withHost  '$remote_addr\t$remote_user\t$time_local\t$host\t$request\t' 
  2.                 '$status\t$body_bytes_sent\t$http_referer\t' 
  3.                 '$http_user_agent'; 

我們使用tab作分隔符,為了方便awk識別列的內容,以防出錯,然后再設置全局的日志,其它server就不需要設置日志了:

access_log  /data/home/logs/nginx/$host.log withHost;

定時獲取一分鐘日志,設置一個定時任務:

* * * * * /data/sh/get_nginx_access.sh

腳本內容為:

  1. #!/bin/bash 
  2.  
  3. logDir=/data/home/logs/nginx/ 
  4. logNames=`ls ${logDir}/*.*.log  |awk -F"/" '{print $NF}'
  5.  
  6. for $logName in $logNames; 
  7. do 
  8. #設置變量 
  9. split_log="/tmp/split_$logName" 
  10. access_log="${logDir}/$logName" 
  11. status_log="/tmp/$logName" 
  12.  
  13. #取出最近一分鐘日志 
  14. tac $access_log  | awk ' 
  15. BEGIN
  16. FS="\t" 
  17. OFS="\t" 
  18. cmd="date -d \"minute ago\" +%H%M%S" 
  19. cmd|getline oneMinuteAgo 
  20. $3 = substr($3,13,8) 
  21. gsub(":","",$3) 
  22. if ($3>=oneMinuteAgo){ 
  23. print 
  24. else { 
  25. exit; 
  26. }' > $split_log 
  27.  
  28.  
  29. #統計狀態碼個數 
  30. awk -F'\t' '{ 
  31. status[$4" "$6]++ 
  32. END
  33. for (i in status) 
  34. print i,status[i] 
  35. ' $split_log  > $status_log 
  36. done 

這個定時任務是每分鐘執行,因為我們監控的頻率是每分鐘,添加這個任務是為了取得最近一分鐘各域名的日志,以及統計各域名的所有狀態碼個數,方便zabbix來獲取所需的數據.

自定義鍵值:

  1. UserParameter=nginx.detect,/data/sh/nginx-detect.sh 
  2. UserParameter=nginx.access[*],awk -v sum=0 -v domain=$1 -v code=$2 '{if($$1 == domain && $$2 == code ){sum+=$$3} }END{print sum}' /tmp/$1.log 
  3. UserParameter=nginx.log[*],awk -F'\t' -v domain=$1 -v code=$2 -v number=$3 -v sum=0 -v line="" '{if ($$4 == domain && $$6 == code ){sum++;line=line$$5"\n" }}END{if (sum > number) print line}' /tmp/split_$1.log | sort | uniq -c | sort -nr | head -10 | sed -e 's/^/<p>/' -e 's/$/<\/p>/' 

nginx-detect.sh腳本內容為:

  1. #!/bin/bash 
  2.  
  3. function json_head { 
  4.     printf "{" 
  5.     printf "\"data\":[" 
  6.  
  7. function json_end { 
  8.     printf "]" 
  9.     printf "}" 
  10.  
  11. function check_first_element { 
  12.     if [[ $FIRST_ELEMENT -ne 1 ]]; then 
  13.         printf "," 
  14.     fi 
  15.     FIRST_ELEMENT=0 
  16.  
  17. FIRST_ELEMENT=1 
  18. json_head 
  19.  
  20. logNames=`ls /data/home/logs/nginx/*.*.log |awk -F"/" '{print $NF}'` 
  21. for logName in $logNames; 
  22. do 
  23. while read domain code count;do 
  24.         check_first_element 
  25.         printf "{" 
  26.         printf "\"{#DOMAIN}\":\"$domain\",\"{#CODE}\":\"$code\"" 
  27.         printf "}" 
  28. done < /tmp/$logName 
  29. done 
  30. json_end 

這里我們定義了三個鍵值,nginx.detect是為了發現所有域名及其所有狀態碼,nginx.access[*]是為了統計指定域名的狀態碼的數量,nginx.log[*]是為了測試指定域名的狀態碼超過指定值時輸出排在前十的url,我們監控nginx訪問日志用到了zabbix的自動發現功能,當我們增加域名時,不需要修改腳本,zabbix會幫助我們自動發現新增的域名并作監控.

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 荃湾区| 昌图县| 邵东县| 尉氏县| 察哈| 台湾省| 申扎县| 伊川县| 五莲县| 武山县| 噶尔县| 临漳县| 屏东县| 东阳市| 五台县| 尼木县| 射阳县| 布尔津县| 望奎县| 陇南市| 宁安市| 双柏县| 鹰潭市| 临高县| 疏附县| 临潭县| 浪卡子县| 江津市| 红原县| 江孜县| 信阳市| 鄢陵县| 胶南市| 德惠市| 类乌齐县| 大城县| 罗甸县| 华池县| 兴文县| 南雄市| 衡南县|