zabbix-nginx监控

我叫张贺,贪财好色。一名合格的LINUX运维工程师,专注于LINUX的学习和研究,曾负责某中型企业的网站运维工作,爱好佛学和跑步。
个人博客:传送阵
笔者微信:zhanghe15069028807,非诚勿扰。

nginx监控

任何一个应用想要被zabbix监控,需要应用本身就支持展示信息,比如nginx和httpd都有一个状态页面,mysql也有,通过mysql –e "show status"就可以获得mysql的状态信息。

nginx监控要监控哪些项目呢

其实也就是状态页里面的那几项,步骤也非常的简单,通过awk取出监控页里面的那几项,然后定义键值就可以了,不知道你是否还记得状态页里面的那几项都是什么意思?我之前在另一个博客里面总结过:[传送门]( [https://www.cnblogs.com/yizhangheka/p/12072445.html#状态模块监控]

(https://www.cnblogs.com/yizhangheka/p/12072445.html#状态模块监控) ), 这里就不再赘述了。

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 

好,下面我们就应该把状态页里面这个项都单独取出来,通过awk就可以了,如下所示:

[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

最后我们就应该来写键值了,这个地方要注意,如果我们直接将上面这些命令写入到键值文件当中会有一个问题,什么问题呢?就是zabbix-server每一次取值都要访问一次状态页面,而nginx的日志文件也会记录一次,这样的话,以后我们再分析日志文件的时候就会发现有好多这样的、对我们无用的访问日志,这些日志记得都是自己访问自己的,对我们分析日志没有任何意义。我们怎么做才能做到尽量不要让这些日志记得到日志文件当中去呢?完全不写入也是现实,起码要访问一次,我们可以这样,就访问一次,然后把访问的信息写入到一个文件当中,以后取值都去这个文件当中取,这样不用行了吗?但是不这个文件也不能一直存在,如果一直存在的话,我们就无法取到实时的信息了,所以可以让这个文件就保存60秒,之后再重新生成即可,我们要写一个脚本来完成这个任务。

[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

出报错的原因是我们在agent做测试时在/tmp已经生成的文件,是用root生成的,而这里运行的时候是以zabbix用户运行的,解决办法很简单在agent删除了/tmp里面的/tmp/nginx_status.txt文件即可。

zabbix-web


原文地址:https://www.cnblogs.com/yizhangheka/p/12133457.html