zabbix 监控 ElasticSearch

Posted 三木燕

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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://localhost:9200/_cluster/stats\',
    \'nodes\'  : \'http://localhost:9200/_nodes/stats\',
    \'indices\': \'http://localhost:9200/_stats\',
    \'health\' : \'http://localhost:9200/_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

  

 

以上是关于zabbix 监控 ElasticSearch的主要内容,如果未能解决你的问题,请参考以下文章

ZABBIX4.0.2监控历史数据存放Elasticsearch及集群高可用方案

ELK与ZABBIX对日志信息内容监控

ELK+zabbix监控tomcat报警及错误日志--报警

Zabbix与ELK整合实现对日志数据的实时监控

elasticsearch-运维-文件缓存

基于ELK+Beats进行系统监控