pyzabbix 并沒(méi)有很多的案例, 參數(shù)要用對(duì)比較困難
pyzabbix 可以開(kāi)調(diào)試模式,輸出debug日志, 可以返回 已經(jīng)存在, 權(quán)限不足等錯(cuò)誤
資料:
github?https://github.com/lukecyca/pyzabbix
用例參考:?https://blog.csdn.net/qq_34355232/article/details/83857114主要是zapi.host.create
增加下面就可以了
#!/usr/bin/env python3
#coding:utf-8
#author lifuhua, in 20190426
import sys
import logging
from pyzabbix import ZabbixAPI
#這段是開(kāi)啟調(diào)試日志,不需要可以關(guān)閉
"""
stream = logging.StreamHandler(sys.stdout)
stream.setLevel(logging.DEBUG)
log = logging.getLogger('pyzabbix')
log.addHandler(stream)
log.setLevel(logging.DEBUG)
#這段是開(kāi)啟調(diào)試日志
"""
from pyzabbix import ZabbixAPI
def login():
zapi = ZabbixAPI("http://zabbix.xxxxxx.cn/zabbix")? # 登錄zabbix
??? zapi.login("lfhapi", "密碼")? ?#替換密碼
??? # print("Connected to Zabbix API Version %s" % zapi.api_version())
??? return zapi
def show_template():
??? #顯示所有template的id 和對(duì)應(yīng) name
??? template = zapi.template.get(output=["templateid","name"])
??? return template
def show_host():
??? #顯示所有主機(jī)的id和對(duì)應(yīng)名字
??? host = zapi.host.get(output=["hostid","host"])
??? return host
def show_drule():
??? #顯示所有自動(dòng)發(fā)現(xiàn)策略
??? drule = zapi.drule.get(filter={"name": "auto_change"},output=["druleid","name"])
??? return drule
def show_host_template(h_id):
??? #顯示主機(jī)上所有的鏈接模板
??? list_host_template = zapi.host.get(output=["hostid"],selectParentTemplates=["templateid","name"],hostids=h_id)
??? return list_host_template
def del_template(h_id,t_id):
??? #把主機(jī)h_id的模板t_id 完全清除
??? del_template = zapi.host.update(hostid=h_id,templates_clear={"templateid": t_id})
??? return del_template
def add_template(h_id,t_id):
??? #添加模板到主機(jī)上
??? #add_template = zapi.template.massadd(templates="10001",hosts={"hostid": "10725"})
??? add_template = zapi.template.massadd(templates=t_id, hosts={"hostid": h_id})
??? return add_template
def del_host(h_id):
??? #刪除主機(jī)
??? del_host = zapi.host.delete(h_id)
??? return del_host
def add_host(h_name,h_ip,g_id,t_id):
??? # 添加主機(jī)
??? add_host = zapi.host.create(
??????? host=h_name,
??????? interfaces=[{"type": "1","main": "1","useip": "1","ip": h_ip,"dns": "","port": "10050"}],
??????? groups=[{"groupid": g_id}],?????????? #我怎么都不明白,為什么這里沒(méi)有中括號(hào)就不行,其他的沒(méi)有中括號(hào)就可以
??????? templates=[{"templateid": t_id}]
??? )
??? return add_host
if __name__ == "__main__":
??? zapi = login()
#??? print(add_host("192.168.11.32","192.168.11.32","33","10001"))
??? print(show_template())
??? print(show_host())
??? print(show_drule())
??? print(show_host_template("10763"))