使用zabbix监控nginx服务(十五)

使用zabbix监控nginx服务

1.开启nginx状态监控

1.安装nginx
[root@192_168_81_220 ~]# yum -y install nginx

2.开启状态监控页面
[root@192_168_81_220 ~]# vim /etc/nginx/nginx.conf
        location /nginx_status {
                stub_status;
        }

3.重启nginx
[root@192_168_81_220 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@192_168_81_220 ~]# systemctl start nginx

4.访问
[root@192_168_81_220 ~]# curl 192.168.81.220/nginx_status
Active connections: 1 
server accepts handled requests
 4 4 9 
Reading: 0 Writing: 1 Waiting: 0 

2.nginx监控状态值详解

Active connections: 1 //当前活跃的连接数,当前有多少个连接访问我

accepts 4 //nginx启动到现在共接收了多少个请求

handled 4 //nginx启动到现在共处理了多少个请求

requests //总的http请求数

Reading: 0 //当前读取的连接请求头数

Writing: 1 //当前响应客户端请求数

Waiting: 0 //当前的等待请求数

writing+waiting的值要等于Active connections的值

3.利用zabbix监控nginx状态值

思路:

​ 1.首先编写获取值的脚本,由于是web页面,因此可以通过curl命令来抓取

​ 2.脚本里面可以定义函数,每一个值对应一个函数,最后在使用case判断传参进来的$1与那个函数匹配,匹配正确则执行函数

​ 3.定义传参形式的监控项key,这样就可以定义一个key值,写不同的$1就可以了

3.1.编写获取监控值脚本

脚本编写思路:首先将curl命令获取的数据保存到一个文件中,然后定义当前时间和文件时间的变量,进行比较,如果当前时间比文件时间大于60s,则把curl命令的文件进行删除,重新curl获取数据并导入到文件中,最后通过定义各种状态的函数,函数里面主要就是通过文件把状态值获取到,再通过case进行判断要传那个参数,最后执行对应参数的命令获取监控值

最后监测进程是否存活可以通过ps查到进程然后通过wc -l获取数量,最后echo这个值,然后创建触发器,最新值等于0就表示进程不存在

1.编写脚本
[root@192_168_81_220 ~]# cd /etc/zabbix/scripts/
[root@192_168_81_220 scripts]# vim tcp_zabbix.sh
#!/bin/bash
#这是一个简单的监控nginx状态值的脚本
#20201030 	---jxl
comm_para=$1
cachefile=/tmp/nginx_status.txt
port=80
cmd="/usr/bin/curl 127.0.0.1:$port/nginx_status"
file_time=`stat -c %Y $cachefile`
now_time=`date +%s`
rm_file=$(($now_time - $file_time))
if [ ! -f $cachefile ];then
	$cmd > $cachefile 2>/dev/null
fi

if [ $rm_file -gt 60 ];then
	rm -rf $cachefile
fi

if [ ! -f $cachefile ];then
	$cmd > $cachefile 2>/dev/null
fi

active() {
	cat $cachefile | awk '/Active/{print $NF}'
	exit 0;
}

accepts() {
	cat /tmp/nginx_status.txt | awk '{if(NR==3){print $1}}'
	exit 0;
}

handled(){ 
	cat /tmp/nginx_status.txt | awk '{if(NR==3){print $2}}'
	exit 0;
}

requests(){
	cat /tmp/nginx_status.txt | awk '{if(NR==3){print $3}}'
	exit 0;
}

reading() {
	cat /tmp/nginx_status.txt | awk '{if(NR==4){print $2}}'
	exit 0;
}

writing() {
	cat /tmp/nginx_status.txt | awk '{if(NR==4){print $4}}'
	exit 0;
}

waiting() {
	cat /tmp/nginx_status.txt | awk '{if(NR==4){print $6}}'
	exit 0;
}

check() {
        nginx_pro_count=`ps aux | grep nginx | grep -v grep | grep -v nginx_status.sh | wc -l`

	echo $nginx_pro_count
}

case "$comm_para" in 
active)
	active
	;;
accepts)
	accepts
	;;
handled)
	handled
	;;
requests)
	requests
	;;
reading)
	reading
	;;
writing)
	writing
	;;
waiting)
	waiting
	;;
check)
	check
	;;
*)	
	echo "invalid status"
	exit 2;
esac

2.给执行权限并测试
[root@192_168_81_220 scripts]# chmod a+x tcp_zabbix.sh
[root@192_168_81_220 scripts]# sh tcp_zabbix.sh active
1

3.2.编写自定义监控项配置文件

1.写配置文件
[root@192_168_81_220 ~]# cd /etc/zabbix/zabbix_agentd.d/
[root@192_168_81_220 zabbix_agentd.d]# vim nginx_status.conf
UserParameter=nginx_status[*],/bin/bash /etc/zabbix/scripts/nginx_status.sh $1

2.重启
[root@192_168_81_220 zabbix_agentd.d]# systemctl restart zabbix-agent

3.用zabbix_server测试监控key
[root@zabbix-server ~]# zabbix_get -s 192.168.81.220 -k nginx_status[active]
1
[root@zabbix-server ~]# zabbix_get -s 192.168.81.220 -k nginx_status[check]
4

成功

3.3.创建监控模板

3.3.1.点击配置—模板—创建模板

在这里插入图片描述

3.3.2.填写模板信息

名称:nginx status template

在这里插入图片描述

3.3.3.创建应用集

名称:nginx status

在这里插入图片描述

3.3.4.创建监控项

名称:Nginx status active

键值:nginx_status[active]

更新间隔:60s,因为脚本里面的判断时间就是60s

应用集选择:nginx status

在这里插入图片描述

其他监控项配置一致,只是参数key值不一样

所有监控项key值

nginx_status[active]
nginx_status[accepts]
nginx_status[handled]
nginx_status[requests]
nginx_status[reading]
nginx_status[writing]
nginx_status[waiting]
nginx_status[check]

所有监控项创建完成

在这里插入图片描述

3.3.5.创建触发器

对监控状态值可以不做触发器,但是要对nginx进程做触发器

点击创建触发器

在这里插入图片描述

填写触发器信息

名称:nginx进程不存在

严重性:严重

表达式:{nginx status template:nginx_status[check].last()}=0
在这里插入图片描述

触发器条件设置

在这里插入图片描述

3.3.6.创建图形

点击创建图形

在这里插入图片描述

名称:nginx状态监控

把所有nginx监控项都选上,最后点击添加
在这里插入图片描述

3.3.7.模板创建完成

在这里插入图片描述

4.监控主机应用nginx监控模板

192.168.81.220为例,192.168.81.230配置一样

点击配置—主机—模板—选择模板—添加

在这里插入图片描述

点击更新

在这里插入图片描述

192.168.81.230在链接模板之前要把脚本和配置文件全部scp过去

[root@192_168_81_220 ~]# scp /etc/zabbix/scripts/nginx_status.sh  [email protected]:/etc/zabbix/scripts/nginx_status.sh 

[root@192_168_81_220 ~]# scp /etc/zabbix/zabbix_agentd.d/nginx_status.conf [email protected]:/etc/zabbix/zabbix_agentd.d/

[root@192_168_81_220 ~]# ssh [email protected] "systemctl restart zabbix-agent"

5.查看最新数据

点击监测—最新数据

已经有值

在这里插入图片描述

在这里插入图片描述

6.触发nginx进程存在并告警

[root@192_168_81_220 ~]# systemctl stop nginx
[root@192_168_81_220 ~]# ps aux | grep nginx | grep -v grep | grep -v nginx_status.sh | wc -l
0
坐等报警即可

仪表盘显示

在这里插入图片描述

报警短信
在这里插入图片描述

7.利用grafana生成nginx状态监控图形

7.1.创建图形

点击创建图形

在这里插入图片描述

选择条形图

在这里插入图片描述

选择完图形后点击edit两次即可添加监控项

7.2.添加监控项

点击add query—填写监控信息

Group:知识点管理平台

Host:$host

Application:nginx status

Item:Nginx status accepts

所有监控项都是这么添加,点击add query就可以了

在这里插入图片描述

7.3.设置图形名称

在这里插入图片描述

7.4.保存图形

在这里插入图片描述

7.5.查看图形

在这里插入图片描述

在右上角可以设置时间

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_44953658/article/details/114686070