zoukankan      html  css  js  c++  java
  • zabbix api

      1 #!/usr/bin/env python
      2 # -*- coding:utf8 -*-
      3 # __author__ = '北方姆Q'
      4 
      5 from pyzabbix import ZabbixAPI
      6 from plugins.duia.singleton import Singleton
      7 
      8 
      9 class ZabbixFactory(Singleton):
     10     def __init__(self, url, username='Admin', password='zabbix'):
     11         self.obj = ZabbixAPI(url)
     12         self.obj.login(username, password)
     13 
     14     def get_hosts(self):
     15         """
     16         获取所有的主机信息列表
     17         :return: dict in list
     18         """
     19         return self.obj.host.get(output='extend')
     20 
     21     def get_host(self, name):
     22         """
     23         获取单台的主机信息
     24         :param name:  需要获取的显示名
     25         :return: dict
     26         """
     27         return self.obj.host.get(output='extend', filter={'name': name})[0]
     28 
     29     def get_host_groups(self):
     30         """
     31         获取所有的主机组信息列表
     32         :return: dict in list
     33         """
     34         return self.obj.hostgroup.get(output='extend')
     35 
     36     def get_hosts_by_group(self, group_name):
     37         """
     38         获取某个主机组内所有主机信息列表
     39         :param group_name: 主机组名
     40         :return: dict in list
     41         """
     42         return self.obj.host.get(groupids=self.__get_host_group_id(group_name), output="extend")
     43 
     44     def get_templates(self):
     45         """
     46         获取所有模版信息
     47         :return: dict in list
     48         """
     49         return self.obj.template.get(output="extend")
     50 
     51     def get_template(self, template_name):
     52         """
     53         根据名称获取单个模版信息
     54         :return: dict
     55         """
     56         return self.obj.template.get(output='extend', filter={"host": template_name})[0]
     57 
     58     def create_group(self, group_name):
     59         """
     60         创建一个新主机组
     61         :param group_name: 新主机组名
     62         :return: 新组id,str
     63         """
     64         ret = self.obj.hostgroup.create(name=group_name)
     65         return ret['groupids'][0]
     66 
     67     def create_host(self, name, ip, groups_name, templates_name, proxy_name=None, status=0):
     68         """
     69         创建一台新主机
     70         :param name: 主机名
     71         :param ip: 主机受监控ip
     72         :param groups_name: 主机所在组,str、list、tuple、set
     73         :param templates_name: 主机关联模版,str、list、tuple、set
     74         :param proxy_name: 使用的代理名称,默认不使用
     75         :param status: 是否启用,默认开启
     76         :return: 新主机id,str
     77         """
     78         groups = [{'groupid': self.__get_host_group_id(groups_name)}] if isinstance(groups_name, str) 
     79             else [{'groupid': self.__get_host_group_id(group_name)} for group_name in groups_name]
     80         templates = [{'templateid': self.__get_template_id(templates_name)}] if isinstance(templates_name, str) 
     81             else [{'templateid': self.__get_template_id(template_name)} for template_name in templates_name]
     82 
     83         if proxy_name is None:
     84             ret = self.obj.host.create(
     85                 host=name,
     86                 status=status,
     87                 groups=groups,
     88                 templates=templates,
     89                 interfaces=[{
     90                     'type': 1,              # 监控类型
     91                     'main': 1,              # 监控类型下第几行
     92                     'useip': 1,             # 使用ip还是dns
     93                     'ip': ip,               # ip框
     94                     'dns': '',              # dns框
     95                     'port': '10050'         # port框
     96                 }]
     97             )
     98         else:
     99             ret = self.obj.host.create(
    100                 host=name,
    101                 status=status,
    102                 groups=groups,
    103                 templates=templates,
    104                 interfaces=[{
    105                     'type': 1,
    106                     'main': 1,
    107                     'useip': 1,
    108                     'ip': ip,
    109                     'dns': '',
    110                     'port': '10050'
    111                 }],
    112                 proxy_hostid=self.__get_proxy_id(proxy_name)
    113             )
    114         return ret['hostids'][0]
    115 
    116     def __get_host_group_id(self, group_name):
    117         """
    118         获取某主机组id
    119         :param group_name: 主机组名
    120         :return: int
    121         """
    122         return int(self.obj.hostgroup.get(output='groupid', filter={'name': group_name.split(',')})[0].get('groupid'))
    123 
    124     def __get_template_id(self, template_name):
    125         """
    126         获取某模版id
    127         :param template_name: 模版名
    128         :return: int
    129         """
    130         return int(self.obj.template.get(output='templateid', filter={'host': template_name.split(',')})[0].get('templateid'))
    131 
    132     def __get_proxy_id(self, proxy_name):
    133         """
    134         获取某代理id
    135         :param proxy_name: 代理名
    136         :return: int
    137         """
    138         return int(self.obj.proxy.get(output='proxyid', filter={'host': proxy_name.split(',')})[0].get('proxyid'))
  • 相关阅读:
    Winform Timer用法,Invoke在Timer的事件中更新控件状态
    日期字符串格式转换
    Windows服务的创建、安装、卸载
    Oracle更新表字段时内容中含有特殊字符&的解决方法
    MVC无刷新查询,PagedList分页控件使用,导出Excel
    网页抓取解析,使用JQuery选择器进行网页解析
    asp.net webform 自定义分页控件
    if __name__ == "__main__": tf.app.run()
    Python 虚拟机
    paper reading----Xception: Deep Learning with Depthwise Separable Convolutions
  • 原文地址:https://www.cnblogs.com/bfmq/p/7852622.html
Copyright © 2011-2022 走看看