#!/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。
时间: 2024-11-08 11:17:55