• Zabbix监控平台(三)生产环境案例


    一,Zabbix生产环境监测案例概述

     

    1.1 项目规划

    • 主机分组

      • 交换机
      • Nginx
      • Tomcat
      • MySQL
      • Apache
      • PHP-fpm
      • redis(也有状态页,自己研究)
      • memcache(也有状态页,自己研究)
    • 监控对象识别:

      • 使用SNMP监控交换(监控交换机或路由器)
      • 使用IPMI监控服务器硬件
      • 使用Agent监控服务器(监控操作系统和软件服务)
      • 使用JMX监控JAVA(监控tomcat等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模板包和源码包

    链接:https://pan.baidu.com/s/1aezlSsXlfn5A-_CJnTUUhw 
    提取码:ndhs ---模板包

    链接:https://pan.baidu.com/s/1YwwtmnWATM9DxAQmZCDG4Q 
    提取码:0nmk ---源码包

     

    二,Zabbix监控MySQL数据库操作实战

    我们需要部署一个MySQL服务和一个zabbix-agentd客户端

     

    2.1 编写监控mysql脚本

    在nginx服务器(192.168.200.69)上安装一个MySQL 
    下面是监控mysql状态的一个shell脚本,内容如下:

    mkdir -p /server/scripts

    cd /server/scripts

    vim check_mysql.sh

    cat check_mysql.sh

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

    image_1cudrmpafhjq1pks66pglg1jjl9.png-98.4kB

     

    2.2在zabbix-agent端创建自定义键值配置文件

     

    创建mysql.status的键值

    chmod +x check_mysql.sh

    chown zabbix.zabbix check_mysql.sh

    cd /etc/zabbix/zabbix_agentd.d/

    vim mysql_status.conf

    cat mysql_status.conf

     
    1. UserParameter=mysql.status[*],/server/scripts/check_mysql.sh $1

    image_1cudrpo93poikoe1pmck7fo1hm.png-40.3kB

     

    删除旧键值模版

    rm -rf userparameter_mysql.conf

    image_1cudrvbe5c8uom31jqcf211onl1g.png-24.2kB

     

    创建mysql.ping和mysql.version的键值

    vim mysql_status.conf

    cat mysql_status.conf

     
    1. UserParameter=mysql.status[*],/server/scripts/check_mysql.sh $1
    2. UserParameter=mysql.ping,/usr/bin/mysqladmin -uroot -plinyaonie ping | grep -c alive
    3. UserParameter=mysql.version,/usr/bin/mysql -V

    image_1cudsdqja1ste1cfbfjicff1nuv3h.png-18.8kB


    image_1cuds08uo1rvrb7dnes1274gu91t.png-34.9kB

     

    重启zabbix-agentd服务

    /etc/init.d/zabbix-agent restart

    image_1cuds2ep64qkrbmkg51ei3sap2a.png-44.2kB

     

    2.3 在zabbix-server端测试键值

    zabbix_get -s 192.168.200.69 -p 10050 -k "mysql.status[Uptime]"

    zabbix_get -s 192.168.200.69 -p 10050 -k "mysql.status[Com_commit]"

    zabbix_get -s 192.168.200.69 -p 10050 -k "mysql.status[Bytes_sent]"

    zabbix_get -s 192.168.200.69 -p 10050 -k "mysql.status[Bytes_received]"

    zabbix_get -s 192.168.200.69 -p 10050 -k "mysql.ping"

    zabbix_get -s 192.168.200.69 -p 10050 -k "mysql.version"

    image_1cudsc22irrdbr719na1gclo2u34.png-59.3kB

     

    2.4 在zabbix-server端Web前台引入zabbix监控MySQL的模版

    zabbix自带监控MySQL的模版,我们只需要将模版引入到主机即可 
    配置-->主机-->选择主机-->模板,然后将MySQL模版连接到此主机即可

    image_1cudspn801q6kl24ktkhk6jo96e.png-103.7kB


    image_1cudstbnn15g3i341olaer46qk8g.png-90.2kB


    image_1cudsudd2td81o3m7c01qm01b8ba6.png-107kB


    image_1cudt0afu7o11kmbp1f81g41gbj.png-222.4kB

    从图中可以发现模版里的键值都是我们之前已经定义好了的键值

     

    三,Zabbix监控Apache、Nginx操作实战

     

    为了避免冲突,若系统中已经安装httpd服务,务必删除

    rpm -q httpd

    /etc/init.d/iptables stop

    setenforce 0

    image_1cue2ahodb021fos1kh24ct1ea69.png-28.4kB

     

    源码构建apache

    tar xf httpd-2.2.17.tar.gz -C /usr/src/

    cd /usr/src/httpd-2.2.17/

    ./configure --prefix=/usr/local/httpd --enable-so --enable-rewrite --enable-charset-lite --enable-cgi && make && make install

    cd /usr/local/httpd/

    ls

    ln -s /usr/local/httpd/bin/* /usr/local/bin/

    cp /usr/local/httpd/bin/apachectl /etc/init.d/httpd

    chmod +x /etc/init.d/httpd

    cd /usr/local/httpd/htdocs/

    echo "`hostname -I` I am apache" > index.html

    cat index.html

    cd /usr/local/httpd/conf

    awk '/#ServerName/{print NR,$0}' httpd.conf

    sed -i -e '97 s/#//;s/example/yunjisuan/' httpd.conf

    sed -n '97p' httpd.conf

    /etc/init.d/httpd start

    netstat -antup | grep httpd

    echo "`hostname -I` www.yunjisuan.com" >> /etc/hosts

    tail -1 /etc/hosts

    curl www.yunjisuan.com

    image_1cue2daagafc17kc1egtm421hofm.png-96.4kB


    image_1cue2kq75qeec124aptmlkd23.png-54.9kB


    image_1cue303an1ih8eh615vi11fqnd746.png-94.9kB

     

    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即可

    image_1cue33ms31bgnm9e5d91796dln53.png-49.5kB

     

    3.1.3 下载apache zabbix模版并解压

     

    从如下地址下载apache zabbix模版(上边有下载链接)

    wget https://github.com/lorf/zapache/archive/master.zip

     

    解压zip格式压缩文件

    unzip master

    ls -d zapache-master

    cd zapache-master/

    ls

    此模版文件中包含了监控apache的脚本文件,zabbix的apache模版文件,还有客户端zabbix自定义传key文件。

    image_1cue3ec0kl201fg46qj18vgnoh5g.png-88.4kB

    首先将zapache放到需要监控的apache服务器上的/etc/zabbix/scripts目录下,如果没有scripts目录,自行创建一个即可。然后执行授权:

    cp zapache /etc/zabbix/scripts/

    cd /etc/zabbix/scripts/

    chmod 755 /etc/zabbix/scripts/zapache

    ./zapache

     
    1. zapache version: 1.5
    2. usage:
    3. ./zapache [<url>] TotalAccesses - Check total accesses.
    4. ./zapache [<url>] TotalKBytes - Check total KBytes.
    5. ./zapache [<url>] CPULoad - Check CPU load.
    6. ./zapache [<url>] Uptime - Check uptime.
    7. ./zapache [<url>] ReqPerSec - Check requests per second.
    8. ./zapache [<url>] BytesPerSec - Check Bytes per second.
    9. ./zapache [<url>] BytesPerReq - Check Bytes per request.
    10. ./zapache [<url>] BusyWorkers - Check busy workers.
    11. ./zapache [<url>] IdleWorkers - Check idle workers.
    12. ./zapache [<url>] version - Version of this script.
    13. ./zapache [<url>] ping - Check if Apache is up.
    14. ./zapache [<url>] WaitingForConnection - Check Waiting for Connection processess.
    15. ./zapache [<url>] StartingUp - Check Starting Up processess.
    16. ./zapache [<url>] ReadingRequest - Check Reading Request processess.
    17. ./zapache [<url>] SendingReply - Check Sending Reply processess.
    18. ./zapache [<url>] KeepAlive - Check KeepAlive Processess.
    19. ./zapache [<url>] DNSLookup - Check DNSLookup Processess.
    20. ./zapache [<url>] ClosingConnection - Check Closing Connection Processess.
    21. ./zapache [<url>] Logging - Check Logging Processess.
    22. ./zapache [<url>] GracefullyFinishing - Check Gracefully Finishing Processess.
    23. ./zapache [<url>] IdleCleanupOfWorker - Check Idle Cleanup of Worker Processess.
    24. ./zapache [<url>] OpenSlotWithNoCurrentProcess - Check Open Slots with No Current Process.

    image_1cue3n4cb5sfen27sm1r8k19dl6d.png-135kB

    接着将userparameter_zapache.conf.sample文件放到/etc/zabbix/zabbix_agentd.d目录下,并重命名为userparameter_zapache.conf文件,然后修改键值命令的路径。

    最后,重启zabbix-agent服务

    cd /root/zapache-master/

    cp userparameter_zapache.conf.sample /etc/zabbix/zabbix_agentd.d/userparameter_zapache.conf

    cd /etc/zabbix/zabbix_agentd.d/

    vim userparameter_zapache.conf

    tail -1 userparameter_zapache.conf

     
    1. UserParameter=zapache[*],/etc/zabbix/scripts/zapache $1 #本行要修改

    /etc/init.d/zabbix-agent restart

    image_1cue41c20r6l3ba8vb1d5braqcb.png-68.7kB

     

    3.1.4 测试键值

     

    在zabbix-agent端查看脚本用法

    cd /etc/zabbix/scripts/

    ./zapache

     
    1. zapache version: 1.5
    2. usage:
    3. ./zapache [<url>] TotalAccesses - Check total accesses.
    4. ./zapache [<url>] TotalKBytes - Check total KBytes.
    5. ./zapache [<url>] CPULoad - Check CPU load.
    6. ./zapache [<url>] Uptime - Check uptime.
    7. ./zapache [<url>] ReqPerSec - Check requests per second.
    8. ./zapache [<url>] BytesPerSec - Check Bytes per second.
    9. ./zapache [<url>] BytesPerReq - Check Bytes per request.
    10. ./zapache [<url>] BusyWorkers - Check busy workers.
    11. ./zapache [<url>] IdleWorkers - Check idle workers.
    12. ./zapache [<url>] version - Version of this script.
    13. ./zapache [<url>] ping - Check if Apache is up.
    14. ./zapache [<url>] WaitingForConnection - Check Waiting for Connection processess.
    15. ./zapache [<url>] StartingUp - Check Starting Up processess.
    16. ./zapache [<url>] ReadingRequest - Check Reading Request processess.
    17. ./zapache [<url>] SendingReply - Check Sending Reply processess.
    18. ./zapache [<url>] KeepAlive - Check KeepAlive Processess.
    19. ./zapache [<url>] DNSLookup - Check DNSLookup Processess.
    20. ./zapache [<url>] ClosingConnection - Check Closing Connection Processess.
    21. ./zapache [<url>] Logging - Check Logging Processess.
    22. ./zapache [<url>] GracefullyFinishing - Check Gracefully Finishing Processess.
    23. ./zapache [<url>] IdleCleanupOfWorker - Check Idle Cleanup of Worker Processess.
    24. ./zapache [<url>] OpenSlotWithNoCurrentProcess - Check Open Slots with No Current Process.

    我们发现脚本需要传递两个参数,即url和选项,然后我们在zabbix-server端进行键值测试

    zabbix_get -s 192.168.200.78 -p 10050 -k "zapache[127.0.0.1 TotalAccesses]"

    zabbix_get -s 192.168.200.78 -p 10050 -k "zapache[www.yunjisuan.com version]"

    zabbix_get -s 192.168.200.78 -p 10050 -k "zapache[www.yunjisuan.com ping]"

    image_1cue48mae991ha9gt35t9ubco.png-34.4kB

     

    3.1.5 在zabbix-server的Web页面上监控apache

     

    (1)创建一个监控apache的主机

    image_1cue4dod21gth1a6jsjvobiajsdl.png-103.6kB


    image_1cue4gu1u9f017cj1sck1dqu1q8el.png-108.7kB

     

    (2)导入apache监控项的模版(上边有模板下载链接)

    zabbix默认并没有监控apache的监控项模版。我们需要将它导入到apache中。在之前解压出来的目录中,有待导入的web模版

     
    1. [root@apache ~]# tree zapache-master/
    2. zapache-master/
    3. ├── httpd-server-status.conf.sample
    4. ├── README.md
    5. ├── zapache-template-active.xml #Web模版
    6. └── zapache-template.xml #Web模版
    7. 0 directories, 4 files

    我们将这两个.xml后缀的拷贝到windows上,然后从Web界面导入进去

    image_1csinasjhvo71d3t98l98s191r44.png-23.8kB


    image_1csinb0k84lghju1gq81b2jeeg4h.png-44.6kB

    导入以后Web端就有监控apache的监控项模版了

     

    (3)将模版链接到apache主机

    image_1cue5raan1k2u1chkigp1ne4f88fh.png-79.9kB


    image_1cue68p7takq11su1j0a13crslskm.png-57.2kB


    image_1cue656ao9uf1hiv121j1bjtj9ek9.png-81.4kB

     

    3.2 实战监控nginx

     

    3.2.1 开启nginx状态页

    vim /usr/local/nginx/conf/nginx.conf

    /usr/local/nginx/sbin/nginx -t

    /usr/local/nginx/sbin/nginx

     
    1. #在nginx的配置文件中,添加status配置
    2. location = /nginx-status {
    3. stub_status on;
    4. access_log off;
    5. allow 127.0.0.1;
    6. allow 192.168.200.78; #zabbix服务器地址一般是内网地址
    7. deny all;
    8. }

    image_1cue6rq4u1s6u19r11uivqctsaqo.png-39.1kB

     

    3.2.2 访问设置好的nginx-status链接

    curl 192.168.200.78/nginx-status

     
    1. nginx status详细说明如下:
    2. Active connections:对后端发起的活动连接数;
    3. acceptsnginx总共处理了多少个连接;
    4. handlednginx成功创建了几次握手;
    5. requestsnginx总共处理了多少请求。
    6. Readingnginx读取客户端的header数;
    7. Writingnginx返回给客户端的header数;
    8. Waitingnginx请求处理完成,正在等待下一请求指令的连接

    image_1cue6u4tm3m84501q6gd2vadr5.png-23.6kB

     

    3.2.3 编写nginx状态监控脚本

    vim nginx_status.sh

    cat nginx_status.sh

     
    1. #!/bin/bash
    2. # Set Variables
    3. HOST="127.0.0.1"
    4. PORT="80"
    5. #Functions to return nginx stats
    6. function active(){
    7. /usr/bin/curl "http://$HOST:$PORT/nginx-status" 2>/dev/null | grep "Active" | awk '{print $NF}'
    8. }
    9. function reading(){
    10. /usr/bin/curl "http://$HOST:$PORT/nginx-status" 2>/dev/null | grep "Reading" | awk '{print $2}'
    11. }
    12. function writing(){
    13. /usr/bin/curl "http://$HOST:$PORT/nginx-status" 2>/dev/null | grep "Writing" | awk '{print $4}'
    14. }
    15. function accepts(){
    16. /usr/bin/curl "http://$HOST:$PORT/nginx-status" 2>/dev/null | awk 'NR==3' | awk '{print $1}'
    17. }
    18. function handled(){
    19. /usr/bin/curl "http://$HOST:$PORT/nginx-status" 2>/dev/null | awk 'NR==3' | awk '{print $2}'
    20. }
    21. function requests(){
    22. /usr/bin/curl "http://$HOST:$PORT/nginx-status" 2>/dev/null | awk 'NR==3' | awk '{print $3}'
    23. }
    24. function ping(){
    25. /sbin/pidof nginx | wc -l
    26. }
    27. # Run the requested function
    28. $1

    image_1cue73rp7n8j12o4cu1a9d3cjs2.png-88.4kB

     

    3.2.4 给脚本可执行权限并且授权

    chmod +x nginx_status.sh

    chown zabbix.zabbix nginx_status.sh

    image_1cue75afb18kb1rps19nc1id7vddsf.png-30.2kB

     

    3.2.5 在zabbix-agent端配置nginx键值配置文件并重启agent客户端

    cd /etc/zabbix/zabbix_agentd.d

    vim nginx.conf

    cat nginx.conf

     
    1. UserParameter=nginx.active,/usr/bin/curl -s "http://127.0.0.1:80/nginx-status" | grep "Active" | awk '{print $NF}'
    2. UserParameter=nginx.status[*],/etc/zabbix/scripts/nginx_status.sh $1

    /etc/init.d/zabbix-agent restart

    image_1cue7vni41mi71fjfqdv1njl1tuatp.png-46.8kB

     

    3.2.6 在zabbix-server端测试键值

    zabbix_get -s 192.168.200.78 -k "nginx.status[reading]" -p 10050

    zabbix_get -s 192.168.200.78 -k "nginx.status[active]" -p 10050

    zabbix_get -s 192.168.200.78 -k "nginx.status[reading]" -p 10050

    zabbix_get -s 192.168.200.78 -k "nginx.status[writing]" -p 10050

    zabbix_get -s 192.168.200.78 -k "nginx.status[ping]" -p 10050

    image_1cue81tcvuno1nrlqk8cv9puqum.png-40.7kB

     

    3.2.7 接下来我们需要在Web端导入nginx监控模版(上文有模板下载链接)

    image_1csine64afjmacp1hndkq7q9s65.png-36.3kB


    image_1cue87i447jenss1hb65kfe113j.png-97.5kB


    image_1cue8bk678p8gc1t8qvpgba518f.png-87.5kB


    image_1cue8d25b10f012c51nqe1cu61png18s.png-90.3kB


    image_1cue8dlvi1tdati6ed9osl1nig199.png-120.6kB

     

    3.2.8 异常调试

    如果Zabbix图形界面一直没有获取到数据,可以通过在zabbix server上执行zabbix_get手动测试: 
    /usr/local/zabbix/bin/zabbix_get -s 192.168.200.78 -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

    image_1csinfi11chr1f4fstgjvd130n7p.png-89.3kB

     

    4.1 配置Tomcat JMX(安装Tomcat步骤省略)

     

    配置jmx,在需要监控的tomcat服务器上,编辑catalina.sh,加入如下配置

    vim /usr/local/tomcat/bin/catalina.sh

    sed -n '249p' /usr/local/tomcat/bin/catalina.sh

     
    1. 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.200.78 -Dcom.sun.management.jmxremote.port=12345"

    这里需要注意,必须增加-Djava.rmi.server.hostname选项,并且后面的IP就是tomcat服务器的IP

    image_1cue92lth1101r7i82d38f1fqk1e.png-34.8kB

     

    4.2 重启Tomcat

    /usr/local/tomcat/bin/shutdown.sh

    /usr/local/tomcat/bin/startup.sh

    netstat -antup | grep java

    image_1cuescd451r651qhcbjpjjb639.png-78kB

     

    4.3 编译zabbix,加入java支持(需要先安装JDK)

    在zabbix server服务器上,编译安装zabbix-server,需要加上--enable-java,以支持jmx监控,如果之前的zabbix server没加此选项,那么需要重新编译安装,编译参数如下:

     
    1. ./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

    tar xf jdk-8u60-linux-x64.tar.gz -C /usr/local/

    ln -s /usr/local/jdk1.8.0_60/ /usr/local/jdk

    sed -i.ori '$a 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' /etc/profile

    tail -3 /etc/profile

    source /etc/profile

    which java

    java -version

    image_1cuesihvtv2o1t4advf19fotj0m.png-104.8kB

     

    4.3.2 重新编译zabbix-server服务器端

     

    重新编译zabbix-server服务器端之前

    cd /usr/local/zabbix/sbin

    ls

    image_1cuesmm004j91agi16c1ji1sid13.png-16.6kB

     

    开始重新编译zabbix-server服务器端

    /etc/init.d/zabbix_server stop

    cd /usr/src/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 && make && make install

    image_1cuesv041f68jud1bps41a17i21g.png-102.2kB

     

    重新编译zabbix-server服务器端之后

    cd /usr/local/zabbix/sbin/

    ll

     
    1. -rwxr-xr-x 1 root root 1165774 Jan 25 22:22 zabbix_agentd
    2. drwxr-xr-x 4 root root 4096 Jan 25 22:22 zabbix_java #多了一个目录
    3. -rwxr-xr-x 1 root root 3672055 Jan 25 22:22 zabbix_proxy
    4. -rwxr-xr-x 1 root root 3966037 Jan 25 22:22 zabbix_server

    cd zabbix_java/

    ll

     
    1. drwxr-xr-x 2 root root 4096 Jan 25 22:22 bin
    2. drwxr-xr-x 2 root root 4096 Jan 25 22:22 lib
    3. -rw-r--r-- 1 root root 791 Jan 25 22:22 settings.sh #java gateway配置脚本
    4. -rwxr-xr-x 1 root root 545 Jan 25 22:22 shutdown.sh #停止java gateway服务
    5. -rwxr-xr-x 1 root root 2025 Jan 25 22:22 startup.sh #启动java gateway服务

    image_1cuet34s61rd3bb46ip1d5q1kco3d.png-59kB

    提示:

    java gateway服务的配置脚本settings.sh不需要修改任何配置。

     

    4.3.3 启动java Gateway服务

    /usr/local/zabbix/sbin/zabbix_java/startup.sh

    netstat -antup | grep 10052

    image_1cuet5r5hjl6p4ie2vjjs1tpn3q.png-69.8kB

     

    4.4 修改zabbix server配置

    默认的情况下,zabbix server未启用javaPollers,需要修改zabbix_server.conf的如下配置

     

    修改以下配置文件的内容

    cd /usr/local/zabbix/etc/

    cat -n zabbix_server.conf | sed -n '215p;223p;231p'

     
    1. 215 # JavaGateway=
    2. 223 # JavaGatewayPort=10052
    3. 231 # StartJavaPollers=0

    image_1cuet8g2k1q5k1f1f1thm2b6tvv47.png-24.1kB

     

    将上述配置文件内容修改成如下

    vim zabbix_server.conf

    cat -n zabbix_server.conf | sed -n '215p;223p;231p'

     
    1. 215 JavaGateway=127.0.0.1 #JavaGateway的地址
    2. 223 JavaGatewayPort=10052
    3. 231 StartJavaPollers=5

    image_1cuetc5hjemf1sg0kkr8pe1i3t54.png-23.2kB

     

    启动zabbix_server服务并查看已经打开的java poller进程

    /etc/init.d/zabbix_server start

    ps -ef | grep "java poller" | grep -v grep

    image_1cuetnr6l1bbnrfm1ueqn2q13r85h.png-52.5kB

     

    4.6 Zabbix图形界面配置

     

    4.6.1 创建一个主机利用JMX接口监控tomcat被监控端

    image_1cuetq7c51k5e1amnqubhq1rkj5u.png-115.2kB


    image_1cuettael1n5ubvgter14qa19ho6b.png-105.7kB

     

    4.6.2 导入zabbix监控tomcat的模版(上边有模板下载链接)

    因为Zabbix自带的Tomcat模版有兼容性问题,并不好用,因此,我们导入自己制作的监控项模版

    image_1cuetum2a1l38eeq1u4915plck6o.png-85.7kB


    image_1cueu03ke1av0gpc51h9c8173b75.png-96.3kB


    image_1csinlai41nrtp0v1f6d140fd33an.png-55.6kB


    image_1cueu6j7i11va1di110e0sl4fh97i.png-86.9kB


    image_1cueu76ns1kl11u2p134u1p1mg2g7v.png-127kB

     

    五,Zabbix监控PHP-FPM操作实战

    php-fpm和nginx一样内建了一个状态页,对于想了解php-fpm的状态以及监控php-fpm非常有帮助

     

    5.1 启用php-fpm状态功能

     

    修改php-fpm配置文件,确保如下配置是打开状态

    pm.status_path = /status,具体操作如下

     

    在Zabbix-server端操作如下

    cd /usr/local/php/etc

    ls

    sed -n '360p' php-fpm.conf

    vim php-fpm.conf +360

    sed -n '360p' php-fpm.conf

    image_1cueud3385dp2me1vjt1k8r1i0a8c.png-30.9kB

     

    重启php-fpm服务

    netstat -antup | grep php-fpm

    pkill php-fpm

    netstat -antup | grep php-fpm

    /usr/local/php/sbin/php-fpm

    netstat -antup | grep php-fpm

    image_1cueuhob2kej1961c6n1hub1kid8v.png-34.7kB

     

    5.2 nginx配置php-fpm状态页面

     

    配置php-fpm状态页面

    vim /usr/local/nginx/conf/nginx.conf

    cat /usr/local/nginx/conf/nginx.conf

     
    1. worker_processes 1;
    2. events {
    3. worker_connections 1024;
    4. }
    5. http {
    6. include mime.types;
    7. default_type application/octet-stream;
    8. sendfile on;
    9. keepalive_timeout 65;
    10. server {
    11. listen 80;
    12. server_name localhost;
    13. location / {
    14. root html;
    15. index index.php index.html index.htm;
    16. }
    17. location = /nginx-status {
    18. stub_status on;
    19. access_log off;
    20. }
    21. location = /status { #添加此location
    22. include fastcgi_params;
    23. fastcgi_pass 127.0.0.1:9000;
    24. fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
    25. }
    26. location ~ .php$ {
    27. fastcgi_pass 127.0.0.1:9000;
    28. fastcgi_index index.php;
    29. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    30. include fastcgi_params;
    31. }
    32. }
    33. }

    image_1cuevftlf9ra93o11j8elo1c0aa6.png-78.4kB

     

    重启动nginx服务

    /usr/local/nginx/sbin/nginx -t

    /usr/local/nginx/sbin/nginx -s reload

    image_1cuevh8r9h0bo6g130n1n7hgk6aj.png-22.6kB

     

    5.3 状态页浏览器访问测试

    php-fpm状态页比较个性化的一个地方是它可以带参数,可以带的参数有json、xml、html,使用zabbix或者nagios监控可以考虑使用xml或者默认方式。

    http://192.168.200.69/status

    image_1cuevj7dla6jid51jh01b3f1s6kb0.png-25.4kB

     

    具体含义说明如下

    curl 127.0.0.1/status

     
    1. pool: www #fpm进程池名称,大多数为www
    2. process manager: dynamic #进程管理方式(static,dynamic or nodemand.dynamic)
    3. start time: 12/Dec/2018:07:35:24 +0800 #启动日期
    4. start since: 1137 #运行时长
    5. accepted conn: 142 #当前FPM进程池接受的请求数
    6. listen queue: 0 #请求等待队列,如果值不是0,那么要增加FPM的进程数
    7. max listen queue: 0 #请求等待队列最高的数量
    8. listen queue len: 128 #socket等待队列长度
    9. idle processes: 1 #空闲进程数量
    10. active processes: 1 #活跃进程数量
    11. total processes: 2 #总进程数量
    12. max active processes: 2 #最大的活跃进程数量(FPM启动开始算)
    13. max children reached: 0 #达到最大子进程的次数,如果值不为0,那么需要调高最大进程数
    14. slow requests: 0 #当启用了php-fpm的slow-log功能时,如果出现php-fpm慢请求这个计数器会增加,一般不当的MySQL查询会触发这个值

    image_1cuevmomncab1rf71dpue441mcfbd.png-34.8kB

     

    其他状态参数显示形式的数据(json,xml,html)

     

    以xml的格式输出PHP-fpm状态页

    curl 127.0.0.1/status?xml

     
    1. <?xml version="1.0" ?>
    2. <status>
    3. <pool>www</pool>
    4. <process-manager>dynamic</process-manager>
    5. <start-time>1516953418</start-time>
    6. <start-since>3731</start-since>
    7. <accepted-conn>647</accepted-conn>
    8. <listen-queue>0</listen-queue>
    9. <max-listen-queue>4</max-listen-queue>
    10. <listen-queue-len>128</listen-queue-len>
    11. <idle-processes>2</idle-processes>
    12. <active-processes>1</active-processes>
    13. <total-processes>3</total-processes>
    14. <max-active-processes>3</max-active-processes>
    15. <max-children-reached>0</max-children-reached>
    16. <slow-requests>0</slow-requests>

    image_1cuevo9uosqh1t0te4s114u11f4bq.png-44.1kB

     

    以json的格式输出PHP-fpm的状态页

    curl 127.0.0.1/status?json

     
    1. {"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]#

    image_1cuevrmj01qaqhl4slb6n8pdac7.png-26.3kB

     

    以html的格式输出PHP-fpm的状态页

    curl 127.0.0.1/status?html

     
    1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    2. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    3. <head><title>PHP-FPM Status Page</title></head>
    4. <body>
    5. <table>
    6. <tr><th>pool</th><td>www</td></tr>
    7. <tr><th>process manager</th><td>dynamic</td></tr>
    8. <tr><th>start time</th><td>26/Jan/2018:02:56:58 -0500</td></tr>
    9. <tr><th>start since</th><td>3748</td></tr>
    10. <tr><th>accepted conn</th><td>650</td></tr>
    11. <tr><th>listen queue</th><td>0</td></tr>
    12. <tr><th>max listen queue</th><td>4</td></tr>
    13. <tr><th>listen queue len</th><td>128</td></tr>
    14. <tr><th>idle processes</th><td>2</td></tr>
    15. <tr><th>active processes</th><td>1</td></tr>
    16. <tr><th>total processes</th><td>3</td></tr>
    17. <tr><th>max active processes</th><td>3</td></tr>
    18. <tr><th>max children reached</th><td>0</td></tr>
    19. <tr><th>slow requests</th><td>0</td></tr>
    20. </table>
    21. </body></html>

    image_1cuevsgqa1e5119p01gkb1htjf6tck.png-74.2kB

     

    5.4 在Zabbix agent端修改配置

    在创建Zabbix的agent端键值配置文件的时候,我们可以考虑利用PHP-fpm状态页的不同输出格式来抓取数据。例如我们利用xml格式获取数据,操作如下:

     

    查看xml格式PHP-fpm状态页数据

    curl 127.0.0.1/status?xml

     
    1. <?xml version="1.0" ?>
    2. <status>
    3. <pool>www</pool>
    4. <process-manager>dynamic</process-manager>
    5. <start-time>1516953418</start-time>
    6. <start-since>3731</start-since>
    7. <accepted-conn>647</accepted-conn>
    8. <listen-queue>0</listen-queue>
    9. <max-listen-queue>4</max-listen-queue>
    10. <listen-queue-len>128</listen-queue-len>
    11. <idle-processes>2</idle-processes>
    12. <active-processes>1</active-processes>
    13. <total-processes>3</total-processes>
    14. <max-active-processes>3</max-active-processes>
    15. <max-children-reached>0</max-children-reached>
    16. <slow-requests>0</slow-requests>

    image_1cuevuhl01isr1qgs1fjpbj621rdh.png-43.9kB

     

    创建zabbix监控的agent端键值配置文件

    cd /etc/zabbix/zabbix_agentd.d/

    vim userparameter_php-fpm.conf

    cat userparameter_php-fpm.conf

     
    1. UserParameter=php-fpm.status[*],/usr/bin/curl -s "http://127.0.0.1/status?xml" | grep "<$1>" | awk -F "[>|<]" '{print $$3}'
    2. 这里需要注意在脚本里如果写成$3的话,脚本会认识是一个变量,因此我们需要用$$3

    image_1cuf0al041cqlo3f1uo79vn1uphdu.png-36.2kB

     

    重启动agent客户端

    /etc/init.d/zabbix-agent restart

    image_1cuf0b8iksftml01k0rb7m1jnkeb.png-18.2kB

     

    5.5 进行键值配置文件测试

    zabbix_get -s 192.168.200.69 -p 10050 -k "php-fpm.status[process-manager]"

    zabbix_get -s 192.168.200.69 -p 10050 -k "php-fpm.status[start-since]"

    zabbix_get -s 192.168.200.69 -p 10050 -k "php-fpm.status[active-processes]"

    image_1cuf0d0a81prjoac7631i4ejqreo.png-31.7kB

     

    5.6 Zabbix图形界面导入我们的监控模版(上边有模板下载链接)

    image_1cuf0fvfu31o164j2nk15mhf9vf5.png-92.9kB


    image_1cuf0gle21706t0udc15qkpfufi.png-96kB


    image_1cuf0hla1us21s63bhi1htot1jfv.png-86.6kB


    image_1cuf0kc4l1cr018hpuunb83iqsgc.png-93.1kB


    image_1cuf0kuuvkl51kkf1s4rh7k1obngp.png-128.9kB

     

    六,Zabbix的Web监测功能

    Zabbix的Web监测功能,没有任何依赖,不需要agent端,全部依赖于Server的主动监测

    利用Web监测,监测Server端的nginx首页

    image_1cuf0n3ub1jua17rjbkk19rlhc3h6.png-125.5kB


    image_1cuf0npmdpu218puusse2vl6khj.png-95.3kB


    image_1csinq0ck1nhc19raroo1g6c156di.png-62.2kB


    image_1csinq46d16m215s6c4k1htm18cgdv.png-29.1kB


    image_1cuf13dr01qe4137d1okk1uisg2ai0.png-57.2kB


    image_1cuf14k4lcf61ie91e0n1oei1raeid.png-81.1kB


    image_1cuf15i2i8nv3f0fv11v3uih4iq.png-98.8kB


    到这里Web监测我们就创建好了,最后再看一下监控结果

    image_1cuf17mrlmaj94jdttq6p4ej7.png-70.8kB

  • 相关阅读:
    Socket编程模式
    Asp.Net Core
    TensorFlow文本与序列的深度模型
    Net
    XSS分析及预防(转)
    MyCAT部署及实现读写分离(转)
    如何搭建NTP服务(转)
    如何搭建DNS服务(转)
    如何高效地向Redis插入大量的数据(转)
    Android 通过广播启动另一个应用的Activity
  • 原文地址:https://www.cnblogs.com/linyaonie/p/11316864.html
Copyright © 2020-2023  润新知