-
Notifications
You must be signed in to change notification settings - Fork 0
/
zabbixdata_get.py
85 lines (77 loc) · 2.35 KB
/
zabbixdata_get.py
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
#!/usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = "ToySun"
__date__ = "2017-12-20"
#version:1.0
from elasticsearch import Elasticsearch
from elasticsearch import helpers
from zabbix_client import ZabbixServerProxy
import json,sys,time,copy
reload(sys)
sys.setdefaultencoding('utf-8')
wait_time = 60
class Zabbix():
def __init__(self):
self.zb = ZabbixServerProxy("http://192.168.10.143/zabbix")
self.zb.user.login(user="Admin",password="zabbix")#auth
def get_hosts(self):
host_data = {
"output": ["hostid", "name"]
}
host_ret = self.zb.host.get(**host_data)
return host_ret
def item_get(self):
for host in self.get_hosts():
key_dict = {}
item_data = {
"output":["itemids","key_"],
"hostids": host['hostid'],
}
item_ret = self.zb.item.get(**item_data)
for item in item_ret:
if self.history_get(item['itemid']) is None:
pass
else:
key_dict[item['key_']] = int(self.history_get(item['itemid']))
key_dict['hostid'] = int(host['hostid'])
key_dict['name'] = host['name']
key_dict['@timestamp'] = time.strftime('%Y-%m-%dT%H:%M:%S+08:00')
self.post_data(json.dumps(key_dict))
time.sleep(wait_time)
return ''
def history_get(self, itemid):
#history:Possible values: 0 - numeric float; 1 - character; 2 - log;
#3 - numeric unsigned; 4 - text. Default: 3
#limit:the number of value. Default/Must: 1
data = { "output": "extend",
"history": 3,
"itemids": itemid,
"sortfield": "clock",
"sortorder": "DESC",
"limit": 1
}
history_ret = self.zb.history.get(**data)
#由于history值为 3
if history_ret:
return history_ret[0]['value']
else:
pass
def post_data(self, info):
es = Elasticsearch("192.168.10.125")
esindex_prefix = "zabbix"
esindex = "%s-%s" % (esindex_prefix, time.strftime('%Y.%m.%d'))
values = []
info_deepcopy = copy.deepcopy(info)
values.append({
"_index": esindex,
"_type": 'test',
"_source": info_deepcopy
})
helpers.bulk(es, values)
print info
if __name__ == "__main__":
zabbix_start = Zabbix()
print "zabbix_post start!"
while 1:
zabbix_start.item_get()
#zabbix_start.item_get()