コード例 #1
0
ファイル: es_monitor.py プロジェクト: Old-wang/es-monitor
def query_datapoints(config):
    lines = config.splitlines()
    es_hosts = lines[0]
    sql = ''.join(lines[1:])
    datapoints = []
    ts = int(time.time())
    try:
        result_map = es_query.execute_sql(es_hosts, sql)
    except urllib2.HTTPError as e:
        LOGGER.exception(e.read())
        sys.exit(1)
    except:
        LOGGER.exception('read datapoint failed')
        sys.exit(1)
    for metric_name, rows in result_map.iteritems():
        for row in rows or []:
            datapoint = {}
            datapoint['timestamp'] = ts
            datapoint['name'] = row.pop('_metric_name', None) or metric_name
            datapoint['value'] = row.pop('value', 0)
            if row:
                tags = {}
                for k, v in row.iteritems():
                    k = to_str(k)
                    if not k.startswith('_'):
                        tags[k] = to_str(v)
                datapoint['tags'] = tags
            datapoints.append(datapoint)
    LOGGER.info('read datapoints: %s' % len(datapoints))
    return datapoints
コード例 #2
0
 def test_save_as(self):
     result_map = es_query.execute_sql(
         None, """
         SELECT * FROM abc;
         SAVE RESULT AS result1;
     """)
     self.assertEqual(['result1'], result_map.keys())
コード例 #3
0
ファイル: es_monitor.py プロジェクト: ztty8888/es-monitor
def query_datapoints(config):
    lines = config.splitlines()
    es_hosts = lines[0]
    sql = ''.join(lines[1:])
    datapoints = []
    ts = int(time.time())
    try:
        result_map = es_query.execute_sql(es_hosts, sql)
    except urllib2.HTTPError as e:
        LOGGER.exception(e.read())
        sys.exit(1)
    except:
        LOGGER.exception('read datapoint failed')
        sys.exit(1)
    for metric_name, rows in result_map.iteritems():
        for row in rows or []:
            datapoint = {}
            datapoint['timestamp'] = ts
            datapoint['name'] = row.pop('_metric_name', None) or metric_name
            datapoint['value'] = row.pop('value', 0)
            if row:
                tags = {}
                for k, v in row.iteritems():
                    k = to_str(k)
                    if not k.startswith('_'):
                        tags[k] = to_str(v)
                datapoint['tags'] = tags
            datapoints.append(datapoint)
    LOGGER.info('read datapoints: %s' % len(datapoints))
    return datapoints
コード例 #4
0
    def test_complex_python_code(self):
        result_map = es_query.execute_sql(
            None, """
result_map['result1'] = []
for i in range(100):
    result_map['result1'].append(i)
        """)
        self.assertEqual(['result1'], result_map.keys())
コード例 #5
0
 def test_remove(self):
     result_map = es_query.execute_sql(
         None, """
         SELECT * FROM abc;
         SAVE RESULT AS result1;
         REMOVE RESULT result1;
     """)
     self.assertEqual([], result_map.keys())
コード例 #6
0
ファイル: execute_sql_test.py プロジェクト: taowen/es-monitor
 def test_python_code(self):
     result_map = es_query.execute_sql(
         None,
         """
         result_map['result1'] = []
     """,
     )
     self.assertEqual(["result1"], result_map.keys())
コード例 #7
0
ファイル: execute_sql_test.py プロジェクト: taowen/es-monitor
 def test_no_save_as(self):
     result_map = es_query.execute_sql(
         None,
         """
         SELECT * FROM abc
     """,
     )
     self.assertEqual(["result"], result_map.keys())
コード例 #8
0
ファイル: execute_sql_test.py プロジェクト: taowen/es-monitor
 def test_save_as(self):
     result_map = es_query.execute_sql(
         None,
         """
         SELECT * FROM abc;
         SAVE RESULT AS result1;
     """,
     )
     self.assertEqual(["result1"], result_map.keys())
コード例 #9
0
ファイル: execute_sql_test.py プロジェクト: taowen/es-monitor
    def test_complex_python_code(self):
        result_map = es_query.execute_sql(
            None,
            """
result_map['result1'] = []
for i in range(100):
    result_map['result1'].append(i)
        """,
        )
        self.assertEqual(["result1"], result_map.keys())
コード例 #10
0
ファイル: execute_sql_test.py プロジェクト: taowen/es-monitor
 def test_remove(self):
     result_map = es_query.execute_sql(
         None,
         """
         SELECT * FROM abc;
         SAVE RESULT AS result1;
         REMOVE RESULT result1;
     """,
     )
     self.assertEqual([], result_map.keys())
コード例 #11
0
ファイル: app.py プロジェクト: Old-wang/es-monitor
def search_with_arguments():
    req = json.loads(flask.request.get_data(parse_form_data=False))
    try:
        resp = {
            'error': None,
            'data': es_query.execute_sql(req['elasticsearch'], req['sql'], req['arguments'])
        }
        return json.dumps(resp, indent=2)
    except:
        etype, value, tb = sys.exc_info()
        resp = {
            'request_args': flask.request.args,
            'request_body': flask.request.get_data(parse_form_data=False),
            'traceback': traceback.format_exception(etype, value, tb),
            'error': str(value),
            'data': None
        }
        return json.dumps(resp, indent=2)
コード例 #12
0
def search():
    if flask.request.method == 'GET':
        sql = flask.request.args.get('q')
    else:
        sql = flask.request.get_data(parse_form_data=False)
    es_hosts = flask.request.args.get('elasticsearch')
    try:
        resp = {'error': None, 'data': es_query.execute_sql(es_hosts, sql)}
        return json.dumps(resp, indent=2)
    except:
        etype, value, tb = sys.exc_info()
        resp = {
            'request_args': flask.request.args,
            'request_body': flask.request.get_data(parse_form_data=False),
            'traceback': traceback.format_exception(etype, value, tb),
            'error': str(value),
            'data': None
        }
        return json.dumps(resp, indent=2)
コード例 #13
0
def search_with_arguments():
    req = json.loads(flask.request.get_data(parse_form_data=False))
    try:
        resp = {
            'error':
            None,
            'data':
            es_query.execute_sql(req['elasticsearch'], req['sql'],
                                 req['arguments'])
        }
        return json.dumps(resp, indent=2)
    except:
        etype, value, tb = sys.exc_info()
        resp = {
            'request_args': flask.request.args,
            'request_body': flask.request.get_data(parse_form_data=False),
            'traceback': traceback.format_exception(etype, value, tb),
            'error': str(value),
            'data': None
        }
        return json.dumps(resp, indent=2)
コード例 #14
0
ファイル: app.py プロジェクト: Old-wang/es-monitor
def search():
    if flask.request.method == 'GET':
        sql = flask.request.args.get('q')
    else:
        sql = flask.request.get_data(parse_form_data=False)
    es_hosts = flask.request.args.get('elasticsearch')
    try:
        resp = {
            'error': None,
            'data': es_query.execute_sql(es_hosts, sql)
        }
        return json.dumps(resp, indent=2)
    except:
        etype, value, tb = sys.exc_info()
        resp = {
            'request_args': flask.request.args,
            'request_body': flask.request.get_data(parse_form_data=False),
            'traceback': traceback.format_exception(etype, value, tb),
            'error': str(value),
            'data': None
        }
        return json.dumps(resp, indent=2)
コード例 #15
0
 def test_python_code(self):
     result_map = es_query.execute_sql(
         None, """
         result_map['result1'] = []
     """)
     self.assertEqual(['result1'], result_map.keys())
コード例 #16
0
 def test_no_save_as(self):
     result_map = es_query.execute_sql(
         None, """
         SELECT * FROM abc
     """)
     self.assertEqual(['result'], result_map.keys())