ZABBIX之NGINX监控

1,端口自动发现
cat nginx_port.py 
#!/usr/bin/env python
import os
import json
t=os.popen("""netstat -antp|awk '/LISTEN/&&/nginx/{print $4}'|awk -F: '{print $NF}' """)
ports = []
for port in  t.readlines():
        r = os.path.basename(port.strip())
        ports += [{'{#NGINXPORT}':r}]
print json.dumps({'data':ports},sort_keys=True,indent=4,separators=(',',':'))

2, 状态脚本

cat >>/usr/local/zabbix/bin/nginx_status <<end
#!/bin/bash  
function active {
/usr/bin/curl "http://localhost/stub-status" 2>/dev/null| grep 'Active' | awk '{print $NF}'
}
function reading {
/usr/bin/curl "http://localhost/stub-status" 2>/dev/null| grep 'Reading' | awk '{print $2}'
}
function writing {
/usr/bin/curl "http://localhost/stub-status" 2>/dev/null| grep 'Writing' | awk '{print $4}'
}
function waiting {
/usr/bin/curl "http://localhost/stub-status" 2>/dev/null| grep 'Waiting' | awk '{print $6}'
}
function accepts {
/usr/bin/curl "http://localhost/stub-status" 2>/dev/null| awk NR==3 | awk '{print $1}'
}
function handled {
/usr/bin/curl "http://localhost/stub-status" 2>/dev/null| awk NR==3 | awk '{print $2}'
}
function requests {
/usr/bin/curl "http://localhost/stub-status" 2>/dev/null| awk NR==3 | awk '{print $3}'
}
# Run the requested function  
$1 
3, 配置文件:
cat >>/usr/local/zabbix/conf/zabbix_agentd.conf <<end
# nginx
UserParameter=nginx[*],/usr/local/zabbix/bin/nginx_status $1
UserParameter=nginx.discovery,/usr/local/zabbix/bin/nginx_port.py

4, 

killall zabbix_agentd
/usr/local/zabbix/sbin/zabbix_agentd
 

posted on 2016-06-02 00:08  Tank-101  阅读(192)  评论(0编辑  收藏  举报