def test_all_good(self): cluster = InfluxDBClusterClient( hosts=self.hosts, database="database", shuffle=True, client_base_class=FakeClient ) self.assertEqual("Success", cluster.query("")) self.assertEqual(3, len(cluster.hosts)) self.assertEqual(0, len(cluster.bad_hosts))
def test_dsn(self): cli = InfluxDBClusterClient.from_DSN(self.dsn_string) self.assertEqual(2, len(cli.clients)) self.assertEqual('http://host1:8086', cli.clients[0]._baseurl) self.assertEqual('uSr', cli.clients[0]._username) self.assertEqual('pWd', cli.clients[0]._password) self.assertEqual('db', cli.clients[0]._database) self.assertFalse(cli.clients[0].use_udp) self.assertEqual('http://host2:8086', cli.clients[1]._baseurl) self.assertEqual('uSr', cli.clients[1]._username) self.assertEqual('pWd', cli.clients[1]._password) self.assertEqual('db', cli.clients[1]._database) self.assertFalse(cli.clients[1].use_udp) cli = InfluxDBClusterClient.from_DSN('udp+' + self.dsn_string) self.assertTrue(cli.clients[0].use_udp) self.assertTrue(cli.clients[1].use_udp) cli = InfluxDBClusterClient.from_DSN('https+' + self.dsn_string) self.assertEqual('https://host1:8086', cli.clients[0]._baseurl) self.assertEqual('https://host2:8086', cli.clients[1]._baseurl) cli = InfluxDBClusterClient.from_DSN('https+' + self.dsn_string, **{'ssl': False}) self.assertEqual('http://host1:8086', cli.clients[0]._baseurl) self.assertEqual('http://host2:8086', cli.clients[1]._baseurl)
def test_dsn_mixed_scheme_case(self): cli = InfluxDBClusterClient.from_DSN("hTTps+inFLUxdb://usr:pWd@host:8086/db") self.assertEqual("pWd", cli._client._password) self.assertEqual("https://*****:*****@host1:8086,usr:pwd@host2:8086/db") self.assertTrue(cli._client.use_udp)
def test_dsn(self): cli = InfluxDBClusterClient.from_DSN(self.dsn_string) self.assertEqual(2, len(cli.clients)) self.assertEqual("http://host1:8086", cli.clients[0]._baseurl) self.assertEqual("uSr", cli.clients[0]._username) self.assertEqual("pWd", cli.clients[0]._password) self.assertEqual("db", cli.clients[0]._database) self.assertFalse(cli.clients[0].use_udp) self.assertEqual("http://host2:8086", cli.clients[1]._baseurl) self.assertEqual("uSr", cli.clients[1]._username) self.assertEqual("pWd", cli.clients[1]._password) self.assertEqual("db", cli.clients[1]._database) self.assertFalse(cli.clients[1].use_udp) cli = InfluxDBClusterClient.from_DSN("udp+" + self.dsn_string) self.assertTrue(cli.clients[0].use_udp) self.assertTrue(cli.clients[1].use_udp) cli = InfluxDBClusterClient.from_DSN("https+" + self.dsn_string) self.assertEqual("https://host1:8086", cli.clients[0]._baseurl) self.assertEqual("https://host2:8086", cli.clients[1]._baseurl) cli = InfluxDBClusterClient.from_DSN("https+" + self.dsn_string, **{"ssl": False}) self.assertEqual("http://host1:8086", cli.clients[0]._baseurl) self.assertEqual("http://host2:8086", cli.clients[1]._baseurl)
def test_two_servers_fail(self): cluster = InfluxDBClusterClient( hosts=self.hosts, database="database", shuffle=False, client_base_class=FakeClient ) self.assertEqual("Success", cluster.query("Fail twice")) self.assertEqual(1, len(cluster.hosts)) self.assertEqual(2, len(cluster.bad_hosts))
def test_dsn(self): cli = InfluxDBClusterClient.from_DSN( 'influxdb://*****:*****@host1:8086,usr:pwd@host2:8086/db') self.assertEqual(2, len(cli.clients)) self.assertEqual('http://*****:*****@host1:8086,usr:pwd@host2:8086/db') self.assertTrue(cli.clients[0].use_udp) self.assertTrue(cli.clients[1].use_udp) cli = InfluxDBClusterClient.from_DSN( 'https+influxdb://usr:pwd@host1:8086,usr:pwd@host2:8086/db') self.assertEqual('https://*****:*****@host1:8086,usr:pwd@host2:8086/db', **{'ssl': False}) self.assertEqual('http://host1:8086', cli.clients[0]._baseurl) self.assertEqual('http://host2:8086', cli.clients[1]._baseurl)
def test_two_servers_fail(self): cluster = InfluxDBClusterClient(hosts=self.hosts, database='database', shuffle=False, client_base_class=FakeClient) self.assertEqual('Success', cluster.query('Fail twice')) self.assertEqual(1, len(cluster.hosts)) self.assertEqual(2, len(cluster.bad_hosts))
def test_one_server_fails(self): cluster = InfluxDBClusterClient( hosts=self.hosts, database="database", shuffle=False, client_base_class=FakeClient ) cluster.clients[0].fail = True self.assertEqual("Success", cluster.query("")) self.assertEqual(2, len(cluster.clients)) self.assertEqual(1, len(cluster.bad_clients))
def test_all_good(self): cluster = InfluxDBClusterClient(hosts=self.hosts, database='database', shuffle=True, client_base_class=FakeClient) self.assertEqual('Success', cluster.query('')) self.assertEqual(3, len(cluster.hosts)) self.assertEqual(0, len(cluster.bad_hosts))
def test_all_fail(self): cluster = InfluxDBClusterClient( hosts=self.hosts, database="database", shuffle=True, client_base_class=FakeClient ) with self.assertRaises(InfluxDBServerError): cluster.query("Fail") self.assertEqual(0, len(cluster.hosts)) self.assertEqual(3, len(cluster.bad_hosts))
def test_all_good(self): cluster = InfluxDBClusterClient(hosts=self.hosts, database='database', shuffle=True, client_base_class=FakeClient) self.assertEqual('Success', cluster.query('')) self.assertEqual(3, len(cluster.clients)) self.assertEqual(0, len(cluster.bad_clients))
def test_one_server_fails(self): cluster = InfluxDBClusterClient(hosts=self.hosts, database='database', shuffle=False, client_base_class=FakeClient) self.assertEqual('Success', cluster.query('Fail once')) self.assertEqual(2, len(cluster.hosts)) self.assertEqual(1, len(cluster.bad_hosts))
def test_one_server_fails(self): cluster = InfluxDBClusterClient(hosts=self.hosts, database='database', shuffle=False, client_base_class=FakeClient) cluster.clients[0].fail = True self.assertEqual('Success', cluster.query('')) self.assertEqual(2, len(cluster.clients)) self.assertEqual(1, len(cluster.bad_clients))
def test_dsn_mixed_scheme_case(self): cli = InfluxDBClusterClient.from_DSN( 'hTTps+inFLUxdb://usr:pWd@host:8086/db') self.assertEqual('pWd', cli.clients[0]._password) self.assertEqual('https://*****:*****@host1:8086,usr:pwd@host2:8086/db') self.assertTrue(cli.clients[0].use_udp)
def test_dsn_mixed_scheme_case(self): cli = InfluxDBClusterClient.from_DSN( 'hTTps+inFLUxdb://usr:pWd@host:8086/db') self.assertEqual('pWd', cli._client._password) self.assertEqual('https://*****:*****@host1:8086,usr:pwd@host2:8086/db') self.assertTrue(cli._client.use_udp)
def test_all_fail(self): cluster = InfluxDBClusterClient(hosts=self.hosts, database='database', shuffle=True, client_base_class=FakeClient) with self.assertRaises(InfluxDBServerError): cluster.query('Fail') self.assertEqual(0, len(cluster.hosts)) self.assertEqual(3, len(cluster.bad_hosts))
def test_recovery(self): cluster = InfluxDBClusterClient( hosts=self.hosts, database="database", shuffle=True, client_base_class=FakeClient ) with self.assertRaises(InfluxDBServerError): cluster.query("Fail") self.assertEqual("Success", cluster.query("")) self.assertEqual(1, len(cluster.hosts)) self.assertEqual(2, len(cluster.bad_hosts))
def test_recovery(self): cluster = InfluxDBClusterClient(hosts=self.hosts, database='database', shuffle=True, client_base_class=FakeClient) with self.assertRaises(InfluxDBServerError): cluster.query('Fail') self.assertEqual('Success', cluster.query('')) self.assertEqual(1, len(cluster.clients)) self.assertEqual(2, len(cluster.bad_clients))
def test_two_servers_fail(self): cluster = InfluxDBClusterClient(hosts=self.hosts, database='database', shuffle=False, client_base_class=FakeClient) cluster.clients[0].fail = True cluster.clients[1].fail = True self.assertEqual('Success', cluster.query('')) self.assertEqual(1, len(cluster.clients)) self.assertEqual(2, len(cluster.bad_clients))
def test_recovery(self): cluster = InfluxDBClusterClient(hosts=self.hosts, database='database', shuffle=True, client_base_class=FakeClient) with self.assertRaises(InfluxDBServerError): cluster.query('Fail') self.assertEqual('Success', cluster.query('')) self.assertEqual(1, len(cluster.hosts)) self.assertEqual(2, len(cluster.bad_hosts))
def __init__(self, cfg): influxdb.__init__(self, cfg) if 'influxdb_extra_tags' in cfg: self.influxdb_extra_tags = ast.literal_eval( cfg['influxdb_extra_tags']) print self.influxdb_extra_tags else: self.influxdb_extra_tags = {} hosts = map(lambda x: (x.split(':')[0], x.split(':')[1]), self.influxdb_servers) self.client = InfluxDBClusterClient(hosts, self.influxdb_user, self.influxdb_password, timeout=self.timeout)
def test_dsn_single_client(self): cli = InfluxDBClusterClient.from_DSN("influxdb://*****:*****@host:8086/db") self.assertEqual("http://*****:*****@host:8086/db") self.assertTrue(cli.clients[0].use_udp) cli = InfluxDBClusterClient.from_DSN("https+influxdb://usr:pwd@host:8086/db") self.assertEqual("https://*****:*****@host:8086/db", **{"ssl": False}) self.assertEqual("http://host:8086", cli.clients[0]._baseurl)
def test_dsn(self): cli = InfluxDBClusterClient.from_DSN(self.dsn_string) self.assertEqual([("host1", 8086), ("host2", 8086)], cli.hosts) self.assertEqual("http://host1:8086", cli._client._baseurl) self.assertEqual("uSr", cli._client._username) self.assertEqual("pWd", cli._client._password) self.assertEqual("db", cli._client._database) self.assertFalse(cli._client.use_udp) cli = InfluxDBClusterClient.from_DSN("udp+" + self.dsn_string) self.assertTrue(cli._client.use_udp) cli = InfluxDBClusterClient.from_DSN("https+" + self.dsn_string) self.assertEqual("https://host1:8086", cli._client._baseurl) cli = InfluxDBClusterClient.from_DSN("https+" + self.dsn_string, **{"ssl": False}) self.assertEqual("http://host1:8086", cli._client._baseurl)
def create_cluster(self): logging.info("connecting to influxdb at :%s",self.hosts) self.client = InfluxDBClusterClient.from_DSN(self.hosts, ssl= self.use_ssl, verify_ssl=self.verify_ssl, timeout=self.timeout, use_udp=self.use_udp) return self.client
def test_dsn(self): cli = InfluxDBClusterClient.from_DSN(self.dsn_string) self.assertEqual([('host1', 8086), ('host2', 8086)], cli.hosts) self.assertEqual('http://host1:8086', cli._client._baseurl) self.assertEqual('uSr', cli._client._username) self.assertEqual('pWd', cli._client._password) self.assertEqual('db', cli._client._database) self.assertFalse(cli._client.use_udp) cli = InfluxDBClusterClient.from_DSN('udp+' + self.dsn_string) self.assertTrue(cli._client.use_udp) cli = InfluxDBClusterClient.from_DSN('https+' + self.dsn_string) self.assertEqual('https://host1:8086', cli._client._baseurl) cli = InfluxDBClusterClient.from_DSN('https+' + self.dsn_string, **{'ssl': False}) self.assertEqual('http://host1:8086', cli._client._baseurl)
def create_cluster(self): logging.info("connecting to influxdb at :%s", self.hosts) self.client = InfluxDBClusterClient.from_DSN( self.hosts, ssl=self.use_ssl, verify_ssl=self.verify_ssl, timeout=self.timeout, use_udp=self.use_udp) return self.client
def test_dsn_single_client(self): cli = InfluxDBClusterClient.from_DSN('influxdb://*****:*****@host:8086/db') self.assertEqual('http://*****:*****@host:8086/db') self.assertTrue(cli.clients[0].use_udp) cli = InfluxDBClusterClient.from_DSN( 'https+influxdb://usr:pwd@host:8086/db') self.assertEqual('https://*****:*****@host:8086/db', **{'ssl': False}) self.assertEqual('http://host:8086', cli.clients[0]._baseurl)
def open(self): """ Connect to InfluxDB cluster. """ try: self.cc = InfluxDBClusterClient( hosts=self.hosts, username=self.influxdb_user, password=self.influxdb_passwd, ssl=self.influxdb_ssl, verify_ssl=self.influxdb_verify_ssl) self.is_connected = CONNECTED except InfluxDBClientError as e: logging.warning("Connection failed: %s" % e) return False return True
class Influxdb(object): def __init__(self): self.influxdb_user = args.influxdb_user self.influxdb_passwd = args.influxdb_passwd self.influxdb_ssl = args.influxdb_ssl self.influxdb_verify_ssl = args.influxdb_verify_ssl self.databases = re.compile(args.databases) self.hosts = [] for host in args.hosts.split(','): self.hosts.append(tuple(filter(None, host.split(':')))) self.is_connected = DISCONNECTED def open(self): """ Connect to InfluxDB cluster. """ try: self.cc = InfluxDBClusterClient( hosts=self.hosts, username=self.influxdb_user, password=self.influxdb_passwd, ssl=self.influxdb_ssl, verify_ssl=self.influxdb_verify_ssl) self.is_connected = CONNECTED except InfluxDBClientError as e: logging.warning("Connection failed: %s" % e) return False return True def get_ds_list(self): dbs = self.cc.get_list_database() return [db for db in dbs if self.databases.search(db['name'])] def get_measurements(self, dbname): self.cc.switch_database(dbname) m = self.cc.query('SHOW MEASUREMENTS') return json.dumps(m.raw['series'][0]['values'])
def __init__(self, cfg): influxdb.__init__(self, cfg) if 'influxdb_extra_tags' in cfg: self.influxdb_extra_tags = ast.literal_eval( cfg['influxdb_extra_tags']) print self.influxdb_extra_tags else: self.influxdb_extra_tags = {} hosts = map(lambda x: (x.split(':')[0],x.split(':')[1]), self.influxdb_servers) self.client = InfluxDBClusterClient(hosts, self.influxdb_user, self.influxdb_password, timeout = self.timeout)
def test_init(self): cluster = InfluxDBClusterClient(hosts=self.hosts, username='******', password='******', database='database', shuffle=False, client_base_class=FakeClient) self.assertEqual(3, len(cluster.hosts)) self.assertEqual(0, len(cluster.bad_hosts)) self.assertIn((cluster._client._host, cluster._client._port), cluster.hosts)
class Influxdb(object): def __init__(self): self.influxdb_user = args.influxdb_user self.influxdb_passwd = args.influxdb_passwd self.influxdb_ssl = args.influxdb_ssl self.influxdb_verify_ssl = args.influxdb_verify_ssl self.databases = re.compile(args.databases) self.hosts = [] for host in args.hosts.split(','): self.hosts.append(tuple(filter(None, host.split(':')))) self.is_connected = DISCONNECTED def open(self): """ Connect to InfluxDB cluster. """ try: self.cc = InfluxDBClusterClient(hosts = self.hosts, username=self.influxdb_user, password=self.influxdb_passwd, ssl=self.influxdb_ssl, verify_ssl=self.influxdb_verify_ssl) self.is_connected = CONNECTED except InfluxDBClientError as e: logging.warning("Connection failed: %s" % e) return False return True def get_ds_list(self): dbs = self.cc.get_list_database() return [db for db in dbs if self.databases.search(db['name'])] def get_measurements(self,dbname): self.cc.switch_database(dbname) m = self.cc.query('SHOW MEASUREMENTS') return json.dumps(m.raw['series'][0]['values'])
def test_init(self): cluster = InfluxDBClusterClient(hosts=self.hosts, username='******', password='******', database='database', shuffle=False, client_base_class=FakeClient) self.assertEqual(3, len(cluster.clients)) self.assertEqual(0, len(cluster.bad_clients)) for idx, client in enumerate(cluster.clients): self.assertEqual(self.hosts[idx][0], client._host) self.assertEqual(self.hosts[idx][1], client._port)
def __init__(self, influxdb_hosts=[('101.201.235.144', 8086)], influxdb_user='******', influxdb_passwd='root', influxdb_database='k8s'): """Init the wrapper Args: influxdb_hosts: list of influxdb host tuple, like (ip_address, ip_port) influxdb_user: influxdb user influxdb_passwd: influxdb password influxdb_database: influxdb database Description: If the function is compiled with docker, Then we can set the influxdb address as SERVICE/PORT in Kubernetes """ if len(influxdb_hosts) == 1: try: self.__connection__ = InfluxDBClient(influxdb_hosts[0][0], influxdb_hosts[0][1], influxdb_user, influxdb_passwd, influxdb_database) except: self.__status__ = 1 else: try: self.__connection__ = InfluxDBClusterClient(influxdb_hosts, influxdb_user, influxdb_passwd, influxdb_database) except: self.__status__ = 1
def run_query(self, query): client = InfluxDBClusterClient.from_DSN(self.configuration['url']) logger.debug("influxdb url: %s", self.configuration['url']) logger.debug("influxdb got query: %s", query) try: results = client.query(query) if not isinstance(results, list): results = [results] json_data = _transform_result(results) error = None except Exception, ex: json_data = None error = ex.message
def run_query(self, query, user): client = InfluxDBClusterClient.from_DSN(self.configuration["url"]) logger.debug("influxdb url: %s", self.configuration["url"]) logger.debug("influxdb got query: %s", query) try: results = client.query(query) if not isinstance(results, list): results = [results] json_data = _transform_result(results) error = None except Exception as ex: json_data = None error = str(ex) return json_data, error
def open(self): """ Connect to InfluxDB cluster. """ try: self.cc = InfluxDBClusterClient(hosts = self.hosts, username=self.influxdb_user, password=self.influxdb_passwd, ssl=self.influxdb_ssl, verify_ssl=self.influxdb_verify_ssl) self.is_connected = CONNECTED except InfluxDBClientError as e: logging.warning("Connection failed: %s" % e) return False return True
def test_healing(self): cluster = InfluxDBClusterClient(hosts=self.hosts, database='database', shuffle=True, healing_delay=1, client_base_class=FakeClient) with self.assertRaises(InfluxDBServerError): cluster.query('Fail') self.assertEqual('Success', cluster.query('')) time.sleep(1.1) self.assertEqual('Success', cluster.query('')) self.assertEqual(2, len(cluster.hosts)) self.assertEqual(1, len(cluster.bad_hosts)) time.sleep(1.1) self.assertEqual('Success', cluster.query('')) self.assertEqual(3, len(cluster.hosts)) self.assertEqual(0, len(cluster.bad_hosts))
def test_healing(self): cluster = InfluxDBClusterClient( hosts=self.hosts, database="database", shuffle=True, healing_delay=1, client_base_class=FakeClient ) with self.assertRaises(InfluxDBServerError): cluster.query("Fail") self.assertEqual("Success", cluster.query("")) time.sleep(1.1) self.assertEqual("Success", cluster.query("")) self.assertEqual(2, len(cluster.hosts)) self.assertEqual(1, len(cluster.bad_hosts)) time.sleep(1.1) self.assertEqual("Success", cluster.query("")) self.assertEqual(3, len(cluster.hosts)) self.assertEqual(0, len(cluster.bad_hosts))
def init_influxdb_client(): global db if not cfg['influxdb']['cluster']: try: from influxdb import InfluxDBClient except ImportError as e: log.exception("Could not import influxdb module: {0}".format(e)) sys.exit(1) db = InfluxDBClient(host=cfg['influxdb']['host'], port=cfg['influxdb']['port'], username=cfg['influxdb']['username'], password=cfg['influxdb']['password'], database=cfg['influxdb']['database'], ssl=cfg['influxdb']['ssl'], verify_ssl=cfg['influxdb']['verify_ssl'], timeout=float(cfg['influxdb']['timeout']), use_udp=cfg['influxdb']['use_udp'], udp_port=cfg['influxdb']['udp_port'], proxies=cfg['influxdb']['proxies']) else: try: from influxdb import InfluxDBClusterClient except ImportError as e: log.exception("Could not import influxdb module: {0}".format(e)) db = InfluxDBClusterClient( hosts=cfg['influxdb']['hosts'], username=cfg['influxdb']['username'], password=cfg['influxdb']['password'], database=cfg['influxdb']['database'], ssl=cfg['influxdb']['ssl'], verify_ssl=cfg['influxdb']['verify_ssl'], timeout=float(cfg['influxdb']['timeout']), use_udp=cfg['influxdb']['use_udp'], udp_port=cfg['influxdb']['udp_port'], shuffle=cfg['influxdb']['shuffle'], healing_delay=cfg['influxdb']['healing_delay'])
def test_dsn_password_caps(self): cli = InfluxDBClusterClient.from_DSN( 'https+influxdb://usr:pWd@host:8086/db') self.assertEqual('pWd', cli._client._password)
def test_dsn_password_caps(self): cli = InfluxDBClusterClient.from_DSN("https+influxdb://usr:pWd@host:8086/db") self.assertEqual("pWd", cli.clients[0]._password)
class influxdb09(influxdb): def __init__(self, cfg): influxdb.__init__(self, cfg) if 'influxdb_extra_tags' in cfg: self.influxdb_extra_tags = ast.literal_eval( cfg['influxdb_extra_tags']) print self.influxdb_extra_tags else: self.influxdb_extra_tags = {} hosts = map(lambda x: (x.split(':')[0],x.split(':')[1]), self.influxdb_servers) self.client = InfluxDBClusterClient(hosts, self.influxdb_user, self.influxdb_password, timeout = self.timeout) def build_url(self, server): """ Returns a url to specified InfluxDB-server """ test_port = server.split(':') if len(test_port) < 2: server = "%s:%i" % (server, self.default_ports[self.scheme]) return "%s://%s/write?u=%s&p=%s" % (self.scheme, server, self.influxdb_user, self.influxdb_password) def send(self, metrics): """ Connect to influxdb and send metrics """ ret = 0 perfdata = {} for m in metrics: ret += 1 if (m.SERVICEDESC == ''): path = m.HOSTCHECKCOMMAND status = m.HOSTSTATE else: path = m.SERVICEDESC status = m.SERVICESTATE # Ensure a float gets passed # A measurement can not have integer and float values try: value = float(m.VALUE) except ValueError: value = 0 # Add project as tag try: project = m.PROJECT except AttributeError: project = "NA" tags = {"check": m.LABEL, "host": m.HOSTNAME, "project": project, "status": status} # Add warning and critical threshold if exists try: tags.update({"warning": float(m.WARN)}) except ValueError: pass try: tags.update({"critical": float(m.CRIT)}) except ValueError: pass try: tags.update({"min": float(m.MIN)}) except ValueError: pass try: tags.update({"max": float(m.MAX)}) except ValueError: pass tags.update(self.influxdb_extra_tags) # perfdata has each project's metrics in a different array perfdata.setdefault(project, []).append({ "time": int(m.TIMET), "measurement": path, "tags": tags, "fields": {"value": value}}) for project in perfdata: try: self.client.write_points(perfdata[project], database=project, time_precision='s', batch_size = self.influxdb_max_metrics) except requests.exceptions.Timeout as error: self.log.critical("Timeout connecting to InfluxDB: %s", error) ret = 0 except requests.exceptions.ConnectionError as error: self.log.critical("Error connecting to InfluxDB: %s", error) ret = 0 except Exception as error: self.log.critical("Error writing points to InfluxDB: %s", error) # If send fails, set sended metrics as 0. # Will print a error message: "...insufficent metrics sent from..." ret = 0 return ret
#!/usr/bin/env python from influxdb import InfluxDBClusterClient influx_host = "127.0.0.1" influx_port = 8086 influx_user = "******" influx_pass = "******" database = "example" influx_timeout = 10 c.influx_max_metrics = 40 c.influx = InfluxDBClusterClient([(influx_host, influx_port)], influx_user, influx_pass, database, timeout=influx_timeout) query = c.influx.query("SHOW FIELD KEYS") values = [] for key, items in query.items(): for i in list(items): values.append(i["fieldKey"]) # String of all mean(values) # Set makes a uniq list cq_values = ",".join(map(lambda v: 'mean("%s") AS "%s"' % (v, v), set(values))) cq_query = 'CREATE CONTINUOUS QUERY {cq_name} ON {database} BEGIN SELECT {cq_values} INTO {database}."rp_{rp}".:MEASUREMENT FROM {database}."{query}"./.*/ GROUP BY time({rp}), * END'
class InfluxDBWrapper(object): """ Wrapper the influxdb API, Return Data Args: __status__: the connection status __connection__: the connection object of the influxdb database """ status = 0 __connection__ = None def __init__(self, influxdb_hosts=[('101.201.235.144', 8086)], influxdb_user='******', influxdb_passwd='root', influxdb_database='k8s'): """Init the wrapper Args: influxdb_hosts: list of influxdb host tuple, like (ip_address, ip_port) influxdb_user: influxdb user influxdb_passwd: influxdb password influxdb_database: influxdb database Description: If the function is compiled with docker, Then we can set the influxdb address as SERVICE/PORT in Kubernetes """ if len(influxdb_hosts) == 1: try: self.__connection__ = InfluxDBClient(influxdb_hosts[0][0], influxdb_hosts[0][1], influxdb_user, influxdb_passwd, influxdb_database) except: self.__status__ = 1 else: try: self.__connection__ = InfluxDBClusterClient(influxdb_hosts, influxdb_user, influxdb_passwd, influxdb_database) except: self.__status__ = 1 def show_tables(self): """ Influxdb SQL name table as measurement. Show tables would be necessary """ return self.show_measurements(); def show_measurements(self): """ The same functions as show_tables """ sql_string = 'SHOW MEASUREMENTS' return self.query(sql_string) def list_series(self, json_str=True): """ Show the database series Args: json_str: the switch whether to open it with JSON """ if json_str == True: return json.dumps(self.__connection__.get_list_series(database)) def show_tag_keys(self, measurements='cpu/node_reservation'): """ Show the measurements tag keys Args: measurements: the target measurements """ sql_string = "SHOW TAG KEYS FROM \"" + measurements + "\"" return self.query(sql_string) def show_field_keys(self, measurements='cpu/node_reservation'): """ Show the measurements field Args: measurements: the target measurements """ sql_string = "SHOW FIELD KEYS FROM \"" + measurements + "\"" return self.query(sql_string) def last_records(self, measurements='cpu/usage'): """ Get the lastest record Args: measurements: the target measurements """ sql_string = "SELECT * FROM \"" + measurements + "\" WHERE time > now() - 1h ORDER BY time DESC LIMIT 20 " return self.query(sql_string) def period_records(self, measurements='cpu/usage_rate', group_by='nodename', time_durations='1h'): """ Show the node cpu usage Args: measurements: the target measurements group_by: the group by object time_duration: time duration from now """ #sql_string = "SELECT * FROM \"" + measurements + "\" WHERE \"hostname\" = 'nodename' AND time > now() - " + time_durations + " GROUP BY \"" + group_by + " \"" sql_string = "SELECT * FROM \"" + measurements + "\" WHERE time > now() - " + time_durations + " GROUP BY \"" + group_by + " \"" return self.query(sql_string) def sum_records(self, measurements='cpu/node_reservation', group_by='nodename', time_durations='1h'): """ Show the node cpu usage Args: measurements: the target measurements group_by: the group by object time_duration: time duration from now """ sql_string = "SELECT sum(\"value\") FROM \"" + measurements + "\" WHERE \"type\" = 'node' AND time > now() - " + time_durations return self.query(sql_string) def avg_records(self, measurements='cpu/node_reservation', group_by='nodename', time_durations='1 hour'): """ Show the node cpu usage Args: measurements: the target measurements group_by: the group by object time_duration: time duration from now """ sql_string = "SELECT sum(\"value\") FROM \"" + measurements + "\" WHERE \"type\" = 'node' AND time > '2015-04-29T00:00:00Z'" return self.query(sql_string) def query(self, sql, json_str=True, params=None, epoch=True, expected_response_code=200, database=None, raise_errors=True): """ Query the influxdb database with sql The return of the InfluxDBClient is not JSON style Do the transform from ResultSet to JSON, So it is easy to read/write Args: sql: the SQL sentences """ data = self.__connection__.query(sql) sql_data = list(data.get_points()) if json_str == True: sql_data = json.dumps(sql_data) return sql_data def write(self, data): """ Write data to series databases TODO """ pass
class influxdb09(influxdb): def __init__(self, cfg): influxdb.__init__(self, cfg) if 'influxdb_extra_tags' in cfg: self.influxdb_extra_tags = ast.literal_eval( cfg['influxdb_extra_tags']) print self.influxdb_extra_tags else: self.influxdb_extra_tags = {} hosts = map(lambda x: (x.split(':')[0], x.split(':')[1]), self.influxdb_servers) self.client = InfluxDBClusterClient(hosts, self.influxdb_user, self.influxdb_password, timeout=self.timeout) def build_url(self, server): """ Returns a url to specified InfluxDB-server """ test_port = server.split(':') if len(test_port) < 2: server = "%s:%i" % (server, self.default_ports[self.scheme]) return "%s://%s/write?u=%s&p=%s" % ( self.scheme, server, self.influxdb_user, self.influxdb_password) def send(self, metrics): """ Connect to influxdb and send metrics """ ret = 0 perfdata = {} for m in metrics: ret += 1 if (m.SERVICEDESC == ''): path = m.HOSTCHECKCOMMAND status = m.HOSTSTATE else: path = m.SERVICEDESC status = m.SERVICESTATE # Ensure a float gets passed # A measurement can not have integer and float values try: value = float(m.VALUE) except ValueError: value = 0 # Add project as tag try: project = m.PROJECT except AttributeError: project = "NA" tags = { "check": m.LABEL, "host": m.HOSTNAME, "project": project, "status": status } # Add warning and critical threshold if exists try: tags.update({"warning": float(m.WARN)}) except ValueError: pass try: tags.update({"critical": float(m.CRIT)}) except ValueError: pass try: tags.update({"min": float(m.MIN)}) except ValueError: pass try: tags.update({"max": float(m.MAX)}) except ValueError: pass tags.update(self.influxdb_extra_tags) # perfdata has each project's metrics in a different array perfdata.setdefault(project, []).append({ "time": int(m.TIMET), "measurement": path, "tags": tags, "fields": { "value": value } }) for project in perfdata: try: self.client.write_points(perfdata[project], database=project, time_precision='s', batch_size=self.influxdb_max_metrics) except requests.exceptions.Timeout as error: self.log.critical("Timeout connecting to InfluxDB: %s", error) ret = 0 except requests.exceptions.ConnectionError as error: self.log.critical("Error connecting to InfluxDB: %s", error) ret = 0 except Exception as error: self.log.critical("Error writing points to InfluxDB: %s", error) # If send fails, set sended metrics as 0. # Will print a error message: "...insufficent metrics sent from..." ret = 0 return ret