1 ##############################################################################
2 # Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
10 from urlparse import urlsplit
12 from influxdb import InfluxDBClient
17 logger = logging.getLogger(__name__)
20 def get_data_db_client():
21 parser = ConfigParser.ConfigParser()
23 parser.read(conf.OUTPUT_CONFIG_FILE_PATH)
24 dispatcher = parser.get('DEFAULT', 'dispatcher')
26 if 'influxdb' != dispatcher:
29 ip = _get_ip(parser.get('dispatcher_influxdb', 'target'))
30 username = parser.get('dispatcher_influxdb', 'username')
31 password = parser.get('dispatcher_influxdb', 'password')
32 db_name = parser.get('dispatcher_influxdb', 'db_name')
33 return InfluxDBClient(ip, conf.PORT, username, password, db_name)
34 except ConfigParser.NoOptionError:
35 logger.error('can not find the key')
40 return urlsplit(url).hostname
43 def _write_data(measurement, field, timestamp, tags):
45 'measurement': measurement,
52 client = get_data_db_client()
54 logger.debug('Start to write data: %s', point)
55 client.write_points([point])
57 logger.debug('dispatcher is not influxdb')
60 def write_data_tasklist(task_id, timestamp, status, error=''):
61 field = {'status': status, 'error': error}
62 tags = {'task_id': task_id}
63 _write_data('tasklist', field, timestamp, tags)
68 client = get_data_db_client()
69 logger.debug('Start to query: %s', query_sql)
70 return list(client.query(query_sql).get_points())
72 logger.error('dispatcher is not influxdb')