zabbix 监控 ElasticSearch

ElasticSearch  可以直接使用zabbix官方的模板

模板地址:

https://github.com/mkhpalm/elastizabbix

 

通过zabbix server 直接监控

1、下载模板文件导入模板

2、把模板关联到zabbix server

3、添加自定义KEY

 

vim /etc/zabbix/scripts/elastizabbix.py
#!/usr/bin/python
import os
import sys
import json
import urllib2
import time
import errno

ttl = 60

stats = {
'cluster': 'http://elasticsearch.xxx.cn:19200/_cluster/stats',
'nodes' : 'http://elasticsearch.xxx.cn:19200/_nodes/stats',
'indices': 'http://elasticsearch.xxx.cn:19200/_stats',
'health' : 'http://elasticsearch.xxx.cn:19200/_cluster/health'
}

def created_file(name):
try:
fd = os.open(name, os.O_WRONLY | os.O_CREAT | os.O_EXCL)
os.close(fd)
return True
except OSError, e:
if e.errno == errno.EEXIST:
return False
raise

def is_older_then(name, ttl):
age = time.time() - os.path.getmtime(name)
return age > ttl

def get_cache(api):
cache = '/tmp/elastizabbix-{0}.json'.format(api)
lock = '/tmp/elastizabbix-{0}.lock'.format(api)
should_update = (not os.path.exists(cache)) or is_older_then(cache, ttl)
if should_update and created_file(lock):
try:
d = urllib2.urlopen(stats[api]).read()
with open(cache, 'w') as f: f.write(d)
except Exception as e:
pass
if os.path.exists(lock):
os.remove(lock)
if os.path.exists(lock) and is_older_then(lock, 300):
os.remove(lock)
ret_data = {}
try:
with open(cache) as data_file:
ret_data = json.load(data_file)
except Exception as e:
ret_data = json.loads(urllib2.urlopen(stats[api]).read())
return ret_data

def get_stat(api, stat):
d = get_cache(api)
keys = []
for i in stat.split('.'):
keys.append(i)
key = '.'.join(keys)
if key in d:
d = d.get(key)
keys = []
return d

def discover_nodes():
d = {'data': []}
for k,v in get_stat('nodes', 'nodes').iteritems():
d['data'].append({'{#NAME}': v['name'], '{#NODE}': k})
return json.dumps(d)

def discover_indices():
d = {'data': []}
for k,v in get_stat('indices', 'indices').iteritems():
d['data'].append({'{#NAME}': k})
return json.dumps(d)


if __name__ == '__main__':
api = sys.argv[1]
stat = sys.argv[2]
if api == 'discover':
if stat == 'nodes':
print discover_nodes()
if stat == 'indices':
print discover_indices()

else:
stat = get_stat(api, stat)
if isinstance(stat, dict):
print ''
else:
print stat

 

vim /etc/zabbix/zabbix_agentd.d/es.conf

UserParameter=elastizabbix[*],/etc/zabbix/scripts/elastizabbix.py $1 $2

  

 

转载于:https://www.cnblogs.com/37yan/p/7095853.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值