-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil_dbase.py
More file actions
90 lines (80 loc) · 3.2 KB
/
util_dbase.py
File metadata and controls
90 lines (80 loc) · 3.2 KB
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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import os.path
from influxdb import InfluxDBClient
from util_funct import get_json_data_from_file, log_event, log_error
def write_to_dbase(jsony_body, db_name):
"""
:param json array,database name
:return:
"""
currentpathdir = os.path.dirname(os.path.realpath(__file__))
cred_file = os.path.join(currentpathdir, "credential.txt")
try:
data_json = get_json_data_from_file(cred_file)
except:
print("**********************************")
print("* Error : no file credential.txt *")
print("**********************************")
return
db_user = data_json['DATABASE_USER_ADMIN']
db_password = data_json['DATABASE_PASSWORD_ADMIN']
client = InfluxDBClient('localhost', 8086, db_user, db_password, db_name)
dbs = client.get_list_database()
d = next((d for d in dbs if d['name'] == db_name), None)
if d is None:
client.create_database(db_name)
if db_name == 'sysinfo':
client.create_retention_policy('autogen', '7d', '1', db_name, False, '7d')
else:
try:
client.create_retention_policy('autogen', '0s', '1', db_name, False, '31d')
except:
log_error("Database policy error when creating database")
# grant privilege for grafana reader user
db_user = data_json['DATABASE_USER_READER']
client.grant_privilege('read', db_name, db_user)
# grant privilege for grafana admin user
db_user = data_json['DATABASE_USER_ADMIN']
client.grant_privilege('all', db_name, db_user)
result = client.write_points(jsony_body)
if not result:
log_error("Database write error")
def init_dbase():
"""
:param none
:return: none
"""
print("**********************************")
print("* INIT DBASE USERS *")
print("**********************************")
currentpathdir = os.path.dirname(os.path.realpath(__file__))
cred_file = os.path.join(currentpathdir, "credential.txt")
try:
data_json = get_json_data_from_file(cred_file)
except:
print("**********************************")
print("* Error : no file credential.txt *")
print("**********************************")
return
client = InfluxDBClient('localhost', 8086)
print("**********************************")
print("* CREATE INFLUXDB GRAFANA USER *")
print("**********************************")
# create a user for grafana
db_user = data_json['DATABASE_USER_READER']
db_password = data_json['DATABASE_PASSWORD_READER']
client.create_user(db_user, db_password, admin=False)
print("**********************************")
print("* CREATE INFLUXDB ADMIN USER *")
print("**********************************")
# create admin user with read/write privilege
db_user = data_json['DATABASE_USER_ADMIN']
db_password = data_json['DATABASE_PASSWORD_ADMIN']
client.create_user(db_user, db_password, admin=True)
if __name__ == "__main__":
'''
start this script for creating the databases
option "auth-enabled = false" must be declared into /etc/influxdb/influxdb.conf
'''
init_dbase()