Varken/varken/iniparser.py

266 lines
12 KiB
Python
Raw Normal View History

from logging import getLogger
2018-12-04 08:45:18 -08:00
from os.path import join, exists
from re import match, compile, IGNORECASE
2018-12-30 13:49:35 -08:00
from configparser import ConfigParser, NoOptionError, NoSectionError
2018-12-11 09:45:43 -08:00
from varken.helpers import clean_sid_check
from varken.structures import SickChillServer
from varken.varkenlogger import BlacklistFilter
from varken.structures import SonarrServer, RadarrServer, OmbiServer, TautulliServer, InfluxServer, CiscoASAFirewall
2018-12-04 08:45:18 -08:00
class INIParser(object):
def __init__(self, data_folder):
2018-12-31 14:48:32 -08:00
self.config = None
2018-12-04 08:45:18 -08:00
self.data_folder = data_folder
2018-12-31 14:48:32 -08:00
self.filtered_strings = None
self.services = ['sonarr', 'radarr', 'ombi', 'tautulli', 'sickchill', 'ciscoasa']
self.logger = getLogger()
2018-12-04 08:45:18 -08:00
self.influx_server = InfluxServer()
2018-12-30 13:49:35 -08:00
try:
2018-12-31 14:48:32 -08:00
self.parse_opts(read_file=True)
2018-12-30 13:49:35 -08:00
except NoSectionError as e:
2018-12-31 14:48:32 -08:00
self.logger.error('Missing section in (varken.ini): %s', e)
self.rectify_ini()
def config_blacklist(self):
filtered_strings = [section.get(k) for key, section in self.config.items()
2018-12-15 22:55:59 -08:00
for k in section if k in BlacklistFilter.blacklisted_strings]
self.filtered_strings = list(filter(None, filtered_strings))
# Added matching for domains that use /locations. ConnectionPool ignores the location in logs
domains_only = [string.split('/')[0] for string in filtered_strings if '/' in string]
self.filtered_strings.extend(domains_only)
2018-12-13 11:55:07 -08:00
for handler in self.logger.handlers:
handler.addFilter(BlacklistFilter(set(self.filtered_strings)))
def enable_check(self, server_type=None):
t = server_type
2018-12-31 15:56:48 -08:00
global_server_ids = self.config.get('global', t)
if global_server_ids.lower() in ['false', 'no', '0']:
self.logger.info('%s disabled.', t.upper())
else:
sids = clean_sid_check(global_server_ids, t)
return sids
2018-12-31 14:48:32 -08:00
def read_file(self, inifile):
config = ConfigParser(interpolation=None)
ini = inifile
file_path = join(self.data_folder, ini)
2018-12-04 08:45:18 -08:00
if exists(file_path):
2018-12-31 14:48:32 -08:00
self.logger.debug('Reading from %s', inifile)
2018-12-04 08:45:18 -08:00
with open(file_path) as config_ini:
2018-12-31 14:48:32 -08:00
config.read_file(config_ini)
return config
2018-12-04 08:45:18 -08:00
else:
2018-12-31 14:48:32 -08:00
self.logger.error('File missing (%s) in %s', ini, self.data_folder)
exit(1)
def write_file(self, inifile):
ini = inifile
file_path = join(self.data_folder, ini)
if exists(file_path):
self.logger.debug('Writing to %s', inifile)
with open(file_path, 'w') as config_ini:
self.config.write(config_ini)
else:
self.logger.error('File missing (%s) in %s', ini, self.data_folder)
2018-12-12 17:34:54 -08:00
exit(1)
2018-12-04 08:45:18 -08:00
2018-12-23 07:25:52 -08:00
def url_check(self, url=None, include_port=True, section=None):
2018-12-13 11:55:07 -08:00
url_check = url
2018-12-23 07:25:52 -08:00
module = section
2018-12-14 12:25:17 -08:00
inc_port = include_port
2018-12-13 11:55:07 -08:00
search = (r'(?:([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}|' # domain...
2018-12-15 22:55:59 -08:00
r'localhost|' # localhost...
2018-12-29 19:57:48 -08:00
r'^[a-zA-Z0-9_-]*|' # hostname only. My soul dies a little every time this is used...
2018-12-15 22:55:59 -08:00
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip
2018-12-14 12:25:17 -08:00
)
# Include search for port if it is needed.
2018-12-14 12:25:17 -08:00
if inc_port:
search = (search + r'(?::\d+)?' + r'(?:/?|[/?]\S+)$')
else:
search = (search + r'(?:/?|[/?]\S+)$')
regex = compile('{}'.format(search), IGNORECASE)
2018-12-14 12:25:17 -08:00
valid = match(regex, url_check) is not None
2018-12-13 11:55:07 -08:00
if not valid:
2018-12-14 12:25:17 -08:00
if inc_port:
2018-12-29 19:57:48 -08:00
self.logger.error('%s is invalid in module [%s]! URL must host/IP and '
'port if not 80 or 443. ie. localhost:8080',
2018-12-23 07:25:52 -08:00
url_check, module)
2018-12-14 12:25:17 -08:00
exit(1)
else:
2018-12-23 07:25:52 -08:00
self.logger.error('%s is invalid in module [%s]! URL must host/IP. ie. localhost', url_check, module)
2018-12-14 12:25:17 -08:00
exit(1)
2018-12-13 11:55:07 -08:00
else:
2018-12-23 07:25:52 -08:00
self.logger.debug('%s is a valid URL in module [%s].', url_check, module)
2018-12-13 11:55:07 -08:00
return url_check
2018-12-31 14:48:32 -08:00
def rectify_ini(self):
self.logger.debug('Rectifying varken.ini with varken.example.ini')
current_ini = self.config
example_ini = self.read_file('varken.example.ini')
for name, section in example_ini.items():
if name not in current_ini:
self.logger.debug('Section %s missing. Adding...', name)
current_ini[name] = {}
for key, value in section.items():
if not current_ini[name].get(key):
self.logger.debug('%s is missing in %s. Adding defaults...', key, name)
current_ini[name][key] = value
self.config = current_ini
self.write_file('varken.ini')
self.parse_opts()
def parse_opts(self, read_file=False):
for service in self.services:
setattr(self, f'{service}_servers', [])
if read_file:
self.config = self.read_file('varken.ini')
self.config_blacklist()
2018-12-04 08:45:18 -08:00
# Parse InfluxDB options
2018-12-23 07:25:52 -08:00
url = self.url_check(self.config.get('influxdb', 'url'), include_port=False, section='influxdb')
2018-12-13 11:55:07 -08:00
2018-12-04 08:45:18 -08:00
port = self.config.getint('influxdb', 'port')
2018-12-13 11:55:07 -08:00
2018-12-04 08:45:18 -08:00
username = self.config.get('influxdb', 'username')
2018-12-13 11:55:07 -08:00
2018-12-04 08:45:18 -08:00
password = self.config.get('influxdb', 'password')
2018-12-29 22:33:55 -08:00
self.influx_server = InfluxServer(url=url, port=port, username=username, password=password)
2018-12-04 08:45:18 -08:00
# Check for all enabled services
for service in self.services:
2018-12-31 15:56:48 -08:00
try:
setattr(self, f'{service}_enabled', self.enable_check(f'{service}_server_ids'))
except NoOptionError as e:
self.logger.error('Missing global %s. Error: %s', f'{service}_server_ids', e)
self.rectify_ini()
return
service_enabled = getattr(self, f'{service}_enabled')
2018-12-13 11:55:07 -08:00
if service_enabled:
for server_id in service_enabled:
server = None
section = f"{service}-{server_id}"
try:
2018-12-23 07:25:52 -08:00
url = self.url_check(self.config.get(section, 'url'), section=section)
2018-12-13 11:55:07 -08:00
apikey = None
if service != 'ciscoasa':
apikey = self.config.get(section, 'apikey')
2018-12-13 11:55:07 -08:00
scheme = 'https://' if self.config.getboolean(section, 'ssl') else 'http://'
2018-12-13 11:55:07 -08:00
verify_ssl = self.config.getboolean(section, 'verify_ssl')
2018-12-13 11:55:07 -08:00
if scheme != 'https://':
verify_ssl = False
if service == 'sonarr':
queue = self.config.getboolean(section, 'queue')
2018-12-04 08:45:18 -08:00
missing_days = self.config.getint(section, 'missing_days')
2018-12-04 08:45:18 -08:00
future_days = self.config.getint(section, 'future_days')
2018-12-13 11:55:07 -08:00
missing_days_run_seconds = self.config.getint(section, 'missing_days_run_seconds')
2018-12-13 11:55:07 -08:00
future_days_run_seconds = self.config.getint(section, 'future_days_run_seconds')
2018-12-13 11:55:07 -08:00
queue_run_seconds = self.config.getint(section, 'queue_run_seconds')
2018-12-13 11:55:07 -08:00
2018-12-29 22:33:55 -08:00
server = SonarrServer(id=server_id, url=scheme + url, api_key=apikey, verify_ssl=verify_ssl,
missing_days=missing_days, future_days=future_days,
missing_days_run_seconds=missing_days_run_seconds,
future_days_run_seconds=future_days_run_seconds,
queue=queue, queue_run_seconds=queue_run_seconds)
2018-12-13 11:55:07 -08:00
if service == 'radarr':
queue = self.config.getboolean(section, 'queue')
2018-12-13 11:55:07 -08:00
queue_run_seconds = self.config.getint(section, 'queue_run_seconds')
2018-12-13 11:55:07 -08:00
get_missing = self.config.getboolean(section, 'get_missing')
2018-12-13 11:55:07 -08:00
get_missing_run_seconds = self.config.getint(section, 'get_missing_run_seconds')
2018-12-29 22:33:55 -08:00
server = RadarrServer(id=server_id, url=scheme + url, api_key=apikey, verify_ssl=verify_ssl,
queue_run_seconds=queue_run_seconds, get_missing=get_missing,
queue=queue, get_missing_run_seconds=get_missing_run_seconds)
2018-12-15 22:55:59 -08:00
if service == 'tautulli':
fallback_ip = self.config.get(section, 'fallback_ip')
2018-12-15 22:55:59 -08:00
get_activity = self.config.getboolean(section, 'get_activity')
2018-12-15 22:55:59 -08:00
get_activity_run_seconds = self.config.getint(section, 'get_activity_run_seconds')
2018-12-15 22:55:59 -08:00
2018-12-18 18:30:04 -08:00
get_stats = self.config.getboolean(section, 'get_stats')
get_stats_run_seconds = self.config.getint(section, 'get_stats_run_seconds')
2018-12-29 22:33:55 -08:00
server = TautulliServer(id=server_id, url=scheme + url, api_key=apikey,
verify_ssl=verify_ssl, get_activity=get_activity,
fallback_ip=fallback_ip, get_stats=get_stats,
get_activity_run_seconds=get_activity_run_seconds,
get_stats_run_seconds=get_stats_run_seconds)
2018-12-15 22:55:59 -08:00
if service == 'ombi':
request_type_counts = self.config.getboolean(section, 'get_request_type_counts')
2018-12-15 22:55:59 -08:00
request_type_run_seconds = self.config.getint(section, 'request_type_run_seconds')
2018-12-15 22:55:59 -08:00
request_total_counts = self.config.getboolean(section, 'get_request_total_counts')
2018-12-15 22:55:59 -08:00
request_total_run_seconds = self.config.getint(section, 'request_total_run_seconds')
2018-12-15 22:55:59 -08:00
2018-12-27 13:28:36 -08:00
issue_status_counts = self.config.getboolean(section, 'get_issue_status_counts')
2018-12-27 11:02:44 -08:00
2018-12-27 13:28:36 -08:00
issue_status_run_seconds = self.config.getint(section, 'issue_status_run_seconds')
2018-12-27 11:02:44 -08:00
2018-12-29 22:33:55 -08:00
server = OmbiServer(id=server_id, url=scheme + url, api_key=apikey, verify_ssl=verify_ssl,
request_type_counts=request_type_counts,
request_type_run_seconds=request_type_run_seconds,
request_total_counts=request_total_counts,
request_total_run_seconds=request_total_run_seconds,
issue_status_counts=issue_status_counts,
issue_status_run_seconds=issue_status_run_seconds)
2018-12-04 08:45:18 -08:00
if service == 'sickchill':
get_missing = self.config.getboolean(section, 'get_missing')
get_missing_run_seconds = self.config.getint(section, 'get_missing_run_seconds')
2018-12-13 11:55:07 -08:00
2018-12-29 22:33:55 -08:00
server = SickChillServer(id=server_id, url=scheme + url, api_key=apikey,
verify_ssl=verify_ssl, get_missing=get_missing,
get_missing_run_seconds=get_missing_run_seconds)
2018-12-13 11:55:07 -08:00
if service == 'ciscoasa':
username = self.config.get(section, 'username')
2018-12-13 11:55:07 -08:00
password = self.config.get(section, 'password')
2018-12-13 11:55:07 -08:00
outside_interface = self.config.get(section, 'outside_interface')
2018-12-13 11:55:07 -08:00
get_bandwidth_run_seconds = self.config.getint(section, 'get_bandwidth_run_seconds')
2018-12-13 11:55:07 -08:00
2018-12-29 22:33:55 -08:00
server = CiscoASAFirewall(id=server_id, url=scheme + url, verify_ssl=verify_ssl,
username=username, password=password,
outside_interface=outside_interface,
get_bandwidth_run_seconds=get_bandwidth_run_seconds)
2018-12-13 11:55:07 -08:00
getattr(self, f'{service}_servers').append(server)
except NoOptionError as e:
2018-12-31 14:48:32 -08:00
self.logger.error('Missing key in %s. Error: %s', section, e)
self.rectify_ini()
return