Pyzabbix
Pyzabbix is a third-party python wrapper for Zabbixapi. From the internet somehow got a source, after looking at the realization of the method is quite ingenious, feel very good to write down. Those source code itself is actually a separate operation of the script, you can use command line parameters to directly manipulate. PYZBX uses JSON to encode and decode request data and return data, and uses some of the methods in URLLIB2 to communicate
If you do not use its script directly, but customize the program itself, basically only use the Zabbixapi class:
from Import = Zabbixapi ("server") #server refers to the URL of the Zabbixweb interface, such as 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
You can then use the Zapi object to implement communication between the program and the ZABBIXAPI.
Official Document Address: Http://www.zabbix.com/documentation/2.4/manual/api
Zapi can be used mainly in the following ways:
Zapi.host.get, Zapi.host.create, Zapi.hostgroup.get, Zapi.host.update, ... Wait a minute. As you can see, the classification of these methods and APIs is consistent, this is the person who wrote this Pyzabbix module is neatly packaged おかげ, easy to use up a lot. In addition, most of these methods support String/list's dual-parameter format. This means that when you want to operate multiple times, but do not want to write a statement, you can directly pass a list in, it will automatically parse out for you.
How to use:
Zapi.hostgroup.get (filter={'groupid':'xxx'},output=[ 'name','groupid'],selecthosts=['name ','hostid'])
A statement like this. A method corresponding to the official API description of an operation, this correspondence is very understood, such as Hostgroup.get is to obtain the host group of information, Host.update is to update some information and other hosts. As for each method parameter, it is related to the request JSON string specified in the API operation corresponding to this method. Look at the correspondence of several request strings and method parameters there will be feelings = =. The field is the parameter name, and the field value is the parameter value.
For example, explain the above statement in detail, it means
I want to get some information about the host group.
This (some) host group GroupID is xxx (filter function, if not write filter, the system will default to all groups of information returned to you, of course, by specifying GroupID filter out of the group must be only one, but the returned JSON string is still a form of a list, Even if there is only one, it will be said later.)
What I'm going to get is the name and GroupID field of this group (the output function, output must be a list, can be empty, but at least will return groupid this field anyway. If you write [' Extend '], the information of all fields is returned)
In addition, I would like to get some information about the hosts in this host group, so I can use the selecthosts parameter, and the values in the list specify which fields of the hosts I want to know about.
The last JSON that comes back is probably this:
[ {" hosts": [ { "HostID": "10001", "name": "Host 1" }, { "HostID": "10002", " Name ":" Host 2 " } ], " GroupID ":" Ten ", " name ":" Main group 1 " }]
* Do not ask why the hosts are not written in output in a parameter = =. The API is designed so that even the JSON request string is written to separate the selecthosts and output by two fields.
このように, using this kind of packaged method to get the JSON string and then parsing the information I want from the JSON string is a common practice. Get basically is this, the other what create, update, the main is to combine the official given request string format and the available fields, and then think about my parameters how to write, test just fine. The overall Pyzabbix is not difficult to use, but the ZABBIXAPI itself some logic is different from the common sense, need to adapt to adapt.
Here are some of the things I need to be aware of during my use:
The argument does not exist, or the value of the argument is not valid (for example, add a testpara= "TestValue" in the above statement or write output as [' name ', ' GroupID ', ' TestItem ']), ZABBIXAPI will not error, but default ignore this parameter, this point is more pit, need to pay attention to.
The host has a property of status, which can be used in host.update to enable and disable a host through the API. However, it should be noted that the value of this status is U ' 0 ' or U ' 1 ', not int or STR, Unicode
Host.update when determining which host to update with is not the filter parameter (in fact, probably out of the Get method, the other is not the filter), but directly have a HostID parameter to specify a HostID, so as to determine a specific host. This is based on the fact that HostID is a host and unique, and can do so.
The host can add a macro by adding the macros parameter when create. For example macros=[{' macro ': ' {$INSTANCE} ', ' Value ': ' Frankid '},{' macro ': ' {$ACCOUNT} ', ' Value ': ' Test_account '}]
Not to be continued ...
"Python" ZABBIXAPI packaging Pyzabbix