码迷,mamicode.com
首页 > Windows程序 > 详细

利用zabbix-api创建item

时间:2016-08-11 23:10:10      阅读:7637      评论:0      收藏:0      [点我收藏+]

标签:通过zabbix api来创建item

#!/usr/bin/python
#-*- coding:utf8 -*-
from zabbix_api import ZabbixAPI
server = "http://172.16.206.130/zabbix"
username = "Admin"
password = "zabbix"
zapi = ZabbixAPI(server=server, path="", log_level=0)
zapi.login(username, password)

#通过主机名获取hostid和interfaceid
def get_hostinfo():
    host_info=zapi.host.get({"selectInterfaces":["interfaceid"],"filter":{"host":["Zabbix server"]}})
    hostid = host_info[0][‘hostid‘]
    interfaceid =  host_info[0][‘interfaces‘][0][‘interfaceid‘]
    return (hostid,interfaceid)

###创建item
def create_item():
    ###判断主机上item是否存在,返回值为True或者False
    item_exist = zapi.item.exists({"host": "Zabbix server", "key_": "proc.num[,,,/etc/nginx/nginx.conf]"})
    if not item_exist:
        a = get_hostinfo()
        hostid = a[0]
        interfaceid = a[1]
        create_item=zapi.item.create(
            {
                "name":"nginx service monitor",
                "key_":"proc.num[,,,/etc/nginx/nginx.conf]",
                "hostid":hostid,
                "type":7,
                "value_type":3,
                "interfaceid":interfaceid,
                "date_type":0,
                "delay":60,
                "history":7,
                "trends":90,
                "status":0
            }
        )
        return "item create success"
    else:
        return "item exists"
if __name__ == "__main__":
    result=create_item()
    print result


脚本思路:

在主机上创建item必须获取主机的hostid,interfaceid,而这两个值必须通过zabbix API提供的host.get方法获取到。然后定义一个函数get_hostinfo,该函数返回这两个hostid和interfaceid。再定义一个创建item的函数create_item,这个函数中先调用get_hostinfo函数,获取到hostid,interfaceid。先判断主机上item是存在,如果不存在,则执行create_item函数,来创建item。


本文出自 “zengestudy” 博客,请务必保留此出处http://zengestudy.blog.51cto.com/1702365/1836937

利用zabbix-api创建item

标签:通过zabbix api来创建item

原文地址:http://zengestudy.blog.51cto.com/1702365/1836937

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!