zabbix之所以能成为比较优秀的监控工具,个人觉得是因为两个特点:一个是数据的存放方式,方便做数据处理,容量规划,归档等,这个之前说过。另一个是其强大的api,利用其api可以方便的和其他的组件进行整合,比如cmdb可以通过zabbix api来实现监控的添加,更新和禁用等,zabbix的官方文档提供了比较详细的api列表。

 因为最近添加screen的需求比较多,就简单写了一个python脚本用来自动化添加screen,有兴趣的可以借鉴下。

使用方式如下:

python ./screen_host.py  -H 主机列表 -n screen名称 -G graph名称

具体的脚本:

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
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/env python
import  urllib2
import  sys
import  json
import  argparse
def  requestJason(url,values):
     data  =  json.dumps(values)
     req  =  urllib2.Request(url, data, { 'Content-Type' 'application/json-rpc' })
     response  =  urllib2.urlopen(req, data)
     data_get  =  response.read()
     output  =  json.loads(data_get)
     print  output
     try :
         message  =  output[ 'result' ]
     except :
         message  =  output[ 'error' ][ 'data' ]
         quit()
     print  json.dumps(message)  
     return  output
             
def  authenticate(url, username, password):
     values  =  { 'jsonrpc' '2.0' ,
               'method' 'user.login' ,
               'params' : {
                   'user' : username,
                   'password' : password
               },
               'id' '0'
               }
     idvalue  =  requestJason(url,values)
     return  idvalue[ 'result' ]
def  getHosts(hostname,url,auth):
     host_list  =  []
     values  =  { 'jsonrpc' '2.0' ,
               'method' 'host.get' ,
               'params' : {
                   'output' : [ "hostid" , "host" ],
                   'filter' : {
                       'host' : hostname
                   }
               },
               'auth' : auth,
               'id' '2'
               }
     output  =  requestJason(url,values)
     for  host  in  output[ 'result' ]:
         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: [selecttype, "name" ],
                 "output" : [ "graphid" , "name" ],
                 "hostids" : host_list,                    
                 "filter" :{ 'name' :name_list},
                 "sortfield" : "name" ,
             },
             "auth" : auth,
             "id" '3'
         })
     print  values
     output  =  requestJason(url,values)
     bb  =  sorted (output[ 'result' ],key  =  lambda  x:x[ 'hosts' ][ 0 ][ 'hostid' ])
     output[ 'result' =  bb
     graphs  =  []
     if  (graphtype  = =  0 ):
         for  in  output[ 'result' ]:
             print  i
             graphs.append(i[ 'graphid' ])
     if  (graphtype  = =  1 ):
         for  in  output[ 'result' ]:
             if  int (i[ 'value_type' ])  in  ( 0 3 ):
                 graphs.append(i[ 'itemid' ])
     graph_list  =  []
     =  0
     =  0
     print  graphs
     for  graph  in  graphs:
         print  "x is "  +  str (x)
         print  "y is "  +  str (y)
         graph_list.append({
             "resourcetype" : graphtype,
             "resourceid" : graph,
             "width" "500" ,
             "height" "100" ,
             "x" str (x),
             "y" str (y),
             "colspan" "0" ,
             "rowspan" "0" ,
             "elements" "0" ,
             "valign" "0" ,
             "halign" "0" ,
             "style" "0" ,
             "url" : "",
             "dynamic" str (dynamic)
         })
         + =  1
         print  type (x)
         print  type (columns)
         if  = =  int (columns):
             =  0
             + =  1
     print  graph_list
     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  in  graphids:
         values[ 'params' ][ 0 ][ 'screenitems' ].append(i)
     output  =  requestJason(url,values)
def  main():
     url  =  'http://xxxx/api_jsonrpc.php'
     username  =  'xxxx'
     password  =  'xxxx'
     auth  =  authenticate(url, username, password)
     host_list  =  getHosts(hostname,url,auth)
     print  host_list
     graph_ids  =  getGraphs(host_list,graphname, url, auth, columns)
     screenCreate(url, auth, screenname, graph_ids, columns)
if  __name__  = =  '__main__' :
     parser  =  argparse.ArgumentParser(description = 'Create Zabbix screen from all of a host Items or Graphs.' )
     parser.add_argument( '-G' , dest = 'graphname' , nargs = '+' ,metavar = ( 'grah name' ),
                         help = 'Zabbix Host Graph to create screen from' )
     parser.add_argument( '-H' , dest = 'hostname' , nargs = '+' ,metavar = ( '10.19.111.145' ),
                         help = 'Zabbix Host to create screen from' )
     parser.add_argument( '-n' , dest = 'screenname' type = str ,
                         help = 'Screen name in Zabbix.  Put quotes around it if you want spaces in the name.' )
     parser.add_argument( '-c' , dest = 'columns' type = int ,
                         help = 'number of columns in the screen' )
     args  =  parser.parse_args()
     print  args
     hostname  =  args.hostname
     screenname  =  args.screenname
     columns  =  args.columns
     graphname  =  args.graphname
     if  columns  is  None :
         columns  =  len (graphname)
     print  columns
     main()

本文转自菜菜光 51CTO博客,原文链接:http://blog.51cto.com/caiguangguang/1387401,如需转载请自行联系原作者