欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

zabbix监控Nginx状态

程序员文章站 2022-04-17 09:45:47
zabbix监控Nginx状态所需:LNMP环境部署、zabbix部署1. 在lnmp环境下部署zabbix1.1 修改/etc/php.ini的配置并重启php-fpm[root@WangJiayue ~]# sed -ri 's/(post_max_size =).*/\1 16M/g' /etc/php.ini[root@WangJiayue ~]# sed -ri 's/(max_execution_time =).*/\1 300/g' /etc/php.ini[root@WangJi...

zabbix监控Nginx状态

所需:LNMP环境部署zabbix部署

1. 在lnmp环境下部署zabbix

1.1 修改/etc/php.ini的配置并重启php-fpm

[root@WangJiayue ~]# sed -ri 's/(post_max_size =).*/\1 16M/g' /etc/php.ini
[root@WangJiayue ~]# sed -ri 's/(max_execution_time =).*/\1 300/g' /etc/php.ini
[root@WangJiayue ~]# sed -ri 's/(max_input_time =).*/\1 300/g' /etc/php.ini
[root@WangJiayue ~]# sed -i '/;date.timezone/a date.timezone = Asia/Shanghai' /etc/php.ini
[root@WangJiayue ~]# service php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm  done
[root@WangJiayue ~]# cd /usr/src/zabbix-5.0.2
[root@WangJiayue zabbix-5.0.2]# mkdir /usr/local/nginx/html/zabbix
[root@WangJiayue zabbix-5.0.2]# cp -a ui/* /usr/local/nginx/html/zabbix/
[root@WangJiayue zabbix-5.0.2]# chown -R nginx.nginx /usr/local/nginx/html/zabbix/
[root@WangJiayue zabbix-5.0.2]# nginx -s reload
[root@WangJiayue zabbix-5.0.2]# service php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm  done

1.2 修改Nginx配置文件

[root@WangJiayue ~]# vim /usr/local/nginx/conf/nginx.conf

location / {
            root   html/zabbix;     //添加zabbix
            index  index.php index.html index.htm;
        }
        
 //添加以下内容
        location ~ \.php$ {
            root           html/zabbix;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
		}
		
[root@WangJiayue ~]# nginx -s reload

1.3 设置zabbix/conf目录的权限,让zabbix有权限生成配置文件zabbix.conf.php并重启Nginx

[root@WangJiayue ~]# chmod 777 /usr/local/nginx/html/zabbix/conf
[root@WangJiayue ~]# ll -d /usr/local/nginx/html/zabbix/conf
drwxrwxrwx 3 nginx nginx 94 7月   6 17:54 /usr/local/nginx/html/zabbix/conf
[root@WangJiayue ~]# nginx -s reload

zabbix监控Nginx状态
这里原来的浏览器提示版本过低不能访问zabbix页面故改为Google 浏览器访问

1.4 web界面配置完成后恢复zabbix/conf目录的权限为755

[root@WangJiayue ~]# chmod 755 /usr/local/nginx/html/zabbix/conf
[root@WangJiayue ~]# ll -d /usr/local/nginx/html/zabbix/conf
drwxr-xr-x 3 nginx nginx 117 8月  12 02:03 /usr/local/nginx/html/zabbix/conf

2. 开启状态界面

 [root@WangJiayue ~]# vim /usr/local/nginx/conf/nginx.conf

//添加以下内容
location /status {
            stub_status on;
            allow 192.168.222.0/24;     //让192.168.222.0网段可以访问
            deny all;
        }

[root@WangJiayue ~]# 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@WangJiayue ~]# nginx -s reload     //重读配置文件

zabbix监控Nginx状态
这里截图整个网页文字太小故截取一部分

3. 编写监控脚本提取所需监控数据

[root@WangJiayue ~]# mkdir /scripts
[root@WangJiayue ~]# cd /scripts/


[root@WangJiayue scripts]# vim handled.sh
#!/bin/bash
status=$(curl -s http://192.168.222.139/status |awk 'NR==3{print $3}')
echo $status
[root@WangJiayue scripts]# chmod +x handled.sh 


[root@WangJiayue scripts]# vim Reading.sh
#!/bin/bash
status=$(curl -s http://192.168.222.139/status |awk 'NR==4{print $2}')
echo $status
[root@WangJiayue scripts]# chmod +x Reading.sh 


[root@WangJiayue scripts]# vim Writing.sh
#!/bin/bash
status=$(curl -s http://192.168.222.139/status |awk 'NR==4{print $4}')
echo $status
[root@WangJiayue scripts]# chmod +x Writing.sh 


[root@WangJiayue scripts]# ll
总用量 12
-rwxr-xr-x 1 root root 98 8月  12 02:13 handled.sh
-rwxr-xr-x 1 root root 98 8月  12 02:14 Reading.sh
-rwxr-xr-x 1 root root 98 8月  12 02:16 Writing.sh

4. 编辑zabbix_agent配置文件

[root@WangJiayue ~]# vim /usr/local/etc/zabbix_agentd.conf

UnsafeUserParameters=1   //取消注释更改数值为1
//在最后添加以下内容
UserParameter=check_handled,/bin/bash /scripts/handled.sh     
UserParameter=check_Reading,/bin/bash /scripts/Reading.sh
UserParameter=check_Writing,/bin/bash /scripts/Writing.sh

[root@WangJiayue ~]# pkill zabbix
[root@WangJiayue ~]# zabbix_server 
[root@WangJiayue ~]# zabbix_agentd 
[root@WangJiayue ~]# ss -antl
State      Recv-Q Send-Q                                                     Local Address:Port                                                                    Peer Address:Port              
LISTEN     0      128                                                                    *:10050                                                                              *:*                  
LISTEN     0      128                                                                    *:10051                                                                              *:*                  
LISTEN     0      128                                                            127.0.0.1:9000                                                                               *:*                  
LISTEN     0      128                                                                    *:80                                                                                 *:*                  
LISTEN     0      128                                                                    *:22                                                                                 *:*                  
LISTEN     0      100                                                            127.0.0.1:25                                                                                 *:*                  
LISTEN     0      80                                                                    :::3306                                                                              :::*                  
LISTEN     0      128                                                                   :::22                                                                                :::*                  
LISTEN     0      100                                                                  ::1:25                                                                                :::*                  
[root@WangJiayue ~]# 

5. 检测key是否可以取到值

[root@WangJiayue ~]# zabbix_get -s 127.0.0.1 -k check_handled
184
[root@WangJiayue ~]# zabbix_get -s 127.0.0.1 -k check_Reading
0
[root@WangJiayue ~]# zabbix_get -s 127.0.0.1 -k check_Writing
1

6. Web界面配置

6.1 添加监控项

zabbix监控Nginx状态
zabbix监控Nginx状态

zabbix监控Nginx状态
zabbix监控Nginx状态
zabbix监控Nginx状态
zabbix监控Nginx状态
zabbix监控Nginx状态
zabbix监控Nginx状态

6.2 查看新产生的数据

zabbix监控Nginx状态
zabbix监控Nginx状态
zabbix监控Nginx状态

本文地址:https://blog.csdn.net/wanfjiayue/article/details/107948495