-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_nntsc_db
executable file
·162 lines (137 loc) · 5.14 KB
/
build_nntsc_db
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
#!/usr/bin/env python3
#
# This file is part of NNTSC.
#
# Copyright (C) 2013-2017 The University of Waikato, Hamilton, New Zealand.
#
# Authors: Shane Alcock
# Brendon Jones
# Andy Bell
#
# All rights reserved.
#
# This code has been developed by the WAND Network Research Group at the
# University of Waikato. For further information please see
# http://www.wand.net.nz/
#
# NNTSC is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# NNTSC is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with NNTSC; if not, write to the Free Software Foundation, Inc.
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# Please report any bugs, questions or comments to [email protected]
#
import sys
import getopt
from influxdb import InfluxDBClient
from influxdb.exceptions import InfluxDBClientError
from libnntsc.database import DBInsert
from libnntsc.influx import InfluxInsertor
from libnntsc.configurator import *
from libnntsc.importer import import_parsers
from libnntsc.parsers.rrd import insert_rrd_streams
from libnntsc.dberrorcodes import *
import libnntscclient.logger as logger
def print_usage(prog):
print("Usage for %s\n" % prog)
print("Available options:")
print(" -C <filename> ")
print(" Specifies the location of the configuration file")
print(" -F ")
print(" Wipe and rebuild the database")
print(" WARNING: all time series data will be lost")
print(" -h ")
print(" Display this usage text")
sys.exit(0)
clean_db = False
enabled_modules = []
opts, rest = getopt.getopt(sys.argv[1:], 'D:C:Fh')
for o, a in opts:
if o == '-C':
conf_fname = a
if o == '-F':
clean_db = True
if o == '-h':
print_usage(sys.argv[0])
nntsc_conf = load_nntsc_config(conf_fname)
if nntsc_conf == 0:
sys.exit(1)
dbconf = get_nntsc_db_config(nntsc_conf)
if dbconf == {}:
sys.exit(1)
for module in nntsc_conf.options('modules'):
if nntsc_conf.get('modules', module) == "yes":
enabled_modules.append(module)
modules = import_parsers(enabled_modules)
if dbconf["name"] == "":
print("No database name specified in the config file. Please edit your config file (%s)" % conf_fname, file=sys.stderr)
sys.exit(1)
db = DBInsert(dbconf["name"], dbconf["user"], dbconf["pass"], dbconf["host"])
db.connect_db(15)
try:
db.build_databases(modules, new=clean_db)
except DBQueryException as e:
logger.log("Failed to create tables for nntsc database")
sys.exit(1)
influxconf = get_influx_config(nntsc_conf)
if influxconf == {}:
logger.log("Invalid options for influx database")
sys.exit(1)
influxdb = None
if influxconf["useinflux"] and "amp" in modules.keys():
try:
# at this point we can't be sure the database exists, so don't
# connect to a database initially
client = InfluxDBClient(
influxconf["host"], influxconf["port"],
influxconf["user"], influxconf["pass"], database=None)
if clean_db:
client.drop_database(influxconf["name"])
# create_database() should be idempotent, so always safe to run
client.create_database(influxconf["name"])
#client.close() # only present in version >= 5.0.0
except DBQueryException as e:
logger.log("Failed to create influx database: %s" % e)
sys.exit(1)
except InfluxDBClientError as e:
if e.code == 403:
logger.log("ERROR: Influx user '%s' missing admin privileges" % influxconf["user"])
else:
logger.log("Failed to create influx database: %s" % e)
sys.exit(1)
try:
influxdb = InfluxInsertor(
influxconf["name"], influxconf["user"], influxconf["pass"],
influxconf["host"], influxconf["port"])
except DBQueryException as e:
logger.log("Failed to connect to influx database: %s" % e)
sys.exit(1)
try:
influxdb.create_retention_policies(influxconf["keepdata"])
except DBQueryException as e:
logger.log("Failed to create retention policies for influx: %s" % e)
sys.exit(1)
for base, mod in modules.items():
mod.create_cqs(db, influxdb)
if 'rrd' in enabled_modules:
rrd_conf = get_nntsc_config(nntsc_conf, 'rrd', 'rrdlist')
if rrd_conf == "NNTSCConfigError" or rrd_conf == "NNTSCConfigMissing":
sys.exit(0)
try:
insert_rrd_streams(db, influxdb, rrd_conf)
except DBQueryException as e:
if e.code == DB_DATA_ERROR:
logger.log("Failed to create RRD streams -- check RRD List for invalid content")
sys.exit(1)
if e.code == DB_GENERIC_ERROR:
logger.log("Database error while creating RRD streams -- may need to manually check database")
sys.exit(1)
# vim: set sw=4 tabstop=4 softtabstop=4 expandtab :