Zabbix3.4.11 监控 Nginx :
上一章我们学习了如何监控系统的cpu等硬件信息,下面我们来监控服务nginx。
首先我们先简单的安装一下nginx。
==================注意!!这里开始都是在客户端操作=====================
[root@zabbix-node2 ~]# yum install epel-release //要添加CentOS 7 EPEL仓库
[root@zabbix-node2 ~]# yum list nginx //查看nginx
[root@zabbix-node2 ~]# yum -y install nginx //安装nginx
[root@zabbix-node2 ~]# firewall-cmd --permanent --zone=public --add-service=http
[root@zabbix-node2 ~]# firewall-cmd --permanent --zone=public --add-service=https
[root@zabbix-node2 ~]# firewall-cmd --reload
访问默认网页测试:
1、获取 Nginx 状态( HTTP Stub Status #查看编译时有没有加入状态监控模块,如果没有需要单独加载)
[root@zabbix-node2 ~]# nginx -V
nginx version: nginx/1.12.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_auth_request_module --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-google_perftools_module --with-debug --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E'
- 配置 nginx.conf
[root@localhost ~]# vim /apps/product/nginx/conf/nginx.conf
location ~ /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
allow 192.168.91.133;
deny all;
}
# 在虚拟主机 server {} 段中加入上面配置,也可以单独定义一个专门用于监控的虚拟主机。
# deny all , 拒绝除 allow 中的主机之外所有主机访问此 URL ,实现过程中如果遇到 403 ,有可能是你把自己测试的机器拒绝了!
[root@zabbix-node2 ~]# systemctl restart nginx //修改完配置文件要重启服务
- Nginx 监控项解释
[root@zabbix-node2 ~]# curl http://127.0.0.1/nginx_status
Active connections: 1
server accepts handled requests
1 1 1
Reading: 0 Writing: 1 Waiting: 0
说明一下:
# Active connections: 对后端发起的活动连接数
# Server accepts handled requests: Nginx 总共处理了 77671 个连接,成功创建了 77671 次握手(没有失败次数),总共处理了 77666 个请求
# Reading: Nginx 读取到客户端的 Header 信息数
# Writing: Nginx 返回给客户端的 Header 信息数
# Waiting: 开启 keep-alive 的情况下,这个值等于 active - ( reading + writing ), 意思是 Nginx已经处理完成,正在等待下一次请求指令的驻留连接
# 在访问效率很高,请求很快被处理完毕的情况下,Waiting 数比较多是正常的。如果 reading + writing 数较多,则说明并发访问量很大,正在处理过程中
课外内容:
[root@zabbix-node2 ~]# yum -y install httpd-tools //安装一下ab做一些压力测试
[root@zabbix-node2 ~]# ab -c 100 -n 1000 http://192.168.91.134/index.html //本机其它机器执行都可以(这里我直接本机)
[root@zabbix-node2 ~]# curl http://127.0.0.1/nginx_status
Active connections: 1
server accepts handled requests
2005 2005 2003
Reading: 0 Writing: 1 Waiting: 0
言归正传
- 编写脚本获取上面的 key 值()
[root@zabbix-node2 ~]# mkdir /script/
[root@zabbix-node2 ~]# vi /script/nginx_status.sh
#!/bin/bash
case $1 in
active)
curl -s http://127.0.0.1/nginx_status | awk '/Active/ {print $3}' ;;
accepts)
curl -s http://127.0.0.1/nginx_status | awk 'NR==3 {print $1}' ;;
handled)
curl -s http://127.0.0.1/nginx_status | awk 'NR==3 {print $2}' ;;
requests)
curl -s http://127.0.0.1/nginx_status | awk 'NR==3 {print $3}' ;;
reading)
curl -s http://127.0.0.1/nginx_status | awk '/Reading/ {print $2}' ;;
writing)
curl -s http://127.0.0.1/nginx_status | awk '/Writing/ {print $4}' ;;
waiting)
curl -s http://127.0.0.1/nginx_status | awk '/Waiting/ {print $6}' ;;
*)
echo "Usage: $0 { active | accepts | handled | requests | reading | writing | waiting }" ;;
esac
# 脚本使用 curl 结合 awk 来获取 key 的值。
# -s 静默模式,如果不加 -s 参数,则获取到的结果是不正确的。
[root@localhost ~]# chmod a+x /script/nginx_status.sh
5、添加自定义 key 配置文件
[root@zabbix-node2 ~]# cat /etc/zabbix/zabbix_agentd.d/nginx_status.conf
## Nginx_status
UserParameter=nginx.active,/script/nginx_status.sh active
UserParameter=nginx.accepts,/script/nginx_status.sh accepts
UserParameter=nginx.handled,/script/nginx_status.sh handled
UserParameter=nginx.requests,/script/nginx_status.sh requests
UserParameter=nginx.reading,/script/nginx_status.sh reading
UserParameter=nginx.writing,/script/nginx_status.sh writing
UserParameter=nginx.waiting,/script/nginx_status.sh waiting
# 也可以直接加到 /etc/zabbix/zabbix_agentd.conf末尾
[root@localhost ~]# vim /etc/zabbix/zabbix_agentd.conf
Include=/usr/local/zabbix/etc/zabbix_agentd.conf.d/*.conf
UnsafeUserParameters=1 # 允许自定义 Key
# 添加上面配置
5、重启 Zabbix_agentd
[root@zabbix-node2 ~]# systemctl restart zabbix-agent
## 注意,上面全部操作都是在被监控端
6、Zabbix 服务端测试能否拿到 Key
[root@zabbx-node1 ~]# /usr/local/zabbix/bin/zabbix_get -s 192.168.91.134 -k nginx.active
1
[root@zabbx-node1 ~]# /usr/local/zabbix/bin/zabbix_get -s 192.168.91.134 -k nginx.requests
2007
# 可以获取到 key 的值,说明配置没有问题了
# 被监控端记得要开启 10050 端口
- Zabbix 监控 Nginx 状态
# 接下来就是在zabbix Web 界面,创建模板、添加监控项了
1.)创建模板(一起跟着我点点点 Configuration --> Templates --> Create template)
2.) 创建应用分组
#现在又回到了模板页,跟我一起点 Template App Nginx Service 模板后面的 Applications --> Create application
3.) 创建监控项
#现在的位置是 Template App Nginx Service 模板中的 Applications 页面,跟我一起点击刚创建的 Nginx status 后面的 Items -> Create item
分别定义 nginx.active | nginx.accepts | nginx.handled | nginx.requests | nginx.reading | nginx.writing | waiting 即可
8、将模板应用到主机
1. 继续跟跟着我点点点 Configuration -> Hosts -> 选择安装nginx的客户机(zabbix-node2.client)
9、创建触发器(这里只创建一个做例子截图,分别定义 nginx.active | nginx.accepts | nginx.handled | nginx.requests | nginx.reading | nginx.writing | nginx.waiting 即可):
10、创建图形:
等30s就可以看到图形啦!
由于我是随便设置触发器的,根据上上节的邮件告警配置。马上就来邮件了,这些都是根据实际生产环境设置参数。这里只是做测试:
Ok到这里监控nginx就告一段落啦!是不是很简单,哈哈!!!希望能帮助到大家。。。。