-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
influx.py
643 lines (462 loc) · 15.7 KB
/
influx.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
# -*- coding: utf-8 -*-
'''
InfluxDB - A distributed time series database
Module to provide InfluxDB compatibility to Salt
(compatible with InfluxDB version 0.5+)
.. versionadded:: 2014.7.0
:depends: - influxdb Python module
:configuration: This module accepts connection configuration details either as
parameters or as configuration settings in /etc/salt/minion on the relevant
minions::
influxdb.host: 'localhost'
influxdb.port: 8086
influxdb.user: 'root'
influxdb.password: 'root'
This data can also be passed into pillar. Options passed into opts will
overwrite options passed into pillar.
'''
# Import Python libs
from __future__ import absolute_import
try:
import influxdb
HAS_INFLUXDB = True
except ImportError:
HAS_INFLUXDB = False
import logging
log = logging.getLogger(__name__)
# Define the module's virtual name
__virtualname__ = 'influxdb'
def __virtual__():
'''
Only load if influxdb lib is present
'''
if HAS_INFLUXDB:
return __virtualname__
return False
def _client(user=None, password=None, host=None, port=None):
if not user:
user = __salt__['config.option']('influxdb.user', 'root')
if not password:
password = __salt__['config.option']('influxdb.password', 'root')
if not host:
host = __salt__['config.option']('influxdb.host', 'localhost')
if not port:
port = __salt__['config.option']('influxdb.port', 8086)
return influxdb.InfluxDBClient(
host=host, port=port, username=user, password=password)
def db_list(user=None, password=None, host=None, port=None):
"""
List all InfluxDB databases
user
The user to connect as
password
The password of the user
host
The host to connect to
port
The port to connect to
CLI Example:
.. code-block:: bash
salt '*' influxdb.db_list
salt '*' influxdb.db_list <user> <password> <host> <port>
"""
client = _client(user=user, password=password, host=host, port=port)
return client.get_list_database()
def db_exists(name, user=None, password=None, host=None, port=None):
'''
Checks if a database exists in InfluxDB
name
Database name to create
user
The user to connect as
password
The password of the user
host
The host to connect to
port
The port to connect to
CLI Example:
.. code-block:: bash
salt '*' influxdb.db_exists <name>
salt '*' influxdb.db_exists <name> <user> <password> <host> <port>
'''
dbs = db_list(user, password, host, port)
if not isinstance(dbs, list):
return False
return name in [db['name'] for db in dbs]
def db_create(name, user=None, password=None, host=None, port=None):
"""
Create a database
name
Database name to create
user
The user to connect as
password
The password of the user
host
The host to connect to
port
The port to connect to
CLI Example:
.. code-block:: bash
salt '*' influxdb.db_create <name>
salt '*' influxdb.db_create <name> <user> <password> <host> <port>
"""
if db_exists(name, user, password, host, port):
log.info('DB {0!r} already exists'.format(name))
return False
client = _client(user=user, password=password, host=host, port=port)
return client.create_database(name)
def db_remove(name, user=None, password=None, host=None, port=None):
"""
Remove a database
name
Database name to remove
user
The user to connect as
password
The password of the user
host
The host to connect to
port
The port to connect to
CLI Example:
.. code-block:: bash
salt '*' influxdb.db_remove <name>
salt '*' influxdb.db_remove <name> <user> <password> <host> <port>
"""
if not db_exists(name, user, password, host, port):
log.info('DB {0!r} does not exist'.format(name))
return False
client = _client(user=user, password=password, host=host, port=port)
return client.delete_database(name)
def user_list(database=None, user=None, password=None, host=None, port=None):
"""
List cluster admins or database users.
If a database is specified: it will return database users list.
If a database is not specified: it will return cluster admins list.
database
The database to list the users from
user
The user to connect as
password
The password of the user
host
The host to connect to
port
The port to connect to
CLI Example:
.. code-block:: bash
salt '*' influxdb.user_list
salt '*' influxdb.user_list <database>
salt '*' influxdb.user_list <database> <user> <password> <host> <port>
"""
client = _client(user=user, password=password, host=host, port=port)
if database:
client.switch_database(database)
if hasattr(client, 'get_list_cluster_admins') and not database:
return client.get_list_cluster_admins()
return client.get_list_users()
def user_exists(
name, database=None, user=None, password=None, host=None, port=None):
'''
Checks if a cluster admin or database user exists.
If a database is specified: it will check for database user existence.
If a database is not specified: it will check for cluster admin existence.
name
User name
database
The database to check for the user to exist
user
The user to connect as
password
The password of the user
host
The host to connect to
port
The port to connect to
CLI Example:
.. code-block:: bash
salt '*' influxdb.user_exists <name>
salt '*' influxdb.user_exists <name> <database>
salt '*' influxdb.user_exists <name> <database> <user> <password> <host> <port>
'''
users = user_list(database, user, password, host, port)
if not isinstance(users, list):
return False
for user in users:
# the dict key could be different depending on influxdb version
username = user.get('user', user.get('name'))
if username:
if username == name:
return True
else:
log.warning('Could not find username in user: %s', user)
return False
def user_create(name, passwd, database=None, user=None, password=None,
host=None, port=None):
"""
Create a cluster admin or a database user.
If a database is specified: it will create database user.
If a database is not specified: it will create a cluster admin.
name
User name for the new user to create
passwd
Password for the new user to create
database
The database to create the user in
user
The user to connect as
password
The password of the user
host
The host to connect to
port
The port to connect to
CLI Example:
.. code-block:: bash
salt '*' influxdb.user_create <name> <passwd>
salt '*' influxdb.user_create <name> <passwd> <database>
salt '*' influxdb.user_create <name> <passwd> <database> <user> <password> <host> <port>
"""
if user_exists(name, database, user, password, host, port):
if database:
log.info('User {0!r} already exists for DB {1!r}'.format(
name, database))
else:
log.info('Cluster admin {0!r} already exists'.format(name))
return False
client = _client(user=user, password=password, host=host, port=port)
if database:
client.switch_database(database)
return client.add_database_user(name, passwd)
return client.add_cluster_admin(name, passwd)
def user_chpass(name, passwd, database=None, user=None, password=None,
host=None, port=None):
"""
Change password for a cluster admin or a database user.
If a database is specified: it will update database user password.
If a database is not specified: it will update cluster admin password.
name
User name for whom to change the password
passwd
New password
database
The database on which to operate
user
The user to connect as
password
The password of the user
host
The host to connect to
port
The port to connect to
CLI Example:
.. code-block:: bash
salt '*' influxdb.user_chpass <name> <passwd>
salt '*' influxdb.user_chpass <name> <passwd> <database>
salt '*' influxdb.user_chpass <name> <passwd> <database> <user> <password> <host> <port>
"""
if not user_exists(name, database, user, password, host, port):
if database:
log.info('User {0!r} does not exist for DB {1!r}'.format(
name, database))
else:
log.info('Cluster admin {0!r} does not exist'.format(name))
return False
client = _client(user=user, password=password, host=host, port=port)
if database:
client.switch_database(database)
return client.update_database_user_password(name, passwd)
return client.update_cluster_admin_password(name, passwd)
def user_remove(name, database=None, user=None, password=None, host=None,
port=None):
"""
Remove a cluster admin or a database user.
If a database is specified: it will remove the database user.
If a database is not specified: it will remove the cluster admin.
name
User name to remove
database
The database to remove the user from
user
User name for the new user to delete
user
The user to connect as
password
The password of the user
host
The host to connect to
port
The port to connect to
CLI Example:
.. code-block:: bash
salt '*' influxdb.user_remove <name>
salt '*' influxdb.user_remove <name> <database>
salt '*' influxdb.user_remove <name> <database> <user> <password> <host> <port>
"""
if not user_exists(name, database, user, password, host, port):
if database:
log.info('User {0!r} does not exist for DB {1!r}'.format(
name, database))
else:
log.info('Cluster admin {0!r} does not exist'.format(name))
return False
client = _client(user=user, password=password, host=host, port=port)
if database:
client.switch_database(database)
return client.delete_database_user(name)
return client.delete_cluster_admin(name)
def retention_policy_get(database,
name,
user=None,
password=None,
host=None,
port=None):
'''
Get an existing retention policy.
database
The database to operate on.
name
Name of the policy to modify.
CLI Example:
.. code-block:: bash
salt '*' influxdb.retention_policy_get metrics default
'''
client = _client(user=user, password=password, host=host, port=port)
for policy in client.get_list_retention_policies(database):
if policy['name'] == name:
return policy
return None
def retention_policy_exists(database,
name,
user=None,
password=None,
host=None,
port=None):
'''
Check if a retention policy exists.
database
The database to operate on.
name
Name of the policy to modify.
CLI Example:
.. code-block:: bash
salt '*' influxdb.retention_policy_exists metrics default
'''
policy = retention_policy_get(name, database, user, password, host, port)
return policy is not None
def retention_policy_add(database,
name,
duration,
replication,
default=False,
user=None,
password=None,
host=None,
port=None):
'''
Add a retention policy.
database
The database to operate on.
name
Name of the policy to modify.
duration
How long InfluxDB keeps the data.
replication
How many copies of the data are stored in the cluster.
default
Whether this policy should be the default or not. Default is False.
CLI Example:
.. code-block:: bash
salt '*' influxdb.retention_policy_add metrics default 1d 1
'''
client = _client(user=user, password=password, host=host, port=port)
client.create_retention_policy(name, duration, replication, database, default)
return True
def retention_policy_alter(database,
name,
duration,
replication,
default=False,
user=None,
password=None,
host=None,
port=None):
'''
Modify an existing retention policy.
database
The database to operate on.
name
Name of the policy to modify.
duration
How long InfluxDB keeps the data.
replication
How many copies of the data are stored in the cluster.
default
Whether this policy should be the default or not. Default is False.
CLI Example:
.. code-block:: bash
salt '*' influxdb.retention_policy_modify metrics default 1d 1
'''
client = _client(user=user, password=password, host=host, port=port)
client.alter_retention_policy(name, database, duration, replication, default)
return True
def query(database, query, time_precision='s', chunked=False, user=None,
password=None, host=None, port=None):
"""
Querying data
database
The database to query
query
Query to be executed
time_precision
Time precision to use ('s', 'm', or 'u')
chunked
Whether is chunked or not
user
The user to connect as
password
The password of the user
host
The host to connect to
port
The port to connect to
CLI Example:
.. code-block:: bash
salt '*' influxdb.query <database> <query>
salt '*' influxdb.query <database> <query> <time_precision> <chunked> <user> <password> <host> <port>
"""
client = _client(user=user, password=password, host=host, port=port)
client.switch_database(database)
return client.query(query, time_precision=time_precision, chunked=chunked)
def login_test(name, password, database=None, host=None, port=None):
'''
Checks if a credential pair can log in at all.
If a database is specified: it will check for database user existence.
If a database is not specified: it will check for cluster admin existence.
name
The user to connect as
password
The password of the user
database
The database to try to log in to
host
The host to connect to
port
The port to connect to
CLI Example:
.. code-block:: bash
salt '*' influxdb.login_test <name>
salt '*' influxdb.login_test <name> <database>
salt '*' influxdb.login_test <name> <database> <user> <password> <host> <port>
'''
try:
client = _client(user=name, password=password, host=host, port=port)
client.get_list_database()
return True
except influxdb.client.InfluxDBClientError as e:
if e.code == 401:
return False
else:
raise