首页 文章 精选 留言 我的

精选列表

搜索[Zabbix],共555篇文章
优秀的个人博客,低调大师

利用zabbix api 带入 组ID 和 监控项目键值 获取所有主机的值

1 2 3 def chaxun(groupid,items_value):是自己写的,带入组 ID 和监控项目键值,返回主机和值的结果。 做了判断,如果这个主机没有想要的监控项目,就去除掉。 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 173 174 175 #!/usr/bin/envpython #coding=utf-8 import urllib.request import json import re url = 'http://XXXXXXXXXXXXXXXX/api_jsonrpc.php' username = 'Admin' password = 'XXXXX' #登陆 def requestJson(url,values): data = json.dumps(values).encode( 'utf-8' ) req = urllib.request.Request(url,data,{ 'Content-Type' : 'application/json-rpc' }) response = urllib.request.urlopen(req,data) a = response.read().decode(encoding = 'utf-8' ) output = json.loads(a) try : message = output[ 'result' ] except : message = output[ 'error' ][ 'data' ] print (message) quit() return output[ 'result' ] ##登陆的API def authenticate(url,username,password): values = { 'jsonrpc' : '2.0' , 'method' : 'user.login' , 'params' :{ 'user' :username, 'password' :password }, 'id' : '0' } idvalue = requestJson(url,values) return idvalue auth = authenticate(url,username,password) ##查询组ID{'groupid':'8','name':'Switch'} def groups(auth): values = { "jsonrpc" : "2.0" , "method" : "hostgroup.get" , "params" :{ "output" :[ "groupid" , "name" ], }, 'auth' :auth, 'id' : '1' } output = requestJson(url,values) return output ##查询主机 def hosts(auth,a): values = { "jsonrpc" : "2.0" , "method" : "host.get" , "params" :{ "output" :[ "groupid" , "name" ], "groupids" :a, }, 'auth' :auth, 'id' : '1' } output = requestJson(url,values) return output ##查询主机项目 def items(auth,a): values = { "jsonrpc" : "2.0" , "method" : "item.get" , "params" :{ "output" :[ "itemids" , "key_" ], "hostids" :a, }, 'auth' :auth, 'id' : '1' } output = requestJson(url,values) return output ##查询项目的历史值'lastvalue':'-14760.0000' def his(auth,itemids): values = { "jsonrpc" : "2.0" , "method" : "item.get" , "params" :{ "output" : "extend" , "history" : 0 , "itemids" :itemids, "sortfield" : "itemid" , "sortorder" : "DESC" , "limit" : 1 }, 'auth' :auth, 'id' : '1' } output = requestJson(url,values) return output ##查询触发项目值和监控项目 def trigger(auth): values = { "jsonrpc" : "2.0" , "method" : "trigger.get" , "params" :{ "output" :[ "description" , ], "filter" :{ "hostid" : "10108" , }, "selectItems" :"", "sortfield" : "hostname" , "sortorder" : "DESC" }, 'auth' :auth, 'id' : '1' } output = requestJson(url,values) return output def chaxun(groupid,items_value): host = hosts(auth,groupid) ##获取所有主机 host_name = [] host_id = [] for i in host: host_id.append(i[ 'hostid' ]) host_name.append(i[ 'name' ]) hostall = dict ( zip (host_id,host_name)) ##所有主机'10118':'云主机:10.8.8.65','10119':'云主机:10.8.8.66', host_names = [] host_ids = [] for i in host_id: ##筛选,有监控项目的符合要求的主机ID,和主机名字 host_id_value = items(auth,i) ##[{'itemid':'29272','key_':'BiosDate'},{'itemid':'29272','key_':'BiosDate'}] for index,value in enumerate (host_id_value): for k,v in value.items(): if items_value in v: ##items_value host_ids.append(host_id_value[index][ 'itemid' ]) host_names.append(i) host_names_list = [] for i in host_names: ##用符合要求的主机获取它的名字 for k,v in hostall.items(): if i in k: host_names_list.append(v) history_value = [] for i in host_ids: ##查询主机ID的最新历史值 history = his(auth,i) history_value.append(history[ 0 ][ 'lastvalue' ]) host_value = dict ( zip (host_names_list,history_value)) return host_value #返回主机和值的对应关系 ret = chaxun( 5 , "ProcThreadCount.[2]" ) #组ID和想要查询的监控项目 print (ret) 本文转自 295631788 51CTO博客,原文链接:http://blog.51cto.com/hequan/1955480,如需转载请自行联系原作者

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

WebStorm

WebStorm

WebStorm 是jetbrains公司旗下一款JavaScript 开发工具。目前已经被广大中国JS开发者誉为“Web前端开发神器”、“最强大的HTML5编辑器”、“最智能的JavaScript IDE”等。与IntelliJ IDEA同源,继承了IntelliJ IDEA强大的JS部分的功能。

用户登录
用户注册