zabbix 应用监控

监控nginx

## 1.首先编写nginx的状态
[root@web02 ~]# vim /etc/nginx/conf.d/1nginx_monitor.conf 
server {
	listen 80;
	server_name _;

	location /{
		root /blog;
		index index.html;
	}

	location /nginx_status {
		stub_status on;
		access_log off;
		allow 127.0.0.1;
		deny all;
	}
}

[root@web02 conf.d]# curl http://127.0.0.1/nginx_status
Active connections: 1 
server accepts handled requests
 1 1 1 
 
## 2.创建zabbix存放脚本目录
[root@web02 ~]# mkdir -p /etc/zabbix/scripts

## 3.编写监控项脚本
[root@web02 ~]# vim /etc/zabbix/scripts/nginx_status.sh
#!/bin/bash

NGINX_PORT=80  #如果端口不同仅需要修改脚本即可,否则修改xml很麻烦
NGINX_COMMAND=$1


nginx_active(){
    /usr/bin/curl -s "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" |awk '/Active/ {print $NF}'
}

nginx_reading(){
    /usr/bin/curl -s "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" |awk '/Reading/ {print $2}'
}

nginx_writing(){
    /usr/bin/curl -s "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" |awk '/Writing/ {print $4}'
       }

nginx_waiting(){
    /usr/bin/curl -s "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" |awk '/Waiting/ {print $6}'
       }

nginx_accepts(){
    /usr/bin/curl -s "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" |awk 'NR==3 {print $1}'
       }

nginx_handled(){
    /usr/bin/curl -s "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" |awk 'NR==3 {print $2}'
       }

nginx_requests(){
    /usr/bin/curl -s "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" |awk 'NR==3 {print $3}'
       }


  case $NGINX_COMMAND in
    active)
        nginx_active;
        ;;
    reading)
        nginx_reading;
        ;;
    writing)
        nginx_writing;
        ;;
    waiting)
        nginx_waiting;
        ;;
    accepts)
        nginx_accepts;
        ;;
    handled)
        nginx_handled;
        ;;
    requests)
        nginx_requests;
        ;;
          *)
        echo $"USAGE:$0 {active|reading|writing|waiting|accepts|handled|requests}"
    esac

## 4.给脚本添加执行权限
[root@web02 ~]# chmod +x /etc/zabbix/scripts/nginx_status.sh

## 5.修改客户端配置文件
[root@web02 ~]# vim /etc/zabbix/zabbix_agentd.d/nginx_status.conf
UserParameter=nginx_status[*],/bin/bash /etc/zabbix/scripts/nginx_status.sh "$1"

## 6.重启agent
[root@web02 ~]# systemctl restart zabbix-agent

## 7.检测
[root@zabbix ~]# zabbix_get -s 172.16.1.8 -k nginx_status[waiting]
0
[root@zabbix ~]# zabbix_get -s 172.16.1.8 -k nginx_status[requests]
5
[root@zabbix ~]# zabbix_get -s 172.16.1.8 -k nginx_status[accepts]
6

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

zabbix监控PHP

修改php配置

## 1.打开php状态模块
[root@web02 ~]# vim /etc/php-fpm.d/www.conf
pm.status_path = /phpfpm_status

## 2.重启php
[root@web02 ~]# systemctl restart php-fpm.service 

修改nginx配置文件

## 1.修改nginx配置文件
[root@web02 ~]# vim /etc/nginx/conf.d/1nginx_monitor.confng
server {
        listen 80;
        server_name _;

        location /nginx_status {
                stub_status on;
                allow 127.0.0.1;
                deny all;
        }

        location ~ ^/(phpfpm_status)$ {
                include fastcgi_params;
                fastcgi_pass    unix:/dev/php.sock;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        }
}

## 2.重启nginx
[root@web02 ~]# systemctl restart nginx

## 3.查看phpfpm状态页
[root@web02 ~]# curl http://127.0.0.1/phpfpm_status
pool:                 www
process manager:      dynamic
start time:           26/Aug/2022:22:14:11 +0800
start since:          440
accepted conn:        1
listen queue:         0
max listen queue:     0
listen queue len:     0
idle processes:       4
active processes:     1
total processes:      5
max active processes: 1
max children reached: 0
slow requests:        0

# PHP-FPM状态解释:
pool #fpm池名称,大多数为www
process manager #进程管理方式dynamic或者static
start time #启动日志,如果reload了fpm,时间会更新
start since #运行时间
accepted conn #当前池接受的请求数
listen queue #请求等待队列,如果这个值不为0,那么需要增加FPM的进程数量
max listen queue #请求等待队列最高的数量
listen queue len #socket等待队列长度
idle processes #空闲进程数量
active processes #活跃进程数量
total processes #总进程数量
max active processes #最大的活跃进程数量(FPM启动开始计算)
max children reached #进程最大数量限制的次数,如果这个数量不为0,那说明你的最大进程数量过小,可以适当调整。
slow requests  # 请求比较慢的数量

编写监控脚本

## 1.脚本内容
[root@web02 ~]# vim /etc/zabbix/scripts/phpfpm_status.sh
#!/bin/bash

PHPFPM_COMMAND=$1
PHPFPM_PORT=80  #根据监听不同端口进行调整

start_since(){
    /usr/bin/curl -s "http://127.0.0.1:"$PHPFPM_PORT"/phpfpm_status" |awk '/^start since:/ {print $NF}'
}

accepted_conn(){
    /usr/bin/curl -s "http://127.0.0.1:"$PHPFPM_PORT"/phpfpm_status" |awk '/^accepted conn:/ {print $NF}'
}

listen_queue(){
    /usr/bin/curl -s "http://127.0.0.1:"$PHPFPM_PORT"/phpfpm_status" |awk '/^listen queue:/ {print $NF}'
}

max_listen_queue(){
    /usr/bin/curl -s "http://127.0.0.1:"$PHPFPM_PORT"/phpfpm_status" |awk '/^max listen queue:/ {print $NF}'
}

listen_queue_len(){
    /usr/bin/curl -s "http://127.0.0.1:"$PHPFPM_PORT"/phpfpm_status" |awk '/^listen queue len:/ {print $NF}'
}

idle_processes(){
    /usr/bin/curl -s "http://127.0.0.1:"$PHPFPM_PORT"/phpfpm_status" |awk '/^idle processes:/ {print $NF}'
}

active_processes(){
    /usr/bin/curl -s "http://127.0.0.1:"$PHPFPM_PORT"/phpfpm_status" |awk '/^active processes:/ {print $NF}'
}

total_processes(){
    /usr/bin/curl -s "http://127.0.0.1:"$PHPFPM_PORT"/phpfpm_status" |awk '/^total processes:/ {print $NF}'
}

max_active_processes(){
    /usr/bin/curl -s "http://127.0.0.1:"$PHPFPM_PORT"/phpfpm_status" |awk '/^max active processes:/ {print $NF}'
}

max_children_reached(){
    /usr/bin/curl -s "http://127.0.0.1:"$PHPFPM_PORT"/phpfpm_status" |awk '/^max children reached:/ {print $NF}'
}

slow_requests(){
    /usr/bin/curl -s "http://127.0.0.1:"$PHPFPM_PORT"/phpfpm_status" |awk '/^slow requests:/ {print $NF}'
}

case $PHPFPM_COMMAND in
    start_since)
        start_since;
        ;;
    accepted_conn)
        accepted_conn;
        ;;
    listen_queue)
        listen_queue;
        ;;
    max_listen_queue)
        max_listen_queue;
        ;;
    listen_queue_len)
        listen_queue_len;
        ;;
    idle_processes)
        idle_processes;
        ;;
    active_processes)
        active_processes;
        ;;
        total_processes)
                total_processes;
                ;;
        max_active_processes)
                max_active_processes;
                ;;
        max_children_reached)
                max_children_reached;
                ;;
        slow_requests)
                slow_requests;
                ;;
          *)
        echo $"USAGE:$0 {start_since|accepted_conn|listen_queue|max_listen_queue|listen_queue_len|idle_processes|active_processes|total_processes|max_active_processes|max_children_reached}"
    esac
    
## 2.添加执行权限
[root@web02 ~]# chmod +x /etc/zabbix/scripts/phpfpm_status.sh

配置zabbix客户端加监控

[root@web02 ~]# cp /etc/zabbix/zabbix_agentd.d/{nginx_status.conf,phpfpm_status.conf}
[root@web02 ~]# vim /etc/zabbix/zabbix_agentd.d/phpfpm_status.conf
UserParameter=phpfpm_status[*],/bin/bash /etc/zabbix/scripts/phpfpm_status.sh "$1"

[root@web02 ~]# systemctl restart zabbix-agent

[root@zabbix ~]# zabbix_get -s 172.16.1.8 -k phpfpm_status[start_since]
2522

[root@zabbix ~]# zabbix_get -s 172.16.1.8 -k phpfpm_status[accepted_conn]
3

[root@zabbix ~]# zabbix_get -s 172.16.1.8 -k phpfpm_status[listen_queue]
0

页面添加监控项

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

zabbix监控MySQL

##  1.开启MySQL上的zabbix-agent
[root@db01 ~]# systemctl start zabbix-agent

## 2.添加主机

image.png

image.png

zabbix监控redis

## 1.安装redis
[root@db01 ~]# yum install -y redis

## 2.启动redis
[root@db01 ~]# systemctl start redis

image.png

image.png

zabbix监控JVM(监控java的虚拟内存)

在Zabbix中,JMX监控数据的获取由专门的代理程序来实现,即Zabbix-Java-Gateway来负责数据的采集,Zabbix-Java-Gateway和JMX的Java程序之间通信获取数据.

JMX在zabbix中运行的流程

1.Zabbix-Server找Zabbix-Java-Gateway获取Java数据
2.Zabbix-Java-Gateway找Java程序获取数据
3.Java程序返回数据给Zabbix-Java-Gateway
4.Zabbix-Java-Gateway返回数据给Zabbix-Server
5.Zabbix-Server进行数据展示

注意:zabbix想要采集java的数据必须要用到zabbix-java-gateway工具

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

## 1.安装Java容器Tomcat
[root@web02 ~]# yum install -y tomcat

## 2.安装zabbix-java-gateway
[root@web02 ~]# rpm -ivh https://mirrors.aliyun.com/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix-java-gateway-5.0.26-1.el7.x86_64.rpm?spm=a2c6h.25603864.0.0.2ed62e2fPf8hxu
Retrieving https://mirrors.aliyun.com/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix-java-gateway-5.0.26-1.el7.x86_64.rpm?spm=a2c6h.25603864.0.0.2ed62e2fPf8hxu

## 3.启动java-gateway
[root@web02 ~]# systemctl start zabbix-java-gateway.service
[root@web02 ~]# netstat -lntup|grep 10052
tcp6       0      0 :::10052                :::*                    LISTEN      2984/java

## 4.修改服务端配置
[root@zabbix ~]# vim /etc/zabbix/zabbix_server.conf
## java-gateway的地址
JavaGateway=172.16.1.7
## java-gateway的端口
JavaGatewayPort=10052
## java-gateway轮询
StartJavaPollers=5

## 5.重启服务端
[root@zabbix ~]# systemctl restart zabbix-server

## 6.修改tomcat启动脚本
[root@web02 ~]# vim /usr/libexec/tomcat/server
CATALINA_OPTS="$CATALINA_OPTS	
-Dcom.sun.management.jmxremote	# 启用远程监控JMX
-Dcom.sun.management.jmxremote.port=12345	# jmx启用远程端口,zabbix添加时必须一致
-Dcom.sun.management.jmxremote.authenticate=false	# 不开启用户密码认证
-Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=172.16.1.8"
# 运行tomcat主机的IP地址

## 7.重启tomcat
[root@web02 ~]# systemctl restart tomcat
[root@web02 ~]# netstat -lntup | grep 12345
tcp6       0      0 :::12345                :::*                    LISTEN      3115/java

image.png

image.png

image.png

image.png

zabbix自定义监控阶段小结

zabbix监控基础架构回顾

image.png

Zabbix监控方式:

  • agent:客户端监控
  • SNMP:监控网络设备(交换机)
  • IPMI:监控主机硬件设备(Dell R710 R720 R730 远程管理卡)
  • JMX:监控JAVA的JVM

Zabbix资源回顾(基于监控项基础的所有资源)