python-zabbix

安装模块

pip3 install urllib_kerberos
pip3 install requests
pip3 install pyyaml

common

目录结构
在这里插入图片描述

common.py

#!/usr/bin/env python3
import yaml
import json
import requests
from requests.packages import urllib3
urllib3.disable_warnings()

class Common(object):
    def __new__(cls, *args, **kwargs):
        if not hasattr(cls, '_instance'):
            cls._instance = object.__new__(cls, *args, **kwargs)
        return cls._instance

    def __init__(self):
        with open('config.yml') as conf_file:
            zconf = yaml.load(conf_file, Loader=yaml.BaseLoader)
            self.url = "%s/zabbix/api_jsonrpc.php" % zconf['server']
            self.user = zconf['user']
            self.password = zconf['password']

    def get_token(self):
        """
        :return: token id
        """

        request_body = {
            "jsonrpc": "2.0",
            "method": "user.login",
            "params": {
                "user": self.user,
                "password": self.password
            },
            "id": 1,
            "auth": None
        }
        token = requests.post(self.url, json=request_body, verify=False).json()['result']
        return token

    def zabbix_get(self, method, params_json):
        """
        :param method: str
        :param params_json: json
        :return: json
        """

        token = self.get_token()
        request_body = {
            "jsonrpc": "2.0",
            "method": method,
            "params": params_json,
            "id": 1,
            "auth": token
        }
        result = requests.get(self.url, json=request_body, verify=False)
        return result

def main():
    if __name__ == '__main__':
        zbx = Common()
        get_temp_body = {
            "output": "extend",
            "filter": {
                "host": ["Ceph PROC"]
            }
        }
        result = zbx.zabbix_get('template.get', get_temp_body)
        print(json.dumps(result.json(), indent=4))
main()

config.yml

---
user: Admin
password: xxxxx
server: http://x.x.x.x

pyzabbix

在这里插入图片描述

pyzabbix/common.py

#!/usr/bin/env python3
import yaml
import json
import requests
from requests.packages import urllib3
urllib3.disable_warnings()

class Common(object):
    def __new__(cls, *args, **kwargs):
        if not hasattr(cls, '_instance'):
            cls._instance = object.__new__(cls, *args, **kwargs)
        return cls._instance

    def __init__(self):
        with open('config.yml') as conf_file:
            zconf = yaml.load(conf_file, Loader=yaml.BaseLoader)
            self.url = "%s/zabbix/api_jsonrpc.php" % zconf['server']
            self.user = zconf['user']
            self.password = zconf['password']

    def get_token(self):
        """
        :return: token id
        """

        request_body = {
            "jsonrpc": "2.0",
            "method": "user.login",
            "params": {
                "user": self.user,
                "password": self.password
            },
            "id": 1,
            "auth": None
        }
        token = requests.post(self.url, json=request_body, verify=False).json()['result']
        return token

    def zabbix_get(self, method, params_json):
        """
        :param method: str
        :param params_json: json
        :return: json
        """

        token = self.get_token()
        request_body = {
            "jsonrpc": "2.0",
            "method": method,
            "params": params_json,
            "id": 1,
            "auth": token
        }
        result = requests.get(self.url, json=request_body, verify=False)
        return result

def main():
    if __name__ == '__main__':
        zbx = Common()
        get_temp_body = {
        "filter": {
            "host": [
                "ceph-mgr"
            ]
        }
        }
        result = zbx.zabbix_get('host.get', get_temp_body)
        print(json.dumps(result.json(), indent=4))
main()

host.py

#!/usr/bin/env python3

import sys
import json
import pyzabbix
from pyzabbix import common

class Zabbix(object):

    def __init__(self):
        pass

    def get_host(self):
        zbx=common.Common()
        get_body= {
            "filter": {
                "host": [
                    "ceph-mgr"
                ]
            }
        }
        result=zbx.zabbix_get('host.get',get_body).json()['result']
        return result


def get():
    zabbix=Zabbix()
    print(zabbix.get_host())


if __name__ == '__main__':
    get()

config.yml

---
user: xxxxxx
password: xxxxxx
server: http://xxxxxx
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wuxingge

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值