{"id":672,"date":"2020-04-07T09:33:01","date_gmt":"2020-04-07T01:33:01","guid":{"rendered":"https:\/\/www.linuxdevops.cn\/?p=672"},"modified":"2020-04-07T09:48:18","modified_gmt":"2020-04-07T01:48:18","slug":"zabbix-batch-add-web-monitoring","status":"publish","type":"post","link":"https:\/\/www.linuxdevops.cn\/2020\/04\/zabbix-batch-add-web-monitoring\/","title":{"rendered":"Zabbix API\u6279\u91cf\u6dfb\u52a0Web \u76d1\u63a7"},"content":{"rendered":"\n
#!\/usr\/bin\/python3.6\nfrom pyzabbix import ZabbixAPI\nimport sys\n\n\nZABBIX_SERVER = \"http:\/\/127.0.0.1:8080\"\nUSER = \"admin\"\nPASSWORD = \"password\"\n# \u8981\u6dfb\u52a0\u5728\u54ea\u53f0\u4e3b\u673a\nHOSTNAME = \"192.168.0.30\"\n\n# \u76d1\u63a7 web\u573a\u666f\u540d\u79f0 \u548c URL\nsetps = [\n {'name': \"\u767e\u5ea6\",'URL': \"https:\/\/www.baidu.com\"},\n {'name': \"CSDN\",'URL': \"https:\/\/www.csdn.net\"}\n]\n\n# \u767b\u5f55\ndef login(ZABBIX_SERVER,USER,PASSWORD):\n zapi = ZabbixAPI(ZABBIX_SERVER)\n zapi.login(USER,PASSWORD)\n return zapi\n# \u83b7\u53d6\u4e3b\u673a\ndef gethostid(auth,HOSTNAME):\n request = ZabbixAPI.do_request(auth, 'host.get', params={ \"filter\": {\"host\":HOSTNAME}})\n if request['result']:\n return request['result'][0]['hostid']\n else:\n print (\"\u627e\u4e0d\u5230\u8be5\u4e3b\u673a\")\n sys.exit(1)\n # \u521b\u5efa\u5e94\u7528\u96c6\ndef getapplicationid(auth,hostid):\n try:\n request = ZabbixAPI.do_request(auth, 'application.create', params={\"name\": \"web\u76d1\u63a7\",\"hostid\": hostid})\n except Exception as e:\n print(e)\n request = ZabbixAPI.do_request(auth, 'application.get', params={\"hostids\": hostid})\n for num in range(0,len (request['result'])):\n if request['result'][num]['name'] == \"web\u76d1\u63a7\":\n return request['result'][num]['applicationid']\n # \u6dfb\u52a0web \u76d1\u63a7\ndef create_web_scenario(auth,URL,hostid,applicationid):\n request = ZabbixAPI.do_request(auth, 'httptest.get', params={\"filter\": {\"name\": URL}})\n if request['result']:\n print('\u8be5web\u76d1\u63a7\u5df2\u7ecf\u6dfb\u52a0\u8fc7\u4e86')\n else:\n try:\n ZabbixAPI.do_request(auth, 'httptest.create',params={\"name\": name,\n \"hostid\": hostid,\n \"applicationid\": applicationid,\n \"delay\": '60',\n \"retries\": '3',\n \"steps\": [ { 'name': URL, 'url': URL, 'no': '1'} ] } )\n except Exception as e:\n print(e)\n\n# \u83b7\u53d6interfaceid\n# def create_interfaceid(auth,hostid):\n# request = ZabbixAPI.do_request(auth, 'hostinterface.get', params={\"output\": \"itemids\",\"hostids\": hostid})\n# print(request)\n# return request['result'][0]['interfaceid']\n# \u521b\u5efa\u76d1\u63a7\u9879\n# def create_item(auth,URL,hostid,interfaceid):\n# ZabbixAPI.do_request(auth,'item.create', params={\"name\": \"URL \u68c0\u6d4b\",\n# \"key_\": \"web.test.fail[{0}]\".format(URL),\n# \"hostid\": hostid,\n# \"type\": 0,\n# \"value_type\": 0,\n# \"interfaceid\": interfaceid,\n# \"applicationid\": applicationid,\n# \"delay\": \"2s\"\n# })\n # \u521b\u5efa\u89e6\u53d1\u5668\ndef create_trigger(auth,HOSTNAME,name):\n expression = \"{\" + \"{0}:web.test.fail[{1}].sum(#3)\".format(HOSTNAME,name) + \"}\" + \"<>0\"\n try:\n ZabbixAPI.do_request(auth, 'trigger.create', params={\"description\": \"\u4ece\u76d1\u63a7\u673aZabbix Server \u8bbf\u95ee{0}\u51fa\u73b0\u95ee\u9898\uff0c\u8bf7\u5c1d\u8bd5\u6392\u67e5\u5bf9\u5e94WEB\u5e94\u7528\".format(name),\n \"expression\": expression,\n \"priority\": '3'})\n except Exception as e:\n print(e)\n\nauth = login(ZABBIX_SERVER,USER,PASSWORD)\nhostid = gethostid(auth,HOSTNAME)\napplicationid=getapplicationid(auth,hostid)\n# interfaceid=create_interfaceid(auth,hostid)\nfor i in setps:\n name = i['name']\n URL = i['URL']\n # create_item(auth,URL,hostid,interfaceid)\n create_web_scenario(auth,URL,hostid,applicationid)\n create_trigger(auth,HOSTNAME,name)<\/pre>\n","protected":false},"excerpt":{"rendered":"#!\/usr\/bin\/python3.6 from pyzabbix import ZabbixAPI imp<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17,13,33],"tags":[],"yoast_head":"\n
Zabbix API\u6279\u91cf\u6dfb\u52a0Web \u76d1\u63a7 - Linux\u81ea\u52a8\u5316\u8fd0\u7ef4<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n