[关闭]
@yangwenbo 2023-06-27T19:00:17.000000Z 字数 30493 阅读 360

zabbix监控平台

生产环境案例(三)

1. Zabbix生产环境监测案例概述

1.1 项目规划

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监测

今天的实战内容,请同学们注意总结过程

2. Zabbix监控MySQL数据库操作实战

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

2.1 Zabbix-Agen端编写监控脚本

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

  1. [root@Zabbix-Agen /]# mkdir -p /server/scripts
  2. [root@Zabbix-Agen /]# cd /server/scripts/
  3. [root@Zabbix-Agen scripts]# cat check_mysql.sh
  4. #!/bin/bash
  5. # author:Mr.yang
  6. MySQL_USER="root"
  7. MySQL_PWD="971108"
  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. [root@Zabbix-Agen scripts]# chmod +x check_mysql.sh
  3. [root@Zabbix-Agen scripts]# chown zabbix.zabbix check_mysql.sh
  4. [root@Zabbix-Agen scripts]# cd /etc/zabbix/zabbix_agentd.d/
  5. #删除旧键值模版
  6. [root@Zabbix-Agen zabbix_agentd.d]# ls
  7. userparameter_mysql.conf
  8. [root@Zabbix-Agen zabbix_agentd.d]# rm -rf userparameter_mysql.conf
  9. #创建mysql.ping和mysql.version的键值
  10. [root@Zabbix-Agen 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 -p971108 ping | grep -c alive
  13. UserParameter=mysql.version,/usr/local/bin/mysql -V
  14. #重启zabbix-agentd服务
  15. [root@Zabbix-Agen 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. [root@Zabbix-Server /]# zabbix_get -s 192.168.200.90 -p 10050 -k "mysql.status[Uptime]"
  2. 855
  3. [root@Zabbix-Server /]# zabbix_get -s 192.168.200.90 -p 10050 -k "mysql.status[Com_commit]"
  4. 0
  5. [root@Zabbix-Server /]# zabbix_get -s 192.168.200.90 -p 10050 -k "mysql.status[Bytes_sent]"
  6. 8926
  7. [root@Zabbix-Server /]# zabbix_get -s 192.168.200.90 -p 10050 -k "mysql.status[Bytes_received]"
  8. 667
  9. [root@Zabbix-Server /]# zabbix_get -s 192.168.200.90 -p 10050 -k "mysql.ping"
  10. 1
  11. [root@Zabbix-Server /]# zabbix_get -s 192.168.200.90 -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模版连接到此主机即可

image_1cnt4ff2m172hq2dk454uo1b7m9.png-63.9kB

image_1cnt4novj1p5u1anl1u51k541gmu1g.png-34.5kB

image_1cnt4ogav6vu11r66c71q2cb21t.png-19.8kB

image_1cnt4r7f4c5oq6q1vc0k261puh2a.png-127.4kB

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

3.Zabbix监控Apache、Nginx操作实战

3.1 实战监控Apache

3.1.1 安装依赖软件包
  1. [root@Zabbix-Apache ~]# yum -y install gcc gcc-c++ make cmake unzip tree
  2. [root@Zabbix-Apache ~]# rpm -qa gcc gcc-c++ make cmake unzip tree
  3. gcc-c++-4.4.7-4.el6.x86_64
  4. tree-1.5.3-2.el6.x86_64
  5. make-3.81-20.el6.x86_64
  6. gcc-4.4.7-4.el6.x86_64
  7. cmake-2.6.4-5.el6.x86_64
  8. unzip-6.0-1.el6.x86_64
3.1.2 源码编译构建apache服务
  1. #为了避免冲突,若系统中已经安装httpd服务,务必删除
  2. [root@Zabbix-Apache ~]# rpm -q httpd
  3. package httpd is not installed
  4. #源码构建apache
  5. [root@Zabbix-Apache yuanmabao]# pwd
  6. /root/yuanmabao
  7. [root@Zabbix-Apache yuanmabao]# tar xf httpd-2.2.17.tar.gz -C /usr/src/
  8. [root@Zabbix-Apache yuanmabao]# cd /usr/src/httpd-2.2.17/
  9. [root@Zabbix-Apache httpd-2.2.17]# ./configure --prefix=/usr/local/httpd --enable-so --enable-rewrite --enable-charset-lite --enable-cgi && make && make install
  10. #以下省略。。。
  1. [root@Zabbix-Apache httpd-2.2.17]# cd /usr/local/httpd/
  2. [root@Zabbix-Apache httpd]# ls
  3. bin build cgi-bin conf error htdocs icons include lib logs man manual modules
  4. [root@Zabbix-Apache httpd]# ln -s /usr/local/httpd/bin/* /usr/local/bin/
  5. [root@Zabbix-Apache httpd]# cp /usr/local/httpd/bin/apachectl /etc/init.d/httpd
  6. [root@Zabbix-Apache httpd]# chmod +x /etc/init.d/httpd
  1. [root@Zabbix-Apache httpd]# cd /usr/local/httpd/htdocs/
  2. [root@Zabbix-Apache htdocs]# echo "`hostname -I` I am apache" > index.html
  3. [root@Zabbix-Apache htdocs]# cat index.html
  4. 192.168.200.91 I am apache
  5. [root@Zabbix-Apache htdocs]# cd /usr/local/httpd/conf
  6. [root@Zabbix-Apache conf]# awk '/#ServerName/{print NR,$0}' httpd.conf
  7. 97 #ServerName www.example.com:80
  8. [root@Zabbix-Apache conf]# sed -i -e '97 s/#//;s/example/yunjisuan/' httpd.conf
  9. [root@Zabbix-Apache conf]# sed -n '97p' httpd.conf
  10. ServerName www.yunjisuan.com:80
  11. [root@Zabbix-Apache conf]# /etc/init.d/httpd start
  12. [root@Zabbix-Apache conf]# netstat -antup | grep httpd
  13. tcp 0 0 :::80 :::* LISTEN 53295/httpd
  1. [root@Zabbix-Apache conf]# echo "`hostname -I` www.yunjisuan.com" >> /etc/hosts
  2. [root@Zabbix-Apache conf]# tail -1 /etc/hosts
  3. 192.168.200.91 www.yunjisuan.com
  4. [root@Zabbix-Apache conf]# curl www.yunjisuan.com
  5. 192.168.200.91 I am apache
3.1.3 开启apache状态页

要打开apache的ServerStatus页面,需要在httpd.conf文件最下边加入代码段

  1. [root@Zabbix-Apache conf]# pwd
  2. /usr/local/httpd/conf
  3. [root@Zabbix-Apache conf]# tail -7 /usr/local/httpd/conf/httpd.conf
  4. ExtendedStatus On
  5. <location /server-status>
  6. SetHandler server-status
  7. Order Deny,Allow
  8. Deny from all
  9. Allow from 127.0.0.1
  10. </location>
  1. #最后,重启动apache即可
  2. [root@Zabbix-Apache conf]# /etc/init.d/httpd restart
3.1.4 下载apache zabbix模版并解压
  1. #从如下地址下载apache zabbix模版:
  2. [root@Zabbix-Apache ~]# wget https://github.com/lorf/zapache/archive/master.zip
  3. #解压zip格式压缩文件
  4. [root@Zabbix-Apache ~]# unzip master
  5. [root@Zabbix-Apache ~]# ls -d zapache-master/
  6. zapache-master/
  7. [root@Zabbix-Apache ~]# tree zapache-master/
  8. zapache-master/
  9. ├── httpd-server-status.conf.sample
  10. ├── README.md
  11. ├── userparameter_zapache.conf.sample
  12. ├── zapache
  13. ├── zapache-template-active.xml
  14. └── zapache-template.xml
  15. 0 directories, 6 files
  • 此模版文件中包含了监控apache的脚本文件,zabbix的apache模版文件,还有客户端zabbix自定义传key文件。
  • 首先将zapache放到需要监控的apache服务器上的/etc/zabbix/scripts目录下,如果没有scripts目录,自行创建一个即可。然后执行授权:
3.1.5 Zabbix-Apache端的agent安装
  1. [root@Zabbix-Apache ~]# wget http://repo.zabbix.com/zabbix/3.2/rhel/6/x86_64/zabbix-agent-3.2.4-1.el6.x86_64.rpm
  2. [root@Zabbix-Apache ~]# rpm -ivh zabbix-agent-3.2.4-1.el6.x86_64.rpm
  1. [root@Zabbix-Apache ~]# cd /etc/zabbix/
  2. [root@Zabbix-Apache zabbix]# cat -n zabbix_agentd.conf | sed -n '13p;32p;95p;103p;120p;136p;147p;265p;284p'
  3. 13 PidFile=/var/run/zabbix/zabbix_agentd.pid
  4. 32 LogFile=/var/log/zabbix/zabbix_agentd.log
  5. 95 Server=192.168.200.89 #这里要取消本地
  6. 103 ListenPort=10050
  7. 120 StartAgents=3
  8. 136 ServerActive=192.168.200.89:10051
  9. 147 Hostname=192.168.200.91
  10. #这里要修改成自己的名字,比如:主机IP
  11. 265 Include=/etc/zabbix/zabbix_agentd.d/
  12. 284 UnsafeUserParameters=1
  1. [root@Zabbix-Apache /]# setenforce 0
  2. [root@Zabbix-Apache /]# /etc/init.d/zabbix-agent start
  3. Starting Zabbix agent: [ OK ]
  4. [root@Zabbix-Apache /]# netstat -antup | grep zabbix_agent
  5. tcp 0 0 0.0.0.0:10050 0.0.0.0:* LISTEN 53511/zabbix_agentd
  6. tcp 0 0 :::10050 :::* LISTEN 53511/zabbix_agentd
  1. [root@Zabbix-Apache ~]# cd zapache-master/
  2. [root@Zabbix-Apache zapache-master]# cd ~
  3. [root@Zabbix-Apache ~]# mkdir -p /etc/zabbix/scripts/
  4. [root@Zabbix-Apache ~]# cp /root/zapache-master/zapache /etc/zabbix/scripts/
  5. [root@Zabbix-Apache ~]# chmod 755 /etc/zabbix/scripts/zapache
  6. [root@Zabbix-Apache ~]# ll -d /etc/zabbix/scripts/zapache
  7. -rwxr-xr-x. 1 root root 6110 Sep 21 22:48 /etc/zabbix/scripts/zapache
  8. [root@Zabbix-Apache ~]# cd /etc/zabbix/scripts/
  9. [root@Zabbix-Apache scripts]# ./zapache
  10. zapache version: 1.5
  11. usage:
  12. ./zapache [<url>] TotalAccesses - Check total accesses.
  13. ./zapache [<url>] TotalKBytes - Check total KBytes.
  14. ./zapache [<url>] CPULoad - Check CPU load.
  15. ./zapache [<url>] Uptime - Check uptime.
  16. ./zapache [<url>] ReqPerSec - Check requests per second.
  17. ./zapache [<url>] BytesPerSec - Check Bytes per second.
  18. ./zapache [<url>] BytesPerReq - Check Bytes per request.
  19. ./zapache [<url>] BusyWorkers - Check busy workers.
  20. ./zapache [<url>] IdleWorkers - Check idle workers.
  21. ./zapache [<url>] version - Version of this script.
  22. ./zapache [<url>] ping - Check if Apache is up.
  23. ./zapache [<url>] WaitingForConnection - Check Waiting for Connection processess.
  24. ./zapache [<url>] StartingUp - Check Starting Up processess.
  25. ./zapache [<url>] ReadingRequest - Check Reading Request processess.
  26. ./zapache [<url>] SendingReply - Check Sending Reply processess.
  27. ./zapache [<url>] KeepAlive - Check KeepAlive Processess.
  28. ./zapache [<url>] DNSLookup - Check DNSLookup Processess.
  29. ./zapache [<url>] ClosingConnection - Check Closing Connection Processess.
  30. ./zapache [<url>] Logging - Check Logging Processess.
  31. ./zapache [<url>] GracefullyFinishing - Check Gracefully Finishing Processess.
  32. ./zapache [<url>] IdleCleanupOfWorker - Check Idle Cleanup of Worker Processess.
  33. ./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. [root@Zabbix-Apache ~]# cd ~/zapache-master/
  2. [root@Zabbix-Apache zapache-master]# mv userparameter_zapache.conf.sample /etc/zabbix/zabbix_agentd.d/userparameter_zapache.conf
  3. [root@Zabbix-Apache zapache-master]# cd /etc/zabbix/zabbix_agentd.d/
  4. [root@Zabbix-Apache zabbix_agentd.d]# tail -1 userparameter_zapache.conf
  5. UserParameter=zapache[*],/etc/zabbix/scripts/zapache \$1
  6. [root@Zabbix-Apache zabbix_agentd.d]# /etc/init.d/zabbix-agent restart
  7. Shutting down Zabbix agent: [ OK ]
  8. Starting Zabbix agent: [ OK ]
3.1.6 测试键值

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

  1. [root@Zabbix-Server ~]# zabbix_get -s 192.168.200.91 -p 10050 -k "zapache[127.0.0.1 TotalAccesses]"
  2. 0
  3. [root@Zabbix-Server ~]# zabbix_get -s 192.168.200.91 -p 10050 -k "zapache[www.yunjisuan.com version]"
  4. 1.5
  5. [root@Zabbix-Server ~]# zabbix_get -s 192.168.200.91 -p 10050 -k "zapache[www.yunjisuan.com ping]"
  6. 1
3.1.7 在zabbix-server的Web页面上监控apache

(1)创建一个监控apache的主机
image_1cntgb55a1ir71lncnua1piq1trjgp.png-44.1kB

image.png-24kB
(2)导入带有apache监控项的模版

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

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

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

image_1cnvj12b94gl12kl15bda7m1mg9.png-22.3kB

image_1cnvj1lmh7gj84s1cqeq3s1vkrm.png-39.5kB

image_1cnvj27nr1v3mbotsfagfg53n13.png-50.8kB

image_1cnvj2mlnb2j1krefv31ftk1rqo1g.png-50.6kB

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

(3)将模版链接到apache主机
image_1cnvj3mhv1jmq1rhq1e21uf5ts51t.png-37.7kB

image_1cnvj44pkoml3ek1291k9b1qgl2a.png-29.8kB

image_1cnvj4jmdkjf1q0fbcdo3d13ub2n.png-35.5kB

3.2 实战监控nginx

3.2.1用本地yum仓库安装依赖包
  1. [root@Zabbix-Apache /]# yum install -y pcre-devel openssl-devel gcc gcc-c++ make
  2. [root@yangwenbo /]# rpm -qa pcre-devel openssl-devel gcc gcc-c++ make
  3. openssl-devel-1.0.1e-15.el6.x86_64
  4. gcc-4.4.7-4.el6.x86_64
  5. gcc-c++-4.4.7-4.el6.x86_64
  6. pcre-devel-7.8-6.el6.x86_64
  7. make-3.81-20.el6.x86_64
3.2.2 安装Nginx
  1. #源码编译Nginx
  2. [root@Zabbix-Apache yuanmabao]# pwd
  3. /yuanmabao
  4. [root@Zabbix-Apache yuanmabao]# useradd -s /sbin/nologin -M www
  5. [root@Zabbix-Apache yuanmabao]# tar xf nginx-1.10.2.tar.gz -C /usr/src/
  6. [root@Zabbix-Apache yuanmabao]# cd /usr/src/nginx-1.10.2/
  7. [root@Zabbix-Apache nginx-1.10.2]# ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_ssl_module && make && make install
  8. ##编译省略。。。
  9. [root@Zabbix-Apache nginx-1.10.2]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/
  1. #创建nginx配置文件模版
  2. [root@Zabbix-Apache nginx-1.10.2]# cd /usr/local/nginx/conf/
  3. [root@Zabbix-Apache conf]# cat nginx.conf
  4. worker_processes 1;
  5. events {
  6. worker_connections 1024;
  7. }
  8. http {
  9. include mime.types;
  10. default_type application/octet-stream;
  11. sendfile on;
  12. keepalive_timeout 65;
  13. server {
  14. listen 80;
  15. server_name localhost;
  16. location / {
  17. root html;
  18. index index.php index.html index.htm;
  19. }
  20. location = /nginx-status { #在nginx的配置文件中,添加status配置
  21. stub_status on;
  22. access_log off;
  23. allow 127.0.0.1;
  24. allow 192.168.200.89; #zabbix服务器地址一般是Zabbix_Server地址
  25. deny all;
  26. }
  27. location ~ \.php$ {
  28. fastcgi_pass 127.0.0.1:9000;
  29. fastcgi_index index.php;
  30. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  31. include fastcgi_params;
  32. }
  33. error_page 500 502 503 504 /50x.html;
  34. location = /50x.html {
  35. root html;
  36. }
  37. }
  38. }
  39. [root@Zabbix-Apache conf]# nginx -t
  40. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  41. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
  42. [root@Zabbix-Apache conf]# /usr/local/nginx/sbin/nginx
  43. [root@Zabbix-Apache conf]# netstat -antup | grep nginx
  44. tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1279/nginx
3.2.3访问设置好的nginx-status链接(之前已配好,这只是模版)
  1. #在zabbix-server上通过如下方式获取nginx状态页面信息:
  2. [root@Zabbix-Server ~]# curl 192.168.200.91/nginx-status
  3. Active connections: 1
  4. server accepts handled requests
  5. 15 15 15
  6. Reading: 0 Writing: 1 Waiting: 0
  7. nginx status详细说明如下:
  8. Active connections:对后端发起的活动连接数;
  9. acceptsnginx总共处理了多少个连接;
  10. handlednginx成功创建了几次握手;
  11. requestsnginx总共处理了多少请求。
  12. Readingnginx读取客户端的header数;
  13. Writingnginx返回给客户端的header数;
  14. Waitingnginx请求处理完成,正在等待下一请求指令的连接
3.2.4 在Zabbix-Apache端编写nginx状态监控脚本
  1. [root@Zabbix-Apache ~]# cd /etc/zabbix/scripts/
  2. [root@Zabbix-Apache scripts]# cat nginx_status.sh
  3. #!/bin/bash
  4. # Set Variables
  5. HOST="127.0.0.1"
  6. PORT="80"
  7. #Functions to return nginx stats
  8. function active(){
  9. /usr/bin/curl "http://$HOST:$PORT/nginx-status" 2>/dev/null | grep "Active" | awk '{print $NF}'
  10. }
  11. function reading(){
  12. /usr/bin/curl "http://$HOST:$PORT/nginx-status" 2>/dev/null | grep "Reading" | awk '{print $2}'
  13. }
  14. function writing(){
  15. /usr/bin/curl "http://$HOST:$PORT/nginx-status" 2>/dev/null | grep "Writing" | awk '{print $4}'
  16. }
  17. function accepts(){
  18. /usr/bin/curl "http://$HOST:$PORT/nginx-status" 2>/dev/null | awk 'NR==3' | awk '{print $1}'
  19. }
  20. function handled(){
  21. /usr/bin/curl "http://$HOST:$PORT/nginx-status" 2>/dev/null | awk 'NR==3' | awk '{print $2}'
  22. }
  23. function requests(){
  24. /usr/bin/curl "http://$HOST:$PORT/nginx-status" 2>/dev/null | awk 'NR==3' | awk '{print $3}'
  25. }
  26. function ping(){
  27. /sbin/pidof nginx | wc -l
  28. }
  29. # Run the requested function
  30. $1
3.2.5 给脚本可执行权限并且授权
  1. [root@Zabbix-Apache scripts]# chmod +x nginx_status.sh
  2. [root@Zabbix-Apache scripts]# chown zabbix.zabbix nginx_status.sh
3.2.6 在zabbix-Apache端配置nginx键值配置文件
  1. [root@Zabbix-Apache ~]# cd /etc/zabbix/zabbix_agentd.d
  2. [root@Zabbix-Apache zabbix_agentd.d]# cat nginx.conf
  3. UserParameter=nginx.active,/usr/bin/curl -s "http://127.0.0.1:80/nginx-status" | grep "Active" | awk '{print $NF}'
  4. UserParameter=nginx.status[*],/etc/zabbix/scripts/nginx_status.sh $1
  5. [root@Zabbix-Apache zabbix_agentd.d]# /etc/init.d/zabbix-agent restart
  6. Shutting down Zabbix agent: [ OK ]
  7. Starting Zabbix agent: [ OK ]
3.2.7 在zabbix-server端测试键值
  1. [root@Zabbix-Server ~]# zabbix_get -s 192.168.200.91 -k "nginx.status[reading]" -p 10050
  2. 0
  3. [root@Zabbix-Server ~]# zabbix_get -s 192.168.200.91 -k "nginx.status[active]" -p 10050
  4. 1
  5. [root@Zabbix-Server ~]# zabbix_get -s 192.168.200.91 -k "nginx.status[reading]" -p 10050
  6. 0
  7. [root@Zabbix-Server ~]# zabbix_get -s 192.168.200.91 -k "nginx.status[writing]" -p 10050
  8. 1
  9. [root@Zabbix-Server ~]# zabbix_get -s 192.168.200.91 -k "nginx.status[ping]" -p 10050
  10. 1
3.2.8 接下来我们需要在Web端导入nginx监控模版

image_1cnvj6itm8pf8bao9q1b0s1v9k44.png-22.5kB

image_1cnvj72pm8tl1ups1b07o4e102n4h.png-55.3kB

image_1cnvj7ld41f561elpiuicsq18rm4u.png-41.3kB

3.2.9 异常调试
  1. 如果Zabbix图形界面一直没有获取到数据,可以通过在zabbix server上执行zabbix_get手动测试:
  2. /usr/local/zabbix/bin/zabbix_get -s 192.168.200.91 -p 10050 -k "nginx.status[active]"
  3. 如果获取数据有问题,会给出相应提示,如果能够获取到数据,那就没问题了。

4.Zabbix监控Tomcat操作实战

zabbix监控tomcat,需要zabbix_server开启java poller,zabbix_java开启javaGateway,端口10052,Tomcat JMX开启12345端口,提供性能数据。

数据获取流程为:java poller<-->javaGateway:10052<-->Tomcat:12345

image_1cnvkjkb11e0b14tn1uth1uou19kg19.png-89.3kB

4.1 部署java环境jdk

4.1.1 jdk的解压安装
  1. [root@Zabbix-Tomcat yuanmabao]# pwd
  2. /root/yuanmabao
  3. [root@Zabbix-Tomcat yuanmabao]# tar xf jdk-8u60-linux-x64.tar.gz -C /usr/local/
  4. [root@Zabbix-Tomcat yuanmabao]# ln -s /usr/local/jdk1.8.0_60 /usr/local/jdk
4.1.2 配置java环境变量
  1. [root@Zabbix-Tomcat yuanmabao]# 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
  2. [root@Zabbix-Tomcat yuanmabao]# tail -3 /etc/profile
  3. export JAVA_HOME=/usr/local/jdk
  4. export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
  5. export CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/lib/tools.jar
4.1.3 让java环境变量立刻生效
  1. [root@Zabbix-Tomcat yuanmabao]# source /etc/profile
4.1.4 检查java环境安装情况
  1. [root@Zabbix-Tomcat yuanmabao]# source /etc/profile
  2. [root@Zabbix-Tomcat yuanmabao]# which java
  3. /usr/local/jdk/bin/java
  4. [root@Zabbix-Tomcat yuanmabao]# java -version #出现以下信息表示部署成功
  5. java version "1.8.0_60"
  6. Java(TM) SE Runtime Environment (build 1.8.0_60-b27)
  7. Java HotSpot(TM) 64-Bit Server VM (build 25.60-b23, mixed mode)

4.2 安装Tomcat

4.2.1 解压安装Tomcat
  1. [root@Zabbix-Tomcat yuanmabao]# pwd
  2. /root/yuanmabao
  3. [root@Zabbix-Tomcat yuanmabao]# tar xf apache-tomcat-8.0.27.tar.gz -C /usr/local/
  4. [root@Zabbix-Tomcat yuanmabao]# ln -s /usr/local/apache-tomcat-8.0.27/ /usr/local/tomcat
4.2.2 配置Tomcat环境变量
  1. [root@Zabbix-Tomcat yuanmabao]# echo 'export TOMCAT_HOME=/usr/local/tomcat' >> /etc/profile
  2. [root@Zabbix-Tomcat yuanmabao]# source /etc/profile
4.2.3 对jdk及Tomcat安装目录递归授权root
  1. [root@Zabbix-Tomcat yuanmabao]# chown -R root.root /usr/local/jdk/ /usr/local/tomcat/
  2. [root@Zabbix-Tomcat yuanmabao]# ll -d /usr/local/jdk/ /usr/local/tomcat/
  3. drwxr-xr-x. 8 root root 4096 Aug 5 2015 /usr/local/jdk/
  4. drwxr-xr-x. 9 root root 4096 Sep 22 01:56 /usr/local/tomcat/
4.2.4 检查环境变量配置情况
  1. [root@Zabbix-Tomcat yuanmabao]# tail -4 /etc/profile
  2. export JAVA_HOME=/usr/local/jdk
  3. export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
  4. export CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/lib/tools.jar
  5. export TOMCAT_HOME=/usr/local/tomcat

4.3 启动Tomcat

启动程序:/usr/local/tomcat/bin/startup.sh
关闭程序:/usr/local/tomcat/bin/shutdown.sh

  1. [root@Zabbix-Tomcat yuanmabao]# cd
  2. [root@Zabbix-Tomcat ~]# /usr/local/tomcat/bin/startup.sh
  3. Using CATALINA_BASE: /usr/local/tomcat
  4. Using CATALINA_HOME: /usr/local/tomcat
  5. Using CATALINA_TMPDIR: /usr/local/tomcat/temp
  6. Using JRE_HOME: /usr/local/jdk
  7. Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
  8. Tomcat started.
  9. [root@Zabbix-Tomcat ~]# netstat -antup | grep java
  10. tcp 0 0 :::8080 :::* LISTEN 1365/java
  11. tcp 0 0 ::ffff:127.0.0.1:8005 :::* LISTEN 1365/java
  12. tcp 0 0 :::8009 :::* LISTEN 1365/java

4.4 访问网站192.168.200.92:8080(注意要关闭防火墙)

4.5 Zabbix-Tomcat端的agent安装

  1. [root@Zabbix-Tomcate ~]# wget http://repo.zabbix.com/zabbix/3.2/rhel/6/x86_64/zabbix-agent-3.2.4-1.el6.x86_64.rpm
  2. [root@Zabbix-Tomcat ~]# rpm -ivh zabbix-agent-3.2.4-1.el6.x86_64.rpm
  1. [root@Zabbix-Tomcat ~]# cd /etc/zabbix/
  2. [root@Zabbix-Tomcat zabbix]# cat -n zabbix_agentd.conf | sed -n '13p;32p;95p;103p;120p;136p;147p;265p;284p'
  3. 13 PidFile=/var/run/zabbix/zabbix_agentd.pid
  4. 32 LogFile=/var/log/zabbix/zabbix_agentd.log
  5. 95 Server=192.168.200.89 #这里要取消本地
  6. 103 ListenPort=10050
  7. 120 StartAgents=3
  8. 136 ServerActive=192.168.200.89:10051
  9. 147 Hostname=192.168.200.92
  10. #这里要修改成自己的名字,比如:主机IP
  11. 265 Include=/etc/zabbix/zabbix_agentd.d/
  12. 284 UnsafeUserParameters=1
  1. [root@Zabbix-Tomcat /]# setenforce 0
  2. [root@Zabbix-Tomcat /]# /etc/init.d/zabbix-agent start
  3. Starting Zabbix agent: [ OK ]
  4. [root@Zabbix-Tomcat /]# netstat -antup | grep zabbix_agent
  5. tcp 0 0 0.0.0.0:10050 0.0.0.0:* LISTEN 53511/zabbix_agentd
  6. tcp 0 0 :::10050 :::* LISTEN 53511/zabbix_agentd

4.6 配置Tomcat JMX

  1. #配置jmx,在需要监控的tomcat服务器上,编辑catalina.sh,加入如下配置
  2. [root@Zabbix-Tomcat ~]# sed -n '249p' /usr/local/tomcat/bin/catalina.sh
  3. 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.92 -Dcom.sun.management.jmxremote.port=12345"

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

4.7 重启Tomcat

  1. [root@Zabbix-Tomcat ~]# /usr/local/tomcat/bin/shutdown.sh
  2. Using CATALINA_BASE: /usr/local/tomcat
  3. Using CATALINA_HOME: /usr/local/tomcat
  4. Using CATALINA_TMPDIR: /usr/local/tomcat/temp
  5. Using JRE_HOME: /usr/local/jdk
  6. Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
  7. [root@Zabbix-Tomcat ~]# netstat -antup | grep java
  1. [root@Zabbix-Tomcat ~]# /usr/local/tomcat/bin/startup.sh
  2. Using CATALINA_BASE: /usr/local/tomcat
  3. Using CATALINA_HOME: /usr/local/tomcat
  4. Using CATALINA_TMPDIR: /usr/local/tomcat/temp
  5. Using JRE_HOME: /usr/local/jdk
  6. Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
  7. Tomcat started.
  8. [root@Zabbix-Tomcat ~]# netstat -antup | grep java
  9. tcp 0 0 :::8080 :::* LISTEN 2135/java
  10. tcp 0 0 ::ffff:127.0.0.1:8005 :::* LISTEN 2135/java
  11. tcp 0 0 :::8009 :::* LISTEN 2135/java

4.8 在zabbix-server端编译zabbix,加入java支持

在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.8.1 在zabbix-server端安装JDK

(1) jdk的解压安装

  1. [root@Zabbix-Server yuanmabao]# pwd
  2. /root/yuanmabao
  3. [root@Zabbix-Server yuanmabao]# tar xf jdk-8u60-linux-x64.tar.gz -C /usr/local/
  4. [root@Zabbix-Server yuanmabao]# ln -s /usr/local/jdk1.8.0_60/ /usr/local/jdk

(2) 配置java环境变量

  1. [root@Zabbix-Server yuanmabao]# 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
  2. [root@Zabbix-Server yuanmabao]# tail -3 /etc/profile
  3. export JAVA_HOME=/usr/local/jdk
  4. export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
  5. export CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/lib/tools.jar

(3) 让java环境变量立刻生效

  1. [root@Zabbix-Server yuanmabao]# source /etc/profile

(4) 检查java环境安装情况

  1. [root@Zabbix-Server yuanmabao]# which java
  2. /usr/local/jdk/bin/java
  3. [root@Zabbix-Server yuanmabao]# java -version #出现以下信息表示部署成功
  4. java version "1.8.0_60"
  5. Java(TM) SE Runtime Environment (build 1.8.0_60-b27)
  6. Java HotSpot(TM) 64-Bit Server VM (build 25.60-b23, mixed mode)
4.8.2 重新编译zabbix-server服务器端

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

  1. [root@Zabbix-Server ~]# ll /usr/local/zabbix/sbin/
  2. total 8596
  3. -rwxr-xr-x. 1 root root 1165774 Sep 20 21:23 zabbix_agentd
  4. -rwxr-xr-x. 1 root root 3668935 Sep 20 21:23 zabbix_proxy
  5. -rwxr-xr-x. 1 root root 3962949 Sep 20 21:23 zabbix_server

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

  1. [root@Zabbix-Server ~]# /etc/init.d/zabbix_server stop
  2. Shutting down zabbix_server: [ OK ]
  3. [root@Zabbix-Server ~]# cd /usr/src/zabbix-3.2.4/
  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 && make && make install
  5. #以下省略。。。

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

  1. [root@Zabbix-Server zabbix-3.2.4]# cd /usr/local/zabbix/sbin/
  2. [root@Zabbix-Server sbin]# ll
  3. total 8600
  4. -rwxr-xr-x. 1 root root 1165774 Sep 22 03:38 zabbix_agentd
  5. drwxr-xr-x. 4 root root 4096 Sep 22 03:38 zabbix_java #多了一个目录
  6. -rwxr-xr-x. 1 root root 3668935 Sep 22 03:38 zabbix_proxy
  7. -rwxr-xr-x. 1 root root 3962949 Sep 22 03:38 zabbix_server
  8. [root@Zabbix-Server sbin]# cd zabbix_java/
  9. [root@Zabbix-Server zabbix_java]# ll
  10. total 20
  11. drwxr-xr-x. 2 root root 4096 Sep 22 03:38 bin
  12. drwxr-xr-x. 2 root root 4096 Sep 22 03:38 lib
  13. -rw-r--r--. 1 root root 791 Sep 22 03:38 settings.sh #java gateway配置脚本
  14. -rwxr-xr-x. 1 root root 545 Sep 22 03:38 shutdown.sh #停止java gateway服务
  15. -rwxr-xr-x. 1 root root 2025 Sep 22 03:38 startup.sh #启动java gateway服务

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

4.8.3 启动java Gateway服务
  1. [root@Zabbix-Server zabbix_java]# /usr/local/zabbix/sbin/zabbix_java/startup.sh
  2. [root@Zabbix-Server zabbix_java]# netstat -antup | grep 10052
  3. tcp 0 0 :::10052 :::* LISTEN 20838/java

4.9 修改zabbix server 配置

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

  1. #修改以下配置文件的内容
  2. [root@Zabbix-Server zabbix_java]# cd /usr/local/zabbix/etc/
  3. [root@Zabbix-Server etc]# cat -n zabbix_server.conf | sed -n '215p;223p;231p'
  4. 215 JavaGateway=127.0.0.1
  5. 223 JavaGatewayPort=10052
  6. 231 StartJavaPollers=5

4.10 重启zabbix_server服务并查看已打开的java poller进程

  1. [root@Zabbix-Server zabbix_java]# /etc/init.d/zabbix_server start
  2. Starting zabbix_server: [ OK ]
  3. [root@Zabbix-Server zabbix_java]# netstat -antup | grep 10051
  4. tcp 0 0 0.0.0.0:10051 0.0.0.0:* LISTEN 21053/zabbix_server
  5. tcp 0 0 192.168.200.89:10051 192.168.200.91:52212 TIME_WAIT -
  6. #查看已经打开的java poller进程
  7. [root@Zabbix-Server zabbix_java]# ps -ef | grep "java poller" | grep -v grep
  8. zabbix 21110 21053 0 03:55 ? 00:00:00 /usr/local/sbin/zabbix_server: java poller #1 [got 0 values in 0.000012 sec, idle 5 sec]
  9. zabbix 21117 21053 0 03:55 ? 00:00:00 /usr/local/sbin/zabbix_server: java poller #2 [got 0 values in 0.000009 sec, idle 5 sec]
  10. zabbix 21118 21053 0 03:55 ? 00:00:00 /usr/local/sbin/zabbix_server: java poller #3 [got 0 values in 0.000005 sec, idle 5 sec]
  11. zabbix 21119 21053 0 03:55 ? 00:00:00 /usr/local/sbin/zabbix_server: java poller #4 [got 0 values in 0.000005 sec, idle 5 sec]
  12. zabbix 21120 21053 0 03:55 ? 00:00:00 /usr/local/sbin/zabbix_server: java poller #5 [got 0 values in 0.000009 sec, idle 5 sec]

4.11 Zabbix图形界面配置

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

image_1cnvrj7ja1e5rp6dsl187312a11m.png-27.3kB

image_1co01akuoheu1r221qg1meq1fjqm.png-46.8kB

image_1cnvrq576kr21j2t16qj1rig1ds840.png-30.3kB

4.11.2 导入zabbix监控tomcat的模版

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

image_1cnvrtaft1mc05p8ak145o1jl84q.png-26.5kB

image_1cnvrv00jpnvgpp156k1vcl8r557.png-41.2kB

image_1cnvs0fu8cs51f861qlv14jvcsl5k.png-58.5kB

image_1cnvs28jl1rvc1uknvqh7tv8p71.png-43.8kB

image_1cnvs3ev31iis191stv2v2b2pl7e.png-34.5kB

4.11.3 最后我们关联模版即可

image_1cnvs6jj51hakdt08ro76u9967r.png-47.9kB

image_1cnvs7c301tabsdqgul1i9tvqc88.png-25.2kB

image_1cnvs9id1v778qh1esourkff18l.png-31.5kB

image_1co00r1sp12q01apkd8k1f10192p9.png-63.1kB

5.Zabbix监控PHP-FPM操作实战

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

5.1 启用php-fpm状态功能

修改php-fpm配置文件,确保如下配置是打开状态:
pm.status_path = /status,具体操作如下

  1. #在Zabbix-server端操作如下
  2. [root@Zabbix-Server ~]# cd /usr/local/php/etc
  3. [root@Zabbix-Server etc]# ls
  4. pear.conf php-fpm.conf php-fpm.conf.default php.ini
  5. [root@Zabbix-Server etc]# sed -n '360p' php-fpm.conf
  6. pm.status_path = /status
  1. #重启php-fpm服务
  2. [root@Zabbix-Server etc]# netstat -antup | grep php-fpm
  3. tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 1604/php-fpm
  4. [root@Zabbix-Server etc]# pkill php-fpm
  5. [root@Zabbix-Server etc]# netstat -antup | grep php-fpm
  6. [root@Zabbix-Server etc]# /usr/local/php/sbin/php-fpm
  7. [root@Zabbix-Server etc]# netstat -antup | grep php-fpm
  8. tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 23924/php-fpm

5.2 nginx配置php-fpm状态页面

在nginx配置文件nginx.conf里配置php状态页,操作如下

  1. [root@Zabbix-Server ~]# cd /usr/local/nginx/conf/
  2. [root@Zabbix-Server conf]# cat nginx.conf
  3. worker_processes 1;
  4. events {
  5. worker_connections 1024;
  6. }
  7. http {
  8. include mime.types;
  9. default_type application/octet-stream;
  10. sendfile on;
  11. keepalive_timeout 65;
  12. server {
  13. listen 80;
  14. server_name localhost;
  15. location / {
  16. root html;
  17. index index.php index.html index.htm;
  18. }
  19. location = /nginx-status {
  20. stub_status on;
  21. access_log off;
  22. }
  23. location = /status { #添加此location
  24. include fastcgi_params;
  25. fastcgi_pass 127.0.0.1:9000;
  26. fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
  27. }
  28. location ~ \.php$ {
  29. fastcgi_pass 127.0.0.1:9000;
  30. fastcgi_index index.php;
  31. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  32. include fastcgi_params;
  33. }
  34. error_page 500 502 503 504 /50x.html;
  35. location = /50x.html {
  36. root html;
  37. }
  38. }
  39. }
  1. #重启动nginx服务
  2. [root@Zabbix-Server conf]# nginx -t
  3. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  4. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
  5. [root@Zabbix-Server conf]# /usr/local/nginx/sbin/nginx -s reload

5.3 状态页浏览器访问测试

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

image_1co020imi1fafgus114t1v1010591j.png-15.4kB

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

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

5.4.1 以xml的格式输出PHP-fpm状态页
  1. [root@Zabbix-Server conf]# curl 127.0.0.1/status?xml
  2. <?xml version="1.0" ?>
  3. <status>
  4. <pool>www</pool>
  5. <process-manager>dynamic</process-manager>
  6. <start-time>1537566549</start-time>
  7. <start-since>1131</start-since>
  8. <accepted-conn>137</accepted-conn>
  9. <listen-queue>0</listen-queue>
  10. <max-listen-queue>0</max-listen-queue>
  11. <listen-queue-len>128</listen-queue-len>
  12. <idle-processes>2</idle-processes>
  13. <active-processes>1</active-processes>
  14. <total-processes>3</total-processes>
  15. <max-active-processes>2</max-active-processes>
  16. <max-children-reached>0</max-children-reached>
  17. <slow-requests>0</slow-requests>
5.4.2 以json的格式输出PHP-fpm的状态页
  1. [root@Zabbix-Server conf]# curl 127.0.0.1/status?json
  2. {"pool":"www","process manager":"dynamic","start time":1537566549,"start since":1159,"accepted conn":140,"listen queue":0,"max listen queue":0,"listen queue len":128,"idle processes":2,"active processes":1,"total processes":3,"max active processes":2,"max children reach
5.4.3 以html的格式输出PHP-fpm的状态页
  1. [root@Zabbix-Server conf]# curl 127.0.0.1/status?html
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  4. <head><title>PHP-FPM Status Page</title></head>
  5. <body>
  6. <table>
  7. <tr><th>pool</th><td>www</td></tr>
  8. <tr><th>process manager</th><td>dynamic</td></tr>
  9. <tr><th>start time</th><td>22/Sep/2018:05:49:09 +0800</td></tr>
  10. <tr><th>start since</th><td>1318</td></tr>
  11. <tr><th>accepted conn</th><td>159</td></tr>
  12. <tr><th>listen queue</th><td>0</td></tr>
  13. <tr><th>max listen queue</th><td>0</td></tr>
  14. <tr><th>listen queue len</th><td>128</td></tr>
  15. <tr><th>idle processes</th><td>2</td></tr>
  16. <tr><th>active processes</th><td>1</td></tr>
  17. <tr><th>total processes</th><td>3</td></tr>
  18. <tr><th>max active processes</th><td>2</td></tr>
  19. <tr><th>max children reached</th><td>0</td></tr>
  20. <tr><th>slow requests</th><td>0</td></tr>
  21. </table>

5.5 在Zabbix-Server端修改agent配置

5.5.1 创建zabbix监控的agent端键值配置文件
  1. [root@Zabbix-Server ~]# cd /etc/zabbix/zabbix_agentd.d/
  2. [root@Zabbix-Server zabbix_agentd.d]# cat userparameter_php-fpm.conf
  3. UserParameter=php-fpm.status[*],/usr/bin/curl -s "http://127.0.0.1/status?xml" | grep "<$1>" | awk -F "[>|<]" '{print $$3}'
  4. #这里需要注意在脚本里如果写成$3的话,脚本会认识是一个变量,因此我们需要用$$3
5.5.2 重启动agent客户端
  1. [root@Zabbix-Server zabbix_agentd.d]# /etc/init.d/zabbix-agent restart
  2. Shutting down Zabbix agent: [ OK ]
  3. Starting Zabbix agent: [ OK ]
  4. [root@Zabbix-Server zabbix_agentd.d]# netstat -antup | grep zabbix_agent
  5. tcp 0 0 0.0.0.0:10050 0.0.0.0:* LISTEN 24658/zabbix_agentd
  6. tcp 0 0 :::10050 :::* LISTEN 24658/zabbix_agentd

5.6 进行键值配置文件测试

  1. [root@Zabbix-Server zabbix_agentd.d]# zabbix_get -s 192.168.200.89 -p 10050 -k "php-fpm.status[process-manager]"
  2. dynamic
  3. [root@Zabbix-Server zabbix_agentd.d]# zabbix_get -s 192.168.200.89 -p 10050 -k "php-fpm.status[start-since]"
  4. 2183
  5. [root@Zabbix-Server zabbix_agentd.d]# zabbix_get -s 192.168.200.89 -p 10050 -k "php-fpm.status[active-processes]"
  6. 1

5.7 Zabbix图形界面导入我们的监控模版

image.png-26.8kB

image.png-43.6kB

image.png-55.7kB

image.png-41.5kB

image.png-24.6kB

6. Zabbix的Web监测功能

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

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

image.png-91.5kB

image.png-51.8kB

image.png-44.3kB

image.png-23.9kB

image.png-29.1kB

image.png-26.8kB

image.png-56.4kB

6.2 查看监控结果

image.png-34.2kB

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注