@cryptotaxi247 / netdata-1 / commits / be3284d4b

fail2ban_plugin: code optimization

Ilya committed Feb 18, 2017 at 01:50 UTC be3284d4b81297d267eb5f3ad8d91b702ba88a7b
1 file changed +46 -34
python.d/fail2ban.chart.py
+46 -34
@@ -65,43 +65,17 @@ class Service(LogService):
65 if not is_accessible(self.log_path, R_OK):
66 self.error('Cannot access file %s' % self.log_path)
67 return False
68 - if not isdir(self.conf_dir):
69 - self.conf_dir = None
70 -
71 - # If "conf_dir" not specified (or not a dir) plugin will use "conf_path"
72 - if not self.conf_dir:
73 - if is_accessible(self.conf_path, R_OK):
74 - with open(self.conf_path, 'rt') as jails_conf:
75 - jails_list = REGEX.findall(' '.join(jails_conf.read().split()))
76 - self.jails_list = jails_list
77 - else:
78 - self.jails_list = list()
79 - self.error('Cannot access jail configuration file %s.' % self.conf_path)
80 - # If "conf_dir" is specified and "conf_dir" is dir plugin will use "conf_dir"
68 +
69 + if self.conf_dir:
70 + jails_list, error = parse_conf_dir(self.conf_dir)
71 else:
82 - dot_local = glob(self.conf_dir + '/*.local') # *.local jail configurations files
83 - dot_conf = glob(self.conf_dir + '/*.conf') # *.conf jail configuration files
84 -
85 - if not any([dot_local, dot_conf]):
86 - self.error('%s is empty or not readable' % self.conf_dir)
87 - # According "man jail.conf" files could be *.local AND *.conf
88 - # *.conf files parsed first. Changes in *.local overrides configuration in *.conf
89 - if dot_conf:
90 - dot_local.extend([conf for conf in dot_conf if conf[:-5] not in [local[:-6] for local in dot_local]])
91 - # Make sure all files are readable
92 - dot_local = [conf for conf in dot_local if is_accessible(conf, R_OK)]
93 - if dot_local:
94 - enabled_jails = list()
95 - for jail_conf in dot_local:
96 - with open(jail_conf, 'rt') as conf:
97 - enabled_jails.extend(REGEX.findall(' '.join(conf.read().split())))
98 - self.jails_list = list(set(enabled_jails))
99 - else:
100 - self.jails_list = list()
101 - self.error('Files in %s not readable' % self.conf_dir)
72 + jails_list, error = parse_conf_path(self.conf_path)
73 +
74 + if not jails_list:
75 + self.error(error)
76
77 # If for some reason parse failed we still can START with default jails_list.
104 - self.jails_list = list(set(self.jails_list) - set(self.exclude)) or ['ssh']
78 + self.jails_list = list(set(jails_list) - set(self.exclude)) or ['ssh']
79 self.data = dict([(jail, 0) for jail in self.jails_list])
80 self.create_dimensions()
81 self.info('Plugin successfully started. Jails: %s' % self.jails_list)
@@ -113,3 +87,41 @@ class Service(LogService):
87 'lines': []}}
88 for jail in self.jails_list:
89 self.definitions['jails_group']['lines'].append([jail, jail, 'incremental'])
90 +
91 +
92 +def parse_conf_dir(conf_dir):
93 + if not isdir(conf_dir):
94 + return list(), '%s is not a directory' % conf_dir
95 +
96 + jail_local = list(filter(lambda local: is_accessible(local, R_OK), glob(conf_dir + '/*.local')))
97 + jail_conf = list(filter(lambda conf: is_accessible(conf, R_OK), glob(conf_dir + '/*.conf')))
98 +
99 + if not (jail_local or jail_conf):
100 + return list(), '%s is empty or not readable' % conf_dir
101 +
102 + # According "man jail.conf" files could be *.local AND *.conf
103 + # *.conf files parsed first. Changes in *.local overrides configuration in *.conf
104 + if jail_conf:
105 + jail_local.extend([conf for conf in jail_conf if conf[:-5] not in [local[:-6] for local in jail_local]])
106 + jails_list = list()
107 + for conf in jail_local:
108 + with open(conf, 'rt') as f:
109 + raw_data = f.read()
110 +
111 + data = ' '.join(raw_data.split())
112 + jails_list.extend(REGEX.findall(data))
113 + jails_list = list(set(jails_list))
114 +
115 + return jails_list, 'can\'t locate any jails in %s. Default jail is [\'ssh\']' % conf_dir
116 +
117 +
118 +def parse_conf_path(conf_path):
119 + if not is_accessible(conf_path, R_OK):
120 + return list(), '%s is not readable' % conf_path
121 +
122 + with open(conf_path, 'rt') as jails_conf:
123 + raw_data = jails_conf.read()
124 +
125 + data = raw_data.split()
126 + jails_list = REGEX.findall(' '.join(data))
127 + return jails_list, 'can\'t locate any jails in %s. Default jail is [\'ssh\']' % conf_path