按照zabbix客户端及自定义监控nginx连接状态
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了按照zabbix客户端及自定义监控nginx连接状态相关的知识,希望对你有一定的参考价值。
一、下载客户端按照包,这里用rpm包安装
[[email protected] ~]# rpm -ivh http://repo.zabbix.com/zabbix/3.0/rhel/7/x86_64/zabbix-agent-3.0.4-1.el7.x86_64.rpm
修改客户端配置文件
Server=127.0.0.1 ServerActive=127.0.0.1
改为zabbix server实际的IP地址(比如zabbix server的IP地址为10.0.0.100)
Server=10.0.0.100 ServerActive=10.0.0.100
重启zabbix 客户端
[[email protected]]# systemctl restart zabbix-agent
二、自定义脚本监控nginx连接状态:
#!/bin/bash HOST=localhost PORT="9999" function active { /usr/bin/curl "http://$HOST:$PORT/nginx-status/" 2>/dev/null| grep ‘Active‘ | awk ‘{print $NF}‘ } function reading { /usr/bin/curl "http://$HOST:$PORT/nginx-status/" 2>/dev/null| grep ‘Reading‘ | awk ‘{print $2}‘ } function writing { /usr/bin/curl "http://$HOST:$PORT/nginx-status/" 2>/dev/null| grep ‘Writing‘ | awk ‘{print $4}‘ } function waiting { /usr/bin/curl "http://$HOST:$PORT/nginx-status/" 2>/dev/null| grep ‘Waiting‘ | awk ‘{print $6}‘ } function accepts { /usr/bin/curl "http://$HOST:$PORT/nginx-status/" 2>/dev/null| awk NR==3 | awk ‘{print $1}‘ } function handled { /usr/bin/curl "http://$HOST:$PORT/nginx-status/" 2>/dev/null| awk NR==3 | awk ‘{print $2}‘ } function requests { /usr/bin/curl "http://$HOST:$PORT/nginx-status/" 2>/dev/null| awk NR==3 | awk ‘{print $3}‘ } $1
将脚本存放与脚本目录,注意该目录根据自己情况定义,我这里用rpm 按照zabbix-agent客户端,所以直接放在/etc/zabbix/script目录下
[[email protected] script]# ll nginx-status.sh -rwx------ 1 root root 947 Mar 22 10:49 nginx-status.sh [[email protected] script]#
不要忘了给脚本执行权限
[[email protected] script]# chmod +x nginx-status.sh [[email protected] script]# [[email protected] script]# ll nginx-status.sh -rwxr-xr-x 1 root root 947 Mar 22 10:49 nginx-status.sh [[email protected] script]#
修改配置文件,在末尾添加如下内容:
[[email protected] zabbix]# vim zabbix_agentd.conf ...................................省略部分...................................................... ### DISK I/O UserParameter=custom.vfs.dev.read.ops[*],cat /proc/diskstats | grep $1 | head -1 | awk ‘{print $$4}‘ UserParameter=custom.vfs.dev.read.ms[*],cat /proc/diskstats | grep $1 | head -1 | awk ‘{print $$7}‘ UserParameter=custom.vfs.dev.write.ops[*],cat /proc/diskstats | grep $1 | head -1 | awk ‘{print $$8}‘ UserParameter=custom.vfs.dev.write.ms[*],cat /proc/diskstats | grep $1 | head -1 | awk ‘{print $$11}‘ UserParameter=custom.vfs.dev.io.active[*],cat /proc/diskstats | grep $1 | head -1 | awk ‘{print $$12}‘ UserParameter=custom.vfs.dev.io.ms[*],cat /proc/diskstats | grep $1 | head -1 | awk ‘{print $$13}‘ UserParameter=custom.vfs.dev.read.sectors[*],cat /proc/diskstats | grep $1 | head -1 | awk ‘{print $$6}‘ UserParameter=custom.vfs.dev.write.sectors[*],cat /proc/diskstats | grep $1 | head -1 | awk ‘{print $$10}‘ ######## TCP connections total UnsafeUserParameters=1 UserParameter=synrecv,/etc/zabbix/script/tcp_connection_status.sh SYNRECV UserParameter=estab,/etc/zabbix/script/tcp_connection_status.sh ESTAB UserParameter=timewait,/etc/zabbix/script/tcp_connection_status.sh TIMEWAIT UserParameter=last_ack,/etc/zabbix/script/tcp_connection_status.sh LAST_ACK UserParameter=total,/etc/zabbix/script/tcp_connection_status.sh TOTAL #####Nginx Status UserParameter=nginx.accepts,/etc/zabbix/script/nginx-status.sh accepts UserParameter=nginx.handled,/etc/zabbix/script/nginx-status.sh handled UserParameter=nginx.requests,/etc/zabbix/script/nginx-status.sh requests UserParameter=nginx.connections.active,/etc/zabbix/script/nginx-status.sh active UserParameter=nginx.connections.reading,/etc/zabbix/script/nginx-status.sh reading UserParameter=nginx.connections.writing,/etc/zabbix/script/nginx-status.sh writing UserParameter=nginx.connections.waiting,/etc/zabbix/script/nginx-status.sh waiting EnableRemoteCommands=1
然后重启zabbix客户端
[[email protected]]# systemctl restart zabbix-agent [[email protected] zabbix]# systemctl status zabbix-agent ● zabbix-agent.service - Zabbix Agent Loaded: loaded (/usr/lib/systemd/system/zabbix-agent.service; disabled; vendor preset: disabled) Active: active (running) since Wed 2017-03-22 11:03:05 CST; 6min ago Process: 13613 ExecStop=/bin/kill -SIGTERM $MAINPID (code=exited, status=0/SUCCESS) Process: 13615 ExecStart=/usr/sbin/zabbix_agentd -c $CONFFILE (code=exited, status=0/SUCCESS) Main PID: 13617 (zabbix_agentd) CGroup: /system.slice/zabbix-agent.service ├─13617 /usr/sbin/zabbix_agentd -c /etc/zabbix/zabbix_agentd.conf ├─13618 /usr/sbin/zabbix_agentd: collector [idle 1 sec] ├─13619 /usr/sbin/zabbix_agentd: listener #1 [waiting for connection] ├─13620 /usr/sbin/zabbix_agentd: listener #2 [waiting for connection] └─13621 /usr/sbin/zabbix_agentd: listener #3 [waiting for connection] Mar 22 11:03:05 localhost systemd[1]: Starting Zabbix Agent... Mar 22 11:03:05 localhost systemd[1]: PID file /run/zabbix/zabbix_agentd.pid not readable (yet?) after start. Mar 22 11:03:05 localhost systemd[1]: Started Zabbix Agent. [[email protected] zabbix]#
最后在服务端添加主机对应的监控项,键值为如下字段:
nginx.accepts nginx.handled nginx.requests nginx.connections.active, nginx.connections.reading nginx.connections.writing nginx.connections.waiting
本文出自 “平平淡淡才是真” 博客,请务必保留此出处http://ucode.blog.51cto.com/10837891/1909146
以上是关于按照zabbix客户端及自定义监控nginx连接状态的主要内容,如果未能解决你的问题,请参考以下文章