• Elasticsearch集群状态脚本及grafana监控面板导出的json文件


    脚本文件:

    #!/usr/bin/env python
    import datetime
    import time
    import urllib
    import json
    import urllib2
    import os
    import sys
    
    # ElasticSearch Cluster to Monitor
    elasticServer = os.environ.get('ES_METRICS_CLUSTER_URL', 'http://10.80.2.83:9200')
    interval = 60
    
    # ElasticSearch Cluster to Send Metrics
    elasticIndex = os.environ.get('ES_METRICS_INDEX_NAME', 'elasticsearch_metrics')
    elasticMonitoringCluster = os.environ.get('ES_METRICS_MONITORING_CLUSTER_URL', 'http://10.80.2.83:9200')
    
    
    def fetch_clusterhealth():
        utc_datetime = datetime.datetime.utcnow()
        endpoint = "/_cluster/health"
        urlData = elasticServer + endpoint
        response = urllib.urlopen(urlData)
        jsonData = json.loads(response.read())
        clusterName = jsonData['cluster_name']
        jsonData['@timestamp'] = str(utc_datetime.strftime('%Y-%m-%dT%H:%M:%S.%f')[:-3])
        post_data(jsonData)
        return clusterName
    
    
    def fetch_clusterstats():
        utc_datetime = datetime.datetime.utcnow()
        endpoint = "/_cluster/stats"
        urlData = elasticServer + endpoint
        response = urllib.urlopen(urlData)
        jsonData = json.loads(response.read())
        jsonData['@timestamp'] = str(utc_datetime.strftime('%Y-%m-%dT%H:%M:%S.%f')[:-3])
        post_data(jsonData)
    
    
    def fetch_nodestats(clusterName):
        utc_datetime = datetime.datetime.utcnow()
        endpoint = "/_cat/nodes?v&h=n"
        urlData = elasticServer + endpoint
        response = urllib.urlopen(urlData)
        nodes = response.read()[1:-1].strip().split('
    ')
        for node in nodes:
            endpoint = "/_nodes/%s/stats" % node.rstrip()
            urlData = elasticServer + endpoint
            response = urllib.urlopen(urlData)
            jsonData = json.loads(response.read())
            nodeID = jsonData['nodes'].keys()
            jsonData['nodes'][nodeID[0]]['@timestamp'] = str(utc_datetime.strftime('%Y-%m-%dT%H:%M:%S.%f')[:-3])
            jsonData['nodes'][nodeID[0]]['cluster_name'] = clusterName
            newJsonData = jsonData['nodes'][nodeID[0]]
            post_data(newJsonData)
    
    
    def fetch_indexstats(clusterName):
        utc_datetime = datetime.datetime.utcnow()
        endpoint = "/_stats"
        urlData = elasticServer + endpoint
        response = urllib.urlopen(urlData)
        jsonData = json.loads(response.read())
        jsonData['_all']['@timestamp'] = str(utc_datetime.strftime('%Y-%m-%dT%H:%M:%S.%f')[:-3])
        jsonData['_all']['cluster_name'] = clusterName
        post_data(jsonData['_all'])
    
    
    def post_data(data):
        utc_datetime = datetime.datetime.utcnow()
        url_parameters = {
            'cluster': elasticMonitoringCluster,
            'index': elasticIndex,
            'index_period': utc_datetime.strftime("%Y.%m.%d"),
        }
        url = "%(cluster)s/%(index)s-%(index_period)s/message" % url_parameters
        headers = {'content-type': 'application/json'}
        try:
            req = urllib2.Request(url, headers=headers, data=json.dumps(data))
            f = urllib2.urlopen(req)
        except Exception as e:
            print "Error:  {}".format(str(e))
    
    
    def main():
        clusterName = fetch_clusterhealth()
        fetch_clusterstats()
        fetch_nodestats(clusterName)
        fetch_indexstats(clusterName)
    
    if __name__ == '__main__':
        try:
            nextRun = 0
            while True:
                    if time.time() >= nextRun:
                            nextRun = time.time() + interval
                            now = time.time()
                            main()
                            elapsed = time.time() - now
                            print "Total Elapsed Time: %s" % elapsed
                            timeDiff = nextRun - time.time()
                            time.sleep(timeDiff)
        except KeyboardInterrupt:
            print 'Interrupted'
            try:
                sys.exit(0)
            except SystemExit:
                os._exit(0)
    es_monitor.py

    grafana面板导出的json文件:

    http://files.cnblogs.com/files/xiaoming279/es_monitor.zip

    界面如下:

  • 相关阅读:
    cookie、session和会话保持
    常见的一些专业术语的概念
    JS中的执行机制(setTimeout、setInterval、promise、宏任务、微任务)
    加密和解密
    ASCII 、UTF-8、Unicode编码
    localhost、127.0.0.1、本机ip、0.0.0.0 的区别
    使用Bootstrap框架的HTML5页面模板
    js判断是否在微信浏览器中打开
    js获取url的参数
    js动态生成下拉列表
  • 原文地址:https://www.cnblogs.com/xiaoming279/p/6222122.html
Copyright © 2020-2023  润新知