Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Packaging pyzabbix of zabbixAPI

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

Pyzabbix is a third-party python packaging for zabbixAPI. I inexplicably got a copy of the source code from the Internet. After looking at it, I found that the implementation method was quite ingenious. I felt good and wrote it down. The source code itself is actually a separate script that can be operated directly with command-line arguments. Pyzbx uses json to encode and decode request data and return data, and uses some methods in urllib2 to communicate

If you do not use its script directly, but customize the relevant program yourself, you will basically only use the class ZabbixAPI:

From pyzabbix import ZabbixAPIzapi = ZabbixAPI ("server") # server refers to the url of the zabbixweb interface, for example, http://192.168.1.101/zabbixzapi.login("username","password") # refers to the user name and password in the zabbix system, not the user name and password of the server itself

Then you can use zapi as an object to communicate between the program and zabbixAPI.

Official document address: http://www.zabbix.com/documentation/2.4/manual/api

The main methods that can be used by zapi:

Zapi.host.get, zapi.host.create, zapi.hostgroup.get, zapi.host.update,. Wait. As you can see, these methods are consistent with the classification of API, which is cleverly packaged by the person who wrote the pyzabbix module, which is much more convenient to use. In addition, most of these methods support the dual parameter format of string/list. That is, when you want to operate many times, but do not want to write a sentence, you can directly send a list in, it will automatically parse it for you.

How to use ■:

Zapi.hostgroup.get (filter= {'groupid':'xxx'}, output= [' name','groupid'], selectHosts= ['name','hostid'])

Sentences like this. One method corresponds to an operation in the official API specification, which is easy to understand. For example, hostgroup.get is to obtain the information of the host group, host.update is to update some information of the host, and so on. As for the parameters of each method, it is related to the request json string specified in the API operation corresponding to this method. If you look at the correspondence between several request strings and method parameters, you will have a sense of =. The field is the parameter name, and the field value is the parameter value.

For example, explain the above sentence in detail, which means

I need to get some information about the host group.

The groupid of this host group is xxx (the function of filter. If you do not write filter, the system will return all the group information to you by default. Of course, there must be only one group filtered by specifying groupid, but the returned json string is still in the form of a list, even if there is only one item, which will be discussed later.)

What I want to get is the name and groupid fields of this group (the function of output, output must be a list, can be empty, but at least the groupid field will be returned anyway. If you write ['extend'], you will return the information of all fields)

In addition, I also need to get some information about the hosts in this host group, so I can use the parameter selectHosts, and the values in the list specify which fields I want to know about these hosts.

The final json returned might look like this:

[{"hosts": [{"hostid": "10001", "name": "Host 1"}, {"hostid": "10002", "name": "Host 2"}] "groupid": "10", "name": "host group 1"}]

* Don't ask why hosts is not a parameter written in output. This is how its API is designed. Even if you write a json request string, you have to separate selectHosts and output into two fields.

It is common practice to use this packaged method to get the json string and then parse the information I want from the json string. Get is basically like this, other create, update, mainly to combine the official request string format and available fields, and then think about how to write my parameters, just test it. Generally speaking, pyzabbix is not difficult to use, but zabbixAPI itself has some logic that is different from common sense and needs to adapt.

Here are some things I need to pay attention to when using ■:

If the parameter of ● itself does not exist, or the value of the parameter is illegal (such as adding testpara= "testvalue" in the above statement or writing output as ['name','groupid','testitem']), zabbixAPI will not report an error, but ignore this parameter by default.

One property of the ● host is status, which can be used in host.update to implement operations through api enable and disable a host. But it should be noted that the value of this status is upright 0' or upright 1percent, not int or str, but unicode.

● host.update does not use the filter parameter when determining which host to update (in fact, it is possible that filter is not used except for the get method). Instead, there is a hostid parameter to specify a hostid to determine a specific host. This is based on the fact that hostid is innate and unique to the host and can be done.

● host can add macros with the macros parameter when create. For example, macros= [{'macro':' {$INSTANCE}', 'value':'frankid'}, {' macro':' {$ACCOUNT}', 'value':'test_account'}]

● to be continued.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report