1. 程式人生 > >Zabbix監控平臺(三)生產環境案例

Zabbix監控平臺(三)生產環境案例

Zabbix監控平臺(三)生產環境案例

  一,Zabbix生產環境監測案例概述

  • 1.1 專案規劃
  • 1.2 SNMP監控流程
  • 1.3 IPMI
  • 1.4 JMX(使用Zabbix Java Gateway代理)
  • 1.5 實戰監控Nginx,Apache,MySQL,PHP-fpm
  • 1.6 Zabbix的Web監測
  • 二,Zabbix監控MySQL資料庫操作實戰
    • 2.1 編寫監控指令碼
    • 2.2 在zabbix-agent端建立自定義鍵值配置檔案
    • 2.3 在zabbix-server端測試鍵值
    • 2.4 在zabbix-server端Web前臺引入zabbix監控MySQL的模版
  • 三,Zabbix監控Apache、Nginx操作實戰
    • 3.1 實戰監控Apache
      • 3.1.1 原始碼編譯構建apache服務
      • 3.1.2 開啟apache狀態頁
      • 3.1.3 下載apache zabbix模版並解壓
      • 3.1.4 測試鍵值
      • 3.1.5 在zabbix-server的Web頁面上監控apache
    • 3.2 實戰監控nginx
      • 3.2.1 開啟nginx狀態頁
      • 3.2.2 訪問設定好的nginx-status連結
      • 3.2.3 編寫nginx狀態監控指令碼
      • 3.2.4 給指令碼可執行許可權並且授權
      • 3.2.5 在zabbix-agent端配置nginx鍵值配置檔案
      • 3.2.6 在zabbix-server端測試鍵值
      • 3.2.7 接下來我們需要在Web端匯入nginx監控模版
      • 3.2.8 異常除錯
  • 四,Zabbix監控Tomcat操作實戰
    • 4.1 配置Tomcat JMX
    • 4.2 重啟Tomcat
    • 4.3 編譯zabbix,加入java支援
      • 4.3.1 在zabbix-server端安裝JDK
      • 4.3.2 重新編譯zabbix-server伺服器端
      • 4.3.3 啟動java Gateway服務
    • 4.4 修改zabbix server 配置
    • 4.5 重啟zabbix_server服務
    • 4.6 Zabbix圖形介面配置
      • 4.6.1 建立一個主機利用JMX介面監控tomcat被監控端
      • 4.6.2 匯入zabbix監控tomcat的模版
  • 五,Zabbix監控PHP-FPM操作實戰
    • 5.1 啟用php-fpm狀態功能
    • 5.2 nginx配置php-fpm狀態頁面
    • 5.3 狀態頁瀏覽器訪問測試
    • 5.4 在Zabbix agent端修改配置
    • 5.5 進行鍵值配置檔案測試
    • 5.6 Zabbix圖形介面匯入我們的監控模版
  • 六,Zabbix的Web監測功能

一,Zabbix生產環境監測案例概述

1.1 專案規劃

  • :主機分組

    • 交換機
    • Nginx
    • Tomcat
    • MySQL
    • Apache
    • PHP-fpm
    • redis(也有狀態頁,同學們自己研究)
    • memcache(也有狀態頁,同學們自己研究)
  • :監控物件識別:

    • 使用SNMP監控交換
    • 使用IPMI監控伺服器硬體
    • 使用Agent監控伺服器
    • 使用JMX監控JAVA
    • 監控MySQL
    • 監控Web狀態
    • 監控Nginx狀態
    • 監控Apache狀態
    • 監控Tomcat狀態

1.2 SNMP監控流程

  1. 交換機上開啟snmp
  2. 在zabbix上新增監控(設定SNMP interfaces)
  3. 關聯監控模版

1.3 IPMI

建議使用自定義item監控伺服器硬體,在本地執行ipmitool命令來獲取資料

可以在本地執行ipmitool命令獲取資料(命令需要安裝)

1.4 JMX(使用Zabbix Java Gateway代理)

用來獲取java資料,本次的實戰內容

1.5 實戰監控Nginx,Apache,MySQL,PHP-fpm

今天的實戰內容,請同學們注意總結過程

1.6 Zabbix的Web監測

今天的實戰內容,請同學們注意總結過程

二,Zabbix監控MySQL資料庫操作實戰

我們需要部署一個MySQL服務和一個zabbix-agentd客戶端

2.1 編寫監控指令碼

在nginx伺服器(192.168.0.221)上安裝一個MySQL
下面是監控mysql狀態的一個shell指令碼,內容如下:

  1. [[email protected] scripts]# pwd
  2. /server/scripts
  3. [[email protected] scripts]# cat check_mysql.sh
  4. #!/bin/bash
  5. # author:Mr.chen
  6. MySQL_USER="root"
  7. MySQL_PWD="123123"
  8. MySQL_HOST="127.0.0.1"
  9. MySQL_PORT="3306"
  10. MySQL_CONN="/usr/local/mysql/bin/mysqladmin -u${MySQL_USER} -h${MySQL_HOST} -P${MySQL_PORT} -p${MySQL_PWD}"
  11. if [ $# -ne "1" ];then
  12. echo "arg error!"
  13. fi
  14. case $1 in
  15. Uptime)
  16. result=`${MySQL_CONN} status | cut -f2 -d":" | cut -f1 -d "T"`
  17. echo $result
  18. ;;
  19. Com_update)
  20. result=`${MySQL_CONN} extended-status | grep -w "Com_update" | cut -d"|" -f3`
  21. echo $result
  22. ;;
  23. Slow_querles)
  24. result=`${MySQL_CONN} status | cut -f5 -d":" | cut -f1 -d"O"`
  25. echo $result
  26. ;;
  27. Com_select)
  28. result=`${MySQL_CONN} extended-status | grep -w "Com_select" | cut -d "|" -f3`
  29. echo $result
  30. ;;
  31. Com_rollback)
  32. result=`${MySQL_CONN} extended-status | grep -w "Com_rollback" | cut -d"|" -f3`
  33. echo $result
  34. ;;
  35. Questions)
  36. result=`${MySQL_CONN} status | cut -f4 -d":" | cut -f1 -d"S"`
  37. echo $result
  38. ;;
  39. Com_insert)
  40. result=`${MySQL_CONN} extended-status | grep -w "Com_insert" | cut -d"|" -f3`
  41. echo $result
  42. ;;
  43. Com_delete)
  44. result=`${MySQL_CONN} extended-status | grep -w "Com_delete" | cut -d"|" -f3`
  45. echo $result
  46. ;;
  47. Com_commit)
  48. result=`${MySQL_CONN} extended-status | grep -w "Com_commit" | cut -d"|" -f3`
  49. echo $result
  50. ;;
  51. Bytes_sent)
  52. result=`${MySQL_CONN} extended-status | grep -w "Bytes_sent" | cut -d"|" -f3`
  53. echo $result
  54. ;;
  55. Bytes_received)
  56. result=`${MySQL_CONN} extended-status | grep -w "Bytes_received" | cut -d"|" -f3`
  57. echo $result
  58. ;;
  59. Com_begin)
  60. result=`${MySQL_CONN} extended-status | grep -w "Com_begin" | cut -d"|" -f3`
  61. echo $result
  62. ;;
  63. *)
  64. echo "Usage:$0(Uptime|Com_update|Slow_querles|Com_rollback|Questions|Com_insert|Com_delete|Com_commit|Bytes_sent|Bytes_received|Com_begin)"
  65. ;;
  66. esac

2.2 在zabbix-agent端建立自定義鍵值配置檔案

  1. #建立mysql.status的鍵值
  2. [[email protected] scripts]# chmod +x check_mysql.sh
  3. [[email protected] scripts]# chown zabbix.zabbix check_mysql.sh
  4. [[email protected] zabbix]# cd /etc/zabbix/zabbix_agentd.d/
  5. [[email protected] zabbix_agentd.d]# cat mysql_status.conf
  6. UserParameter=mysql.status[*],/server/scripts/check_mysql.sh $1
  7. #刪除舊鍵值模版
  8. [[email protected] zabbix_agentd.d]# rm -rf userparameter_mysql.conf
  9. #建立mysql.ping和mysql.version的鍵值
  10. [[email protected] zabbix_agentd.d]# cat mysql_status.conf
  11. UserParameter=mysql.status[*],/server/scripts/check_mysql.sh $1
  12. UserParameter=mysql.ping,/usr/local/mysql/bin/mysqladmin -uroot -p123123 ping | grep -c alive
  13. UserParameter=mysql.version,/usr/local/bin/mysql -V
  14. #重啟zabbix-agentd服務
  15. [[email protected] zabbix_agentd.d]# /etc/init.d/zabbix-agent restart
  16. Shutting down Zabbix agent: [ OK ]
  17. Starting Zabbix agent: [ OK ]

2.3 在zabbix-server端測試鍵值

  1. [[email protected]_Server scripts]# zabbix_get -s 192.168.0.221 -p 10050 -k "mysql.status[Uptime]"
  2. 2791
  3. [[email protected]_Server scripts]# zabbix_get -s 192.168.0.221 -p 10050 -k "mysql.status[Com_commit]"
  4. 0
  5. [[email protected]_Server scripts]# zabbix_get -s 192.168.0.221 -p 10050 -k "mysql.status[Bytes_sent]"
  6. 144628
  7. [[email protected]_Server scripts]# zabbix_get -s 192.168.0.221 -p 10050 -k "mysql.status[Bytes_received]"
  8. 3934
  9. [[email protected]_Server scripts]# zabbix_get -s 192.168.0.221 -p 10050 -k "mysql.ping"
  10. 1
  11. [[email protected]_Server scripts]# zabbix_get -s 192.168.0.221 -p 10050 -k "mysql.version"
  12. /usr/local/bin/mysql Ver 14.14 Distrib 5.5.32, for linux2.6 (x86_64) using readline 5.1

2.4 在zabbix-server端Web前臺引入zabbix監控MySQL的模版

zabbix自帶監控MySQL的模版,我們只需要將模版引入到主機即可
配置-->主機-->選擇主機-->模板,然後將MySQL模版連線到此主機即可

QQ截圖20180122210222.png-42.2kB

QQ截圖20180122210235.png-25.5kB

QQ截圖20180122210257.png-20.8kB

QQ截圖20180122210610.png-36.3kB

QQ截圖20180122210625.png-88kB

從圖中可以發現模版裡的鍵值都是我們之前已經定義好了的鍵值。

三,Zabbix監控Apache、Nginx操作實戰

3.1 實戰監控Apache

3.1.1 原始碼編譯構建apache服務

  1. #為了避免衝突,若系統中已經安裝httpd服務,務必刪除
  2. [[email protected] ~]# rpm -q httpd
  3. package httpd is not installed
  4. [[email protected] ~]# /etc/init.d/iptables stop
  5. [[email protected] ~]# setenforce 0
  6. setenforce: SELinux is disabled
  7. #原始碼構建apache
  8. [[email protected] httpd-2.2.17]# tar xf httpd-2.2.17.tar.gz -C /usr/src/
  9. [[email protected] httpd-2.2.17]# cd /usr/src/httpd-2.2.17/
  10. [[email protected] httpd-2.2.17]# ./configure --prefix=/usr/local/httpd --enable-so --enable-rewrite --enable-charset-lite --enable-cgi && make && make install
  11. [[email protected] httpd-2.2.17]# cd /usr/local/httpd/
  12. [[email protected] httpd]# ls
  13. bin build cgi-bin conf error htdocs icons include lib logs man manual modules
  14. [[email protected] httpd]# ln -s /usr/local/httpd/bin/* /usr/local/bin/
  15. [[email protected] httpd]# cp /usr/local/httpd/bin/apachectl /etc/init.d/httpd
  16. [[email protected] httpd]# chmod +x /etc/init.d/httpd
  17. [[email protected] ~]# cd /usr/local/httpd/htdocs/
  18. [[email protected] htdocs]# echo "`hostname -I` I am apache" > index.html
  19. [[email protected] htdocs]# cat index.html
  20. 192.168.0.222 I am apache
  21. [[email protected] conf]# awk '/#ServerName/{print NR,$0}' httpd.conf
  22. 97 #ServerName www.example.com:80
  23. [[email protected] conf]# sed -i -e '97 s/#//;s/example/yunjisuan/' httpd.conf
  24. [[email protected] conf]# sed -n '97p' httpd.conf
  25. ServerName www.yunjisuan.com:80
  26. [[email protected] conf]# /etc/init.d/httpd start
  27. [[email protected] conf]# netstat -antup | grep httpd
  28. tcp 0 0 :::80 :::* LISTEN 53288/httpd
  29. [[email protected] conf]# echo "`hostname -I` www.yunjisuan.com" >> /etc/hosts
  30. [[email protected] conf]# tail -1 /etc/hosts
  31. 192.168.0.222 www.yunjisuan.com
  32. [[email protected] conf]# curl www.yunjisuan.com
  33. 192.168.0.222 I am apache

3.1.2 開啟apache狀態頁

要開啟apache的ServerStatus頁面,需要在httpd.conf檔案最下邊加入程式碼段

  1. ExtendedStatus On
  2. <location /server-status>
  3. SetHandler server-status
  4. Order Deny,Allow
  5. Deny from all
  6. Allow from 127.0.0.1
  7. </location>

重啟動apache即可

3.1.3 下載apache zabbix模版並解壓

  1. #從如下地址下載apache zabbix模版:
  2. [[email protected] ~]# wget https://github.com/lorf/zapache/archive/master.zip
  3. #解壓zip格式壓縮檔案
  4. [[email protected] ~]# unzip master
  5. [[email protected] ~]# ls -d zapache-master
  6. zapache-master
  7. [[email protected] ~]# cd zapache-master/
  8. [[email protected] ~]# tree zapache-master/
  9. zapache-master/
  10. ├── httpd-server-status.conf.sample
  11. ├── README.md
  12. ├── userparameter_zapache.conf.sample
  13. ├── zapache
  14. ├── zapache-template-active.xml
  15. └── zapache-template.xml
  16. 0 directories, 6 files
  • 此模版檔案中包含了監控apache的指令碼檔案,zabbix的apache模版檔案,還有客戶端zabbix自定義傳key檔案。
  • 首先將zapache放到需要監控的apache伺服器上的/etc/zabbix/scripts目錄下,如果沒有scripts目錄,自行建立一個即可。然後執行授權:
  1. [[email protected] zapache-master]# chmod 755 /etc/zabbix/scripts/zapache
  2. [[email protected] scripts]# cd /etc/zabbix/scripts/
  3. [[email protected] scripts]# ./zapache
  4. zapache version: 1.5
  5. usage:
  6. ./zapache [<url>] TotalAccesses - Check total accesses.
  7. ./zapache [<url>] TotalKBytes - Check total KBytes.
  8. ./zapache [<url>] CPULoad - Check CPU load.
  9. ./zapache [<url>] Uptime - Check uptime.
  10. ./zapache [<url>] ReqPerSec - Check requests per second.
  11. ./zapache [<url>] BytesPerSec - Check Bytes per second.
  12. ./zapache [<url>] BytesPerReq - Check Bytes per request.
  13. ./zapache [<url>] BusyWorkers - Check busy workers.
  14. ./zapache [<url>] IdleWorkers - Check idle workers.
  15. ./zapache [<url>] version - Version of this script.
  16. ./zapache [<url>] ping - Check if Apache is up.
  17. ./zapache [<url>] WaitingForConnection - Check Waiting for Connection processess.
  18. ./zapache [<url>] StartingUp - Check Starting Up processess.
  19. ./zapache [<url>] ReadingRequest - Check Reading Request processess.
  20. ./zapache [<url>] SendingReply - Check Sending Reply processess.
  21. ./zapache [<url>] KeepAlive - Check KeepAlive Processess.
  22. ./zapache [<url>] DNSLookup - Check DNSLookup Processess.
  23. ./zapache [<url>] ClosingConnection - Check Closing Connection Processess.
  24. ./zapache [<url>] Logging - Check Logging Processess.
  25. ./zapache [<url>] GracefullyFinishing - Check Gracefully Finishing Processess.
  26. ./zapache [<url>] IdleCleanupOfWorker - Check Idle Cleanup of Worker Processess.
  27. ./zapache [<url>] OpenSlotWithNoCurrentProcess - Check Open Slots with No Current Process.
  • 接著將userparameter_zapache.conf.sample檔案放到/etc/zabbix/zabbix_agentd.d目錄下,並重命名為userparameter_zapache.conf檔案,然後修改鍵值命令的路徑。
  • 最後,重啟zabbix-agent服務
  1. [[email protected] zapache-master]# mv userparameter_zapache.conf.sample /etc/zabbix/zabbix_agentd.d/userparameter_zapache.conf
  2. [[email protected] zabbix_agentd.d]# tail -1 userparameter_zapache.conf
  3. UserParameter=zapache[*],/etc/zabbix/