2018-12-17 17:12:37 -08:00
|
|
|
from logging import getLogger
|
2018-12-04 08:45:18 -08:00
|
|
|
from os.path import join, exists
|
2018-12-17 17:12:37 -08:00
|
|
|
from re import match, compile, IGNORECASE
|
|
|
|
from configparser import ConfigParser, NoOptionError
|
2018-12-11 09:45:43 -08:00
|
|
|
|
|
|
|
from varken.helpers import clean_sid_check
|
2018-12-17 17:12:37 -08:00
|
|
|
from varken.structures import SickChillServer
|
2018-12-11 21:37:49 -08:00
|
|
|
from varken.varkenlogger import BlacklistFilter
|
2018-12-09 19:41:38 -08:00
|
|
|
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-17 17:12:37 -08:00
|
|
|
self.config = ConfigParser(interpolation=None)
|
2018-12-04 08:45:18 -08:00
|
|
|
self.data_folder = data_folder
|
|
|
|
|
2018-12-17 19:16:46 -08:00
|
|
|
self.services = ['sonarr', 'radarr', 'ombi', 'tautulli', 'sickchill', 'ciscoasa']
|
|
|
|
for service in self.services:
|
|
|
|
setattr(self, f'{service}_servers', [])
|
|
|
|
|
2018-12-17 17:12:37 -08:00
|
|
|
self.logger = getLogger()
|
2018-12-11 21:37:49 -08:00
|
|
|
|
2018-12-04 08:45:18 -08:00
|
|
|
self.influx_server = InfluxServer()
|
|
|
|
|
2018-12-10 16:49:22 -08:00
|
|
|
self.parse_opts()
|
2018-12-04 08:45:18 -08:00
|
|
|
|
2018-12-11 21:37:49 -08:00
|
|
|
self.filtered_strings = None
|
|
|
|
|
|
|
|
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]
|
2018-12-11 21:37:49 -08:00
|
|
|
self.filtered_strings = list(filter(None, filtered_strings))
|
2018-12-17 18:19:41 -08:00
|
|
|
# Added matching for domains that use /locations. ConnectionPool ignores the location in logs
|
2018-12-17 19:16:46 -08:00
|
|
|
domains_only = [string.split('/')[0] for string in filtered_strings if '/' in string]
|
2018-12-17 18:19:41 -08:00
|
|
|
self.filtered_strings.extend(domains_only)
|
2018-12-13 11:55:07 -08:00
|
|
|
|
2018-12-11 21:37:49 -08:00
|
|
|
for handler in self.logger.handlers:
|
|
|
|
handler.addFilter(BlacklistFilter(set(self.filtered_strings)))
|
|
|
|
|
2018-12-04 19:17:33 -08:00
|
|
|
def enable_check(self, server_type=None):
|
|
|
|
t = server_type
|
2018-12-10 16:49:22 -08:00
|
|
|
try:
|
|
|
|
global_server_ids = self.config.get('global', t)
|
|
|
|
if global_server_ids.lower() in ['false', 'no', '0']:
|
2018-12-11 21:37:49 -08:00
|
|
|
self.logger.info('%s disabled.', t.upper())
|
2018-12-10 16:49:22 -08:00
|
|
|
else:
|
2018-12-11 09:45:43 -08:00
|
|
|
sids = clean_sid_check(global_server_ids, t)
|
2018-12-10 16:49:22 -08:00
|
|
|
return sids
|
2018-12-17 17:12:37 -08:00
|
|
|
except NoOptionError as e:
|
2018-12-11 21:37:49 -08:00
|
|
|
self.logger.error(e)
|
2018-12-04 19:17:33 -08:00
|
|
|
|
2018-12-04 08:45:18 -08:00
|
|
|
def read_file(self):
|
|
|
|
file_path = join(self.data_folder, 'varken.ini')
|
|
|
|
if exists(file_path):
|
|
|
|
with open(file_path) as config_ini:
|
|
|
|
self.config.read_file(config_ini)
|
2018-12-11 21:37:49 -08:00
|
|
|
self.config_blacklist()
|
2018-12-04 08:45:18 -08:00
|
|
|
else:
|
2018-12-12 17:34:54 -08:00
|
|
|
self.logger.error('Config file missing (varken.ini) in %s', self.data_folder)
|
|
|
|
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
|
|
|
|
2018-12-21 18:00:06 -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
|
|
|
)
|
2018-12-23 07:53:25 -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+)$')
|
|
|
|
|
2018-12-17 17:12:37 -08:00
|
|
|
regex = compile('{}'.format(search), IGNORECASE)
|
2018-12-14 12:25:17 -08:00
|
|
|
|
2018-12-17 17:12:37 -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-04 08:45:18 -08:00
|
|
|
def parse_opts(self):
|
|
|
|
self.read_file()
|
|
|
|
# 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')
|
|
|
|
|
|
|
|
self.influx_server = InfluxServer(url, port, username, password)
|
|
|
|
|
2018-12-17 19:16:46 -08:00
|
|
|
# Check for all enabled services
|
|
|
|
for service in self.services:
|
|
|
|
setattr(self, f'{service}_enabled', self.enable_check(f'{service}_server_ids'))
|
|
|
|
service_enabled = getattr(self, f'{service}_enabled')
|
2018-12-13 11:55:07 -08:00
|
|
|
|
2018-12-17 19:16:46 -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
|
|
|
|
2018-12-17 19:16:46 -08:00
|
|
|
apikey = None
|
2018-12-18 18:35:58 -08:00
|
|
|
if service != 'ciscoasa':
|
2018-12-17 19:16:46 -08:00
|
|
|
apikey = self.config.get(section, 'apikey')
|
2018-12-13 11:55:07 -08:00
|
|
|
|
2018-12-17 19:16:46 -08:00
|
|
|
scheme = 'https://' if self.config.getboolean(section, 'ssl') else 'http://'
|
2018-12-13 11:55:07 -08:00
|
|
|
|
2018-12-17 19:16:46 -08:00
|
|
|
verify_ssl = self.config.getboolean(section, 'verify_ssl')
|
2018-12-13 11:55:07 -08:00
|
|
|
|
2018-12-17 19:16:46 -08:00
|
|
|
if scheme != 'https://':
|
|
|
|
verify_ssl = False
|
2018-12-11 09:58:11 -08:00
|
|
|
|
2018-12-17 19:16:46 -08:00
|
|
|
if service == 'sonarr':
|
|
|
|
queue = self.config.getboolean(section, 'queue')
|
2018-12-04 08:45:18 -08:00
|
|
|
|
2018-12-17 19:16:46 -08:00
|
|
|
missing_days = self.config.getint(section, 'missing_days')
|
2018-12-04 08:45:18 -08:00
|
|
|
|
2018-12-17 19:16:46 -08:00
|
|
|
future_days = self.config.getint(section, 'future_days')
|
2018-12-13 11:55:07 -08:00
|
|
|
|
2018-12-17 19:16:46 -08:00
|
|
|
missing_days_run_seconds = self.config.getint(section, 'missing_days_run_seconds')
|
2018-12-13 11:55:07 -08:00
|
|
|
|
2018-12-17 19:16:46 -08:00
|
|
|
future_days_run_seconds = self.config.getint(section, 'future_days_run_seconds')
|
2018-12-13 11:55:07 -08:00
|
|
|
|
2018-12-17 19:16:46 -08:00
|
|
|
queue_run_seconds = self.config.getint(section, 'queue_run_seconds')
|
2018-12-13 11:55:07 -08:00
|
|
|
|
2018-12-17 19:16:46 -08:00
|
|
|
server = SonarrServer(server_id, scheme + url, apikey, verify_ssl, missing_days,
|
|
|
|
missing_days_run_seconds, future_days, future_days_run_seconds,
|
|
|
|
queue, queue_run_seconds)
|
2018-12-13 11:55:07 -08:00
|
|
|
|
2018-12-17 19:16:46 -08:00
|
|
|
if service == 'radarr':
|
|
|
|
queue = self.config.getboolean(section, 'queue')
|
2018-12-13 11:55:07 -08:00
|
|
|
|
2018-12-17 19:16:46 -08:00
|
|
|
queue_run_seconds = self.config.getint(section, 'queue_run_seconds')
|
2018-12-13 11:55:07 -08:00
|
|
|
|
2018-12-17 19:16:46 -08:00
|
|
|
get_missing = self.config.getboolean(section, 'get_missing')
|
2018-12-13 11:55:07 -08:00
|
|
|
|
2018-12-17 19:16:46 -08:00
|
|
|
get_missing_run_seconds = self.config.getint(section, 'get_missing_run_seconds')
|
2018-12-11 09:58:11 -08:00
|
|
|
|
2018-12-17 19:16:46 -08:00
|
|
|
server = RadarrServer(server_id, scheme + url, apikey, verify_ssl, queue, queue_run_seconds,
|
|
|
|
get_missing, get_missing_run_seconds)
|
2018-12-15 22:55:59 -08:00
|
|
|
|
2018-12-17 19:16:46 -08:00
|
|
|
if service == 'tautulli':
|
|
|
|
fallback_ip = self.config.get(section, 'fallback_ip')
|
2018-12-15 22:55:59 -08:00
|
|
|
|
2018-12-17 19:16:46 -08:00
|
|
|
get_activity = self.config.getboolean(section, 'get_activity')
|
2018-12-15 22:55:59 -08:00
|
|
|
|
2018-12-17 19:16:46 -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-17 19:16:46 -08:00
|
|
|
server = TautulliServer(server_id, scheme + url, fallback_ip, apikey, verify_ssl,
|
2018-12-18 18:30:04 -08:00
|
|
|
get_activity, get_activity_run_seconds, get_stats,
|
|
|
|
get_stats_run_seconds)
|
2018-12-15 22:55:59 -08:00
|
|
|
|
2018-12-17 19:16:46 -08:00
|
|
|
if service == 'ombi':
|
|
|
|
request_type_counts = self.config.getboolean(section, 'get_request_type_counts')
|
2018-12-15 22:55:59 -08:00
|
|
|
|
2018-12-17 19:16:46 -08:00
|
|
|
request_type_run_seconds = self.config.getint(section, 'request_type_run_seconds')
|
2018-12-15 22:55:59 -08:00
|
|
|
|
2018-12-17 19:16:46 -08:00
|
|
|
request_total_counts = self.config.getboolean(section, 'get_request_total_counts')
|
2018-12-15 22:55:59 -08:00
|
|
|
|
2018-12-17 19:16:46 -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-17 19:16:46 -08:00
|
|
|
server = OmbiServer(server_id, scheme + url, apikey, verify_ssl, request_type_counts,
|
|
|
|
request_type_run_seconds, request_total_counts,
|
2018-12-29 19:57:48 -08:00
|
|
|
request_total_run_seconds, issue_status_counts,
|
2018-12-29 11:46:28 -08:00
|
|
|
issue_status_run_seconds)
|
2018-12-04 08:45:18 -08:00
|
|
|
|
2018-12-17 19:16:46 -08:00
|
|
|
if service == 'sickchill':
|
|
|
|
get_missing = self.config.getboolean(section, 'get_missing')
|
2018-12-09 19:41:38 -08:00
|
|
|
|
2018-12-17 19:16:46 -08:00
|
|
|
get_missing_run_seconds = self.config.getint(section, 'get_missing_run_seconds')
|
2018-12-13 11:55:07 -08:00
|
|
|
|
2018-12-17 19:16:46 -08:00
|
|
|
server = SickChillServer(server_id, scheme + url, apikey, verify_ssl,
|
|
|
|
get_missing, get_missing_run_seconds)
|
2018-12-13 11:55:07 -08:00
|
|
|
|
2018-12-17 19:16:46 -08:00
|
|
|
if service == 'ciscoasa':
|
|
|
|
username = self.config.get(section, 'username')
|
2018-12-13 11:55:07 -08:00
|
|
|
|
2018-12-17 19:16:46 -08:00
|
|
|
password = self.config.get(section, 'password')
|
2018-12-13 11:55:07 -08:00
|
|
|
|
2018-12-17 19:16:46 -08:00
|
|
|
outside_interface = self.config.get(section, 'outside_interface')
|
2018-12-13 11:55:07 -08:00
|
|
|
|
2018-12-17 19:16:46 -08:00
|
|
|
get_bandwidth_run_seconds = self.config.getint(section, 'get_bandwidth_run_seconds')
|
2018-12-13 11:55:07 -08:00
|
|
|
|
2018-12-17 19:16:46 -08:00
|
|
|
server = CiscoASAFirewall(server_id, scheme + url, username, password, outside_interface,
|
|
|
|
verify_ssl, get_bandwidth_run_seconds)
|
2018-12-13 11:55:07 -08:00
|
|
|
|
2018-12-17 19:16:46 -08:00
|
|
|
getattr(self, f'{service}_servers').append(server)
|
2018-12-11 09:58:11 -08:00
|
|
|
|
2018-12-17 19:16:46 -08:00
|
|
|
except NoOptionError as e:
|
|
|
|
setattr(self, f'{service}_enabled', False)
|
|
|
|
self.logger.error('%s disabled. Error: %s', section, e)
|