ElasticSearch 可以直接使用zabbix官方的模板
模板地址:
通过zabbix server 直接监控
1、下载模板文件导入模板
2、把模板关联到zabbix server
3、添加自定义KEY
vim /etc/zabbix/scripts/elastizabbix.py#!/usr/bin/pythonimport osimport sysimport jsonimport urllib2import timeimport errnottl = 60stats = {'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 Trueexcept OSError, e:if e.errno == errno.EEXIST:return Falseraisedef is_older_then(name, ttl):age = time.time() - os.path.getmtime(name)return age > ttldef 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:passif 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_datadef 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 ddef 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.confUserParameter=elastizabbix[*],/etc/zabbix/scripts/elastizabbix.py $1 $2