#!/usr/bin/python3.6
from pyzabbix import ZabbixAPI
import sys


ZABBIX_SERVER = "http://127.0.0.1:8080"
USER = "admin"
PASSWORD = "password"
# 要添加在哪台主机
HOSTNAME = "192.168.0.30"

# 监控 web场景名称 和 URL
setps = [
  {'name': "百度",'URL': "https://www.baidu.com"},
  {'name': "CSDN",'URL': "https://www.csdn.net"}
]

# 登录
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']
      # 添加web 监控
def create_web_scenario(auth,URL,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": name,
                                                           "hostid": hostid,
                                                           "applicationid": applicationid,
                                                           "delay": '60',
                                                           "retries": '3',
                                                           "steps": [ { 'name': URL, 'url': URL, 'no': '1'} ] } )
    except Exception as e:
      print(e)

# 获取interfaceid
# def create_interfaceid(auth,hostid):
#   request = ZabbixAPI.do_request(auth, 'hostinterface.get', params={"output": "itemids","hostids": hostid})
#   print(request)
#   return request['result'][0]['interfaceid']
# 创建监控项
# def create_item(auth,URL,hostid,interfaceid):
#   ZabbixAPI.do_request(auth,'item.create', params={"name": "URL 检测",
#                                                   "key_": "web.test.fail[{0}]".format(URL),
#                                                   "hostid": hostid,
#                                                   "type": 0,
#                                                   "value_type": 0,
#                                                   "interfaceid": interfaceid,
#                                                   "applicationid": applicationid,
#                                                   "delay": "2s"
#                                                   })
  # 创建触发器
def create_trigger(auth,HOSTNAME,name):
  expression = "{" + "{0}:web.test.fail[{1}].sum(#3)".format(HOSTNAME,name) + "}" + "<>0"
  try:
    ZabbixAPI.do_request(auth, 'trigger.create', params={"description": "从监控机Zabbix Server 访问{0}出现问题,请尝试排查对应WEB应用".format(name),
                                                         "expression": expression,
                                                         "priority": '3'})
  except Exception as e:
    print(e)

auth = login(ZABBIX_SERVER,USER,PASSWORD)
hostid = gethostid(auth,HOSTNAME)
applicationid=getapplicationid(auth,hostid)
# interfaceid=create_interfaceid(auth,hostid)
for i in setps:
  name = i['name']
  URL = i['URL']
  # create_item(auth,URL,hostid,interfaceid)
  create_web_scenario(auth,URL,hostid,applicationid)
  create_trigger(auth,HOSTNAME,name)

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据