利用ZABBIX的RPC-JSON作API扩展应用示例

计划将ZABBIX的一些状态可以在另一个应用的显示GRAPH及链接。

故而在网上找了几个文档,作了一个测试。

https://www.zabbix.com/documentation/2.4/manual/api/reference/graph/get

http://www.xue163.com/334/1654/3340800.html

 

复制代码
#!/usr/bin/env python
#coding: utf-8

import requests
import json


class zabbixtools:


    def __init__(self):
        self.url = "http://10.1.1.1/api_jsonrpc.php"
        self.headers = {"Content-Type": "application/json"}
        self.authID = self.user_login()

        
    def user_login(self):
        data = {
            "jsonrpc": "2.0",
            "method": "user.login",
            "params": {
                "user": "guest",
                "password": "xxxxxxxx"
                },
            "id": 0
            }
        try:
            response = requests.post(
                self.url, data=json.dumps(data), headers=self.headers).json()
        except requests.RequestException as e:
            print(e)
        else:
            authID = response['result']
            return authID


    def host_get(self,hostname):
        data = {
            "jsonrpc": "2.0",
            "method": "host.get",
            "params": {
                "output": "extend",
                "filter": {
                    "host": [hostname,]
                    }
                },
            "auth": self.authID,
            "id": 1
            }
        try:
            response = requests.post(
                self.url, data=json.dumps(data), headers=self.headers).json()
        except requests.RequestException as e:
            print(e)
        else:
            hostID = response['result'][0]['hostid']
            return hostID

    def graph_get(self,hostid):
        data = {
            "jsonrpc": "2.0",
            "method": "graph.get",
            "params": {
                "output": "extend",
                "hostids": hostid,
                "sortfield": "name"
                },
            "auth": self.authID,
            "id": 2
            }
        try:
            response = requests.post(
                self.url, data=json.dumps(data), headers=self.headers).json()
        except requests.RequestException as e:
            print(e)
        else:
            graphID = []
            
            for item in response['result']:
                graph_dict = {}

                graph_dict[item['graphid']] = item['name']
                graphID.append(graph_dict)
            #print graphID
            return graphID
        
        

        

def main():
    test = zabbixtools()
    hostID = test.host_get("cnsz032955")
    graphID = test.graph_get(hostID)
    for graph_item in graphID:
        for key in graph_item:
            print graph_item[key],": http://10.1.1.1/charts.php?hostid={hostID}&graphid={graphID}".format(hostID=hostID, graphID=key)

if __name__ == "__main__":
    main()
复制代码

载图:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值