{"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":"\nZabbix API\u6279\u91cf\u6dfb\u52a0Web \u76d1\u63a7 - Linux\u81ea\u52a8\u5316\u8fd0\u7ef4<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.linuxdevops.cn\/2020\/04\/zabbix-batch-add-web-monitoring\/\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Zabbix API\u6279\u91cf\u6dfb\u52a0Web \u76d1\u63a7 - Linux\u81ea\u52a8\u5316\u8fd0\u7ef4\" \/>\n<meta property=\"og:description\" content=\"#!\/usr\/bin\/python3.6 from pyzabbix import ZabbixAPI imp\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.linuxdevops.cn\/2020\/04\/zabbix-batch-add-web-monitoring\/\" \/>\n<meta property=\"og:site_name\" content=\"Linux\u81ea\u52a8\u5316\u8fd0\u7ef4\" \/>\n<meta property=\"article:published_time\" content=\"2020-04-07T01:33:01+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-04-07T01:48:18+00:00\" \/>\n<meta name=\"author\" content=\"\u7ba1\u7406\u5458\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.linuxdevops.cn\/2020\/04\/zabbix-batch-add-web-monitoring\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.linuxdevops.cn\/2020\/04\/zabbix-batch-add-web-monitoring\/\"},\"author\":{\"name\":\"\u7ba1\u7406\u5458\",\"@id\":\"https:\/\/www.linuxdevops.cn\/#\/schema\/person\/3e206335d5796fdd8679e449df72a0d1\"},\"headline\":\"Zabbix API\u6279\u91cf\u6dfb\u52a0Web \u76d1\u63a7\",\"datePublished\":\"2020-04-07T01:33:01+00:00\",\"dateModified\":\"2020-04-07T01:48:18+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.linuxdevops.cn\/2020\/04\/zabbix-batch-add-web-monitoring\/\"},\"wordCount\":3,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.linuxdevops.cn\/#\/schema\/person\/3e206335d5796fdd8679e449df72a0d1\"},\"articleSection\":[\"Linux\",\"Python\",\"zabbix\"],\"inLanguage\":\"zh-CN\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.linuxdevops.cn\/2020\/04\/zabbix-batch-add-web-monitoring\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.linuxdevops.cn\/2020\/04\/zabbix-batch-add-web-monitoring\/\",\"url\":\"https:\/\/www.linuxdevops.cn\/2020\/04\/zabbix-batch-add-web-monitoring\/\",\"name\":\"Zabbix API\u6279\u91cf\u6dfb\u52a0Web \u76d1\u63a7 - Linux\u81ea\u52a8\u5316\u8fd0\u7ef4\",\"isPartOf\":{\"@id\":\"https:\/\/www.linuxdevops.cn\/#website\"},\"datePublished\":\"2020-04-07T01:33:01+00:00\",\"dateModified\":\"2020-04-07T01:48:18+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.linuxdevops.cn\/2020\/04\/zabbix-batch-add-web-monitoring\/#breadcrumb\"},\"inLanguage\":\"zh-CN\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.linuxdevops.cn\/2020\/04\/zabbix-batch-add-web-monitoring\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.linuxdevops.cn\/2020\/04\/zabbix-batch-add-web-monitoring\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.linuxdevops.cn\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Linux\",\"item\":\"https:\/\/www.linuxdevops.cn\/linux\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Zabbix API\u6279\u91cf\u6dfb\u52a0Web \u76d1\u63a7\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.linuxdevops.cn\/#website\",\"url\":\"https:\/\/www.linuxdevops.cn\/\",\"name\":\"Linux\u81ea\u52a8\u5316\u8fd0\u7ef4\",\"description\":\"Linux\u81ea\u52a8\u5316\u8fd0\u7ef4\u7b14\u8bb0\",\"publisher\":{\"@id\":\"https:\/\/www.linuxdevops.cn\/#\/schema\/person\/3e206335d5796fdd8679e449df72a0d1\"},\"alternateName\":\"linuxdevops\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.linuxdevops.cn\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-CN\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/www.linuxdevops.cn\/#\/schema\/person\/3e206335d5796fdd8679e449df72a0d1\",\"name\":\"\u7ba1\u7406\u5458\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-CN\",\"@id\":\"https:\/\/www.linuxdevops.cn\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/www.linuxdevops.cn\/wp-content\/uploads\/2019\/07\/cropped-index.jpg\",\"contentUrl\":\"https:\/\/www.linuxdevops.cn\/wp-content\/uploads\/2019\/07\/cropped-index.jpg\",\"width\":512,\"height\":512,\"caption\":\"\u7ba1\u7406\u5458\"},\"logo\":{\"@id\":\"https:\/\/www.linuxdevops.cn\/#\/schema\/person\/image\/\"},\"description\":\"\u7ba1\u7406\u5458\",\"url\":\"https:\/\/www.linuxdevops.cn\/author\/root\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Zabbix API\u6279\u91cf\u6dfb\u52a0Web \u76d1\u63a7 - Linux\u81ea\u52a8\u5316\u8fd0\u7ef4","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.linuxdevops.cn\/2020\/04\/zabbix-batch-add-web-monitoring\/","og_locale":"zh_CN","og_type":"article","og_title":"Zabbix API\u6279\u91cf\u6dfb\u52a0Web \u76d1\u63a7 - Linux\u81ea\u52a8\u5316\u8fd0\u7ef4","og_description":"#!\/usr\/bin\/python3.6 from pyzabbix import ZabbixAPI imp","og_url":"https:\/\/www.linuxdevops.cn\/2020\/04\/zabbix-batch-add-web-monitoring\/","og_site_name":"Linux\u81ea\u52a8\u5316\u8fd0\u7ef4","article_published_time":"2020-04-07T01:33:01+00:00","article_modified_time":"2020-04-07T01:48:18+00:00","author":"\u7ba1\u7406\u5458","twitter_card":"summary_large_image","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.linuxdevops.cn\/2020\/04\/zabbix-batch-add-web-monitoring\/#article","isPartOf":{"@id":"https:\/\/www.linuxdevops.cn\/2020\/04\/zabbix-batch-add-web-monitoring\/"},"author":{"name":"\u7ba1\u7406\u5458","@id":"https:\/\/www.linuxdevops.cn\/#\/schema\/person\/3e206335d5796fdd8679e449df72a0d1"},"headline":"Zabbix API\u6279\u91cf\u6dfb\u52a0Web \u76d1\u63a7","datePublished":"2020-04-07T01:33:01+00:00","dateModified":"2020-04-07T01:48:18+00:00","mainEntityOfPage":{"@id":"https:\/\/www.linuxdevops.cn\/2020\/04\/zabbix-batch-add-web-monitoring\/"},"wordCount":3,"commentCount":0,"publisher":{"@id":"https:\/\/www.linuxdevops.cn\/#\/schema\/person\/3e206335d5796fdd8679e449df72a0d1"},"articleSection":["Linux","Python","zabbix"],"inLanguage":"zh-CN","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.linuxdevops.cn\/2020\/04\/zabbix-batch-add-web-monitoring\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.linuxdevops.cn\/2020\/04\/zabbix-batch-add-web-monitoring\/","url":"https:\/\/www.linuxdevops.cn\/2020\/04\/zabbix-batch-add-web-monitoring\/","name":"Zabbix API\u6279\u91cf\u6dfb\u52a0Web \u76d1\u63a7 - Linux\u81ea\u52a8\u5316\u8fd0\u7ef4","isPartOf":{"@id":"https:\/\/www.linuxdevops.cn\/#website"},"datePublished":"2020-04-07T01:33:01+00:00","dateModified":"2020-04-07T01:48:18+00:00","breadcrumb":{"@id":"https:\/\/www.linuxdevops.cn\/2020\/04\/zabbix-batch-add-web-monitoring\/#breadcrumb"},"inLanguage":"zh-CN","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.linuxdevops.cn\/2020\/04\/zabbix-batch-add-web-monitoring\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.linuxdevops.cn\/2020\/04\/zabbix-batch-add-web-monitoring\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.linuxdevops.cn\/"},{"@type":"ListItem","position":2,"name":"Linux","item":"https:\/\/www.linuxdevops.cn\/linux\/"},{"@type":"ListItem","position":3,"name":"Zabbix API\u6279\u91cf\u6dfb\u52a0Web \u76d1\u63a7"}]},{"@type":"WebSite","@id":"https:\/\/www.linuxdevops.cn\/#website","url":"https:\/\/www.linuxdevops.cn\/","name":"Linux\u81ea\u52a8\u5316\u8fd0\u7ef4","description":"Linux\u81ea\u52a8\u5316\u8fd0\u7ef4\u7b14\u8bb0","publisher":{"@id":"https:\/\/www.linuxdevops.cn\/#\/schema\/person\/3e206335d5796fdd8679e449df72a0d1"},"alternateName":"linuxdevops","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.linuxdevops.cn\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-CN"},{"@type":["Person","Organization"],"@id":"https:\/\/www.linuxdevops.cn\/#\/schema\/person\/3e206335d5796fdd8679e449df72a0d1","name":"\u7ba1\u7406\u5458","image":{"@type":"ImageObject","inLanguage":"zh-CN","@id":"https:\/\/www.linuxdevops.cn\/#\/schema\/person\/image\/","url":"https:\/\/www.linuxdevops.cn\/wp-content\/uploads\/2019\/07\/cropped-index.jpg","contentUrl":"https:\/\/www.linuxdevops.cn\/wp-content\/uploads\/2019\/07\/cropped-index.jpg","width":512,"height":512,"caption":"\u7ba1\u7406\u5458"},"logo":{"@id":"https:\/\/www.linuxdevops.cn\/#\/schema\/person\/image\/"},"description":"\u7ba1\u7406\u5458","url":"https:\/\/www.linuxdevops.cn\/author\/root\/"}]}},"_links":{"self":[{"href":"https:\/\/www.linuxdevops.cn\/wp-json\/wp\/v2\/posts\/672"}],"collection":[{"href":"https:\/\/www.linuxdevops.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.linuxdevops.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.linuxdevops.cn\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.linuxdevops.cn\/wp-json\/wp\/v2\/comments?post=672"}],"version-history":[{"count":2,"href":"https:\/\/www.linuxdevops.cn\/wp-json\/wp\/v2\/posts\/672\/revisions"}],"predecessor-version":[{"id":676,"href":"https:\/\/www.linuxdevops.cn\/wp-json\/wp\/v2\/posts\/672\/revisions\/676"}],"wp:attachment":[{"href":"https:\/\/www.linuxdevops.cn\/wp-json\/wp\/v2\/media?parent=672"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.linuxdevops.cn\/wp-json\/wp\/v2\/categories?post=672"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.linuxdevops.cn\/wp-json\/wp\/v2\/tags?post=672"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}