zabbix-nginx monitoring

My name He Zhang, greedy lust. A qualified LINUX operation and maintenance engineers, focusing on LINUX study and research, was responsible for the website operation and maintenance of a medium-sized enterprises, loving Buddhist and running.
Personal blog: Chuan Songzhen
author micro letter: zhanghe15069028807, You Are the One.

nginx monitoring

Any application zabbix want to be monitored application itself needs to show support information, such as nginx and has a status page httpd, mysql also, through mysql –e "show status"you can get mysql status information.

nginx monitoring what items you want to monitor it ?

In fact, there is a status page that several steps are very simple, remove the pages inside that several monitoring by awk, then define the key on it, I do not know if you remember that there are several status page What means? In another blog before I summarize the inside too: the portal , I will not repeat them here.

zabbix-server(version:3.4) 192.168.80.22
zabbix-agent/nginx 192.168.80.23

zabbix-agent/nginx

//安装并配置nginx
[root@nginx ~]#  grep -Ev "^$|#" /etc/nginx/nginx.conf.default > /etc/nginx/nginx.conf
[root@nginx ~]# vim /etc/nginx/nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        location  /nginx_status {         #加了这一个localtion
                stub_status;
                allow 192.168.80.0/24;
                allow 127.0.0.1/32;
                deny all;
        }
    }
}

//重启nginx之后测试一下是否能访问自己的状态页
[root@nginx ~]# curl 192.168.80.23/nginx_status
Active connections: 1 
server accepts handled requests
 16 16 16 
Reading: 0 Writing: 1 Waiting: 0 

Well, here we should put inside the Status page items are taken out individually, by awk on it, as shown below:

[root@nginx ~]# curl 127.0.0.1/nginx_status 2>/dev/null | awk  'NR==1{print $3}'
1
[root@nginx ~]# curl 127.0.0.1/nginx_status 2>/dev/null | awk  'NR==3{print $1}'
9
[root@nginx ~]# curl 127.0.0.1/nginx_status 2>/dev/null | awk  'NR==3{print $2}'
10
[root@nginx ~]# curl 127.0.0.1/nginx_status 2>/dev/null | awk  'NR==3{print $3}'
11
[root@nginx ~]# curl 127.0.0.1/nginx_status 2>/dev/null | awk  'NR==4{print $2}'
0
[root@nginx ~]# curl 127.0.0.1/nginx_status 2>/dev/null | awk  'NR==4{print $4}'
1
[root@nginx ~]# curl 127.0.0.1/nginx_status 2>/dev/null | awk  'NR==4{print $6}'
0

Finally, we should write key, and this place should pay attention to, if we directly above the commands written to the file which will be a key question, what is the problem? Is zabbix-server value must visit once every status page, and nginx log file will be recorded once, this is the case, then we'll analyze the log file when there are a lot of you will find this on our access logs useless these logs are to remember their own access, does not make sense for us to analyze the log. How do we do to try to remember not to let these logs to the log file them go? Reality is not written completely, at least to visit once, we can, on one visit, and then writes the information access to a file, the file after the values ​​are going to take them, so do not line yet? But the document also does not always exist, if they persist, we will not be able to get real-time information, so you can make the file will be kept for 60 seconds, and then re-generate can we write a script to accomplish this task .

[root@nginx scripts]# pwd
/scripts
[root@nginx scripts]# vim nginx_status.sh 
#!/bin/bash

#定义变量
NGINX_COMMAND=$1
NGINX_PORT=80
CACHEFILE="/tmp/nginx_status.txt"
CMD="/usr/bin/curl http://127.0.0.1:"$NGINX_PORT"/nginx_status/"

#先判断一下文件是否存在,不存在就创建
if [ ! -f $CACHEFILE ];then
    $CMD >$CACHEFILE 2>/dev/null
fi

#60秒之后自动删除,删除之后立马再创建,形成一个循环
TIMEFLM=`stat -c %Y $CACHEFILE`  #创建文件的时候距离1970年是多少秒
TIMENOW=`date +%s`               #现在距离1970年已经过去了多少秒
if [ `expr $TIMENOW - $TIMEFLM` -gt 60 ]
then
    rm -f $CACHEFILE
fi
if [ ! -f $CACHEFILE ];then
    $CMD >$CACHEFILE 2>/dev/null
fi

#这下面就是各种取值了,不过这里是先定义函数,然后再通过case来调用;
nginx_active () {
    grep 'Active' $CACHEFILE | awk '{print $NF}'
        exit 0;
}
nginx_reading () {
    grep 'Reading' $CACHEFILE | awk '{print $2}'
        exit 0;
}
nginx_writing () {
    grep 'Writing' $CACHEFILE | awk '{print $4}'
        exit 0;
}
nginx_waiting () {
    grep 'Waiting' $CACHEFILE | awk '{print $6}'
        exit 0;
}
nginx_accepts () {
    awk NR==3 $CACHEFILE | awk '{print $1}'
        exit 0;
}
nginx_handled () {
    awk NR==3 $CACHEFILE | awk '{print $2}'
        exit 0;
}
nginx_requests () {
    awk NR==3 $CACHEFILE | awk '{print $3}'
        exit 0;
}
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 "Invalied credentials"
exit 2;
esac

//别忘记加权限
[root@nginx scripts]# chmod +x nginx_status.sh 

//最后一步,写键值,重启服务
[root@nginx zabbix_agentd.d]# pwd
/etc/zabbix/zabbix_agentd.d
[root@nginx zabbix_agentd.d]# cat nginx_status.conf 
UserParameter=nginx_status[*],/bin/bash /scripts/nginx_status.sh $1
[root@nginx zabbix_agentd.d]# systemctl restart zabbix-agent

zabbix-server

//先验证一下键值
[root@zabbix ~]# zabbix_get -s 192.168.80.23 -k nginx_status[requests]
rm: cannot remove ‘/tmp/nginx_status.txt’: Operation not permitted
20

The reason for the error is that when we do testing agent / tmp file has been generated, is generated by the root, and time is running here zabbix users to run, the solution is very simple agent deleted / tmp inside / tmp / nginx_status.txt file.

zabbix-web


Guess you like

Origin www.cnblogs.com/yizhangheka/p/12133457.html