Zabbix監控平臺(三)生產環境案例
阿新 • • 發佈:2019-01-13
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 異常除錯
- 3.1 實戰監控Apache
- 四,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監控流程
- 交換機上開啟snmp
- 在zabbix上新增監控(設定SNMP interfaces)
- 關聯監控模版
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指令碼,內容如下:
[[email protected] scripts]# pwd
/server/scripts
[[email protected] scripts]# cat check_mysql.sh
#!/bin/bash
# author:Mr.chen
MySQL_USER="root"
MySQL_PWD="123123"
MySQL_HOST="127.0.0.1"
MySQL_PORT="3306"
MySQL_CONN="/usr/local/mysql/bin/mysqladmin -u${MySQL_USER} -h${MySQL_HOST} -P${MySQL_PORT} -p${MySQL_PWD}"
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_querles)
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_querles|Com_rollback|Questions|Com_insert|Com_delete|Com_commit|Bytes_sent|Bytes_received|Com_begin)"
;;
esac
2.2 在zabbix-agent端建立自定義鍵值配置檔案
#建立mysql.status的鍵值
[[email protected] scripts]# chmod +x check_mysql.sh
[[email protected] scripts]# chown zabbix.zabbix check_mysql.sh
[[email protected] zabbix]# cd /etc/zabbix/zabbix_agentd.d/
[[email protected] zabbix_agentd.d]# cat mysql_status.conf
UserParameter=mysql.status[*],/server/scripts/check_mysql.sh $1
#刪除舊鍵值模版
[[email protected] zabbix_agentd.d]# rm -rf userparameter_mysql.conf
#建立mysql.ping和mysql.version的鍵值
[[email protected] zabbix_agentd.d]# cat mysql_status.conf
UserParameter=mysql.status[*],/server/scripts/check_mysql.sh $1
UserParameter=mysql.ping,/usr/local/mysql/bin/mysqladmin -uroot -p123123 ping | grep -c alive
UserParameter=mysql.version,/usr/local/bin/mysql -V
#重啟zabbix-agentd服務
[[email protected] zabbix_agentd.d]# /etc/init.d/zabbix-agent restart
Shutting down Zabbix agent: [ OK ]
Starting Zabbix agent: [ OK ]
2.3 在zabbix-server端測試鍵值
[[email protected]_Server scripts]# zabbix_get -s 192.168.0.221 -p 10050 -k "mysql.status[Uptime]"
2791
[[email protected]_Server scripts]# zabbix_get -s 192.168.0.221 -p 10050 -k "mysql.status[Com_commit]"
0
[[email protected]_Server scripts]# zabbix_get -s 192.168.0.221 -p 10050 -k "mysql.status[Bytes_sent]"
144628
[[email protected]_Server scripts]# zabbix_get -s 192.168.0.221 -p 10050 -k "mysql.status[Bytes_received]"
3934
[[email protected]_Server scripts]# zabbix_get -s 192.168.0.221 -p 10050 -k "mysql.ping"
1
[[email protected]_Server scripts]# zabbix_get -s 192.168.0.221 -p 10050 -k "mysql.version"
/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模版連線到此主機即可
從圖中可以發現模版裡的鍵值都是我們之前已經定義好了的鍵值。
三,Zabbix監控Apache、Nginx操作實戰
3.1 實戰監控Apache
3.1.1 原始碼編譯構建apache服務
#為了避免衝突,若系統中已經安裝httpd服務,務必刪除
[[email protected] ~]# rpm -q httpd
package httpd is not installed
[[email protected] ~]# /etc/init.d/iptables stop
[[email protected] ~]# setenforce 0
setenforce: SELinux is disabled
#原始碼構建apache
[[email protected] httpd-2.2.17]# tar xf httpd-2.2.17.tar.gz -C /usr/src/
[[email protected] httpd-2.2.17]# cd /usr/src/httpd-2.2.17/
[[email protected] httpd-2.2.17]# ./configure --prefix=/usr/local/httpd --enable-so --enable-rewrite --enable-charset-lite --enable-cgi && make && make install
[[email protected] httpd-2.2.17]# cd /usr/local/httpd/
[[email protected] httpd]# ls
bin build cgi-bin conf error htdocs icons include lib logs man manual modules
[[email protected] httpd]# ln -s /usr/local/httpd/bin/* /usr/local/bin/
[[email protected] httpd]# cp /usr/local/httpd/bin/apachectl /etc/init.d/httpd
[[email protected] httpd]# chmod +x /etc/init.d/httpd
[[email protected] ~]# cd /usr/local/httpd/htdocs/
[[email protected] htdocs]# echo "`hostname -I` I am apache" > index.html
[[email protected] htdocs]# cat index.html
192.168.0.222 I am apache
[[email protected] conf]# awk '/#ServerName/{print NR,$0}' httpd.conf
97 #ServerName www.example.com:80
[[email protected] conf]# sed -i -e '97 s/#//;s/example/yunjisuan/' httpd.conf
[[email protected] conf]# sed -n '97p' httpd.conf
ServerName www.yunjisuan.com:80
[[email protected] conf]# /etc/init.d/httpd start
[[email protected] conf]# netstat -antup | grep httpd
tcp 0 0 :::80 :::* LISTEN 53288/httpd
[[email protected] conf]# echo "`hostname -I` www.yunjisuan.com" >> /etc/hosts
[[email protected] conf]# tail -1 /etc/hosts
192.168.0.222 www.yunjisuan.com
[[email protected] conf]# curl www.yunjisuan.com
192.168.0.222 I am apache
3.1.2 開啟apache狀態頁
要開啟apache的ServerStatus頁面,需要在httpd.conf檔案最下邊加入程式碼段
ExtendedStatus On
<location /server-status>
SetHandler server-status
Order Deny,Allow
Deny from all
Allow from 127.0.0.1
</location>
重啟動apache即可
3.1.3 下載apache zabbix模版並解壓
#從如下地址下載apache zabbix模版:
[[email protected] ~]# wget https://github.com/lorf/zapache/archive/master.zip
#解壓zip格式壓縮檔案
[[email protected] ~]# unzip master
[[email protected] ~]# ls -d zapache-master
zapache-master
[[email protected] ~]# cd zapache-master/
[[email protected] ~]# tree zapache-master/
zapache-master/
├── httpd-server-status.conf.sample
├── README.md
├── userparameter_zapache.conf.sample
├── zapache
├── zapache-template-active.xml
└── zapache-template.xml
0 directories, 6 files
- 此模版檔案中包含了監控apache的指令碼檔案,zabbix的apache模版檔案,還有客戶端zabbix自定義傳key檔案。
- 首先將zapache放到需要監控的apache伺服器上的/etc/zabbix/scripts目錄下,如果沒有scripts目錄,自行建立一個即可。然後執行授權:
[[email protected] zapache-master]# chmod 755 /etc/zabbix/scripts/zapache
[[email protected] scripts]# cd /etc/zabbix/scripts/
[[email protected] scripts]# ./zapache
zapache version: 1.5
usage:
./zapache [<url>] TotalAccesses - Check total accesses.
./zapache [<url>] TotalKBytes - Check total KBytes.
./zapache [<url>] CPULoad - Check CPU load.
./zapache [<url>] Uptime - Check uptime.
./zapache [<url>] ReqPerSec - Check requests per second.
./zapache [<url>] BytesPerSec - Check Bytes per second.
./zapache [<url>] BytesPerReq - Check Bytes per request.
./zapache [<url>] BusyWorkers - Check busy workers.
./zapache [<url>] IdleWorkers - Check idle workers.
./zapache [<url>] version - Version of this script.
./zapache [<url>] ping - Check if Apache is up.
./zapache [<url>] WaitingForConnection - Check Waiting for Connection processess.
./zapache [<url>] StartingUp - Check Starting Up processess.
./zapache [<url>] ReadingRequest - Check Reading Request processess.
./zapache [<url>] SendingReply - Check Sending Reply processess.
./zapache [<url>] KeepAlive - Check KeepAlive Processess.
./zapache [<url>] DNSLookup - Check DNSLookup Processess.
./zapache [<url>] ClosingConnection - Check Closing Connection Processess.
./zapache [<url>] Logging - Check Logging Processess.
./zapache [<url>] GracefullyFinishing - Check Gracefully Finishing Processess.
./zapache [<url>] IdleCleanupOfWorker - Check Idle Cleanup of Worker Processess.
./zapache [<url>] OpenSlotWithNoCurrentProcess - Check Open Slots with No Current Process.
- 接著將userparameter_zapache.conf.sample檔案放到/etc/zabbix/zabbix_agentd.d目錄下,並重命名為userparameter_zapache.conf檔案,然後修改鍵值命令的路徑。
- 最後,重啟zabbix-agent服務
[[email protected] zapache-master]# mv userparameter_zapache.conf.sample /etc/zabbix/zabbix_agentd.d/userparameter_zapache.conf
[[email protected] zabbix_agentd.d]# tail -1 userparameter_zapache.conf
UserParameter=zapache[*],/etc/zabbix/