1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| #!/usr/bin/env python3 # Author: 思想就是武器 https://zealot.top # Mail: 18976787@qq.com from pyzabbix import ZabbixAPI import sys from re import compile,IGNORECASE ZABBIX_SERVER = "" USER = "" PASSWORD = "" HOSTNAME = "" def login(ZABBIX_SERVER,USER,PASSWORD): zapi = ZabbixAPI(ZABBIX_SERVER) zapi.login(USER,PASSWORD) return zapi def gethostid(auth,HOSTNAME): request = ZabbixAPI.do_request(auth, 'host.get', params={ "filter": {"host":HOSTNAME}}) if request['result']: return request['result'][0]['hostid'] else: print ("找不到该主机") sys.exit(1) def getapplicationid(auth,hostid): try: request = ZabbixAPI.do_request(auth, 'application.create', params={"name": "web监控","hostid": hostid}) except Exception as e: print(e) request = ZabbixAPI.do_request(auth, 'application.get', params={"hostids": hostid}) for num in range(0,len (request['result'])): if request['result'][num]['name'] == "web监控": return request['result'][num]['applicationid'] def create_web_scenario(auth,URL,URL1,hostid,applicationid): request = ZabbixAPI.do_request(auth, 'httptest.get', params={ "filter": {"name": URL}}) if request['result']: print('该web监控已经添加过了' ) else: try: ZabbixAPI.do_request(auth, 'httptest.create',params={"name": URL,"hostid": hostid,"applicationid": applicationid, "delay": '60s',"retries": '3', "steps": [ { 'name': URL1, 'url': URL1, 'no': '1', 'status_codes': r'200'} ] } ) except Exception as e: print(e) def create_trigger(auth,HOSTNAME,URL,URL1): expression="{"+"{0}:web.test.fail[{1}].last()".format(HOSTNAME,URL)+"}"+"<>0" recovery_expression="{"+"{0}:web.test.fail[{1}].last()".format(HOSTNAME,URL)+"}"+"=0" try: ZabbixAPI.do_request(auth, 'trigger.create', params={"description": "{0}-WEB无法访问".format(URL),"expression": expression,"priority":"5","recovery_mode": "1","recovery_expression": recovery_expression}) except Exception as e: print(e)
with open('weblist.txt','r') as f: for x in f.readlines(): URL=x URL1=URL.split(':',1)[1] if len(URL) > 64 : URL=URL.split(':',1)[0] auth = login(ZABBIX_SERVER,USER,PASSWORD) hostid = gethostid(auth,HOSTNAME) applicationid=getapplicationid(auth,hostid) create_web_scenario(auth,URL,URL1,hostid,applicationid) create_trigger(auth,HOSTNAME,URL,URL1)
|