Example #1
0
 def test_write_points_batch_multiple_series(self):
     dummy_points = [
         {
             "points": [["1", 1, 1.0], ["2", 2, 2.0], ["3", 3, 3.0],
                        ["4", 4, 4.0], ["5", 5, 5.0]],
             "name":
             "foo",
             "columns": ["val1", "val2", "val3"]
         },
         {
             "points": [["1", 1, 1.0], ["2", 2, 2.0], ["3", 3, 3.0],
                        ["4", 4, 4.0], ["5", 5, 5.0], ["6", 6, 6.0],
                        ["7", 7, 7.0], ["8", 8, 8.0]],
             "name":
             "bar",
             "columns": ["val1", "val2", "val3"]
         },
     ]
     expected_last_body = [{
         'points': [['7', 7, 7.0], ['8', 8, 8.0]],
         'name': 'bar',
         'columns': ['val1', 'val2', 'val3']
     }]
     with requests_mock.Mocker() as m:
         m.register_uri(requests_mock.POST,
                        "http://localhost:8086/db/db/series")
         cli = InfluxDBClient('localhost', 8086, 'username', 'password',
                              'db')
         cli.write_points(data=dummy_points, batch_size=3)
     self.assertEqual(m.call_count, 5)
     self.assertEqual(expected_last_body, m.request_history[4].json())
Example #2
0
    def test_request_retry_raises(self, mock_request):
        """Test that three connection errors will not be handled."""
        class CustomMock(object):
            """Define CustomMock object."""

            def __init__(self):
                """Initialize the object."""
                self.i = 0

            def connection_error(self, *args, **kwargs):
                """Test the connection error for CustomMock."""
                self.i += 1

                if self.i < 4:
                    raise requests.exceptions.ConnectionError
                else:
                    r = requests.Response()
                    r.status_code = 200
                    return r

        mock_request.side_effect = CustomMock().connection_error

        cli = InfluxDBClient(database='db')

        with self.assertRaises(requests.exceptions.ConnectionError):
            cli.write_points(self.dummy_points)
Example #3
0
 def test_write_points_bad_precision(self):
     cli = InfluxDBClient()
     with self.assertRaisesRegexp(
             Exception,
             "Invalid time precision is given. \(use 's', 'm', 'ms' or 'u'\)"
     ):
         cli.write_points(self.dummy_points, time_precision='g')
Example #4
0
 def test_get_database_list_deprecated(self):
     """Test deprecated get database list for TestInfluxDBClient."""
     data = [{"name": "a_db"}]
     with _mocked_session('get', 200, data):
         cli = InfluxDBClient('host', 8086, 'username', 'password')
         self.assertEqual(len(cli.get_database_list()), 1)
         self.assertEqual(cli.get_database_list()[0]['name'], 'a_db')
Example #5
0
    def test_write(self):
        with requests_mock.Mocker() as m:
            m.register_uri(
                requests_mock.POST,
                "http://localhost:8086/write"
            )
            cli = InfluxDBClient(database='db')
            cli.write(
                {"database": "mydb",
                 "retentionPolicy": "mypolicy",
                 "points": [{"name": "cpu_load_short",
                             "tags": {"host": "server01",
                                      "region": "us-west"},
                             "timestamp": "2009-11-10T23:00:00Z",
                             "values": {"value": 0.64}}]}
            )

            self.assertEqual(
                json.loads(m.last_request.body),
                {"database": "mydb",
                 "retentionPolicy": "mypolicy",
                 "points": [{"name": "cpu_load_short",
                             "tags": {"host": "server01",
                                      "region": "us-west"},
                             "timestamp": "2009-11-10T23:00:00Z",
                             "values": {"value": 0.64}}]}
            )
Example #6
0
 def test_write_points_batch(self):
     with _mocked_session('post', 200, self.dummy_points):
         cli = InfluxDBClient('host', 8086, 'username', 'password', 'db')
         assert cli.write_points(
             data=self.dummy_points,
             batch_size=2
         ) is True
Example #7
0
    def test_query_chunked(self):
        cli = InfluxDBClient(database='db')
        example_object = {
            'points': [
                [1415206250119, 40001, 667],
                [1415206244555, 30001, 7],
                [1415206228241, 20001, 788],
                [1415206212980, 10001, 555],
                [1415197271586, 10001, 23]
            ],
            'name': 'foo',
            'columns': [
                'time',
                'sequence_number',
                'val'
            ]
        }
        example_response = \
            json.dumps(example_object) + json.dumps(example_object)

        with requests_mock.Mocker() as m:
            m.register_uri(
                requests_mock.GET,
                "http://localhost:8086/db/db/series",
                text=example_response
            )

            self.assertListEqual(
                cli.query('select * from foo', chunked=True),
                [example_object, example_object]
            )
Example #8
0
    def test_request_retry(self, mock_request):
        """Test that two connection errors will be handled."""
        class CustomMock(object):
            """Define CustomMock object."""

            def __init__(self):
                self.i = 0

            def connection_error(self, *args, **kwargs):
                """Test connection error in CustomMock."""
                self.i += 1

                if self.i < 3:
                    raise requests.exceptions.ConnectionError
                else:
                    r = requests.Response()
                    r.status_code = 200
                    return r

        mock_request.side_effect = CustomMock().connection_error

        cli = InfluxDBClient(database='db')
        cli.write_points(
            self.dummy_points
        )
Example #9
0
    def test_get_continuous_queries(self):
        cli = InfluxDBClient(database='db')

        with requests_mock.Mocker() as m:

            # Tip: put this in a json linter!
            example_response = '[ { "name": "continuous queries", "columns"' \
                               ': [ "time", "id", "query" ], "points": [ [ ' \
                               '0, 1, "select foo(bar,95) from \\"foo_bar' \
                               's\\" group by time(5m) into response_times.' \
                               'percentiles.5m.95" ], [ 0, 2, "select perce' \
                               'ntile(value,95) from \\"response_times\\" g' \
                               'roup by time(5m) into response_times.percen' \
                               'tiles.5m.95" ] ] } ]'

            m.register_uri(
                requests_mock.GET,
                "http://localhost:8086/db/db/series",
                text=example_response
            )

            self.assertListEqual(
                cli.get_list_continuous_queries(),
                [
                    'select foo(bar,95) from "foo_bars" group '
                    'by time(5m) into response_times.percentiles.5m.95',

                    'select percentile(value,95) from "response_times" group '
                    'by time(5m) into response_times.percentiles.5m.95'
                ]
            )
Example #10
0
    def get(self, request, app_name):
        try:
            app = request.user.webapp_set.get(name=app_name)
            now = int(datetime.now().timestamp())  # second
            end = int(request.GET.get("end", now))
            start = request.GET.get("start", end - 3600)  # 1 day

            influxdb_client = InfluxDBClient(settings.INFLUXDB["HOST"],
                                             settings.INFLUXDB["PORT"],
                                             settings.INFLUXDB["USERNAME"],
                                             settings.INFLUXDB["PASSWORD"],
                                             settings.INFLUXDB["DBNAME"])
            mesos_app_id = "app-" + app.uuid

            query = "SELECT COUNT(DISTINCT(mesos_task_id)) as instances, MEAN(cpu_usage) as mean_cpu, MEAN(mem_usage) as mean_mem  "
            query += "FROM monitoring "
            query += "WHERE app_uuid = '{}' and time > {}s and time < {}s ".format(
                mesos_app_id, start, end)
            query += "GROUP BY time(10s)"
            metrics = influxdb_client.query(query)
            if metrics:
                return JsonResponse({"data": metrics[0]["points"]})
                return JsonResponse({"data": "sd"})
            else:
                return JsonResponse({"data": query})
        except Exception as e:
            traceback.print_exc()
            return JsonResponse({"data": "11"})
Example #11
0
    def test_query_chunked_unicode(self):
        """Test unicode chunked query for TestInfluxDBClient object."""
        cli = InfluxDBClient(database='db')
        example_object = {
            'points': [
                [1415206212980, 10001, u('unicode-\xcf\x89')],
                [1415197271586, 10001, u('more-unicode-\xcf\x90')]
            ],
            'name': 'foo',
            'columns': [
                'time',
                'sequence_number',
                'val'
            ]
        }
        example_response = \
            json.dumps(example_object) + json.dumps(example_object)

        with requests_mock.Mocker() as m:
            m.register_uri(
                requests_mock.GET,
                "http://localhost:8086/db/db/series",
                text=example_response
            )

            self.assertListEqual(
                cli.query('select * from foo', chunked=True),
                [example_object, example_object]
            )
Example #12
0
 def test_query_bad_precision(self):
     cli = InfluxDBClient()
     with self.assertRaisesRegexp(
             Exception,
             "Invalid time precision is given. \(use 's', 'm', 'ms' or 'u'\)"
     ):
         cli.query('select column_one from foo', time_precision='g')
Example #13
0
 def test_query_bad_precision(self):
     cli = InfluxDBClient()
     with self.assertRaisesRegexp(
         Exception,
         "Invalid time precision is given. \(use 's', 'm', 'ms' or 'u'\)"
     ):
         cli.query('select column_one from foo', time_precision='g')
Example #14
0
    def test_get_continuous_queries(self):
        cli = InfluxDBClient(database='db')

        with requests_mock.Mocker() as m:

            # Tip: put this in a json linter!
            example_response = '[ { "name": "continuous queries", "columns"' \
                               ': [ "time", "id", "query" ], "points": [ [ ' \
                               '0, 1, "select foo(bar,95) from \\"foo_bar' \
                               's\\" group by time(5m) into response_times.' \
                               'percentiles.5m.95" ], [ 0, 2, "select perce' \
                               'ntile(value,95) from \\"response_times\\" g' \
                               'roup by time(5m) into response_times.percen' \
                               'tiles.5m.95" ] ] } ]'

            m.register_uri(requests_mock.GET,
                           "http://localhost:8086/db/db/series",
                           text=example_response)

            self.assertListEqual(cli.get_list_continuous_queries(), [
                'select foo(bar,95) from "foo_bars" group '
                'by time(5m) into response_times.percentiles.5m.95',
                'select percentile(value,95) from "response_times" group '
                'by time(5m) into response_times.percentiles.5m.95'
            ])
Example #15
0
    def test_query_chunked(self):
        cli = InfluxDBClient(database='db')
        example_object = {
            'points': [
                [1415206250119, 40001, 667],
                [1415206244555, 30001, 7],
                [1415206228241, 20001, 788],
                [1415206212980, 10001, 555],
                [1415197271586, 10001, 23]
            ],
            'name': 'foo',
            'columns': [
                'time',
                'sequence_number',
                'val'
            ]
        }
        example_response = \
            json.dumps(example_object) + json.dumps(example_object)

        with requests_mock.Mocker() as m:
            m.register_uri(
                requests_mock.GET,
                "http://localhost:8086/db/db/series",
                text=example_response
            )

            self.assertListEqual(
                cli.query('select * from foo', chunked=True),
                [example_object, example_object]
            )
Example #16
0
class MyStockInflux():

    def __init__(self):
        db_name = 'stock'
        self.client = InfluxDBClient(
            'localhost', 8086, 'root', 'root', db_name)
        # all_dbs_list = self.client.get_list_database()
        # that list comes back like: [{u'name': u'hello_world'}]
        # if db_name not in [str    (x['name']) for x in all_dbs_list]:
        #   print "Creating db {0}".format(db_name)
        #   self.client.create_database(db_name)
        # else:
        #   print "Reusing db {0}".format(db_name)
        self.client.switch_db(db_name)

    def parser(self, symbol):
        points = []
        for h in MyStockHistorical.objects.filter(stock__symbol=symbol).order_by('date_stamp'):
            points = [[h.id, h.date_stamp.strftime('%Y-%m-%d'), time.mktime(h.date_stamp.timetuple()), float(
                h.open_price), float(h.high_price), float(h.low_price), float(h.close_price), float(h.adj_close), h.vol]]

            self.client.write_points([{
                'name': symbol,
                'columns': ['id', 'date', 'time', 'open', 'high', 'low', 'close', 'adj_close', 'vol'],
                'points':points
            }], time_precision='s')

        logger.debug('%s completed' % symbol)
Example #17
0
    def test_write(self):
        with requests_mock.Mocker() as m:
            m.register_uri(
                requests_mock.POST,
                "http://localhost:8086/write"
            )
            cli = InfluxDBClient(database='db')
            cli.write(
                {"database": "mydb",
                 "retentionPolicy": "mypolicy",
                 "points": [{"name": "cpu_load_short",
                             "tags": {"host": "server01",
                                      "region": "us-west"},
                             "timestamp": "2009-11-10T23:00:00Z",
                             "values": {"value": 0.64}}]}
            )

            self.assertEqual(
                json.loads(m.last_request.body),
                {"database": "mydb",
                 "retentionPolicy": "mypolicy",
                 "points": [{"name": "cpu_load_short",
                             "tags": {"host": "server01",
                                      "region": "us-west"},
                             "timestamp": "2009-11-10T23:00:00Z",
                             "values": {"value": 0.64}}]}
            )
Example #18
0
def check_heal(nodes_to_monitor,depl_id):
    if cooldown():
       print('Exiting...\n')
       exit(0)
    c = CloudifyClient('localhost')
    c_influx = InfluxDBClient(host='localhost', port=8086, database='cloudify')
    f=open('/home/ubuntu/logfile','w')
    f.write('in check heal\n')
    c = CloudifyClient('localhost')
    # compare influx data (monitoring) to cloudify desired state

    for node_name in nodes_to_monitor:
        instances=c.node_instances.list(depl_id,node_name)
#        f.write('instances{0}\n'.format(instances))
        for instance in instances:
            q_string='SELECT MEAN(value) FROM /' + depl_id + '\.' + node_name + '\.' + instance.id + '\.cpu_total_system/ GROUP BY time(10s) '\
                     'WHERE  time > now() - 40s'
            f.write('query string is{0}\n'.format(q_string))
            try:
               result=c_influx.query(q_string)
               f.write('result is {0} \n'.format(result))
               if not result:
                open('/home/ubuntu/cooldown','a').close()
                utime('/home/ubuntu/cooldown',None)
                execution_id=c.executions.start(depl_id,'heal',{'node_id': instance.id})
            except InfluxDBClientError as ee:
               f.write('DBClienterror {0}\n'.format(str(ee)))
               f.write('instance id is {0}\n'.format(instance))   
            except Exception as e:
               f.write(str(e))
def check_heal(nodes_to_monitor, depl_id):
    if cooldown():
        print('Exiting...\n')
        exit(0)
    c = CloudifyClient('localhost')
    c_influx = InfluxDBClient(host='localhost', port=8086, database='cloudify')
    f = open('/home/ubuntu/logfile', 'w')
    f.write('in check heal\n')
    c = CloudifyClient('localhost')
    # compare influx data (monitoring) to cloudify desired state

    for node_name in nodes_to_monitor:
        instances = c.node_instances.list(depl_id, node_name)
        #        f.write('instances{0}\n'.format(instances))
        for instance in instances:
            q_string='SELECT MEAN(value) FROM /' + depl_id + '\.' + node_name + '\.' + instance.id + '\.cpu_total_system/ GROUP BY time(10s) '\
                     'WHERE  time > now() - 40s'
            f.write('query string is{0}\n'.format(q_string))
            try:
                result = c_influx.query(q_string)
                f.write('result is {0} \n'.format(result))
                if not result:
                    open('/home/ubuntu/cooldown', 'a').close()
                    utime('/home/ubuntu/cooldown', None)
                    execution_id = c.executions.start(depl_id, 'heal',
                                                      {'node_id': instance.id})
            except InfluxDBClientError as ee:
                f.write('DBClienterror {0}\n'.format(str(ee)))
                f.write('instance id is {0}\n'.format(instance))
            except Exception as e:
                f.write(str(e))
Example #20
0
    def test_request_retry(self, mock_request):
        """Test that two connection errors will be handled."""
        class CustomMock(object):
            """Define CustomMock object."""

            def __init__(self):
                self.i = 0

            def connection_error(self, *args, **kwargs):
                """Test connection error in CustomMock."""
                self.i += 1

                if self.i < 3:
                    raise requests.exceptions.ConnectionError
                else:
                    r = requests.Response()
                    r.status_code = 200
                    return r

        mock_request.side_effect = CustomMock().connection_error

        cli = InfluxDBClient(database='db')
        cli.write_points(
            self.dummy_points
        )
Example #21
0
    def test_request_retry_raises(self, mock_request):
        """Test that three connection errors will not be handled."""
        class CustomMock(object):
            """Define CustomMock object."""
            def __init__(self):
                """Initialize the object."""
                self.i = 0

            def connection_error(self, *args, **kwargs):
                """Test the connection error for CustomMock."""
                self.i += 1

                if self.i < 4:
                    raise requests.exceptions.ConnectionError
                else:
                    r = requests.Response()
                    r.status_code = 200
                    return r

        mock_request.side_effect = CustomMock().connection_error

        cli = InfluxDBClient(database='db')

        with self.assertRaises(requests.exceptions.ConnectionError):
            cli.write_points(self.dummy_points)
Example #22
0
    def test_query_chunked_unicode(self):
        cli = InfluxDBClient(database='db')
        example_object = {
            'points': [
                [1415206212980, 10001, u('unicode-\xcf\x89')],
                [1415197271586, 10001, u('more-unicode-\xcf\x90')]
            ],
            'name': 'foo',
            'columns': [
                'time',
                'sequence_number',
                'val'
            ]
        }
        example_response = \
            json.dumps(example_object) + json.dumps(example_object)

        with requests_mock.Mocker() as m:
            m.register_uri(
                requests_mock.GET,
                "http://localhost:8086/db/db/series",
                text=example_response
            )

            self.assertListEqual(
                cli.query('select * from foo', chunked=True),
                [example_object, example_object]
            )
Example #23
0
 def test_get_database_list_deprecated(self):
     data = [
         {"name": "a_db"}
     ]
     with _mocked_session('get', 200, data):
         cli = InfluxDBClient('host', 8086, 'username', 'password')
         assert len(cli.get_database_list()) == 1
         assert cli.get_database_list()[0]['name'] == 'a_db'
Example #24
0
    def test_scheme(self):
        cli = InfluxDBClient('host', 8086, 'username', 'password', 'database')
        assert cli._baseurl == 'http://host:8086'

        cli = InfluxDBClient(
            'host', 8086, 'username', 'password', 'database', ssl=True
        )
        assert cli._baseurl == 'https://host:8086'
Example #25
0
 def test_get_list_database(self):
     data = [
         {"name": "a_db"}
     ]
     with _mocked_session('get', 200, data):
         cli = InfluxDBClient('host', 8086, 'username', 'password')
         assert len(cli.get_list_database()) == 1
         assert cli.get_list_database()[0]['name'] == 'a_db'
Example #26
0
 def test_write_points_batch_invalid_size(self):
     with requests_mock.Mocker() as m:
         m.register_uri(requests_mock.POST,
                        "http://localhost:8086/db/db/series")
         cli = InfluxDBClient('localhost', 8086, 'username', 'password',
                              'db')
         cli.write_points(data=self.dummy_points, batch_size=-2)
     self.assertEqual(1, m.call_count)
Example #27
0
 def test_write_points_batch_invalid_size(self):
     with requests_mock.Mocker() as m:
         m.register_uri(requests_mock.POST,
                        "http://localhost:8086/db/db/series")
         cli = InfluxDBClient('localhost', 8086,
                              'username', 'password', 'db')
         cli.write_points(data=self.dummy_points, batch_size=-2)
     self.assertEqual(1, m.call_count)
Example #28
0
 def test_get_database_list_deprecated(self):
     data = [
         {"name": "a_db"}
     ]
     with _mocked_session('get', 200, data):
         cli = InfluxDBClient('host', 8086, 'username', 'password')
         self.assertEqual(len(cli.get_database_list()), 1)
         self.assertEqual(cli.get_database_list()[0]['name'], 'a_db')
def test_case_of_n_clients(num_of_clients):
    global influx_client
    # Get Client
    influx_client = InfluxDBClient(HOST, PORT, USER, PASSWORD, DBNAME)

    # Greate Database
    print("Create database: " + DBNAME)
    try:
        influx_client.create_database(DBNAME)
    except InfluxDBClientError:
        # Drop and create
        influx_client.delete_database(DBNAME)
        #influx_client.drop_database(DBNAME)
        influx_client.create_database(DBNAME)

    # Add retention policy
    print("Create a retention policy")
    #influx_client.create_retention_policy(retention_policy_name, '3d', 3, default=True)

    # Init benchmark_helper
    STARTED_TIMESTAMP = int(time.time())
    influx_benchmark_helper.init(STARTED_TIMESTAMP, RUNNING_SECONDS,
                                 STATS_INTERVAL, BATCH_SIZE)

    print("Generating  clients.. "),
    clients = generate_clients(num_of_clients)
    print("Done!\n")

    print("Run multiple clients and do batch writes "),
    # Run the clients! Do batch writes with multi clients
    map(lambda thread: thread.start(), clients)
    print("Done!\n")

    # Create and start the print stats thread
    stats_thread = Thread(target=print_stats_worker(num_of_clients))
    stats_thread.daemon = True
    stats_thread.start()

    # And join them all but the stats, that we don't care about
    map(lambda thread: thread.join(), clients)
    # A call to thread1.join() blocks the thread in which you're making the call, until thread1 is finished.
    # It's like wait_until_finished(thread1).

    # Record final results into report.txt file
    is_final_result = True
    print_stats(num_of_clients, is_final_result)

    time.sleep(2)

    # Do query
    # query = "SELECT MEAN(value) FROM %s WHERE time > now() - 20d GROUP BY time(500m)" % (series_name)
    # result = influx_client.query(query)
    # print("Result: {0}".format(result))

    # Drop Database
    print("Drop database: " + DBNAME)
    influx_client.delete_database(DBNAME)
Example #30
0
    def test_scheme(self):
        """Test database scheme for TestInfluxDBClient object."""
        cli = InfluxDBClient('host', 8086, 'username', 'password', 'database')
        self.assertEqual(cli._baseurl, 'http://host:8086')

        cli = InfluxDBClient(
            'host', 8086, 'username', 'password', 'database', ssl=True
        )
        self.assertEqual(cli._baseurl, 'https://host:8086')
Example #31
0
 def test_get_list_database(self):
     """Test get list of databases for TestInfluxDBClient."""
     data = [
         {"name": "a_db"}
     ]
     with _mocked_session('get', 200, data):
         cli = InfluxDBClient('host', 8086, 'username', 'password')
         self.assertEqual(len(cli.get_list_database()), 1)
         self.assertEqual(cli.get_list_database()[0]['name'], 'a_db')
Example #32
0
    def test_add_database_user_bad_permissions(self):
        cli = InfluxDBClient()

        with self.assertRaisesRegexp(
                Exception,
                "'permissions' must be \(readFrom, writeTo\) tuple"):
            cli.add_database_user(new_password='******',
                                  new_username='******',
                                  permissions=('hello', 'hello', 'hello'))
Example #33
0
    def test_delete_database_user(self):
        with requests_mock.Mocker() as m:
            m.register_uri(requests_mock.DELETE,
                           "http://localhost:8086/db/db/users/paul")

            cli = InfluxDBClient(database='db')
            cli.delete_database_user(username='******')

            self.assertIsNone(m.last_request.body)
Example #34
0
 def test_write_points_bad_precision(self):
     cli = InfluxDBClient()
     with self.assertRaisesRegexp(
         Exception,
         "Invalid time precision is given. \(use 's', 'm', 'ms' or 'u'\)"
     ):
         cli.write_points(
             self.dummy_points,
             time_precision='g'
         )
Example #35
0
    def test_delete_points(self):
        with _mocked_session('delete', 204) as mocked:
            cli = InfluxDBClient('host', 8086, 'username', 'password', 'db')
            assert cli.delete_points("foo") is True

            assert len(mocked.call_args_list) == 1
            args, kwds = mocked.call_args_list[0]

            assert kwds['params'] == {'u': 'username', 'p': 'password'}
            assert kwds['url'] == 'http://host:8086/db/db/series/foo'
Example #36
0
    def test_alter_database_admin(self):
        with requests_mock.Mocker() as m:
            m.register_uri(requests_mock.POST,
                           "http://localhost:8086/db/db/users/paul")

            cli = InfluxDBClient(database='db')
            cli.alter_database_admin(username='******', is_admin=False)

            self.assertDictEqual(json.loads(m.last_request.body),
                                 {'admin': False})
Example #37
0
    def test_alter_database_user_password(self):
        with requests_mock.Mocker() as m:
            m.register_uri(requests_mock.POST,
                           "http://localhost:8086/db/db/users/paul")

            cli = InfluxDBClient(database='db')
            cli.alter_database_user(username='******', password='******')

            self.assertDictEqual(json.loads(m.last_request.body),
                                 {'password': '******'})
Example #38
0
    def test_write_points_string(self):
        with requests_mock.Mocker() as m:
            m.register_uri(requests_mock.POST,
                           "http://localhost:8086/db/db/series")

            cli = InfluxDBClient(database='db')
            cli.write_points(str(json.dumps(self.dummy_points)))

            self.assertListEqual(json.loads(m.last_request.body),
                                 self.dummy_points)
Example #39
0
    def test_update_cluster_admin_password(self):
        with requests_mock.Mocker() as m:
            m.register_uri(requests_mock.POST,
                           "http://localhost:8086/cluster_admins/paul")

            cli = InfluxDBClient(database='db')
            cli.update_cluster_admin_password(username='******',
                                              new_password='******')

            self.assertDictEqual(json.loads(m.last_request.body),
                                 {'password': '******'})
Example #40
0
 def connect(self):
     try:
         self.logger.info("Connecting to InfluxDB server at " + self.influx_host + ':' + str(self.influx_port) + " (database " + self.influx_db + ")")
         client = InfluxDBClient(self.influx_host, self.influx_port, self.influx_user, self.influx_pwd, self.influx_db)
         # Test connection
         client.query("list series")
         self.client = client
     except:
         message = "Couldn't connect to influxdb"
         self.logger.error(message)
         raise Exception(message)
Example #41
0
def twitch_parse(data):
	for item in data['streams']: #check the json responce for the item details
		twitch_game_type = item['game']
		twitch_user = item['channel']['display_name']
		current_viewer = item['viewers']
		stream = item['_id']
		current_fav = item['channel']['followers']
		print(twitch_game_type, twitch_user, current_viewer, stream, current_fav)
		json_body = [{"points":[[twitch_game_type,twitch_user,current_viewer,stream,current_fav]],"name":"twitch_stream_counting","columns":["games_type","twitch_username","current_viewer_count","steam_id","current_favorites"]}]
		db = InfluxDBClient('74.82.3.100', 8086, 'poker_user', 'pokergraph', 'pokergraph')
		db.write_points(json_body)
Example #42
0
class InfluxBackend(BaseMetricsBackend):
    def __init__(self, **kwargs):
        kwargs.setdefault('use_udp', True)
        self.client = InfluxDBClient(**kwargs)

    def write(self, name, **data):
        self.client.write_points([{
            "name": name,
            "columns": list(data.keys()),
            "points": [list(data.values())]
        }])
Example #43
0
    def test_delete_database_user(self):
        with requests_mock.Mocker() as m:
            m.register_uri(
                requests_mock.DELETE,
                "http://localhost:8086/db/db/users/paul"
            )

            cli = InfluxDBClient(database='db')
            cli.delete_database_user(username='******')

            self.assertIsNone(m.last_request.body)
Example #44
0
    def test_delete_points(self):
        with _mocked_session('delete', 204) as mocked:
            cli = InfluxDBClient('host', 8086, 'username', 'password', 'db')
            self.assertTrue(cli.delete_points("foo"))

            self.assertEqual(len(mocked.call_args_list), 1)
            args, kwds = mocked.call_args_list[0]

            self.assertEqual(kwds['params'],
                             {'u': 'username', 'p': 'password'})
            self.assertEqual(kwds['url'], 'http://host:8086/db/db/series/foo')
Example #45
0
 def __init__(self, url, username=None, password=None):
     self.url = url
     parse = urlparse.urlparse(url)
     self.username = username or parse.username
     self.password = password or parse.password
     self.hostname = parse.hostname
     self.port = parse.port
     self.idb = InfluxDBClient(host=self.hostname,
                               port=self.port,
                               username=username,
                               password=password)
Example #46
0
    def test_delete_cluster_admin(self):
        with requests_mock.Mocker() as m:
            m.register_uri(
                requests_mock.DELETE,
                "http://localhost:8086/cluster_admins/paul",
                status_code=200,
            )

            cli = InfluxDBClient(database='db')
            cli.delete_cluster_admin(username='******')

            self.assertIsNone(m.last_request.body)
Example #47
0
class Logger(object):
    store_influx = False
    store_rrd = False
    influx_db = {}
    series = []
    log_info = True
    log_error = True

    def __init__(self, influx_config, rrd=False, influx=True, log_error=True, log_info=True):
        self.store_influx = influx
        if self.store_influx:
            self.influx_db = InfluxDBClient(
                influx_config["host"],
                influx_config["port"],
                influx_config["user"],
                influx_config["pw"],
                influx_config["db"],
            )

            # if(self.store_rrd):

    def append_series(self, data):
        self.series.append(data)

    def write_series(self):
        if self.store_influx:
            # print "write_series"
            try:
                if self.series != []:
                    self.influx_db.write_points(self.series)
                    self.series = []
            except InfluxDBClientError:
                print sys.exc_info()
                self.influx_ClientError()
            except requests.exceptions.ConnectionError:
                print sys.exc_info()
                self.influx_ConnectionError()
        else:
            self.series = []

    def log(self, str):
        if self.log_info:
            print colored("logger:", "cyan"), colored(str, "magenta")

    def log_error(self, str):
        if self.log_error:
            print colored("error:", "red"), colored(str, "magenta")

    def influx_ConnectionError(self):
        self.log_error("INFLUX ConnectionError")

    def influx_ClientError(self):
        self.log_error("INFLUX ClientError")
Example #48
0
    def test_update_database_user_password_current_user(self):
        cli = InfluxDBClient(username='******',
                             password='******',
                             database='database')
        with requests_mock.Mocker() as m:
            m.register_uri(requests_mock.POST,
                           "http://localhost:8086/db/database/users/root")

            cli.update_database_user_password(username='******',
                                              new_password='******')

            self.assertEqual(cli._password, 'bye')
Example #49
0
    def test_alter_database_user_permissions(self):
        with requests_mock.Mocker() as m:
            m.register_uri(requests_mock.POST,
                           "http://localhost:8086/db/db/users/paul")

            cli = InfluxDBClient(database='db')
            cli.alter_database_user(username='******', permissions=('^$', '.*'))

            self.assertDictEqual(json.loads(m.last_request.body), {
                'readFrom': '^$',
                'writeTo': '.*'
            })
Example #50
0
    def test_add_database_user_bad_permissions(self):
        cli = InfluxDBClient()

        with self.assertRaisesRegexp(
                Exception,
                "'permissions' must be \(readFrom, writeTo\) tuple"
        ):
            cli.add_database_user(
                new_password='******',
                new_username='******',
                permissions=('hello', 'hello', 'hello')
            )
Example #51
0
    def test_delete_cluster_admin(self):
        with requests_mock.Mocker() as m:
            m.register_uri(
                requests_mock.DELETE,
                "http://localhost:8086/cluster_admins/paul",
                status_code=200,
            )

            cli = InfluxDBClient(database='db')
            cli.delete_cluster_admin(username='******')

            self.assertIsNone(m.last_request.body)
def test_case_of_n_clients(num_of_clients):
    global influx_client
    # Get Client
    influx_client = InfluxDBClient(HOST, PORT, USER, PASSWORD, DBNAME)

    # Greate Database
    print("Create database: " + DBNAME)
    try:
        influx_client.create_database(DBNAME)
    except InfluxDBClientError:
        # Drop and create
        influx_client.delete_database(DBNAME)
        #influx_client.drop_database(DBNAME)
        influx_client.create_database(DBNAME)

    # Add retention policy
    print("Create a retention policy")
    #influx_client.create_retention_policy(retention_policy_name, '3d', 3, default=True)

    # Init benchmark_helper
    STARTED_TIMESTAMP = int(time.time())
    influx_benchmark_helper.init(STARTED_TIMESTAMP, RUNNING_SECONDS, STATS_INTERVAL, BATCH_SIZE)


    print("Generating  clients.. "),
    clients = generate_clients(num_of_clients)
    print("Done!\n")

    print("Run multiple clients and do batch writes "),
    # Run the clients! Do batch writes with multi clients
    map(lambda thread: thread.start(), clients)
    print("Done!\n")

    # Create and start the print stats thread
    stats_thread = Thread(target=print_stats_worker(num_of_clients))
    stats_thread.daemon = True
    stats_thread.start()

    # And join them all but the stats, that we don't care about
    map(lambda thread: thread.join(), clients)
    # A call to thread1.join() blocks the thread in which you're making the call, until thread1 is finished.
    # It's like wait_until_finished(thread1).

    # Record final results into report.txt file
    is_final_result = True
    print_stats(num_of_clients, is_final_result)

    time.sleep(2)

    # Do query
    # query = "SELECT MEAN(value) FROM %s WHERE time > now() - 20d GROUP BY time(500m)" % (series_name)
    # result = influx_client.query(query)
    # print("Result: {0}".format(result))

    # Drop Database
    print("Drop database: " + DBNAME)
    influx_client.delete_database(DBNAME)
Example #53
0
def check_heal(nodes_to_monitor,
               deployment_id,
               cooldown_path,
               cooldown_time,
               time_diff,
               influxdb_port,
               database,
               host,
               logger):
    if cool_down(cooldown_path, cooldown_time):
        logger.info('Exiting from check_heal...')
        exit(0)
    logger.info('In check_heal. Getting clients.')
    influx_client = InfluxDBClient(host=host,
                                   port=int(influxdb_port),
                                   database=database)
    cloudify_client = CloudifyClient(host)
    # compare influx data (monitoring) to Cloudify desired state

    for node_name in nodes_to_monitor:
        instances = cloudify_client.node_instances.list(deployment_id,
                                                        node_name)
        for instance in instances:
            logger.info("Deployment_id: %s, node_name: %s, instance_id: %s "
                        % (deployment_id, node_name, instance.id))
            q_string = 'SELECT MEAN(value) FROM ' \
                       '/{0}\.{1}\.{2}\.cpu_total_system/ GROUP BY time(10s) ' \
                       'WHERE  time > now() - {3}s'.format(deployment_id,
                                                           node_name,
                                                           instance.id,
                                                           time_diff)
            logger.info('query string is:{0}'.format(q_string))
            try:
                result = influx_client.query(q_string)
                logger.info('Query result is {0} \n'.format(result))
                if not result:
                    logger.info("Opening {0} and closing it\n".format(
                        cooldown_path))
                    open(cooldown_path, 'a').close()
                    logger.info("utime {0}\n".format(cooldown_path))
                    os.utime(cooldown_path, None)
                    logger.info("Healing {0}\n".format(instance.id))
                    execution = cloudify_client.executions.start(
                        deployment_id,
                        'heal',
                        {'node_instance_id': instance.id})
                    logger.info('execution: {0}'.format(str(execution)))
            except InfluxDBClientError as ee:
                logger.info('DBClienterror {0}\n'.format(str(ee)))
                logger.info('instance id is {0}\n'.format(str(instance.id)))
            except Exception as e:
                logger.error('An error : %s' % str(e))
Example #54
0
    def test_write_bad_precision_udp(self):
        cli = InfluxDBClient(
            'localhost', 8086, 'root', 'root',
            'test', use_udp=True, udp_port=4444
        )

        with self.assertRaisesRegexp(
                Exception,
                "InfluxDB only supports seconds precision for udp writes"
        ):
            cli.write_points(
                self.dummy_points,
                time_precision='ms'
            )
Example #55
0
def run_query(input_str):
    influx_client = InfluxDBClient(host='localhost', port=8086, database='cloudify')
    q_string = 'SELECT value FROM /' + '.*' + input_str + '.*' + '/' + ' WHERE  time > now() - 40s'
    try:
        result = influx_client.query(q_string)
        print 'Query result is {0} \n'.format(result)
        if not result:
            print "There are no results {0}".format()
        else:
            print result
    except InfluxDBClientError as ee:
        print 'DBClienterror {0}\n'.format(str(ee))
    except Exception as e:
        print str(e)
Example #56
0
    def test_write_points_udp(self):
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        port = random.randint(4000, 8000)
        s.bind(('0.0.0.0', port))

        cli = InfluxDBClient(
            'localhost', 8086, 'root', 'root',
            'test', use_udp=True, udp_port=port
        )
        cli.write_points(self.dummy_points)

        received_data, addr = s.recvfrom(1024)

        self.assertEqual(self.dummy_points,
                         json.loads(received_data.decode(), strict=True))
Example #57
0
 def test_query(self):
     data = [
         {
             "name": "foo",
             "columns": ["time", "sequence_number", "column_one"],
             "points": [
                 [1383876043, 16, "2"], [1383876043, 15, "1"],
                 [1383876035, 14, "2"], [1383876035, 13, "1"]
             ]
         }
     ]
     with _mocked_session('get', 200, data):
         cli = InfluxDBClient('host', 8086, 'username', 'password', 'db')
         result = cli.query('select column_one from foo;')
         self.assertEqual(len(result[0]['points']), 4)