原创:用zabbix api批量添加we
短信预约 -IT技能 免费直播动态提醒
代码如下,进攻大家参考,转载的话说明来源
前面5个变量自行补齐,
#!/usr/local/python3.51/bin/python3
# Author: Anxinhai@wondersgroup
# Mail: 29338383@qq.com
from pyzabbix import ZabbixAPI
import sys
from re import compile,IGNORECASE
ZABBIX_SERVER = ""
USER = ""
PASSWORD = ""
HOSTNAME = ""
URL=""
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,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": '60',"retries": '3', "steps": [ { 'name': URL, 'url': URL, 'no': '1'} ] } )
except Exception as e:
print(e)
def create_trigger(auth,HOSTNAME,URL):
expression="{"+"{0}:web.test.fail[{1}].last()".format(HOSTNAME,URL)+"}"+"<>0"
try:
ZabbixAPI.do_request(auth, 'trigger.create', params={"description": "从监控机(172.18.11.34)访问{0}出现问题,如果网络和主机性能没问题,并且是单节点报错请尝试重启对应的tomcat".format(URL),"expression": expression,"priority":5})
except Exception as e:
print(e)
auth = login(ZABBIX_SERVER,USER,PASSWORD)
hostid = gethostid(auth,HOSTNAME)
applicationid=getapplicationid(auth,hostid)
create_web_scenario(auth,URL,hostid,applicationid)
create_trigger(auth,HOSTNAME,URL)
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341