🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# 自定义nginx活动连接数监控 (1)添加用户自定义参数 ``` [root@host-10-197-22-14 ~]# cat /etc/zabbix/zabbix_agentd.d/nginx.conf UserParameter=nginx.active,/usr/bin/curl -s "http://10.197.22.14:8080/nginx-status" | grep 'Active' | awk '{print $NF}' ``` (2)重启zabbix-agent systemctl restart zabbix-agent (3)在server端使用zabbix_get 测试获取 [root@host ~]# zabbix_get -s 10.197.22.14 -p 10050 -k "nginx.active" 1 (4)自定义监控内容 在web界面创建item ![7-37](http://pded8ke3e.bkt.clouddn.com/7-37.png) 自定义图形 ![7-38](http://pded8ke3e.bkt.clouddn.com/7-38.png) (5)查看图表信息 ![7-39](http://pded8ke3e.bkt.clouddn.com/7-39.png) # 监控nginx 添加监控大致流程 - 1.开启Nginx监控 - 2.编写脚本来进行数据采集。 - 3.设置用户自定义参数 - 4.重启zabbix-agent - 5.添加item - 6.创建图形 - 7.创建触发器 - 8.创建模板 (1)自定义监控脚本:zabbix_linux_plugin.sh ``` #!/bin/bash tcp_status_fun(){ TCP_STAT=$1 #netstat -n | awk '/^tcp/ {++state[$NF]} END {for(key in state) print key,state[key]}' > /tmp/netstat.tmp ss -ant | awk 'NR>1 {++s[$1]} END {for(k in s) print k,s[k]}' > /tmp/netstat.tmp TCP_STAT_VALUE=$(grep "$TCP_STAT" /tmp/netstat.tmp | cut -d ' ' -f2) if [ -z $TCP_STAT_VALUE ];then TCP_STAT_VALUE=0 fi echo $TCP_STAT_VALUE } nginx_status_fun(){ NGINX_PORT=$1 NGINX_COMMAND=$2 nginx_active(){ /usr/bin/curl "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2>/dev/null| grep 'Active' | awk '{print $NF}' } nginx_reading(){ /usr/bin/curl "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2>/dev/null| grep 'Reading' | awk '{print $2}' } nginx_writing(){ /usr/bin/curl "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2>/dev/null| grep 'Writing' | awk '{print $4}' } nginx_waiting(){ /usr/bin/curl "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2>/dev/null| grep 'Waiting' | awk '{print $6}' } nginx_accepts(){ /usr/bin/curl "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2>/dev/null| awk NR==3 | awk '{print $1}' } nginx_handled(){ /usr/bin/curl "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2>/dev/null| awk NR==3 | awk '{print $2}' } nginx_requests(){ /usr/bin/curl "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2>/dev/null| awk NR==3 | awk '{print $3}' } 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; esac } memcached_status_fun(){ M_PORT=$1 M_COMMAND=$2 echo -e "stats\nquit" | nc 127.0.0.1 "$M_PORT" | grep "STAT $M_COMMAND " | awk '{print $3}' } redis_status_fun(){ R_PORT=$1 R_COMMAND=$2 (echo -en "INFO \r\n";sleep 1;) | nc 127.0.0.1 "$R_PORT" > /tmp/redis_"$R_PORT".tmp REDIS_STAT_VALUE=$(grep ""$R_COMMAND":" /tmp/redis_"$R_PORT".tmp | cut -d ':' -f2) echo $REDIS_STAT_VALUE } main(){ case $1 in tcp_status) tcp_status_fun $2; ;; nginx_status) nginx_status_fun $2 $3; ;; memcached_status) memcached_status_fun $2 $3; ;; redis_status) redis_status_fun $2 $3; ;; *) echo $"Usage: $0 {tcp_status key|memcached_status key|redis_status key|nginx_status key}" esac } main $1 $2 $3 ``` (2)上传脚本到/etc/zabbix/zabbix_agentd.d (3)添加权限 ``` [root@host-10-197-22-14 zabbix_agentd.d]# chmod 777 zabbix_linux_plugin.sh ``` (4)修改nginx配置文件统一标准:`vim /usr/local/nginx/conf/nginx.conf` ``` location /nginx_status { stub_status on; access_log off; allow 127.0.0.1; deny all; } ``` (5)重载nginx ``` [root@host-10-197-22-14 zabbix_agentd.d]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx-1.15.2/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx-1.15.2/conf/nginx.conf test is successful [root@host-10-197-22-14 zabbix_agentd.d]# /usr/local/nginx/sbin/nginx -s reload ``` (6)验证 在客户端验证 ``` [root@host-10-197-22-14 zabbix_agentd.d]# ./zabbix_linux_plugin.sh nginx_status 8080 active 1 [root@host-10-197-22-14 zabbix_agentd.d]# cat linux.conf UserParameter=linux_status[*],/etc/zabbix/zabbix_agentd.d/zabbix_linux_plugin.sh "$1" "$2" "$3" [root@lhost-10-197-22-14 zabbix_agentd.d]# systemctl restart zabbix-agent ``` 在服务端验证 ``` [root@host-10-197-22-12 ~]# zabbix_get -s 10.197.22.14 -k linux_status[nginx_status,8080,active] 1 ``` (7)创建模板 ![7-47](http://pded8ke3e.bkt.clouddn.com/7-47.png) 填写Template name ![7-48](http://pded8ke3e.bkt.clouddn.com/7-48.png) 给Template Nginx Statusu模板添加item ![7-49](http://pded8ke3e.bkt.clouddn.com/7-49.png) 创建item ![7-50](http://pded8ke3e.bkt.clouddn.com/7-50.png) 用克隆的方法添加6个item ![7-51](http://pded8ke3e.bkt.clouddn.com/7-51.png) ![7-52](http://pded8ke3e.bkt.clouddn.com/7-52.png) 添加完成如图所示 ![7-53](http://pded8ke3e.bkt.clouddn.com/7-53.png) 创建图表 ![7-54](http://pded8ke3e.bkt.clouddn.com/7-54.png) ![7-55](http://pded8ke3e.bkt.clouddn.com/7-55.png) 给host-10-197-22-14主机链接模板 ![7-56](http://pded8ke3e.bkt.clouddn.com/7-56.png) ![7-57](http://pded8ke3e.bkt.clouddn.com/7-57.png) 导出模板 ![7-58](http://pded8ke3e.bkt.clouddn.com/7-58.png) 查看图表数据 ![7-59](http://pded8ke3e.bkt.clouddn.com/7-59.png) # nginx监控tcp和触发器添加 tcp模板下载链路:[请点击这里](https://pan.baidu.com/s/1fzoYyh8RzU9Qf93pIzRt1Q) 导入tcp模板 ![7-60](http://pded8ke3e.bkt.clouddn.com/7-60.png) 给host-10-197-22-12和host-10-197-22-14节点链接模板 ![7-61](http://pded8ke3e.bkt.clouddn.com/7-61.png) 查看图表 ![7-62](http://pded8ke3e.bkt.clouddn.com/7-62.png) 添加一个触发器 ![7-63](http://pded8ke3e.bkt.clouddn.com/7-63.png) ![7-64](http://pded8ke3e.bkt.clouddn.com/7-64.png) dashboard界面已经报警 ![7-65](http://pded8ke3e.bkt.clouddn.com/7-65.png)