博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
zabbix学习之路---------zabbix监控nginx进程以及指标
阅读量:3950 次
发布时间:2019-05-24

本文共 5671 字,大约阅读时间需要 18 分钟。

场景描述:

nginx里可以设置性能监控,来查看到客户端的连接请求情况,可以利用这一点在zabbix设置自定义监控项来帮助运维人员来采集nginx的连接请求情况并构建图形展示。

前提准备:

nginx开启stub_status功能,如果没有开启此功能需要重新编译加上–with-http_stub_status_module参数

[root@localhost ~]# nginx -Vnginx version: nginx/1.7.0built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module

操作步骤

1 使用nginx -V查看是否开启stub_status功能,如果没有需要重新编译。

[root@localhost ~]# nginx -Vnginx version: nginx/1.7.0built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module

在这里插入图片描述

2. 修改nginx.conf文件,添加location

在server底下添加一个location匹配规则

location /status {
stub_status on; ##开启stub_status功能 access_log off; ##关闭记录访问日志 allow 127.0.0.1; ##允许本地访问 deny all; ##拒绝所有}

在这里插入图片描述

3. 检查nginx文件,重载nginx配置文件

[root@localhost ~]# nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful[root@localhost ~]# nginx -s reload

4. 本地访问本机IP/status查看nginx指标

指标介绍:
Active connections:当前活动的客户端连接数有多少(包括在等待阶段的)
accepts:接受的客户端连接总数
handled:已处理的连接总数(通常与accepts数量一致,但存在某些资源限制的情况导致数量不一致)
requests:客户端请求总数(包括接受请求或拒绝请求的) Reading:nginx正在读取请求标头的当前连接数。
Writing:nginx正在将响应写回到客户端的当前连接数。 Waiting:当前等待请求的空闲客户端连接数

[root@localhost ~]# curl http://127.0.0.1/statusActive connections: 1 server accepts handled requests 1 1 1 Reading: 0 Writing: 1 Waiting: 0

5. 编写监控nginx脚本

[root@localhost etc]# mkdir /usr/local/etc/script[root@localhost conf]# cd /usr/local/etc/[root@localhost etc]# lsscript  zabbix_agentd.conf  zabbix_agentd.conf.bak  zabbix_agentd.conf.d[root@localhost etc]# cat /usr/local/etc/script/ngx_status.sh #!/bin/bash#function: monitor nginx1.16 for zabbix5.0#blog: www.qiufeng5.cn#version: 1.0#date: 2020-09-17#定义Nginx status页面ngx_status="http://127.0.0.1/status"#判断status页面是否存活ngx_status_code() {
http_code=`curl -o /dev/null -s -w %{
http_code} ${
ngx_status}` if [ ${http_code} == "200" ];then return 1 else echo "Nginx status is not running." fi}#获取当前活动的客户端连接数active() {
ngx_status_code || curl -s ${ngx_status} | grep "Active" | awk '{print $NF}'}#获取接收客户端连接的总数量accepts() {
ngx_status_code || curl -s ${ngx_status} | awk NR==3 | awk '{print $1}'}#获取已处理的连接总数量handled() {
ngx_status_code || curl -s ${ngx_status} | awk NR==3 | awk '{print $2}'}#获取客户端请求总数量requests() {
ngx_status_code || curl -s ${ngx_status} | awk NR==3 | awk '{print $3}'}#获取正在读取请求标头的当前连接数量reading() {
ngx_status_code || curl -s ${ngx_status} | grep "Reading" | awk '{print $2}'}#获取正在将响应写回到客户端的当前连接数量writing() {
ngx_status_code || curl -s ${ngx_status} | grep "Writing" | awk '{print $2}'}#获取当前正在等待响应的客户端连接数量waiting() {
ngx_status_code || curl -s ${ngx_status} | grep "Waiting" | awk '{print $2}'}#使用位置变量控制脚本输出case $1 in active) active;; accepts) accepts;; handled) handled;; requests) requests;; reading) reading;; writing) writing;; waiting) waiting;; *) echo "Unknown options"esac

6. 修改zabbix_agentd文件,允许使用接受自定义传来的参数

[root@localhost etc]# vim zabbix_agentd.confLogFile=/var/log/zabbix/zabbix_agentd.logServer=ServerIPListenPort=10050ServerActive=serverIPHostname=nginxInclude=/usr/local/etc/zabbix_agentd.conf.d/*.confUnsafeUserParameters=1        ##允许自定义脚本传来的参数  [root@localhost etc]# systemctl restart zabbix_agentd     #重启zabbix_agentd

7.创建自定义监控项文件

[root@localhost etc]# vim /usr/local/etc/zabbix_agentd.conf.d/userparameter_nginx.conf##把下面内容添加到userparameter_nginx.conf中UserParameter=nginx.active,bash /usr/local/etc/script/ngx_status.sh activeUserParameter=nginx.accepts,bash /usr/local/etc/script/ngx_status.sh acceptsUserParameter=nginx.handled,bash /usr/local/etc/script/ngx_status.sh handledUserParameter=nginx.requests,bash /usr/local/etc/script/ngx_status.sh requestsUserParameter=nginx.reading,bash /usr/local/etc/script/ngx_status.sh readingUserParameter=nginx.writing,bash /usr/local/etc/script/ngx_status.sh writingUserParameter=nginx.waiting,bash /usr/local/etc/script/ngx_status.sh waiting

8. 创建完成后,在zabbix_server端使用命令测试获取nginx指标信息

[root@localhost bin]# cd /usr/local/zabbix5.0/bin/[root@localhost bin]# ll  -rwxr-xr-x. 1 zabbix zabbix  517024 Apr 25 10:12 zabbix_get  -rwxr-xr-x. 1 zabbix zabbix 2841736 Apr 25 10:12 zabbix_js  -rwxr-xr-x. 1 zabbix zabbix  854320 Apr 25 10:12 zabbix_sender[root@localhost bin]# ./zabbix_get -s 10.2.61.215 -k nginx.active1

9. 再用agent端自己访问本机的status

获取到的信息是一样的,那就证明配置的没有问题

[root@localhost etc]# curl http://127.0.0.1/statusActive connections: 1 server accepts handled requests 1123 1123 1123 Reading: 0 Writing: 1 Waiting: 0

在这里插入图片描述

10. 确认好获取到的信息是正确之后,在zabbixweb界面设置自定义模板

点击配置------模板------创建模板
在这里插入图片描述
模板名称自定义即可。
在这里插入图片描述
点击模板的监控项-------创建监控项
在这里插入图片描述
名称:自定义设置
类型:zabbix客户端
键值:就是nginx.active

#其他监控项的键值根据刚才写的监控脚本里的函数来定义,如nginx.requests、nginx.accepts等

编写完成后点击确定

在这里插入图片描述
按照第一个监控项依次添加其他的监控项获取信息。
在这里插入图片描述

这里获取到的信息只是为了查看到nginx的请求连接量,如果有需求可以创建触发器来触发告警。接下来添加监控nginx进程,nginx进程需要添加触发器触发告警

在这里插入图片描述

11. 监控项创建完毕,再创建触发器来触发告警
在这里插入图片描述
点击触发器------创建触发器
在这里插入图片描述在这里插入图片描述

结果:取决于nginx进程的进程数

间隔:触发监控项的时间间隔
在这里插入图片描述
触发器名称自定义即可,表达式选择完毕后点击添加
在这里插入图片描述
在这里插入图片描述
12. 把自定义的模板链接到nginx服务器上点击更新
在这里插入图片描述

现在触发器和监控项都设置完毕,测试一下nginx进程宕掉会触发告警吗?

在nginx主机上停掉nginx测试

[root@localhost etc]# killall nginxYou have new mail in /var/spool/mail/root[root@localhost etc]# ps -ef | grep nginxroot     30044  2809  0 16:49 pts/1    00:00:00 grep --color=auto nginx

在这里插入图片描述

成功触发告警
在这里插入图片描述
接下来设置图形,以图形化的方式展现出来
找到配置—模板—nginx模板—图形
在这里插入图片描述
选择对应的监控项点击确定即可
在这里插入图片描述
其他的图形也是如上图一样操作
最终的效果图
在这里插入图片描述

转载地址:http://gnqwi.baihongyu.com/

你可能感兴趣的文章
低迷时,谁在坚持CSR
查看>>
致谢指南
查看>>
领导转型:六个方式帮助你建立好的团队
查看>>
从员工到总监,你要明白的8个道理
查看>>
领导不可不知的十大管理定律
查看>>
如何分析Email模块接收、发送邮件失败的Log
查看>>
GPS如何进入省电模式
查看>>
GPS打开失败
查看>>
如何增加电量显示格数,并提示剩余电量?
查看>>
Key Launcher上底下的shortcut如何修改默认值以及如果修改Key Launcher上widget的默认显示顺序
查看>>
Java支持播放哪些multi media格式
查看>>
Audio播放完毕后设置时间无法正确获取
查看>>
打开了一个size不为零的文件,读取到的值却为零的一种分析和解决方法
查看>>
Aplix VM安装Java应用在main menu上不能显示自己的图标,而是显示一朵小花的解决方法
查看>>
Aplix VM安装第一个Java应用在main menu上看不到图标的解决方法
查看>>
java 在cosmos下修改设置,提示“setting are not modifiable”的解释
查看>>
JAD中常见字段的介绍。
查看>>
对于SIM menu update by OTA的解释
查看>>
如何去掉OTA provisioning的PIN码验证
查看>>
如何实现首次开机自动power on 蓝牙?
查看>>