Linux實戰教學筆記51:Zabbix監控平臺3.2.4(三)生產環境案例

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

1.1 項目規劃

  • [x] :主機分組
    • 交換機
    • Nginx
    • Tomcat
    • MySQL
    • Apache
    • PHP-fpm
    • redis(也有狀態頁,同窗們本身研究)
    • memcache(也有狀態頁,同窗們本身研究)
  • [x] :監控對象識別:
    • 使用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命令來獲取數據php

能夠在本地執行ipmitool命令獲取數據(命令須要安裝)html

1.4 JMX(使用Zabbix Java Gateway代理)

用來獲取java數據,本次的實戰內容java

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

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

1.6 Zabbix的Web監測

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

二,Zabbix監控MySQL數據庫操做實戰

咱們須要部署一個MySQL服務和一個zabbix-agentd客戶端linux

2.1 編寫監控腳本

在nginx服務器(192.168.0.221)上安裝一個MySQL
下面是監控mysql狀態的一個shell腳本,內容以下:ios

[root@nginx scripts]# pwd
/server/scripts
[root@nginx 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的鍵值
[root@nginx scripts]# chmod +x check_mysql.sh 
[root@nginx scripts]# chown zabbix.zabbix check_mysql.sh
[root@nginx zabbix]# cd /etc/zabbix/zabbix_agentd.d/
[root@nginx zabbix_agentd.d]# cat mysql_status.conf 
UserParameter=mysql.status[*],/server/scripts/check_mysql.sh $1

#刪除舊鍵值模版
[root@nginx zabbix_agentd.d]# rm -rf userparameter_mysql.conf 

#建立mysql.ping和mysql.version的鍵值
[root@nginx 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服務
[root@nginx zabbix_agentd.d]# /etc/init.d/zabbix-agent restart
Shutting down Zabbix agent:                                [  OK  ]
Starting Zabbix agent:                                     [  OK  ]

2.3 在zabbix-server端測試鍵值

[root@Zabbix_Server scripts]# zabbix_get -s 192.168.0.221 -p 10050 -k "mysql.status[Uptime]"
2791
[root@Zabbix_Server scripts]# zabbix_get -s 192.168.0.221 -p 10050 -k "mysql.status[Com_commit]"
0
[root@Zabbix_Server scripts]# zabbix_get -s 192.168.0.221 -p 10050 -k "mysql.status[Bytes_sent]"
144628
[root@Zabbix_Server scripts]# zabbix_get -s 192.168.0.221 -p 10050 -k "mysql.status[Bytes_received]"
3934
[root@Zabbix_Server scripts]# zabbix_get -s 192.168.0.221 -p 10050 -k "mysql.ping"
1
[root@Zabbix_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模版鏈接到此主機便可nginx

QQ截圖20180122210222.png-42.2kB

QQ截圖20180122210235.png-25.5kB

QQ截圖20180122210257.png-20.8kB

QQ截圖20180122210610.png-36.3kB

QQ截圖20180122210625.png-88kB

從圖中能夠發現模版裏的鍵值都是咱們以前已經定義好了的鍵值。git

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

3.1 實戰監控Apache

3.1.1 源碼編譯構建apache服務

#爲了不衝突,若系統中已經安裝httpd服務,務必刪除
[root@apache ~]# rpm -q httpd
package httpd is not installed
[root@apache ~]# /etc/init.d/iptables stop
[root@apache ~]# setenforce 0
setenforce: SELinux is disabled

#源碼構建apache
[root@apache httpd-2.2.17]# tar xf httpd-2.2.17.tar.gz -C /usr/src/
[root@apache httpd-2.2.17]# cd /usr/src/httpd-2.2.17/
[root@apache httpd-2.2.17]# ./configure --prefix=/usr/local/httpd --enable-so --enable-rewrite --enable-charset-lite --enable-cgi && make && make install
[root@apache httpd-2.2.17]# cd /usr/local/httpd/
[root@apache httpd]# ls
bin  build  cgi-bin  conf  error  htdocs  icons  include  lib  logs  man  manual  modules
[root@apache httpd]# ln -s /usr/local/httpd/bin/* /usr/local/bin/
[root@apache httpd]# cp /usr/local/httpd/bin/apachectl /etc/init.d/httpd
[root@apache httpd]# chmod +x /etc/init.d/httpd
[root@apache ~]# cd /usr/local/httpd/htdocs/
[root@apache htdocs]# echo "`hostname -I` I am apache" > index.html
[root@apache htdocs]# cat index.html 
192.168.0.222  I am apache
[root@apache conf]# awk '/#ServerName/{print NR,$0}' httpd.conf 
97 #ServerName www.example.com:80
[root@apache conf]# sed -i -e '97 s/#//;s/example/yunjisuan/' httpd.conf 
[root@apache conf]# sed -n '97p' httpd.conf 
ServerName www.yunjisuan.com:80
[root@apache conf]# /etc/init.d/httpd start
[root@apache conf]# netstat -antup | grep httpd
tcp        0      0 :::80                       :::*                        LISTEN      53288/httpd         
[root@apache conf]# echo "`hostname -I` www.yunjisuan.com" >> /etc/hosts
[root@apache conf]# tail -1 /etc/hosts
192.168.0.222  www.yunjisuan.com
[root@apache conf]# curl www.yunjisuan.com
192.168.0.222  I am apache

3.1.2 開啓apache狀態頁

要打開apache的ServerStatus頁面,須要在httpd.conf文件最下邊加入代碼段github

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模版:
[root@apache ~]# wget https://github.com/lorf/zapache/archive/master.zip
#解壓zip格式壓縮文件
[root@apache ~]# unzip master
[root@apache ~]# ls -d zapache-master
zapache-master
[root@apache ~]# cd zapache-master/
[root@apache ~]# 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目錄,自行建立一個便可。而後執行受權:
[root@apache zapache-master]# chmod 755 /etc/zabbix/scripts/zapache
[root@apache scripts]# cd /etc/zabbix/scripts/
[root@apache 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服務
[root@apache zapache-master]# mv userparameter_zapache.conf.sample /etc/zabbix/zabbix_agentd.d/userparameter_zapache.conf
[root@apache zabbix_agentd.d]# tail -1 userparameter_zapache.conf 
UserParameter=zapache[*],/etc/zabbix/scripts/zapache \$1    #本行要修改
[root@apache zapache-master]# /etc/init.d/zabbix-agent restart
Shutting down Zabbix agent:                                [  OK  ]
Starting Zabbix agent:                                     [  OK  ]

3.1.4 測試鍵值

#在zabbix-agent端查看腳本用法
[root@apache zabbix_agentd.d]# cd /etc/zabbix/scripts/
[root@apache 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.

咱們發現腳本須要傳遞兩個參數,即url和選項
而後咱們在zabbix-server端進行鍵值測試

[root@Zabbix_Server scripts]# zabbix_get -s 192.168.0.222 -p 10050 -k "zapache[127.0.0.1 TotalAccesses]"
9
[root@Zabbix_Server scripts]# zabbix_get -s 192.168.0.222 -p 10050 -k "zapache[www.yunjisuan.com version]"
1.5
[root@Zabbix_Server scripts]# zabbix_get -s 192.168.0.222 -p 10050 -k "zapache[www.yunjisuan.com ping]"
1

3.1.5 在zabbix-server的Web頁面上監控apache

(1)建立一個監控apache的主機

QQ截圖20180122233702.png-36.7kB

QQ截圖20180122233846.png-28.8kB

(2)導入帶有apache監控項的模版

zabbix默認並無監控apache的監控項模版。咱們須要將它導入到apache中。在以前解壓出來的目錄中,有待導入的web模版

[root@apache ~]# tree zapache-master/
zapache-master/
├── httpd-server-status.conf.sample
├── README.md
├── zapache-template-active.xml #Web模版
└── zapache-template.xml    #Web模版

0 directories, 4 files

咱們將這兩個.xml後綴的拷貝到windows上,而後從Web界面導入進去

QQ截圖20180122234457.png-17.2kB

QQ截圖20180122234649.png-32.8kB

導入之後Web端就有監控apache的監控項模版了

(3)將模版連接到apache主機

QQ截圖20180122234811.png-27.5kB

QQ截圖20180122234841.png-23.9kB

QQ截圖20180122235146.png-26.2kB

3.2 實戰監控nginx

3.2.1 開啓nginx狀態頁

#在nginx的配置文件中,添加status配置
location = /nginx-status {
    stub_status on;
    access_log  off;
    allow 127.0.0.1;
    allow 192.168.0.220;    #zabbix服務器地址通常是內網地址
    deny all;
}

3.2.2 訪問設置好的nginx-status連接

#在zabbix-server上經過以下方式獲取nginx狀態頁面信息:
root@Zabbix_Server ~]# curl 192.168.0.221/nginx-status
Active connections: 1 
server accepts handled requests
 634 634 618 
Reading: 0 Writing: 1 Waiting: 0 

nginx status詳細說明以下:
Active connections:對後端發起的活動鏈接數;
accepts:nginx總共處理了多少個鏈接;
handled:nginx成功建立了幾回握手;
requests:nginx總共處理了多少請求。
Reading:nginx讀取客戶端的header數;
Writing:nginx返回給客戶端的header數;
Waiting:nginx請求處理完成,正在等待下一請求指令的鏈接

3.2.3 編寫nginx狀態監控腳本

#下面是nginx狀態監控抓取腳本,內容以下:
[root@nginx scripts]# cat nginx_status.sh 
#!/bin/bash
# Set Variables

HOST="127.0.0.1"
PORT="80"

#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 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

3.2.4 給腳本可執行權限而且受權

[root@nginx scripts]# chmod +x nginx_status.sh 
[root@nginx scripts]# chown zabbix.zabbix nginx_status.sh

3.2.5 在zabbix-agent端配置nginx鍵值配置文件

[root@nginx zabbix_agentd.d]# pwd
/etc/zabbix/zabbix_agentd.d
[root@nginx zabbix_agentd.d]# cat nginx.conf 
UserParameter=nginx.active,/usr/bin/curl -s "http://127.0.0.1:80/nginx-status" | grep "Active" | awk '{print $NF}'
UserParameter=nginx.status[*],/etc/zabbix/scripts/nginx_status.sh $1
#重啓agent客戶端
[root@nginx zabbix_agentd.d]# /etc/init.d/zabbix-agent restart
Shutting down Zabbix agent:                                [  OK  ]
Starting Zabbix agent:                                     [  OK  ]

3.2.6 在zabbix-server端測試鍵值

[root@Zabbix_Server ~]# zabbix_get -s 192.168.0.221 -k "nginx.status[reading]" -p 10050
0
[root@Zabbix_Server ~]# zabbix_get -s 192.168.0.221 -k "nginx.status[active]" -p 10050
1
[root@Zabbix_Server ~]# zabbix_get -s 192.168.0.221 -k "nginx.status[reading]" -p 10050
0
[root@Zabbix_Server ~]# zabbix_get -s 192.168.0.221 -k "nginx.status[writing]" -p 10050
1
[root@Zabbix_Server ~]# zabbix_get -s 192.168.0.221 -k "nginx.status[ping]" -p 10050
1

3.2.7 接下來咱們須要在Web端導入nginx監控模版

QQ截圖20180126105441.png-26.7kB

QQ截圖20180126105511.png-49.3kB

QQ截圖20180126105523.png-34.5kB

QQ截圖20180126105544.png-29.5kB

3.2.8 異常調試

若是Zabbix圖形界面一直沒有獲取到數據,能夠經過在zabbix server上執行zabbix_get手動測試:
/usr/local/zabbix/bin/zabbix_get -s 192.168.0.221 -p 10050 -k "nginx.status[active]"
若是獲取數據有問題,會給出相應提示,若是可以獲取到數據,那就沒問題了。

四,Zabbix監控Tomcat操做實戰

zabbix監控tomcat,須要zabbix_server開啓java poller,zabbix_java開啓javaGateway,端口10052,Tomcat JMX開啓12345端口,提供性能數據。

數據獲取流程爲:java poller<-->javaGateway:10052<-->Tomcat:12345

QQ截圖20180126111607.png-76.3kB

4.1 配置Tomcat JMX

#配置jmx,在須要監控的tomcat服務器上,編輯catalina.sh,加入以下配置
[root@Tomcat bin]# sed -n '249p' /usr/local/tomcat/bin/catalina.sh 
CATALINA_OPTS="-server -Xms256m -Xmx512m -XX:PermSize=64M -XX:MaxPermSize=128m -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=192.168.0.224 -Dcom.sun.management.jmxremote.port=12345"

這裏須要注意,必須增長-Djava.rmi.server.hostname選項,而且後面的IP就是tomcat服務器的IP

4.2 重啓Tomcat

[root@Tomcat bin]# /usr/local/tomcat/bin/shutdown.sh 
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/local/jdk
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
[root@Tomcat ~]# netstat -antup | grep java
[root@Tomcat bin]# /usr/local/tomcat/bin/startup.sh 
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/local/jdk
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
[root@Tomcat zabbix]# netstat -antup | grep java
tcp        0      0 :::54031                    :::*                        LISTEN      1521/java           
tcp        0      0 :::8080                     :::*                        LISTEN      1521/java           
tcp        0      0 :::53881                    :::*                        LISTEN      1521/java           
tcp        0      0 :::12345                    :::*                      LISTEN      1521/java          #新增端口 
tcp        0      0 ::ffff:127.0.0.1:8005       :::*                        LISTEN      1521/java           
tcp        0      0 :::8009                     :::*                        LISTEN      1521/java

4.3 編譯zabbix,加入java支持

在zabbix server服務器上,編譯安裝zabbix-server,須要加上--enable-java,以支持jmx監控,若是以前的zabbix server沒加此選項,那麼須要從新編譯安裝,編譯參數以下:

./configure --prefix=/usr/local/zabbix --with-mysql --with-net-snmp --with-libcurl --enable-server --enable-agent --enable-proxy --enable-java --with-libxml2

特別提示:
在啓用--enable-java編譯zabbix以前,zabbix server服務器上須要安裝JDK,並須要設置JAVA_HOME,以讓系統可以識別到jdk的路徑

4.3.1 在zabbix-server端安裝JDK

#解壓jdk
[root@Zabbix_Server ~]# tar xf jdk-8u60-linux-x64.tar.gz -C /usr/local/
[root@Zabbix_Server ~]# ln -s /usr/local/jdk1.8.0_60/ /usr/local/jdk
#配置java環境變量
[root@Zabbix_Server ~]# sed -i.ori '$a export JAVA_HOME=/usr/local/jdk\nexport PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH\nexport CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/lib/tools.jar' /etc/profile
[root@Zabbix_Server ~]# tail -3 /etc/profile
export JAVA_HOME=/usr/local/jdk
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
export CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/lib/tools.jar
#讓環境變量馬上生效
[root@Zabbix_Server ~]# source /etc/profile
#檢查java環境變量狀況
[root@Zabbix_Server ~]# which java
/usr/local/jdk/bin/java
[root@Zabbix_Server ~]# java -version
java version "1.8.0_60"
Java(TM) SE Runtime Environment (build 1.8.0_60-b27)
Java HotSpot(TM) 64-Bit Server VM (build 25.60-b23, mixed mode)

4.3.2 從新編譯zabbix-server服務器端

#從新編譯zabbix-server服務器端以前
[root@Zabbix_Server ~]# ll /usr/local/zabbix/sbin/
total 8604
-rwxr-xr-x 1 root root 1165774 Jan 20 15:16 zabbix_agentd
-rwxr-xr-x 1 root root 3672055 Jan 20 15:16 zabbix_proxy
-rwxr-xr-x 1 root root 3966037 Jan 20 15:16 zabbix_server

#開始從新編譯zabbix-server服務器端
[root@Zabbix_Server ~]# 
[root@Zabbix_Server ~]# /etc/init.d/zabbix_server stop
Shutting down zabbix_server:                               [  OK  ]
[root@Zabbix_Server ~]# cd /usr/src/zabbix-3.2.4/
[root@Zabbix_Server zabbix-3.2.4]# ./configure --prefix=/usr/local/zabbix --with-mysql --with-net-snmp --with-libcurl --enable-server --enable-agent --enable-proxy --enable-java --with-libxml2
[root@Zabbix_Server zabbix-3.2.4]# make && make install

#從新編譯zabbix-server服務器端以後
[root@Zabbix_Server zabbix-3.2.4]# cd /usr/local/zabbix/sbin/
[root@Zabbix_Server sbin]# ll
total 8608
-rwxr-xr-x 1 root root 1165774 Jan 25 22:22 zabbix_agentd
drwxr-xr-x 4 root root    4096 Jan 25 22:22 zabbix_java     #多了一個目錄
-rwxr-xr-x 1 root root 3672055 Jan 25 22:22 zabbix_proxy
-rwxr-xr-x 1 root root 3966037 Jan 25 22:22 zabbix_server
[root@Zabbix_Server sbin]# cd zabbix_java/
[root@Zabbix_Server zabbix_java]# ll
total 20
drwxr-xr-x 2 root root 4096 Jan 25 22:22 bin
drwxr-xr-x 2 root root 4096 Jan 25 22:22 lib
-rw-r--r-- 1 root root  791 Jan 25 22:22 settings.sh    #java gateway配置腳本
-rwxr-xr-x 1 root root  545 Jan 25 22:22 shutdown.sh    #中止java gateway服務
-rwxr-xr-x 1 root root 2025 Jan 25 22:22 startup.sh     #啓動java gateway服務

提示:
java gateway服務的配置腳本settings.sh不須要修改任何配置。

4.3.3 啓動java Gateway服務

[root@Zabbix_Server zabbix_java]# /usr/local/zabbix/sbin/zabbix_java/startup.sh 
[root@Zabbix_Server zabbix_java]# netstat -antup | grep 10052
tcp        0      0 :::10052                    :::*                        LISTEN      13126/java

4.4 修改zabbix server 配置

默認的狀況下,zabbix server未啓用javaPollers,須要修改zabbix_server.conf的以下配置

[root@Zabbix_Server zabbix]# cd /usr/local/zabbix/etc/

#修改如下配置文件的內容
[root@Zabbix_Server etc]# cat -n zabbix_server.conf | sed -n '215p;223p;231p'
   215  # JavaGateway=
   223  # JavaGatewayPort=10052
   231  # StartJavaPollers=0

#將上述配置文件內容修改爲以下
[root@Zabbix_Server etc]# cat -n zabbix_server.conf | sed -n '215p;223p;231p'
   215  JavaGateway=127.0.0.1   #JavaGateway的地址
   223  JavaGatewayPort=10052
   231  StartJavaPollers=5

#查看已經打開的java poller進程
[root@Zabbix_Server ~]# ps -ef | grep "java poller" | grep -v grep
zabbix    13233  13172  0 Jan25 ?        00:00:00 /usr/local/sbin/zabbix_server: java poller #1 [got 0 values in 0.000001 sec, idle 5 sec]
zabbix    13234  13172  0 Jan25 ?        00:00:00 /usr/local/sbin/zabbix_server: java poller #2 [got 0 values in 0.000015 sec, idle 5 sec]
zabbix    13235  13172  0 Jan25 ?        00:00:00 /usr/local/sbin/zabbix_server: java poller #3 [got 0 values in 0.000001 sec, idle 5 sec]
zabbix    13236  13172  0 Jan25 ?        00:00:00 /usr/local/sbin/zabbix_server: java poller #4 [got 0 values in 0.000001 sec, idle 5 sec]
zabbix    13237  13172  0 Jan25 ?        00:00:00 /usr/local/sbin/zabbix_server: java poller #5 [got 0 values in 0.000001 sec, idle 5 sec]

4.5 重啓zabbix_server服務

[root@Zabbix_Server ~]# /etc/init.d/zabbix_server start
Starting zabbix_server:                                    [  OK  ]
[root@Zabbix_Server ~]# netstat -antup | grep 10051
tcp        0      0 0.0.0.0:10051               0.0.0.0:*                   LISTEN      13172/zabbix_server 
tcp        0      0 192.168.0.220:10051         192.168.0.221:56752         TIME_WAIT   -                   
tcp        0      0 192.168.0.220:10051         192.168.0.221:56754         TIME_WAIT   -                   
tcp        0      0 192.168.0.220:10051         192.168.0.221:56751         TIME_WAIT   -                   
tcp        0      0 192.168.0.220:10051         192.168.0.220:47162         TIME_WAIT   -

4.6 Zabbix圖形界面配置

4.6.1 建立一個主機利用JMX接口監控tomcat被監控端

1.png-49.1kB

2.png-44.5kB

4.6.2 導入zabbix監控tomcat的模版

由於Zabbix自帶的Tomcat模版有兼容性問題,並很差用,所以,咱們導入本身製做的監控項模版

1.png-28.6kB

2.png-53.5kB

3.png-29.9kB

QQ截圖20180128192111.png-42.8kB

最後咱們關聯模版便可。

QQ截圖20180128211056.png-60.3kB

五,Zabbix監控PHP-FPM操做實戰

php-fpm和nginx同樣內建了一個狀態頁,對於想了解php-fpm的狀態以及監控php-fpm很是有幫助

5.1 啓用php-fpm狀態功能

修改php-fpm配置文件,確保以下配置是打開狀態:
pm.status_path = /status,具體操做以下

#在Zabbix-server端操做以下
[root@Zabbix_Server etc]# pwd
/usr/local/php/etc
[root@Zabbix_Server etc]# ls
pear.conf  php-fpm.conf  php-fpm.conf.default  php.ini
[root@Zabbix_Server etc]# sed -n '360p' php-fpm.conf
;pm.status_path = /status
[root@Zabbix_Server etc]# vim php-fpm.conf +360
[root@Zabbix_Server etc]# sed -n '360p' php-fpm.conf
pm.status_path = /status

#重啓php-fpm服務
[root@Zabbix_Server ~]# netstat -antup | grep php-fpm
tcp        0      0 127.0.0.1:9000              0.0.0.0:*                   LISTEN      1145/php-fpm
[root@Zabbix_Server ~]# pkill php-fpm
[root@Zabbix_Server ~]# netstat -antup | grep 9000
[root@Zabbix_Server ~]# /usr/local/php/sbin/php-fpm 
[root@Zabbix_Server ~]# netstat -antup | grep php-fpm
tcp        0      0 127.0.0.1:9000              0.0.0.0:*                   LISTEN      18918/php-fpm

5.2 nginx配置php-fpm狀態頁面

在nginx配置文件nginx.conf裏配置php狀態頁,操做以下

[root@Zabbix_Server ~]# cd /usr/local/nginx/
[root@Zabbix_Server nginx]# ls
client_body_temp  conf  fastcgi_temp  html  logs  proxy_temp  sbin  scgi_temp  uwsgi_temp
#配置php-fpm狀態頁面
[root@Zabbix_Server nginx]# vim conf/nginx.conf
[root@Zabbix_Server nginx]# cat conf/nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.php index.html index.htm;
        }
        location = /nginx-status {
            stub_status on;
            access_log off;
        }
        location = /status {         #添加此location
            include fastcgi_params;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
        }
        location ~ \.php$ {
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
    }
}
#重啓動nginx服務
[root@Zabbix_Server nginx]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@Zabbix_Server nginx]# /usr/local/nginx/sbin/nginx -s reload

5.3 狀態頁瀏覽器訪問測試

php-fpm狀態頁比較個性化的一個地方是它能夠帶參數,能夠帶的參數有json、xml、html,使用zabbix或者nagios監控能夠考慮使用xml或者默認方式。

QQ截圖20180130230603.png-19kB

#具體含義說明以下
[root@Zabbix_Server nginx]# curl 127.0.0.1/status
pool:                 www           #fpm進程池名稱,大多數爲www
process manager:      dynamic       #進程管理方式(static,dynamic or nodemand.dynamic)
start time:           26/Jan/2018:02:56:58 -0500    #啓動日期
start since:          2972          #運行時長
accepted conn:        517           #當前FPM進程池接受的請求數
listen queue:         0             #請求等待隊列,若是值不是0,那麼要增長FPM的進程數
max listen queue:     4             #請求等待隊列最高的數量
listen queue len:     128           #socket等待隊列長度
idle processes:       2             #空閒進程數量
active processes:     1             #活躍進程數量
total processes:      3             #總進程數量
max active processes: 3             #最大的活躍進程數量(FPM啓動開始算)
max children reached: 0             #達到最大子進程的次數,若是值不爲0,那麼須要調高最大進程數
slow requests:        0             #當啓用了php-fpm的slow-log功能時,若是出現php-fpm慢請求這個計數器會增長,通常不當的MySQL查詢會觸發這個值

其餘狀態參數顯示形式的數據(json,xml,html)

#以xml的格式輸出PHP-fpm狀態頁
[root@Zabbix_Server nginx]# curl 127.0.0.1/status?xml
<?xml version="1.0" ?>
<status>
<pool>www</pool>
<process-manager>dynamic</process-manager>
<start-time>1516953418</start-time>
<start-since>3731</start-since>
<accepted-conn>647</accepted-conn>
<listen-queue>0</listen-queue>
<max-listen-queue>4</max-listen-queue>
<listen-queue-len>128</listen-queue-len>
<idle-processes>2</idle-processes>
<active-processes>1</active-processes>
<total-processes>3</total-processes>
<max-active-processes>3</max-active-processes>
<max-children-reached>0</max-children-reached>
<slow-requests>0</slow-requests>

#以json的格式輸出PHP-fpm的狀態頁
</status>[root@Zabbix_Server nginx]# curl 127.0.0.1/status?json
{"pool":"www","process manager":"dynamic","start time":1516953418,"start since":3736,"accepted conn":648,"listen queue":0,"max listen queue":4,"listen queue len":128,"idle processes":2,"active processes":1,"total processes":3,"max active processes":3,"max children reached":0,"slow requests":0}[root@Zabbix_Server nginx]# 

#以html的格式輸出PHP-fpm的狀態頁
[root@Zabbix_Server nginx]# curl 127.0.0.1/status?html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head><title>PHP-FPM Status Page</title></head>
<body>
<table>
<tr><th>pool</th><td>www</td></tr>
<tr><th>process manager</th><td>dynamic</td></tr>
<tr><th>start time</th><td>26/Jan/2018:02:56:58 -0500</td></tr>
<tr><th>start since</th><td>3748</td></tr>
<tr><th>accepted conn</th><td>650</td></tr>
<tr><th>listen queue</th><td>0</td></tr>
<tr><th>max listen queue</th><td>4</td></tr>
<tr><th>listen queue len</th><td>128</td></tr>
<tr><th>idle processes</th><td>2</td></tr>
<tr><th>active processes</th><td>1</td></tr>
<tr><th>total processes</th><td>3</td></tr>
<tr><th>max active processes</th><td>3</td></tr>
<tr><th>max children reached</th><td>0</td></tr>
<tr><th>slow requests</th><td>0</td></tr>
</table>
</body></html>

5.4 在Zabbix agent端修改配置

在建立Zabbix的agent端鍵值配置文件的時候,咱們能夠考慮利用PHP-fpm狀態頁的不一樣輸出格式來抓取數據。例如咱們利用xml格式獲取數據,操做以下:

#查看xml格式PHP-fpm狀態頁數據
root@Zabbix_Server nginx]# curl 127.0.0.1/status?xml
<?xml version="1.0" ?>
<status>
<pool>www</pool>
<process-manager>dynamic</process-manager>
<start-time>1516953418</start-time>
<start-since>3731</start-since>
<accepted-conn>647</accepted-conn>
<listen-queue>0</listen-queue>
<max-listen-queue>4</max-listen-queue>
<listen-queue-len>128</listen-queue-len>
<idle-processes>2</idle-processes>
<active-processes>1</active-processes>
<total-processes>3</total-processes>
<max-active-processes>3</max-active-processes>
<max-children-reached>0</max-children-reached>
<slow-requests>0</slow-requests>

#建立zabbix監控的agent端鍵值配置文件
[root@Zabbix_Server ~]# cd /etc/zabbix/zabbix_agentd.d/
[root@Zabbix_Server zabbix_agentd.d]# cat userparameter_php-fpm.conf 
UserParameter=php-fpm.status[*],/usr/bin/curl -s "http://127.0.0.1/status?xml" | grep "<$1>" | awk -F "[>|<]" '{print $$3}'    #這裏須要注意在腳本里若是寫成$3的話,腳本會認識是一個變量,所以咱們須要用$$3

#重啓動agent客戶端
[root@Zabbix_Server ~]# /etc/init.d/zabbix-agent restart
Shutting down Zabbix agent:                                [  OK  ]
Starting Zabbix agent:                                     [  OK  ]

5.5 進行鍵值配置文件測試

[root@Zabbix_Server zabbix_agentd.d]# zabbix_get -s 192.168.0.220 -p 10050 -k "php-fpm.status[process-manager]"
dynamic
[root@Zabbix_Server zabbix_agentd.d]# zabbix_get -s 192.168.0.220 -p 10050 -k "php-fpm.status[start-since]"
5634
[root@Zabbix_Server zabbix_agentd.d]# zabbix_get -s 192.168.0.220 -p 10050 -k "php-fpm.status[active-processes]"
1

5.6 Zabbix圖形界面導入咱們的監控模版

QQ截圖20180130235651.png-51.5kB

QQ截圖20180130235713.png-28kB

六,Zabbix的Web監測功能

Zabbix的Web監測功能,沒有任何依賴,不須要agent端,所有依賴於Server的主動監測

利用Web監測,監測Server端的nginx首頁

QQ截圖20180131001529.png-53.9kB

QQ截圖20180131001542.png-34.3kB

QQ截圖20180131002003.png-47.8kB

QQ截圖20180131002105.png-22kB

QQ截圖20180131002314.png-23.3kB

QQ截圖20180131002455.png-25.5kB

QQ截圖20180131002555.png-32kB

到這裏Web監測咱們就建立好了,最後再看一下監控結果

QQ截圖20180131002646.png-22.9kB

七,思考與做業

請同窗們在Zabbix監控MySQL的模版示例的基礎上,自定義一個監控數據庫主從複製的監控項

IP地址 主機名 用途
192.168.0.220 Zabbix_Server服務器 Zabbix監控端/主MySQL
192.168.0.221 Nginx服務器 Nginx服務器/從MySQL
相關文章
相關標籤/搜索