elasticsearch

This commit is contained in:
netkiller
2016-11-28 16:59:26 +08:00
parent 281dcbd766
commit 48d0df0aca
4 changed files with 4317 additions and 0 deletions

13
elasticsearch/README.md Normal file
View File

@@ -0,0 +1,13 @@
# Elasticsearch for Zabbix 3.2
## install python3
# yum install -y python34
# chmod +x /srv/zabbix/libexec/elasticsearch
# /srv/zabbix/libexec/elasticsearch indices _all.total.flush.total_time_in_millis
25557

109
elasticsearch/elasticsearch Normal file
View File

@@ -0,0 +1,109 @@
#!/usr/bin/env python3
##################################################
# AUTHOR: Neo <netkiller@msn.com>
# WEBSITE: http://www.netkiller.cn
# Descriptionzabbix 通过 status 模块监控 nginx
# NoteZabbix 3.2
# DateTime: 2016-11-22
##################################################
try:
import time, json
import os, sys, errno
import logging, logging.handlers
#import configparser
from optparse import OptionParser, OptionGroup
#from urllib import
import urllib.request
except ImportError as err:
print("Error: %s" %(err))
class Elasticsearch():
stats = {
'cluster': 'http://localhost:9200/_cluster/stats',
'nodes' : 'http://localhost:9200/_nodes/stats',
'indices': 'http://localhost:9200/_stats',
'health' : 'http://localhost:9200/_cluster/health'
}
def __init__(self):
self.ttl = 60
pass
def lock(self,name):
try:
fd = os.open(name, os.O_WRONLY | os.O_CREAT | os.O_EXCL)
os.close(fd)
return True
except OSError as e:
if e.errno == errno.EEXIST:
return False
raise
def expire(self,key, ttl):
if not os.path.exists(key):
return True
elif (time.time() - os.path.getmtime(key)) > ttl:
return True
else:
return False
#return self.lock(key+".lock")
def open(self, module):
text=""
json_obj = None
cache = '/tmp/elastizabbix-{0}.json'.format(module)
if self.expire(cache, self.ttl):
text = urllib.request.urlopen(self.stats[module]).read().decode("utf-8")
with open(cache, 'w') as f: f.write(text)
json_obj = json.loads(text)
else:
json_obj = json.load(open(cache))
return json_obj
def get(self, module, keyworld):
json_obj = self.open(module);
keys = []
for i in keyworld.split('.'):
keys.append(i)
key = '.'.join(keys)
if key in json_obj:
json_obj = json_obj.get(key)
keys = []
return json_obj
def discover(self,module):
d= {'data': []}
if module == 'nodes':
for k,v in self.get('nodes', 'nodes').items():
d['data'].append({'{#NAME}': v['name'], '{#NODE}': k})
if module == "indices":
for k,v in self.get('indices', 'indices').items():
d['data'].append({'{#NAME}': k})
return json.dumps(d)
def main(self):
parser = OptionParser(usage='usage: %prog <module> <keyword>', version="%prog 1.0.0", description='Elasticsearch for Zabbix')
(options, args) = parser.parse_args()
if not len(args) == 2:
parser.print_help()
sys.exit(1)
module = args[0]
keyword = args[1]
if module in self.stats.keys():
print(self.get(module, keyword))
elif module == "discover":
print(self.discover(keyword))
else:
parser.print_help()
if __name__ == '__main__':
try:
elastic = Elasticsearch()
elastic.main()
except KeyboardInterrupt:
print ("Crtl+C Pressed. Shutting down.")

View File

@@ -0,0 +1,11 @@
############################################################
# Elasticsearch - statistics
#
# Author: Neo Chen <netkiller@msn.com>
# Website: http://www.netkiller.cn
############################################################
# Discovery
# Return statistics
UserParameter=elasticsearch.status[*],/srv/zabbix/libexec/elasticsearch $1 $2

File diff suppressed because it is too large Load Diff