nginx_upstream_check_module 模块应用于zabbix监控 (自定义python)

注:

当nginx 应用了nginx_upstream_check_module 模块,虽然可以看到upstream 的service状态,但是如何做到实时监控报警

nginx_upstream_check_module 模块应用于zabbix监控 (自定义python)_第1张图片

[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('.*',i).group().replace('','').replace('','') for i in resStr if re.search('.*',i) is not None]
    tdList = [re.search('.*',i).group().replace('','').replace('','') for i in resStr if re.search('.*',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)

nginx_upstream_check_module 模块应用于zabbix监控 (自定义python)_第2张图片

vim /etc/zabbix/nginx_check_upstream.py

nginx_upstream_check_module 模块应用于zabbix监控 (自定义python)_第3张图片

cat /etc/zabbix/zabbix_agentd.conf

nginx_upstream_check_module 模块应用于zabbix监控 (自定义python)_第4张图片

nginx_upstream_check_module 模块应用于zabbix监控 (自定义python)_第5张图片

image.png

你可能感兴趣的:(zabbix,nginx-module,python3.x)