乐趣区

关于zabbix:nginxupstreamcheckmodule-模块应用于zabbix监控-自定义python

注:

当 nginx 利用了 nginx_upstream_check_module 模块,尽管能够看到 upstream 的 service 状态,然而如何做到实时监控报警

[root@sit-cdpapp-162l zabbix]# cat nginx_check_upstream.py 
#!/usr/bin/python
# -*- coding:utf-8 -*-


# from django.test import TestCase
import requests,re
import os,sys

domainname = sys.argv[1]
try:
    environment = sys.argv[2]
except IndexError:
    environment = ''

def NginxSstatus(url):
    r = requests.get(url)
    resStr = r.text
    resStr = resStr.split('\n')
    thList = [re.search('<th>.*</th>',i).group().replace('<th>','').replace('</th>','') for i in resStr if re.search('<th>.*</th>',i) is not None]
    tdList = [re.search('<td>.*</td>',i).group().replace('<td>','').replace('</td>','') for i in resStr if re.search('<td>.*</td>',i) is not None]
    resList = [tdList[i:i+len(thList)] for i in range(0,len(tdList),len(thList))]
    # resList.insert(0,thList)
    resList = [dict(map(lambda x,y:[x,y],thList,li)) for li in resList]
    return resList


if __name__ == "__main__":
    if environment == '':
        li = NginxSstatus('http://{tdomain}.xxxx.com/nstatus'.format(tenv=environment,tdomain=domainname))
    else:
        li = NginxSstatus('http://{tenv}-{tdomain}.xxxx.com/nstatus'.format(tenv=environment,tdomain=domainname))
    #print(li)
    #print(type(li[0]['Index']))
    code = []
    for i in range(len(li)):
        if li[i]['Status'] == 'down':
            code.append(li[i])
        else:
            code.append('success')

    print(code)

vim /etc/zabbix/nginx_check_upstream.py

cat /etc/zabbix/zabbix_agentd.conf

退出移动版