Creating host macros with the Zabbix API

Source: Internet
Author: User

#!/usr/bin/python#coding:utf-8from zabbix_api import zabbixapiimport jsonserver= "/http 172.16.206.130/zabbix "Username=" Admin "password=" Zabbix "Zapi = zabbixapi (server=server,path=" ", Log_ level=0) Zapi.login (Username,password) info=[{"name":  "ECS0001",  "IP":  "172.16.206.128",  " Bandwidthout ": " 1M ", " internet_ip ": " ", " Team ": " Tank ", " id ": " I-23zmvm8uz " },{"name":  "ECS0007", "IP":  "172.16.206.130",  "Bandwidthout":  "1M",  "internet_ip":   "120.54.6.78",  "Team":  "Tesla",  "id":  "i-23s02ihno"}]macro_list = []for  i in info:    visible_name = i[' name ']     band = i[' Bandwidthout ']#    print visible_name,band     if band !=  "0M": # #计算出网络带宽宏的值          network _THRESHOLD = STR (int(Int (Band.strip (' M ')) *0.8*1000))  +  ' K ' #        print network_threshold# #通过主机可见名查找hostid            hostinfo=zapi.host.get ({"Output": "Extend", " Filter ": {" name ": Visible_name}})          hostid=hostinfo[0][' HostID ']# #通过hostid获取主机的所有宏          host_macro_get= Zapi.usermacro.get ({"Output": "Extend", "Hostids": HostID})           If len (Host_macro_get)  ==0:             Host_macro_create=zapi.usermacro.create ({"HostID": HostID, "macro": "{$NETWORK _threshold}", "Value": Network_ Threshold})             print  "host %s  has no macro,but create a new macro " % visible_name          else:              for X in host_macro_get:                  macro = x[' Macro ']                  MACRO_LIST.APPEND (macro)               if  "{$NETWORK _threshold}"  not in  macro_list:# #通过hostid创建主机宏                        host_macro_create=zapi.usermacro.create ({"HostID": HostID , "macro": "{$NETWORK _threshold}", "Value": Network_threshold})                   print  "host  $s  add a macro"  % visible_name             else:# #获取主机上 {$NETWORK _THRESHOLD} macro value                    for y in host_macro_get:                     if y[' macro '] ==  ' {$NETWORK _threshold} ':                          current_network_threshold_value = y[' value ']                          macro_id = y[' hostmacroid ']                         if network_ Threshold == current_network_threshold_value:                             print  "the  Current macro value on host %s is latest " %visible_name                          else:# #更新NETWORK_THRESHOLD宏的值                               host_macro_update=zapi.usermacro.update ({"hostmacroid": macro_id, "value": Network_threshold})                               print  "macro value on host %s  Updated " %visible_name    else:         print  "The host %s  has no internet_ip " % visible_name


Requirements: Add public network bandwidth monitoring for all cloud hosts, but not every host has a public IP, the bandwidth of the host with public IP is not exactly the same, for example, some are 1M, some 5M, bandwidth monitoring threshold is the bandwidth value *0.8. For example, 1M bandwidth, the monitoring threshold is 0.8M. (Note: After testing, in the Zabbix trigger this is 0.8M, will not issue an alarm, but the threshold is greater than 1M of the decimal can be alarm, suspect is less than 1M, Zabbix automatically change units to K caused. Then simply define the bandwidth monitoring thresholds by unifying the units to K, and be sure to be integers. 1300K is possible, but 1300.5K does not alarm. )


Scripting Ideas: Monitoring thresholds Add host macros to each host, not manually, but add macros to the host through the Zabbix API.

The specific process is as follows:

Through the CMDB interface to get all the host information, including Zabbix host visible name and bandwidth information, make a judgment, the public network bandwidth of 0M, do not add host macros. When not 0M, first determine whether the host is stored on the macro, if it does not exist, create this macro, if present, first compare whether the value of the macro and the bandwidth value calculated by the alarm threshold is equal, if equal, indicating that the bandwidth has not been updated, do not do processing. If they are not equal, the bandwidth is adjusted and the bandwidth threshold on the host macro needs to be updated.



This article from "Zengestudy" blog, declined reprint!

Creating host macros with the Zabbix API

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.