您的位置:首页 > 编程语言 > Python开发

使用python的zabbix_api模块

2016-04-01 18:16 579 查看
使用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

本文出自 “漂泊的鱼” 博客,请务必保留此出处http://faded.blog.51cto.com/6375932/1759324
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: