-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrapconfig.py
68 lines (58 loc) · 2.24 KB
/
trapconfig.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
'''
Created on Nov 14, 2011
Last modified Nov 14, 2011
@author: marco
'''
import sys
import ConfigParser as configparser
class TrapConfig:
# config file structure
struct = {'general': {'nsca_command': True, 'strip_domains': False},
'logging': {'debug': True, 'log': True, 'logfile': False},
'traps': {},
'ignore': {'traps': [], 'bgp_ip': []}}
# these options must be boolean
boolean = [('logging', 'debug'), ('logging', 'log')]
def __init__(self, configfile):
self.conf = configparser.ConfigParser()
# case sensitive reading
self.conf.optionxform = str
try:
self.conf.readfp(open(configfile))
except IOError:
sys.exit()
def get_conf(self):
if self.__check_struct() and self.__check_dep():
return self.struct
return None
def __check_struct(self):
for section in self.struct.keys():
for option in self.struct[section].keys():
try:
self.struct[section][option] = self.conf.get(section, option)
for b in self.boolean:
if b[0] == section and b[1] == option:
self.struct[section][option] = self.conf.getboolean(section, option)
except configparser.NoOptionError:
if self.struct[section][option]:
print("'[%s]->%s' missing, exiting." % (section, option))
return False
return self.__load_general('traps')
def __check_dep(self):
# logfile: != False if log = True
if self.struct['logging']['log'] and not self.struct['logging']['logfile']:
return False
return True
def __load_general(self, section):
try:
options = self.conf.options(section)
if not options:
print("No trap details to catch in '[%s]', exiting." % section)
return False
for option in options:
self.struct[section][option] = self.conf.get(section, option)
return True
except configparser.Error:
print("Missing '[%s]' section, exiting." % section)
return False
# EOF