Python and Zabbix APIs to implement and modify the function of monitor host

Source: Internet
Author: User
Tags auth json in python

Zabbix API can be very convenient management monitoring items, powerful, very suitable for two times development. The main function of this article is that Django Asset Management update can sync to Zabbix monitoring platform, unified platform function, ensure data consistency.

Zabbix API

The Host.create,host.delete,host.update,host.get and Hostinterface.update methods are mainly used in the Zabbix API. The following has been create for example to do the simple use instructions.

The code is as follows Copy Code
{
"Jsonrpc": "2.0",
"Method": "Host.create",
"Params": {
"Host": "Linux Server",
"Interfaces": [
{
' Type ': 1,
"Main": 1,
"Useip": 1,
"IP": "192.168.3.1",
"DNS": "",
"Port": "10050"
}
],
"Groups": [
{
"GroupID": "50"
}
],
"Templates": [
{
"TemplateID": "20045"
}
],
"Inventory": {
"Macaddress_a": "01234",
"Macaddress_b": "56768"
}
},
"Auth": "038e1d7b1735c6a5436ee9eae095879e",
"id": 1
}

Parameters

JSONRPC: Default 2.0, no modification

Method: Specify the methods of the API

Params: the requested parameter. Where host is the hostname, the name that is displayed on the Zabbix page. Interfaces is the real information of the host, including type, IP address and port, etc. Groups the group to join when the host is added, this must be specified. Templates represents the Zabbix template ID to use when adding a host, optionally, the recommended addition of a common template ID number. Inventory Asset Management, this I did not add.

Auth: The SessionID number after landing

ID: The ID number of the operation, and this particular effect is not studied in depth. What number is the request ID, and what number is displayed when returned.

After the success of the creation, the following contents are returned:

The code is as follows Copy Code
{
"Jsonrpc": "2.0",
"Result": {
"Hostids": [
"107819"
]
},
"id": 1
}

Python calls

After understanding the Zabbix API, it is simpler to implement in Python, input parameters, initiate a request, and return the result.

User Login exit code is similar to the following:

  code is as follows copy code
def login ():
url = settings. zabbix_url+ "/api_jsonrpc.php"
data = Json.dumps (
{
"Jsonrpc": "2.0",
"Method": "User.login",
"Params": {
"User": Settings. Zabbix_username,
"Password": Settings. Zabbix_password
},
"id": 0
})
result = Requesturl (URL, data)
return result

def logout (SessionID):
url = settings. zabbix_url+ "/api_jsonrpc.php"
Data=json.dumps (
{
"Jsonrpc": "2.0",
"Method": "User.logout",
"Params": [],
"id": 1,
"Auth": SessionID
}
)
Return Requesturl (URL, data)

The code to create the host is similar to the following:

  code is as follows copy code
def genhost ( Sessionid,ip,groupids):
 url = settings. zabbix_url+ "/api_jsonrpc.php"
 groups_list=[]
 for groupid in groupids:
 groups_ List.append ({"GroupID": GroupID})
 params_dict={
  "host": IP,
  "Interfaces": [
 {
  "type": 1,
  "main": 1,
  "Useip": 1,
  "IP": IP,
  "DNS": "",
  "Port" ":" 10050 "
 }
 ],
 " groups ": Groups_list,
 " Templates ": [
 {
 ] TemplateID ": 10114
 
 ],
 
 data=json.dumps (
 {
 " JSONRPC ": "2.0",
  "method": "Host.create",
  "params": params_dict,
  "auth": SessionID,
  "id ": 3
 }"
 return requesturl (URL, data)

When the IP update is more cumbersome to write, you need to get HostID and Hostinterfaceid separately, and then invoke the Host.update and Hostinterfa.update methods respectively.

Related Article

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.