写文章

利用zabbix api进行管理

2018-11-29 08:35:05

3636 | 0 | 3

zabbix 现在之所以那么流行,个人感觉跟zabbix 强大的API有一个很大的关系,利用API可以帮我们完成很多事情:

    1、获取相关组,主机信息。

    2、比如有人喜欢获取历史数据重新出图。

    3、添加删除主机,绑定删除模板。

    4、添加删除维护周期


这里我使用pipy提供的zabbix_client模块来进行,这样就不用我们自己去写登录函数,只要在模块开始指定用户名密码即可:


1、安装zabbix_client:

 pip install zabbxi_client


2、API管理脚本,替换成自己用户名密码即可:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#coding:utf-8
import time
from zabbix_client import ZabbixServerProxy
class Zabbix():
    def __init__(self):
        self.zb = ZabbixServerProxy("http://192.168.10.100/zabbix")
        self.zb.user.login(user="Admin", password="zabbix")
     ############## 查询组所有组获取组id ###############
    def get_hostgroup(self):
        data = {
           "output":['groupid','name']
         }
        ret = self.zb.hostgroup.get(**data)
        return ret 
   
     ########### 通过组id获取相关组内的所有主机 ###############
1
2
3
4
5
6
7
8
9
10
11
12
13
    def get_hostid(self,groupids=2):
        data = {
        "output": ["hostid""name"],
        "groupids": groupids
        }
        ret = self.zb.host.get(**data)
        return ret
     ########## 通过获取的hostid查找相关监控想itemid ###################
    def item_get(self, hostids="10115"):
        data = {
       "output":["itemids","key_"],
       "hostids": hostids,
        }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
        ret = self.zb.item.get(**data)
        return ret
  ######### 通过itemid(传入itemid和i0表示flast类型)获取相关监控项的历史数据 ###########
    def history_get(self, itemid, i ,limit=10):
        data = "output""extend",
          "history": i,
          "itemids": [itemid], 
          "limit": limit 
          }
        ret = self.zb.history.get(**data)
        return ret
   
   ###############添加主机并且指定到组(传入主机名,IP地址和组ID)#####################
    def add_zabbix_host(self,hostname="test_zabbix",ip="192.168.10.100",groupid="2"):
        data = {
         "host": hostname,
         "interfaces": [
            {
                "type"1,
                "main"1,
                "useip"1,
                "ip": ip,
                "dns": "",
                "port""10050"
            }
         ],
         "groups": [
             {
                "groupid": groupid
             }
         ]
        }
        ret = self.zb.host.create(data)
        return ret
    #####################查看现有模板#######################
    def get_template(self):
        datalist = []
        datadict={}
        data = {
            "output":["templateid","name"]
        }
        ret =  self.zb.template.get(data)
        for in ret:
            datadict[i['name']] = i['templateid']
            datalist.append(datadict)
        return datalist 
     
     #################### 关联主机到模板##################################
    def link_template(self, hostid=10156, templateids=10001):
        data = {
            "hostid":hostid,
             "templates":templateids
        }      
   
        ret = self.zb.host.update(data)
        return ret
    
    ###################  添加维护周期,,######################################
  
    def create_maintenance(self,name="test",hostids=10156,time=2):
        data =  {
            "name": name,
            "active_since"1458142800,
            "active_till"1489678800,
            "hostids": [
                hostids
            ],
            "timeperiods": [
                {
                    "timeperiod_type"0,
                    "period"3600
                }
            ]
        }
        ret = self.zb.maintenance.create(data)
        self.host_status(101301)
        return ret
    ################获取维护周期,,#########################
    def get_maintenance(self):
        data = {
            "output""extend",
            "selectGroups""extend",
            "selectTimeperiods""extend"
        }
        ret = self.zb.maintenance.get(data)
        return ret
    ##############获取维护周期之后,通过传入maintenanceid删除维护周期###########
    def del_maintenance(self,maintenanceids):
        return self.zb.maintenance.delete(maintenanceids) 
    #########################添加维护周期时候需要吧zabbix_host设置成非监控状态##################
    def host_status(self, hostid, status):
        data = {
            "hostid":hostid,
            "status":status
        }
        return self.zb.host.update(data)
    ###########通过hostids删除主机id,顺带也删除模板#########
     
    def host_del(self,hostids=10155):
        return self.zb.host.delete(hostids)
if __name__ == "__main__":
    zabbix_server = Zabbix()
    #print zabbix_server.get_hostgroup()
    #print zabbix_server.get_hostid()
    #print zabbix_server.item_get(10156)   
    #data = zabbix_server.history_get("24889",0) 
    #print zabbix_server.get_hostgroup()
    #print zabbix_server.add_zabbix_host()
    #data = zabbix_server.get_template()
    #print data[0]['Template OS Linux']
    #print zabbix_server.link_template()
    #print zabbix_server.create_maintenance()
   # print zabbix_server.host_del(10155)
    #print zabbix_server.get_maintenance()
    print zabbix_server.del_maintenance(15)

3

收藏
分享