Пример #1
0
 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))
Пример #2
0
 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))
Пример #3
0
 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))
Пример #4
0
 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))
Пример #5
0
 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))
Пример #6
0
 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))
Пример #7
0
 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))
Пример #8
0
 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))
Пример #9
0
 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))
Пример #10
0
 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))
Пример #11
0
 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))
Пример #12
0
 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))
Пример #13
0
 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))
Пример #14
0
 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))
Пример #15
0
 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))
Пример #16
0
 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))
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'])
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'])
Пример #19
0
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