zabbix创建screen脚本

简介:

    下面是创建screen的脚本,可以把一个主机的所有graphs放在一个screen里查看。

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
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/env python
import  urllib2
import  json
import  argparse
  
  
def  authenticate(url, username, password):
     values  =  { 'jsonrpc' '2.0' ,
               'method' 'user.login' ,
               'params' : {
                   'user' : username,
                   'password' : password
               },
               'id' '0'
               }
  
     data  =  json.dumps(values)
     req  =  urllib2.Request(url, data, { 'Content-Type' 'application/json-rpc' })
     response  =  urllib2.urlopen(req, data)
     output  =  json.loads(response.read())
  
     try :
         message  =  output[ 'result' ]
     except :
         message  =  output[ 'error' ][ 'data' ]
         print  message
         quit()
  
     return  output[ 'result' ]
  
  
def  getGraph(hostname, url, auth, graphtype, dynamic, columns):
     if  (graphtype  = =  0 ):
         selecttype  =  [ 'graphid' ]
         select  =  'selectGraphs'
     if  (graphtype  = =  1 ):
         selecttype  =  [ 'itemid' 'value_type' ]
         select  =  'selectItems'
  
     values  =  { 'jsonrpc' '2.0' ,
               'method' 'host.get' ,
               'params' : {
                   select: selecttype,
                   'output' : [ 'hostid' 'host' ],
                   'searchByAny' 1 ,
                   'filter' : {
                       'host' : hostname
                   }
               },
               'auth' : auth,
               'id' '2'
               }
  
     data  =  json.dumps(values)
     req  =  urllib2.Request(url, data, { 'Content-Type' 'application/json-rpc' })
     response  =  urllib2.urlopen(req, data)
     host_get  =  response.read()
  
     output  =  json.loads(host_get)
     # print json.dumps(output)
  
     graphs  =  []
     if  (graphtype  = =  0 ):
         for  in  output[ 'result' ][ 0 ][ 'graphs' ]:
             graphs.append(i[ 'graphid' ])
  
     if  (graphtype  = =  1 ):
         for  in  output[ 'result' ][ 0 ][ 'items' ]:
             if  int (i[ 'value_type' ])  in  ( 0 3 ):
                 graphs.append(i[ 'itemid' ])
  
     graph_list  =  []
     =  0
     =  0
  
     for  graph  in  graphs:
         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
         if  = =  columns:
             =  0
             + =  1
  
     return  graph_list
  
  
def  screenCreate(url, auth, screen_name, graphids, columns):
     # print graphids
     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)
  
     data  =  json.dumps(values)
     req  =  urllib2.Request(url, data, { 'Content-Type' 'application/json-rpc' })
     response  =  urllib2.urlopen(req, data)
     host_get  =  response.read()
  
     output  =  json.loads(host_get)
  
     try :
         message  =  output[ 'result' ]
     except :
         message  =  output[ 'error' ][ 'data' ]
  
     print  json.dumps(message)
  
  
def  main():
     # 修改下面三行
     url  =  'http://<zabbix url>/zabbix/api_jsonrpc.php'
     username  =  "Your API Users Username"
     password  =  "Your API Users Username"
  
     parser  =  argparse.ArgumentParser(description = 'Create Zabbix screen from all of a host Items or Graphs.' )
     parser.add_argument( 'hostname' , metavar = 'H' type = str ,
                         help = 'Zabbix Host to create screen from' )
     parser.add_argument( 'screenname' , metavar = 'N' 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 , default = 3 ,
                         help = 'number of columns in the screen (default: 3)' )
     parser.add_argument( '-d' , dest = 'dynamic' , action = 'store_true' ,
                         help = 'enable for dynamic screen items (default: disabled)' )
     parser.add_argument( '-t' , dest = 'screentype' , action = 'store_true' ,
                         help = 'set to 1 if you want item simple graphs created (default: 0, regular graphs)' )
  
     args  =  parser.parse_args()
     hostname  =  args.hostname
     screen_name  =  args.screenname
     columns  =  args.columns
     dynamic  =  ( 1  if  args.dynamic  else  0 )
     screentype  =  ( 1  if  args.screentype  else  0 )
  
     auth  =  authenticate(url, username, password)
     graphids  =  getGraph(hostname, url, auth, screentype, dynamic, columns)
  
     print  "Screen Name: "  +  screen_name
     print  "Total Number of Graphs: "  +  str ( len (graphids))
  
     screenCreate(url, auth, screen_name, graphids, columns)
  
if  __name__  = =  '__main__' :
     main()

    脚本使用方法:

wKiom1UKQlOT0WhWAAI79jqD5kg662.jpg

    效果图:

wKioL1UKQsHTEECGAAalER_ze3c800.jpg


    脚本官方地址:https://www.zabbix.org/wiki/Python_script_to_create_Screen_from_all_Items/Graphs_of_a_host



本文转自 张斌_青岛 51CTO博客,原文链接:http://blog.51cto.com/qicheng0211/1622152

相关文章
|
7月前
|
监控 Ubuntu Shell
用shell脚本部署zabbix
用shell脚本部署zabbix
129 1
|
9月前
|
监控 网络协议 Shell
使用脚本安装zabbix agent 6.0(新手友好)
使用脚本安装zabbix agent 6.0(新手友好)
|
9月前
|
监控 Shell
项目采坑日志——zabbix agent执行脚本,提示 Permission denied
项目采坑日志——zabbix agent执行脚本,提示 Permission denied
338 0
|
监控 数据安全/隐私保护
zabbix 5.0 一键部署脚本
zabbix 5.0 一键部署脚本
382 0
|
运维 监控
一键部署zabbix监控平台脚本
应一位网友的要求,让我帮忙写个关于一键部署zabbix监控平台的脚本。这个脚本的思路,与我前面写的一个脚本是很相似的,所以用下班时间匆忙写了一个,算是能跑起来吧。
149 0
一键部署zabbix监控平台脚本
|
运维 监控 Java
shell脚本配合zabbix实现tomcat的故障自愈
shell脚本配合zabbix实现tomcat的故障自愈 1.背景及实现方式方法 Tomcat运行JAVA类的程序代码经常会导致内存溢出,往往都是收到告警后再去处理,收到报警再处理就会拖延故障解决的时间,因此就需要靠故障自愈的机制来解决人为干预的成本。
330 0
shell脚本配合zabbix实现tomcat的故障自愈
|
监控 网络协议 Shell
shell脚本实现zabbix监控信息收集
zabbix信息收集 一般zabbix信息收集都是写成脚本 思路:将监控的信息写成函数,使用位置参数$1来调用函数即可,一般都是zabbix来传参
464 0
|
监控 关系型数据库 MySQL
linux下安装zabbix服务器shell脚本-添加主机-邮件监控报警zabbix-自动化运维
分布式网络监控平台;分为手动和自动安装,添加主机-邮件监控报警zabbix
926 0
|
Python
ZABBIX告警脚本——python发送邮件
自定义Zabbix告警,首先需要编写告警脚本,告警脚本需要支持3个参数: 1.告警接收人 2.主题 3.内容
371 0
ZABBIX告警脚本——python发送邮件
|
监控 关系型数据库 MySQL
zabbix server 一键安装脚本
!/usr/bin/python import os import socket def sto(): os.system('sed -i "s/SELINUX=enabled/SELINUX=disabled/g" /etc/selinux/config') os.
907 0

推荐镜像

更多