Python访问Zabbix api

1、python访问zabbix api

from urllib import parse, request
import json, requests

class Zabbix_api(object):
    def __init__(self, ip):
        self.url = 'http://%s/zabbix/api_jsonrpc.php' % ip
        self.headers = {'User-Agent': 'Python Chrome', "Content-Type": "application/json-rpc"}
        self.auth_data = {
            "jsonrpc": "2.0",
            "method": "user.login",
            "params": {
                "user": "admin",
                "password": "zabbix"
            },
            "id": 1,
            "auth": None
        }
        self.token = requests.post(url=self.url, data=json.dumps(self.auth_data).encode(encoding='utf-8'),
                                   headers=self.headers).json()['result']

    def result(self, data):
        data['auth'] = self.token
        res = requests.post(url=self.url, data=json.dumps(data).encode(encoding='utf-8'), headers=self.headers).json()
        # req = request.Request(url=self.url, data=json.dumps(data).encode(encoding='utf-8'), headers=self.headers)
        # res = request.urlopen(req)
        # res=json.loads(res.read())
        return res['result']


if __name__ == '__main__':
    data = {
        "jsonrpc": "2.0",
        "method": "host.get",
        "params": {
            # 'output': ['hostid', 'host', 'name', 'templateid', ],
            'output': 'extend',
            'search': {
                "name": 'BJ_Sw*'
            },
            'searchWildcardsEnabled': True,
        },
        "auth": "038e1d7b1735c6a5436ee9eae095879e",
        "id": 1
    }
    host_get = Zabbix_api('x.x.x.x')
    res = host_get.result(data)
    print(res)

2、ajax调用zabbix的api示例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/static/plugins/bootstrap-3.3.7/css/bootstrap.css">
    <script src="/static/js/jquery-3.2.1.js"></script>
    <script src="/static/plugins/bootstrap-3.3.7/js/bootstrap.js"></script>
    <script src="/static/js/echarts.min.js"></script>
</head>
<body>
<input type="button" class="btn btn-primary" value="view_zabbix" id="zabbix">
</body>
<script>
    $("#zabbix").click(function () {
        var par = {
            "jsonrpc": "2.0",
            "method": "user.login",
            "params": {
                "user": "admin",
                "password": "zabbix"
            },
            "id": 1,
            "auth": null
        };
        $.ajax({
            url: "http://ip/api_jsonrpc.php",
            type: "POST",
            headers: {
                "Content-Type": "application/json"
            },
            dataType: "json",
            data: JSON.stringify(par),     //这里必须将对象转成string类型
            success: function (data) {
                console.log(data)
            }
        })
    })
</script>

 

转载于:https://www.cnblogs.com/xwupiaomiao/p/11158164.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值