利用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/env python
#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)
     =  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  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  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  in    host_names:   ## 用符合要求的主机  获取它的名字
         for  k, v  in  hostall.items():
             if  in  k:
                 host_names_list.append(v)
 
 
     history_value  =  []
     for  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,如需转载请自行联系原作者
目录
相关文章
|
1月前
|
监控 供应链 数据库连接
电商API:销量监控与竞品分析利器
电商数据接口API在现代电商运营中至关重要,可实现品牌价格、销量、评论等数据监控,优化销售策略。接入主流平台如淘宝、天猫、京东等API,或使用RPA技术取数,保障数据安全与效率。通过数据库连接、ERP直连等方式整合分析数据,监控竞品与价格,掌握市场动态。同时,注重数据安全性、技术支持及成本效益,助力企业在竞争中脱颖而出,提升业务效率与竞争力。
72 0
|
5月前
|
JSON 前端开发 API
以项目登录接口为例-大前端之开发postman请求接口带token的请求测试-前端开发必学之一-如果要学会联调接口而不是纯写静态前端页面-这个是必学-本文以优雅草蜻蜓Q系统API为实践来演示我们如何带token请求接口-优雅草卓伊凡
以项目登录接口为例-大前端之开发postman请求接口带token的请求测试-前端开发必学之一-如果要学会联调接口而不是纯写静态前端页面-这个是必学-本文以优雅草蜻蜓Q系统API为实践来演示我们如何带token请求接口-优雅草卓伊凡
175 5
以项目登录接口为例-大前端之开发postman请求接口带token的请求测试-前端开发必学之一-如果要学会联调接口而不是纯写静态前端页面-这个是必学-本文以优雅草蜻蜓Q系统API为实践来演示我们如何带token请求接口-优雅草卓伊凡
|
4月前
|
人工智能 Java API
ai-api-union项目,适配各AI厂商api
本项目旨在实现兼容各大模型厂商API的流式对话和同步对话接口,现已支持智谱、豆包、通义、通义版DeepSeek。项目地址:[https://gitee.com/alpbeta/ai-api-union](https://gitee.com/alpbeta/ai-api-union)。通过`ChatController`类暴露两个接口,入参为`ChatRequest`,包含会话ID、大模型标识符和聊天消息列表。流式对话返回`Flux<String>`,同步调用返回`String`
164 2
|
5月前
|
自然语言处理 API 开发者
DeepSeek-Free-API:DeepSeekV3免费的api接口,需要使用api方式的同学可以参考一下这个项目,可以收藏起来试一下
嗨,大家好,我是小华同学。今天为大家介绍一个开源项目——DeepSeek V3 Free 服务。该项目基于 DeepSeek-V3 R1 大模型,提供免费、高性能的 API,支持高速流式输出、多轮对话、联网搜索和深度思考等功能。适用于智能客服、内容创作、教育辅助等场景。部署方式灵活,支持 Docker、Docker-compose、Render、Vercel 和原生部署。欢迎关注我们,获取更多优质开源项目和高效工作学习方法。
1581 15
|
7月前
|
JSON 监控 API
使用Zabbix API
使用Zabbix API
423 67
|
6月前
|
存储 搜索推荐 API
拼多多根据ID取商品详情原数据API接口的开发、运用与收益
拼多多作为中国电商市场的重要参与者,通过开放平台提供了丰富的API接口,其中根据ID取商品详情原数据的API接口尤为重要。该接口允许开发者通过编程方式获取商品的详细信息,为电商数据分析、竞品分析、价格监测、商品推荐等多个领域带来了丰富的应用场景和显著的收益。
256 10
|
8月前
|
Prometheus 监控 Java
深入探索:自制Agent监控API接口耗时实践
在微服务架构中,监控API接口的调用耗时对于性能优化至关重要。通过监控接口耗时,我们可以识别性能瓶颈,优化服务响应速度。本文将分享如何自己动手实现一个Agent来统计API接口的调用耗时,提供一种实用的技术解决方案。
260 3
|
8月前
|
监控 数据可视化 Java
深入探索:自制Agent监控API接口耗时
在微服务架构中,监控API接口的调用耗时对于性能优化至关重要。通过监控这些指标,我们可以识别瓶颈,优化系统性能。本文将分享如何自己动手实现一个Agent来统计API接口的调用耗时,提供一种有效的监控解决方案。
178 2
|
7月前
|
搜索推荐 数据挖掘 API
拼多多根据ID取商品详情原数据API接口的开发应用与收益
拼多多作为中国知名电商平台,为开发者和企业提供丰富的API接口,助力快速接入平台,实现商品推广、订单管理等功能。其中,根据ID取商品详情原数据的API接口尤为重要,具备高效性、稳定性和安全性,广泛应用于电商数据分析、价格监测、竞品分析、商品推荐系统、移动应用开发及精准营销等领域,为企业带来显著收益。
209 0

推荐镜像

更多