首页 > 系统相关 >7.1 zabbix实现Nginx的监控

7.1 zabbix实现Nginx的监控

时间:2022-09-02 02:00:32浏览次数:48  
标签:status nginx agent Nginx zabbix 7.1 20220827 root

7.实现Nginx、Mysql的监控

7.1 zabbix实现Nginx的监控

脚本:nginx_status.sh
模板:nginx-template-magedu-jiege.xml
对nginx的活动连接和当前状态等运行状态进行监控
配置示例:
location /nginx_status {
stub_status;
allow 172.31.0.0/16;
allow 127.0.0.1;
deny all;
}
状态页用于输出nginx的基本状态信息:
输出信息示例:
Active connections: 291
server accepts handled requests
16630948 16630948 31070465
上面三个数字分别对应accepts,handled,requests三个值
Reading: 6 Writing: 179 Waiting: 106
Active connections: 当前处于活动状态的客户端连接数,包括连接等待空闲连接数。
accepts:统计总值,Nginx自启动后已经接受的客户端请求的总数。
handled:统计总值,Nginx自启动后已经处理完成的客户端请求的总数,通常等于accepts,除非有因
worker_connections限制等被拒绝的连接。
requests:统计总值,Nginx自启动后客户端发来的总的请求数。
Reading:当前状态,正在读取客户端请求报文首部的连接的连接数。
Writing:当前状态,正在向客户端发送响应报文过程中的连接数。
Waiting:当前状态,正在等待客户端发出请求的空闲连接数,开启 keep-alive的情况下,这个值等于 active –
(reading+writing),

7.1.1 在zabbix-agent上部署Nginx服务
[root@zabbix-agent ~]#apt install nginx -y

[root@zabbix-agent ~]#vim /apps/nginx/conf/nginx.conf
......
server {
listen 80;
listen [::]:80;
server_name _;
root /usr/share/nginx/html;

    location / {
            root html;
            index index.html index.htm;
            }
    location /nginx_status {
            stub_status;
            allow 172.31.0.0/16;
            allow 127.0.0.1;
            deny all; 
            }

........

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

这些值等下传给下面的脚本用

[root@zabbix-agent ~]#curl http://127.0.0.1:80/nginx_status
Active connections: 1
server accepts handled requests
4 4 4
Reading: 0 Writing: 1 Waiting: 0

7.1.2 监控项脚本
脚本放在zabbix-agent主机上.

zabbix-agent主机:
[root@zabbix-agent ~]#cat nginx_status.sh

!/bin/bash

Date 2022/08/24

Author Huangdashuai

nginx_status_fun(){
NGINX_PORT=$1
NGINX_COMMAND=$2

    nginx_active(){
	/usr/bin/curl "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2> /dev/null| grep 'Active' | awk '{print $NF}'
        } 
    nginx_reading(){
	/usr/bin/curl "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2> /dev/null | grep 'Reading' | awk '{print $2}'
            } 
    nginx_writing(){
            /usr/bin/curl "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2> /dev/null | grep 'Writing' | awk '{print $4}'
            } 
    nginx_waiting(){
            /usr/bin/curl "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2> /dev/null | grep 'Waiting' | awk '{print $6}'
            } 
    nginx_accepts(){
            /usr/bin/curl "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2> /dev/null | awk NR==3 | awk '{print $1}'
            }
    nginx_handled(){
            /usr/bin/curl "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2> /dev/null | awk NR==3 | awk '{print $2}'
            }
    nginx_requests(){
            /usr/bin/curl "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2> /dev/null | awk NR==3 | awk '{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;
            esac

}
main (){
case $1 in
nginx_status)
nginx_status_fun $2 $3;
;;
*)
echo $"Usage: $0 {nginx_status key}"
esac
}
main $1 $2 $3

在zabbix-agent-nginx上测试
[root@zabbix-agent ~]#bash nginx_status.sh nginx_status 80 active
1
[root@zabbix-agent ~]#bash nginx_status.sh nginx_status 80 handled
4
[root@zabbix-agent ~]#bash nginx_status.sh nginx_status 80 writing
1
[root@zabbix-agent ~]#bash nginx_status.sh nginx_status 80 waiting
0
[root@zabbix-agent ~]#bash nginx_status.sh nginx_status 80 reading
0

脚本放到zabbix-agent的指定目录/etc/zabbix/zabbix_agentd.d/下,给执行权限
[root@zabbix-agent ~]#cp nginx_status.sh /etc/zabbix/zabbix_agentd.d/
[root@zabbix-agent ~]# chmod +x nginx_status.sh

7.1.3 zabbix agent添加自定义监控项
[root@zabbix-agent ~]# vim /etc/zabbix/zabbix_agentd.conf
.......

UserParameter=

UserParameter=nginx.status,systemctl status nginx | awk NR==5'{print $3}'| awk -F '(' '{print $2}'| awk -F')' '{print $1}'
UserParameter=nginx.status[*],/etc/zabbix/zabbix_agentd.d/nginx_status.sh "$1" "$2" "$3"

####### LOADABLE MODULES #######
.......

[root@zabbix-server ~]#systemctl restart zabbix-agent nginx

7.1.4 zabbix server测试监控项数据
安装zabbix-get
[root@zabbix-server ~]# apt install zabbix-get

[root@zabbix-server ~]#zabbix_get -s 172.31.0.108 -p 10050 -k "nginx.status["nginx_status",80,"active"]"
1

[root@zabbix-server ~]#systemctl restart zabbix-server apache2

7.1.5 zabbix web界面添加被监控主机
浏览器访问: http://172.31.0.101/zabbix/



7.1.6 zabbix web导入模板
配置-模板-导入

7.1.7 将TCP监控模板关联至主机

7.1.8 验证监控数据

总结:
本次实验图表一开始获取不到数据,解决办法:

(1)实现各个主机之间时间同步。

(2)zabbix-agent中的key的的名称nginx.status要与模版中的key名称要一致,且在zabbix-server上测试成功。

[root@zabbix-agent ~]#vim /etc/zabbix/zabbix_agentd.conf

UserParameter=

UserParameter=nginx.status[*],/etc/zabbix/zabbix_agentd.d/nginx_status.sh "$1" "$2" "$3"

[root@zabbix-server ~]#zabbix_get -s 172.31.0.108 -p 10050 -k "nginx.status[nginx_status,80,active]"
1

(3)看日志分析问题,可以看出问题所在
[root@zabbix-agent /var/log/zabbix-agent]#tail zabbix_agentd.log
2761:20220827:195829.406 TLS support: YES
2761:20220827:195829.406 **************************
2761:20220827:195829.406 using configuration file: /etc/zabbix/zabbix_agentd.conf
2761:20220827:195829.407 agent #0 started [main process]
2773:20220827:195829.411 agent #5 started [active checks #1]
2773:20220827:195829.411 active check configuration update from [127.0.0.1:10051] started to fail (cannot connect to [[127.0.0.1]:10051]: [111] Connection refused)
2772:20220827:195829.412 agent #4 started [listener #3]
2771:20220827:195829.412 agent #3 started [listener #2]
2770:20220827:195829.412 agent #2 started [listener #1]
2769:20220827:195829.412 agent #1 started [collector]

(4)更新zabbix-agent配置文件配置,,重启zabbix-server和zabbix-agent
[root@zabbix-agent ~]#vim /etc/zabbix/zabbix_agentd.conf
ListenPort=10050
Server=172.31.0.101 #zabbxi-server IP
StartAgents=3
ServerActive=172.31.0.101 #zabbxi-server IP
Hostname=172.31.0.108 #zabbxi-agent IP

[root@zabbix-server ~]#visystemctl restart zabbix-server apache2
[root@zabbix-agent ~]#visystemctl restart zabbix-agent nginx

再次想看日志:
[root@zabbix-agent /var/log/zabbix-agent]#tail zabbix_agentd.log -f
3015:20220827:201547.054 TLS support: YES
3015:20220827:201547.054 **************************
3015:20220827:201547.054 using configuration file: /etc/zabbix/zabbix_agentd.conf
3015:20220827:201547.054 agent #0 started [main process]
3026:20220827:201547.055 agent #1 started [collector]
3030:20220827:201547.055 agent #5 started [active checks #1]
3029:20220827:201547.056 agent #4 started [listener #3]
3028:20220827:201547.056 agent #3 started [listener #2]
3027:20220827:201547.057 agent #2 started [listener #1]
3030:20220827:201550.056 active check configuration update from [172.31.0.101:10051] started to fail (ZBX_TCP_READ() timed out)
3030:20220827:201650.644 active check configuration update from [172.31.0.101:10051] is working again

标签:status,nginx,agent,Nginx,zabbix,7.1,20220827,root
From: https://www.cnblogs.com/biaoming534/p/16648407.html

相关文章

  • 6. 部署Zabbix监控
    6. 部署Zabbix监控 6.1 安装mysql[root@ubuntu~]#hostnamectlset-hostnamemysql-master;exit[root@mysql-master ~]#aptinstall-ymysql-server #配置my......
  • zabbix
    目录zabbix部署zabbix部署lamp已构建完成解压mysql8.0[root@localhostlocal]#tarxfmysql-8.0.28-linux-glibc2.12-x86_64.tar.xz修改所属主和组[root@localho......
  • zabbix_server优化
    造成zabbix性能下降的因素如表所示zabbix性能低下的表现1.   zabbix队列有太多被延迟的item,可以通过administration-queue查看2.   zabbix绘图中经常出现断图,一些i......
  • zabbix4.0.5自动安装脚本
    拿包请在下方留言拿包请在下方留言环境:centos7服务器可以上外网即可 #自动化部署zabbix脚本#!/bin/bash#版本详情#1.zabbix-4.0.5.tar.gz#2.nginx-1.16.1.tar......
  • 记一次因网络变更导致zabbix连接es报400和404
    背景zabbix历史数据存储到es集群,正常工作中的时候,因网络变更导致zabbixserver连接不上es,zabbix日志首先报400错误,之后一直404,es那边报查询相关的错误。现象如下图:......
  • 安装Zabbix客户端zabbix-agent2
    Ubuntu系统:下载链接:http://mirrors.aliyun.com/zabbix/zabbix/6.0/ubuntu/pool/main/z/zabbix/?spm=a2c6h.25603864.0.0.5e804167HzHeoe以ubuntu20.04的64位系统为例:Inde......
  • nginx配置文件单独创建和管理
    1、在nginx主配置文件nginx.conf的http模块下引入配置文件夹(注意路径的正确性)http{   include/usr/local/nginx/conf/conf.d/*.conf;   #这里就是引入的子配置文......
  • 学习日记 Day4. Nginx
    title:Day4Nginxdate:2022-08-2922:20:35tags:NginxNginxNginx一个高性能的HTTP和反向代理的web服务器,同时也提供IMAP/POP3/SMTP服务。作用即:反向代理、负载......
  • 10,zabbix web 监控
    url正常返回的值1.创建Webscenarios2.创建zabbix触发器name:short_messagesendstatusisnot100Expression:{u04zbx01.yaya.corp:web.test.fail[short_messa......
  • nginx的优化和防盗链
    nginx的优化和防盗链一、nginx的优化1.隐藏版本号(1)隐藏版本号的原因为了安全,如果暴露版本信息,黑客可以通过版本信息,得知该版本存在的漏洞,进而对服务器进行攻击。隐藏......