Call Zabbix API for bulk management of hosts and monitoring items

Source: Internet
Author: User

First affixed to the Zabbix official website and the address of the Chinese official website:

Https://www.zabbix.com/documentation/3.4/manual/api/reference/item/object

Https://www.zabbix.com/documentation/3.4/zh/manual/api any problem at any time can find the answer on the official website almost

 首先,我们想用利用zabbix的restful api来访问zabbix,肯定是需要登陆认证的。在zabbix的后续操作中,必须要有一个TOKEN,这也是官方介绍的:官方上实现的方法如下

{
"Jsonrpc": "2.0",
"Method": "User.login",
"Params": {
"User": "Admin",
"Password": "Zabbix"
},
"id": 1,
"Auth": null
}
Version of the JSON-RPC protocol used by JSONRPC-API; Zabbix API Implementation JSON-RPC version 2.0; (required)
Method-calls the API methods;
Params-parameters that will be passed to the API method;
ID-Any identifier of the request; This ID needs a special understanding, which I have not understood before. Here the ID is actually a flag, set how much does not matter, mainly to do the return flag with, that is, the number of settings here, the return data will also have a same ID used to flag this return corresponding to the request!
Auth-user authentication token; Because we haven't got one, it's set to null.

After understanding, we construct a request function----purpose is to obtain token
def Init():
Self.url = ' http://192.168.1.10/zabbix/api_jsonrpc.php '
Self.headers = {' Content-type ': ' Application/json '}
Auth = {
"Jsonrpc": "2.0",
"Method": "User.login",
"Params": {
"User": "admin", # # #验证
"Password": "Zabbix"
},
"id": 1,
"Auth": None,
}
Response = Requests.post (Self.url, Data=json.dumps (auth), headers=self.headers)
Authid = Json.loads (response.text) [' Result '] # # Auth ID is token

OK, we've got the token, and then we can do whatever we like!

The second step we get all the host list information (in fact, you can also go to the database to take-but if you go to the database to write database functions to enter account password, etc...) )

Def get_hosts ():
neirong={
"Jsonrpc": "2.0",
"Method": "Host.get",
"Params": {
"Output": [
"HostID",
"Host"
],
"Selectinterfaces": [
"InterfaceID",
"IP"
]
},
"id": 2,
"Auth": "AAA"
}
Response = Requests.post (Self.url, Data=json.dumps (Neirong), headers=self.headers)
Print (Response.text)

Note that the INTERFACSE here is important because you will need this thing for later operations on the host item (such as adding item). I've been stuck here for a long time, thinking this isn't important. The official website is as follows:

Step three: Create a host item
We're on the machine. Create a host item and we'll create a previous step we've got to the host's HostID 10255 InterfaceID 3 These two are added item necessary for creating an instance of the item official website:

{
"Jsonrpc": "2.0",
"Method": "Item.create",
"Params": {
"Name": "Free disk space on $",
"Key_": "Vfs.fs.size[/home/joe/,free]",
"HostID": "10084",
"Type": 0,
"Value_type": 3,
"InterfaceID": "1",
"Delay": 30
},
"Auth": "0424bd59b807674191e7d77572075f33",
"ID": 3
}
OK, let's change it a little bit:

DEF itemCreate ():
Neirong ={
"Jsonrpc": "2.0",
"Method": "Item.create",
"Params": {
"Name": "Free disk space on $",
"Key
": "vfs.fs.size[/boot,pused]",
"HostID": "10255",
"Type": 0,
"Value_type": 0,
"InterfaceID": "3",
"Delay": 5
},
"Auth": Authid, (note that this autid is a pit one will explain)
"ID": 3
}
Response1 = Requests.post (Self.url, Data=json.dumps (Neirong), headers=self.headers)
Print (Response1.text)
Print ("OK")

            这里面的type  value_type的意思可以去官网详细看看 我这里设置的是0 0  也就是代表 zabbix_agent float  相信有zabbix基础的同学是秒懂

Let's take a look at the graphical interface of Zabbix:

OK, to this end, the graph does not come out to see the script below:

def graf_create (self, Authid):
Neirong = {
"Jsonrpc": "2.0",
"Method": "Graph.create",
"Params": {
"Name": "Test1",
"width": 900,
"Height": 200,
"Gitems": [
{
"Itemid": "28257",
"Color": "00aa00"
}
]
},
"Auth": Authid,
"ID": 4
}

    response1 = requests.post(self.url, data=json.dumps(neirong), headers=self.headers)    print(response1)    print(response1.text)    print("OK")

Let's talk about that authid problem.

Call this function

Call Zabbix API for bulk management of hosts and monitoring items

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.