(ii) Python calls the Zabbix API from getting started to discarding--log in and get an authentication token

Source: Internet
Author: User

The URL to access the Zabbix API is:

http://x.x.x.x/zabbix/api_jsonrpc.php

x.x.x.x may be your IP or domain name


Overview of the Access process:

1, first Login

2. Zabbix server returns a token after successful authentication

3, with this token to access a variety of data, do a variety of operations

4, Complete!


First, log in with Restclient


In the body of the JSON request, you have the following properties:

    • jsonrpc-Version of the JSON-RPC protocol used by the API; Zabbix API Implementation JSON-RPC version 2.0;

    • method-called API method;

    • params-parameters that will be passed to the API method;

    • id-Any identifier of the request;

    • auth-user authentication token; Because we haven't got one, it's set to null.


After the credentials are correctly supplied, the response returned by the API will contain the user authentication token (in JSON format):

{

"Jsonrpc": "2.0",

"Result": "140f4524c02e2731dd74c48d29aa5ce8", #这个就是token

"id": 1

}


Ii. sign in with Python

# -*- coding:utf-8 -*-import urllib2import jsonurl =  ' Http://x.x.x.x/zabbix /api_jsonrpc.php ' header = {' content-type ':  ' Application/json '}req = json.dumps (     {         "Jsonrpc":  "2.0",          "Method":  "User.login",         " Params ": {            " user ": " Admin ",              "Password":  "Your password"          },         "id": 0,     }) Def auth ():     r = urllib2. Request (url=url, headers=header, data=req)     response =  Urllib2.urlopen (R)     token = json.loAds (Response.read ())     print (token) if __name__ ==  ' __main__ ':     auth ()

The resulting response:

(ii) Python calls the Zabbix API from getting started to discarding--log in and get an authentication token

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.