-
Notifications
You must be signed in to change notification settings - Fork 0
/
nmsprime_get_env.py
executable file
·85 lines (64 loc) · 1.91 KB
/
nmsprime_get_env.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
#!/usr/bin/env python3
import os
import os.path
from pprint import pprint
import sys
nmsprime_path = '/var/www/nmsprime'
tasks = {
'db' : 'DB_DATABASE',
'host' : 'DB_HOST',
'password' : 'DB_PASSWORD',
'user' : 'DB_USERNAME',
'db_ccc' : 'CCC_DB_DATABASE',
'host_ccc' : 'CCC_DB_HOST',
'password_ccc' : 'CCC_DB_PASSWORD',
'user_ccc' : 'CCC_DB_USERNAME',
'db_cacti' : 'CACTI_DB_DATABASE',
'host_cacti' : 'CACTI_DB_HOST',
'password_cacti' : 'CACTI_DB_PASSWORD',
'user_cacti' : 'CACTI_DB_USERNAME',
'db_icinga2' : 'ICINGA2_DB_DATABASE',
'host_icinga2' : 'ICINGA2_DB_HOST',
'password_icinga2' : 'ICINGA2_DB_PASSWORD',
'user_icinga2' : 'ICINGA2_DB_USERNAME',
}
mappings = {v: k for k, v in tasks.items()}
envs = ['local', 'global']
env_path = '/etc/nmsprime/env'
def error():
print('Usage: {} [{}] [{}]'.format(sys.argv[0], '|'.join(tasks.keys()), '|'.join(envs)))
sys.exit(1)
if (len(sys.argv) is not 3):
error()
if sys.argv[1] not in tasks.keys():
error()
if sys.argv[2] not in envs:
error()
env_files = []
if sys.argv[2] == 'local' or sys.argv[2] == 'global':
for f in os.listdir(env_path):
if f.endswith('.env'):
env_files.append(f)
def getValue(rawData):
data = rawData.strip()
data = data.replace('"', '')
data = data.replace("'", "")
data = data.split(',')[0]
return data
lines = []
for env_file in env_files:
with open(os.path.join(env_path, env_file), 'r') as fh:
newlines = fh.readlines()
for l in newlines:
lines.append(l)
data = {}
for line in lines:
line = line.strip()
# ignore comments
if line.startswith('#'):
continue
parts = line.split('=')
key = parts[0].strip()
if key in mappings.keys():
data[mappings[key]] = getValue(parts[1])
print(data[sys.argv[1]])