Zabbix API学习_认证篇

 

zabbix很多都是可以直接通过 zabbix api 来进行辅助操作。

官方文档地址:https://www.zabbix.com/documentation

 

通过API操作,都必须组认证,拿到了认证token之后,才能进行相应操作:

 

#-*- coding:utf-8 -*-
'''
Created on 2016-6-8

@author: 
'''

import json
import urllib2
from urllib2 import URLError

class Zabbix_Auth:
    
    def __init__(self):
        self.url = 'http://zabbix.balabala.com
        self.header = {"Content-Type":"application/json"}


    def user_login(self):
        data = json.dumps({
                           "jsonrpc": "2.0",
                           "method": "user.authenticate",
                           "params": {
                                      "user": "admin",
                                      "password": "admin"
                                      },
                           "id": 0
                           })

        request = urllib2.Request(self.url, data)
        for key in self.header:
            request.add_header(key, self.header[key])

        try:
            result = urllib2.urlopen(request)
        except URLError as e:
            print "\033[041m authenticate is failed, please check it !\033[0m", e.code
        else:
            response = json.loads(result.read())
            result.close()
            self.authID = response['result']
            return self.authID
        

  

其他地方调用,应用这个类,然后调用对应的方法即可。

 

posted @ 2016-07-13 16:35  schangech  阅读(1260)  评论(0编辑  收藏  举报