34233 发表于 2016-2-1 10:16:27

使用zabbix api创建screen

zabbix的screen功能可以把graph聚合起来,统一进行展示,
我们的需求是把同一个主机组的同一个item聚合起来,比如一个screen显示同一个组的所有主机的内存使用率,达到类似ganglia的效果,
由于服务器较多,所以我们调用zabbix api来进行创建。


脚本如下:适用于v2.2.11

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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import json
import urllib2
import argparse

def requestjson(url, values):
    data = json.dumps(values)
    req = urllib2.Request(url, data, {'Content-Type': 'application/json-rpc'})
    res = urllib2.urlopen(req, data)
    output = json.loads(res.read())

    return output

def authenticate(url, username, password):
    values = {'jsonrpc': '2.0',
            'method': 'user.login',
            'params': {
                  'user': username,
                  'password': password
            },
            'id': '0'
    }
    output = requestjson(url, values)

    return output['result']

def gethosts(groupname, url, auth):
    host_list = []
    values = {'jsonrpc': '2.0',
            'method': 'hostgroup.get',
            'params': {
                  'output': 'extend',
                  'filter': {'name': groupname},
                  'selectHosts': ['hostid']
            },
            'auth': auth,
            'id': '2'
    }
    output = requestjson(url, values)
    for host in output['result']['hosts']:
      host_list.append(host['hostid'])

    return host_list

def getgraphs(host_list, name_list, url, auth, columns, graphtype=0, dynamic=0):
    if (graphtype == 0):
      selecttype = ['graphid']
      select = 'selectGraphs'
    if (graphtype == 1):
      selecttype = ['itemid', 'value_type']
      select = 'selectItems'

    values = ({'jsonrpc': '2.0',
               'method': 'graph.get',
               'params': {
                   select: ,
                   'output': ['graphid', 'name'],
                   'hostids': host_list,
                   'filter': {'name': name_list},
                   'sortfield': 'name'
               },
               'auth': auth,
               'id': '3'
               })

    output = requestjson(url, values)
    bb = sorted(output['result'])

    graphs = []
    if (graphtype == 0):
      for i in bb:
            graphs.append(i['graphid'])
    if (graphtype == 1):
      for i in bb:
            if int(i['value_type']) in (0, 3):
                graphs.append(i['itemid'])

    graph_list = []
    x = 0
    y = 0
    for graph in graphs:
      graph_list.append({
            'resourcetype': graphtype,
            'resourceid': graph,
            'width': '300',
            'height': '100',
            'x': str(x),
            'y': str(y),
            'colspan': '0',
            'rowspan': '0',
            'elements': '0',
            'valign': '0',
            'halign': '0',
            'style': '0',
            'url': '',
            'dynamic': str(dynamic)
      })
      x += 1
      if x == int(columns):
            x = 0
            y += 1

    return graph_list

def screencreate(url, auth, screen_name, graphids, columns):
    columns = int(columns)
    if len(graphids) % columns == 0:
      vsize = len(graphids) / columns
    else:
      vsize = (len(graphids) / columns) + 1

    values = {'jsonrpc': '2.0',
            'method': 'screen.create',
            'params': [{
                  'name': screen_name,
                  'hsize': columns,
                  'vsize': vsize,
                  'screenitems': []
            }],
            'auth': auth,
            'id': 2
            }
    for i in graphids:
      values['params']['screenitems'].append(i)
    output = requestjson(url, values)

def main():
    url = 'http://10.0.0.141/zabbix/api_jsonrpc.php'
    username = 'admin'
    password = '__Q&(0sH1zNAdD'
    auth = authenticate(url, username, password)
    host_list = gethosts(groupname, url, auth)
    graph_ids = getgraphs(host_list, graphname, url, auth, columns)
    screencreate(url, auth, screen_name, graph_ids, columns)

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('-g', dest='groupname', nargs='+', metavar='groupname', type=str, help='which group you want to select')
    parser.add_argument('-G', dest='graphname', nargs='+', metavar='graphname', type=str, help='which graph you want to select')
    parser.add_argument('-c', dest='columns', metavar='columns', type=int, help='the screen columns')
    parser.add_argument('-n', dest='screen_name', metavar='screen_name', type=str, help='the screen name')
    args = parser.parse_args()

    groupname = args.groupname
    graphname = args.graphname
    columns = args.columns
    screen_name = args.screen_name

    main()





使用方法:
python create_screen.py -g servers -G 'Network traffic on em1' -c 4 -n 'servers Network traffic on em1'
页: [1]
查看完整版本: 使用zabbix api创建screen