2018-12-31 16:25:03 -08:00
|
|
|
from shutil import copyfile
|
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
|
2018-12-30 13:49:35 -08:00
|
|
|
from configparser import ConfigParser, NoOptionError, NoSectionError
|
2018-12-11 09:45:43 -08:00
|
|
|
|
2018-12-11 21:37:49 -08:00
|
|
|
from varken.varkenlogger import BlacklistFilter
|
2019-01-04 13:30:27 -08:00
|
|
|
from varken.structures import SickChillServer, UniFiServer
|
|
|
|
from varken.helpers import clean_sid_check, rfc1918_ip_check
|
2019-01-05 13:45:31 -08:00
|
|
|
from varken.structures import SonarrServer, RadarrServer, OmbiServer, TautulliServer, InfluxServer
|
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
|
2019-01-05 13:45:31 -08:00
|
|
|
self.services = ['sonarr', 'radarr', 'ombi', 'tautulli', 'sickchill', 'unifi']
|
2018-12-17 19:16:46 -08:00
|
|
|
|
2018-12-17 17:12:37 -08:00
|
|
|
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()
|
2018-12-11 21:37:49 -08:00
|
|
|
|
|
|
|
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)
|
2019-01-02 21:35:29 -08:00
|
|
|
# Added matching for domains that use :port. ConnectionPool splits the domain/ip from the port
|
|
|
|
without_port = [string.split(':')[0] for string in filtered_strings if ':' in string]
|
|
|
|
self.filtered_strings.extend(without_port)
|
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-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-04 19:17:33 -08:00
|
|
|
|
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-31 16:25:03 -08:00
|
|
|
|
|
|
|
if not exists(file_path):
|
2018-12-31 14:48:32 -08:00
|
|
|
self.logger.error('File missing (%s) in %s', ini, self.data_folder)
|
2018-12-31 16:25:03 -08:00
|
|
|
if inifile == 'varken.ini':
|
|
|
|
try:
|
|
|
|
self.logger.debug('Creating varken.ini from varken.example.ini')
|
|
|
|
copyfile(join(self.data_folder, 'varken.example.ini'), file_path)
|
|
|
|
except IOError as e:
|
|
|
|
self.logger.error("Varken does not have permission to write to %s. Error: %s - Exiting.", e,
|
|
|
|
self.data_folder)
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
self.logger.debug('Reading from %s', inifile)
|
|
|
|
with open(file_path) as config_ini:
|
|
|
|
config.read_file(config_ini)
|
|
|
|
|
|
|
|
return config
|
2018-12-31 14:48:32 -08:00
|
|
|
|
|
|
|
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
|
|
|
|
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-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
|
|
|
|
2018-12-17 19:16:46 -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
|
2018-12-17 19:16:46 -08:00
|
|
|
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
|
2019-01-04 13:30:27 -08:00
|
|
|
if service not in ['ciscoasa', 'unifi']:
|
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://'
|
|
|
|
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
|
|
|
|
2019-01-04 13:30:27 -08:00
|
|
|
if service in ['sonarr', 'radarr']:
|
2018-12-17 19:16:46 -08:00
|
|
|
queue = self.config.getboolean(section, 'queue')
|
2019-01-04 13:30:27 -08:00
|
|
|
queue_run_seconds = self.config.getint(section, 'queue_run_seconds')
|
2018-12-04 08:45:18 -08:00
|
|
|
|
2019-01-04 13:30:27 -08:00
|
|
|
if service == 'sonarr':
|
2018-12-17 19:16:46 -08:00
|
|
|
missing_days = self.config.getint(section, 'missing_days')
|
|
|
|
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')
|
|
|
|
future_days_run_seconds = self.config.getint(section, 'future_days_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
|
|
|
|
2018-12-17 19:16:46 -08:00
|
|
|
if service == 'radarr':
|
|
|
|
get_missing = self.config.getboolean(section, 'get_missing')
|
|
|
|
get_missing_run_seconds = self.config.getint(section, 'get_missing_run_seconds')
|
2018-12-11 09:58:11 -08:00
|
|
|
|
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
|
|
|
|
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
|
|
|
|
2019-01-04 13:30:27 -08:00
|
|
|
get_stats = self.config.getboolean(section, 'get_stats')
|
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-18 18:30:04 -08:00
|
|
|
get_stats_run_seconds = self.config.getint(section, 'get_stats_run_seconds')
|
|
|
|
|
2019-01-01 20:41:23 -08:00
|
|
|
invalid_wan_ip = rfc1918_ip_check(fallback_ip)
|
|
|
|
|
|
|
|
if invalid_wan_ip:
|
2019-01-04 13:30:27 -08:00
|
|
|
self.logger.error('Invalid fallback_ip [%s] set for %s-%s!', fallback_ip, service,
|
|
|
|
server_id)
|
2019-01-01 20:41:23 -08:00
|
|
|
exit(1)
|
|
|
|
|
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
|
|
|
|
2018-12-17 19:16:46 -08:00
|
|
|
if service == 'ombi':
|
2019-01-04 13:30:27 -08:00
|
|
|
issue_status_counts = self.config.getboolean(section, 'get_issue_status_counts')
|
2018-12-17 19:16:46 -08:00
|
|
|
request_type_counts = self.config.getboolean(section, 'get_request_type_counts')
|
|
|
|
request_total_counts = self.config.getboolean(section, 'get_request_total_counts')
|
2018-12-15 22:55:59 -08:00
|
|
|
|
2018-12-27 13:28:36 -08:00
|
|
|
issue_status_run_seconds = self.config.getint(section, 'issue_status_run_seconds')
|
2019-01-04 13:30:27 -08:00
|
|
|
request_type_run_seconds = self.config.getint(section, 'request_type_run_seconds')
|
|
|
|
request_total_run_seconds = self.config.getint(section, 'request_total_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
|
|
|
|
2018-12-17 19:16:46 -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
|
|
|
|
2019-01-05 13:45:31 -08:00
|
|
|
if service == 'unifi':
|
2018-12-17 19:16:46 -08:00
|
|
|
username = self.config.get(section, 'username')
|
|
|
|
password = self.config.get(section, 'password')
|
2019-01-04 13:30:27 -08:00
|
|
|
site = self.config.get(section, 'site').lower()
|
|
|
|
usg_name = self.config.get(section, 'usg_name')
|
|
|
|
get_usg_stats_run_seconds = self.config.getint(section, 'get_usg_stats_run_seconds')
|
2018-12-11 09:58:11 -08:00
|
|
|
|
2019-01-04 13:30:27 -08:00
|
|
|
server = UniFiServer(id=server_id, url=scheme + url, verify_ssl=verify_ssl, site=site,
|
|
|
|
username=username, password=password, usg_name=usg_name,
|
|
|
|
get_usg_stats_run_seconds=get_usg_stats_run_seconds)
|
|
|
|
|
|
|
|
getattr(self, f'{service}_servers').append(server)
|
2018-12-17 19:16:46 -08:00
|
|
|
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
|