• zabbix api


    # coding=utf8
    # Autor : Always V
    # Time  : 2018/9/13 15:56
    # File  : zabbix_api.py
    # Software PyCharm
    
    '''
     env: python3.6 zabbix3.4.11
     zabbix api文档地址:https://www.zabbix.com/documentation/3.4/
    '''
    import json
    from urllib import request
    
    
    url = "http://xx.xx.xx.xx/zabbix/api_jsonrpc.php"
    header = {"Content-Type":"application/json"}
    
    def get_sumbit_data(src_method, src_params, src_no, is_or_not_auth=""):
        '''
        :param src_method: 需要调用的接口
        :param src_params: 需要填的参数
        :param src_no: id号
        :param is_or_not_auth:权限id
        :return: 返回json数据
        '''
        if not is_or_not_auth:
            data = json.dumps(
                {
                    "jsonrpc": "2.0",
                    "method": src_method,
                    "params": src_params,
                    "id": src_no ,
                }).encode('utf-8')
        else:
            data = json.dumps(
                {
                    "jsonrpc": "2.0",
                    "method": src_method,
                    "params": src_params,
                    "id": src_no ,
                    "auth": is_or_not_auth
                }).encode('utf-8')
        req = request.Request(url, headers=header, data=data)
        try:
            ret_urlopen = request.urlopen(req)
            return  req, ret_urlopen
        except Exception as e:
            print("Auth Failed, Please Check Your Name And Password:", e)
    
    class ZabbixApi(object):
        def __init__(self):
            params = {"user": "xxxx", "password": "xx@xxx"}
            self.req, self.result = get_sumbit_data("user.login", params, 0)
    
        def _get_auth_id(self):
            response = self.result.read().decode('utf-8')
            page = json.loads(response)
            self.result.close()
            self.auth_id = format(page.get('result'))
            print("Auth Successful. The Auth ID Is: {}".format(page.get('result')))
    
        def _get_hostsgroup_id(self):
            params = {"output": ["groupid", "name"],}
            self.req, self.result = get_sumbit_data("hostgroup.get", params, 1,self.auth_id)
            response = self.result.read().decode('utf-8')
            page = json.loads(response)
            self.result.close()
            for hostsgroup in page.get('result'):
                print("----host_groups----",hostsgroup)
    
        def _get_hosts_id(self):
            #groupids的值是上面获取的主机组id
            params = {"output": ["hostid", "name"],"groupids": "2",}
            self.req, self.result = get_sumbit_data("host.get", params, 0,self.auth_id)
            response = self.result.read().decode('utf-8')
            page = json.loads(response)
            self.result.close()
            for  hostid in page.get("result"):
                print("_get_hosts_id Successful. The _get_hosts_id ID Is: ",hostid)
    
        def _get_items_id(self):
            #hostids指的是host的id
            params = {"output": ["itemids", "key_", "history"], "hostids": "10255",}
            self.req, self.result = get_sumbit_data("item.get", params, 1, self.auth_id)
            response = self.result.read().decode('utf-8')
            page = json.loads(response)
            self.result.close()
            for itemsid in page.get('result'):
                print("----=-=-=-s=-----",itemsid)
    
        def _get_history_monitor_data(self):
            #history参数需要一一对应,具体含义:
            # 0 - numeric float;数字浮点数
            # 1 - character;字符
            # 2 - log; 日志
            # 3 - numeric unsigned; 数字符号
            # 4 - text.文本
            params = {"output": "extend","history": 1,"itemids": "28312","limit": 10}
            self.req, self.result = get_sumbit_data("item.get", params, 1, self.auth_id)
            response = self.result.read()
            page = response.decode('utf-8')
            page = json.loads(page)
            self.result.close()
            for data in page.get("result"):
                print("----=-=-=-=-----",data)
                
    zapi = ZabbixApi()
    zapi._get_auth_id()
    zapi._get_hostsgroup_id()
    zapi._get_hosts_id()
    zapi._get_items_id()
    zapi._get_history_monitor_data()
  • 相关阅读:
    解决 Windows 下的 :所选择的任务“{0}”不再存在。若要查看当前任务,请单击“刷新”。
    学习编译更好的 DAO 的技巧
    宇宙的起源演讲全文(斯蒂芬·霍金)
    java對象序列化的兩種使用方法
    Ubuntu硬盘安装与配置(3D效果)
    java異常處理
    [zt]JDBC对数据库的事务操作
    [zt]spring本地事务与JTA事务实现解析
    Debian溫習
    在oracle中增大session數量
  • 原文地址:https://www.cnblogs.com/st12345/p/9644811.html
Copyright © 2020-2023  润新知