使用python的zabbix_api模块,以下是简单的zabbix api的使用

zabbix api文档参考https://www.zabbix.com/documentation/2.2/manual/api/reference


安装zabbix_api模块

easy_install zabbix_api或者pip install zabbix_api安装模块


 在zabbix上创建主机:


vim create_host.py

#/usr/bin/env python
from zabbix_api import ZabbixAPI
import sys

server = "http://172.16.133.133/zabbix"
username = "Admin"
password = "zabbix"
zapi = ZabbixAPI(server=server, path="", log_level=0)
zapi.login(username, password)

ip = sys.argv[1]

#获取主机组"Linux servers" 的groupid
group_id = zapi.hostgroup.get({"output":  "extend","filter": {"name": "Linux servers"}})[0]['groupid']

#获取模版"Template OS Linux"的templateid
template_id = zapi.template.get({"output": "extend","filter": {"host": "Template OS Linux"}})[0]['templateid']

# 创建主机,加入主机组"Linux servers",链接模版"Template OS Linux"
if zapi.host.exists({"host": "test"}):
    print 'host already exists'
else:
    create_host=zapi.host.create({"host": "test","groups": [{"groupid": group_id}],"interfaces": [{"type": "1","main": "1","us
eip": "1","ip": ip,"port": "10050","dns": ""}],"templates": [{"templateid": template_id}],"inventory_mode": -1,"name": "za
bbix_test"}) 
    print create_host


python create_host.py 172.16.133.134 即可将172.16.133.134加入zabbix,host_name为test,visible name为zabbix_test