2018-12-04 08:45:18 -08:00
|
|
|
import configparser
|
2018-12-04 16:58:21 -08:00
|
|
|
import logging
|
2018-12-13 11:55:07 -08:00
|
|
|
import re
|
|
|
|
|
2018-12-04 08:45:18 -08:00
|
|
|
from sys import exit
|
|
|
|
from os.path import join, exists
|
2018-12-11 09:45:43 -08:00
|
|
|
|
|
|
|
from varken.helpers import clean_sid_check
|
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-15 22:55:59 -08:00
|
|
|
from varken.structures import SickChillServer
|
2018-12-04 08:45:18 -08:00
|
|
|
|
|
|
|
|
|
|
|
class INIParser(object):
|
|
|
|
def __init__(self, data_folder):
|
2018-12-09 19:41:38 -08:00
|
|
|
self.config = configparser.ConfigParser(interpolation=None)
|
2018-12-04 08:45:18 -08:00
|
|
|
self.data_folder = data_folder
|
|
|
|
|
2018-12-11 21:37:49 -08:00
|
|
|
self.logger = logging.getLogger()
|
|
|
|
|
2018-12-04 08:45:18 -08:00
|
|
|
self.influx_server = InfluxServer()
|
|
|
|
|
|
|
|
self.sonarr_enabled = False
|
|
|
|
self.sonarr_servers = []
|
|
|
|
|
|
|
|
self.radarr_enabled = False
|
|
|
|
self.radarr_servers = []
|
|
|
|
|
|
|
|
self.ombi_enabled = False
|
|
|
|
self.ombi_servers = []
|
|
|
|
|
|
|
|
self.tautulli_enabled = False
|
|
|
|
self.tautulli_servers = []
|
|
|
|
|
2018-12-15 22:55:59 -08:00
|
|
|
self.sickchill_enabled = False
|
|
|
|
self.sickchill_servers = []
|
|
|
|
|
2018-12-09 19:41:38 -08:00
|
|
|
self.ciscoasa_enabled = False
|
|
|
|
self.ciscoasa_firewalls = []
|
2018-12-04 08:45:18 -08:00
|
|
|
|
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-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
|
|
|
|
except configparser.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-14 12:25:17 -08:00
|
|
|
def url_check(self, url=None, include_port=True):
|
2018-12-13 11:55:07 -08:00
|
|
|
url_check = url
|
2018-12-14 12:25:17 -08:00
|
|
|
inc_port = include_port
|
2018-12-13 11:55:07 -08:00
|
|
|
|
2018-12-15 22:55:59 -08:00
|
|
|
search = (r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' # domain...
|
|
|
|
r'localhost|' # localhost...
|
|
|
|
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip
|
2018-12-14 12:25:17 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
if inc_port:
|
|
|
|
search = (search + r'(?::\d+)?' + r'(?:/?|[/?]\S+)$')
|
|
|
|
else:
|
|
|
|
search = (search + r'(?:/?|[/?]\S+)$')
|
|
|
|
|
|
|
|
regex = re.compile('{}'.format(search), re.IGNORECASE)
|
|
|
|
|
|
|
|
print(re.match(regex, url_check))
|
2018-12-13 11:55:07 -08:00
|
|
|
|
|
|
|
valid = re.match(regex, url_check) is not None
|
|
|
|
if not valid:
|
2018-12-14 12:25:17 -08:00
|
|
|
if inc_port:
|
2018-12-15 22:55:59 -08:00
|
|
|
self.logger.error('%s is invalid! URL must host/IP and port if not 80 or 443. ie. localhost:8080',
|
|
|
|
url_check)
|
2018-12-14 12:25:17 -08:00
|
|
|
exit(1)
|
|
|
|
else:
|
|
|
|
self.logger.error('%s is invalid! URL must host/IP. ie. localhost', url_check)
|
|
|
|
exit(1)
|
2018-12-13 11:55:07 -08:00
|
|
|
else:
|
2018-12-15 22:55:59 -08:00
|
|
|
self.logger.debug('%s is a valid URL in the config.', url_check)
|
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-14 12:25:17 -08:00
|
|
|
url = self.url_check(self.config.get('influxdb', 'url'), include_port=False)
|
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)
|
|
|
|
|
|
|
|
# Parse Sonarr options
|
2018-12-04 11:46:28 -08:00
|
|
|
self.sonarr_enabled = self.enable_check('sonarr_server_ids')
|
2018-12-04 08:45:18 -08:00
|
|
|
|
|
|
|
if self.sonarr_enabled:
|
2018-12-11 09:45:43 -08:00
|
|
|
for server_id in self.sonarr_enabled:
|
2018-12-15 22:55:59 -08:00
|
|
|
section = 'sonarr-' + str(server_id)
|
2018-12-11 11:33:08 -08:00
|
|
|
try:
|
2018-12-15 22:55:59 -08:00
|
|
|
url = self.url_check(self.config.get(section, 'url'))
|
2018-12-13 11:55:07 -08:00
|
|
|
|
2018-12-15 22:55:59 -08:00
|
|
|
apikey = self.config.get(section, 'apikey')
|
2018-12-13 11:55:07 -08:00
|
|
|
|
2018-12-15 22:55:59 -08:00
|
|
|
scheme = 'https://' if self.config.getboolean(section, 'ssl') else 'http://'
|
2018-12-13 11:55:07 -08:00
|
|
|
|
2018-12-15 22:55:59 -08:00
|
|
|
verify_ssl = self.config.getboolean(section, 'verify_ssl')
|
2018-12-13 11:55:07 -08:00
|
|
|
|
2018-12-11 11:33:08 -08:00
|
|
|
if scheme != 'https://':
|
|
|
|
verify_ssl = False
|
2018-12-13 11:55:07 -08:00
|
|
|
|
2018-12-15 22:55:59 -08:00
|
|
|
queue = self.config.getboolean(section, 'queue')
|
2018-12-13 11:55:07 -08:00
|
|
|
|
2018-12-15 22:55:59 -08:00
|
|
|
missing_days = self.config.getint(section, 'missing_days')
|
2018-12-13 11:55:07 -08:00
|
|
|
|
2018-12-15 22:55:59 -08:00
|
|
|
future_days = self.config.getint(section, 'future_days')
|
2018-12-13 11:55:07 -08:00
|
|
|
|
2018-12-15 22:55:59 -08:00
|
|
|
missing_days_run_seconds = self.config.getint(section, 'missing_days_run_seconds')
|
2018-12-13 11:55:07 -08:00
|
|
|
|
2018-12-15 22:55:59 -08:00
|
|
|
future_days_run_seconds = self.config.getint(section, 'future_days_run_seconds')
|
2018-12-13 11:55:07 -08:00
|
|
|
|
2018-12-15 22:55:59 -08:00
|
|
|
queue_run_seconds = self.config.getint(section, 'queue_run_seconds')
|
2018-12-11 11:33:08 -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-11 11:33:08 -08:00
|
|
|
self.sonarr_servers.append(server)
|
|
|
|
except configparser.NoOptionError as e:
|
2018-12-15 22:55:59 -08:00
|
|
|
self.sonarr_enabled = False
|
2018-12-11 21:37:49 -08:00
|
|
|
self.logger.error(
|
2018-12-15 22:55:59 -08:00
|
|
|
'%s disabled. Error: %s', section, e)
|
2018-12-04 08:45:18 -08:00
|
|
|
|
|
|
|
# Parse Radarr options
|
2018-12-04 11:46:28 -08:00
|
|
|
self.radarr_enabled = self.enable_check('radarr_server_ids')
|
2018-12-04 08:45:18 -08:00
|
|
|
|
|
|
|
if self.radarr_enabled:
|
2018-12-11 09:45:43 -08:00
|
|
|
for server_id in self.radarr_enabled:
|
2018-12-15 22:55:59 -08:00
|
|
|
section = 'radarr-' + str(server_id)
|
2018-12-11 09:58:11 -08:00
|
|
|
try:
|
2018-12-15 22:55:59 -08:00
|
|
|
url = self.url_check(self.config.get(section, 'url'))
|
2018-12-13 11:55:07 -08:00
|
|
|
|
2018-12-15 22:55:59 -08:00
|
|
|
apikey = self.config.get(section, 'apikey')
|
2018-12-13 11:55:07 -08:00
|
|
|
|
2018-12-15 22:55:59 -08:00
|
|
|
scheme = 'https://' if self.config.getboolean(section, 'ssl') else 'http://'
|
2018-12-13 11:55:07 -08:00
|
|
|
|
2018-12-15 22:55:59 -08:00
|
|
|
verify_ssl = self.config.getboolean(section, 'verify_ssl')
|
2018-12-13 11:55:07 -08:00
|
|
|
|
2018-12-11 09:58:11 -08:00
|
|
|
if scheme != 'https://':
|
|
|
|
verify_ssl = False
|
2018-12-13 11:55:07 -08:00
|
|
|
|
2018-12-15 22:55:59 -08:00
|
|
|
queue = self.config.getboolean(section, 'queue')
|
2018-12-13 11:55:07 -08:00
|
|
|
|
2018-12-15 22:55:59 -08:00
|
|
|
queue_run_seconds = self.config.getint(section, 'queue_run_seconds')
|
2018-12-13 11:55:07 -08:00
|
|
|
|
2018-12-15 22:55:59 -08:00
|
|
|
get_missing = self.config.getboolean(section, 'get_missing')
|
2018-12-13 11:55:07 -08:00
|
|
|
|
2018-12-15 22:55:59 -08:00
|
|
|
get_missing_run_seconds = self.config.getint(section, 'get_missing_run_seconds')
|
2018-12-11 09:58:11 -08:00
|
|
|
|
|
|
|
server = RadarrServer(server_id, scheme + url, apikey, verify_ssl, queue, queue_run_seconds,
|
|
|
|
get_missing, get_missing_run_seconds)
|
|
|
|
self.radarr_servers.append(server)
|
|
|
|
except configparser.NoOptionError as e:
|
|
|
|
self.radarr_enabled = False
|
2018-12-11 21:37:49 -08:00
|
|
|
self.logger.error(
|
2018-12-15 22:55:59 -08:00
|
|
|
'%s disabled. Error: %s', section, e)
|
2018-12-04 08:45:18 -08:00
|
|
|
|
|
|
|
# Parse Tautulli options
|
2018-12-04 11:46:28 -08:00
|
|
|
self.tautulli_enabled = self.enable_check('tautulli_server_ids')
|
2018-12-04 08:45:18 -08:00
|
|
|
|
|
|
|
if self.tautulli_enabled:
|
2018-12-11 09:45:43 -08:00
|
|
|
for server_id in self.tautulli_enabled:
|
2018-12-15 22:55:59 -08:00
|
|
|
section = 'tautulli-' + str(server_id)
|
2018-12-11 09:58:11 -08:00
|
|
|
try:
|
2018-12-15 22:55:59 -08:00
|
|
|
url = self.url_check(self.config.get(section, 'url'))
|
2018-12-13 11:55:07 -08:00
|
|
|
|
2018-12-15 22:55:59 -08:00
|
|
|
fallback_ip = self.config.get(section, 'fallback_ip')
|
2018-12-13 11:55:07 -08:00
|
|
|
|
2018-12-15 22:55:59 -08:00
|
|
|
apikey = self.config.get(section, 'apikey')
|
2018-12-13 11:55:07 -08:00
|
|
|
|
2018-12-15 22:55:59 -08:00
|
|
|
scheme = 'https://' if self.config.getboolean(section, 'ssl') else 'http://'
|
2018-12-13 11:55:07 -08:00
|
|
|
|
2018-12-15 22:55:59 -08:00
|
|
|
verify_ssl = self.config.getboolean(section, 'verify_ssl')
|
2018-12-13 11:55:07 -08:00
|
|
|
|
2018-12-11 09:58:11 -08:00
|
|
|
if scheme != 'https://':
|
|
|
|
verify_ssl = False
|
2018-12-13 11:55:07 -08:00
|
|
|
|
2018-12-15 22:55:59 -08:00
|
|
|
get_activity = self.config.getboolean(section, 'get_activity')
|
2018-12-13 11:55:07 -08:00
|
|
|
|
2018-12-15 22:55:59 -08:00
|
|
|
get_activity_run_seconds = self.config.getint(section, 'get_activity_run_seconds')
|
2018-12-11 09:58:11 -08:00
|
|
|
|
|
|
|
server = TautulliServer(server_id, scheme + url, fallback_ip, apikey, verify_ssl, get_activity,
|
|
|
|
get_activity_run_seconds)
|
|
|
|
self.tautulli_servers.append(server)
|
|
|
|
except configparser.NoOptionError as e:
|
|
|
|
self.tautulli_enabled = False
|
2018-12-11 21:37:49 -08:00
|
|
|
self.logger.error(
|
2018-12-15 22:55:59 -08:00
|
|
|
'%s disabled. Error: %s', section, e)
|
2018-12-04 08:45:18 -08:00
|
|
|
|
2018-12-04 11:46:28 -08:00
|
|
|
# Parse Ombi options
|
|
|
|
self.ombi_enabled = self.enable_check('ombi_server_ids')
|
2018-12-04 08:45:18 -08:00
|
|
|
|
|
|
|
if self.ombi_enabled:
|
2018-12-11 09:45:43 -08:00
|
|
|
for server_id in self.ombi_enabled:
|
2018-12-15 22:55:59 -08:00
|
|
|
section = 'ombi-' + str(server_id)
|
2018-12-11 09:58:11 -08:00
|
|
|
try:
|
2018-12-15 22:55:59 -08:00
|
|
|
url = self.url_check(self.config.get(section, 'url'))
|
2018-12-13 11:55:07 -08:00
|
|
|
|
2018-12-15 22:55:59 -08:00
|
|
|
apikey = self.config.get(section, 'apikey')
|
2018-12-13 11:55:07 -08:00
|
|
|
|
2018-12-15 22:55:59 -08:00
|
|
|
scheme = 'https://' if self.config.getboolean(section, 'ssl') else 'http://'
|
2018-12-13 11:55:07 -08:00
|
|
|
|
2018-12-15 22:55:59 -08:00
|
|
|
verify_ssl = self.config.getboolean(section, 'verify_ssl')
|
2018-12-13 11:55:07 -08:00
|
|
|
|
2018-12-11 09:58:11 -08:00
|
|
|
if scheme != 'https://':
|
|
|
|
verify_ssl = False
|
2018-12-13 11:55:07 -08:00
|
|
|
|
2018-12-15 22:55:59 -08:00
|
|
|
request_type_counts = self.config.getboolean(section, 'get_request_type_counts')
|
2018-12-13 11:55:07 -08:00
|
|
|
|
2018-12-15 22:55:59 -08:00
|
|
|
request_type_run_seconds = self.config.getint(section, 'request_type_run_seconds')
|
2018-12-13 11:55:07 -08:00
|
|
|
|
2018-12-15 22:55:59 -08:00
|
|
|
request_total_counts = self.config.getboolean(section, 'get_request_total_counts')
|
2018-12-13 11:55:07 -08:00
|
|
|
|
2018-12-15 22:55:59 -08:00
|
|
|
request_total_run_seconds = self.config.getint(section, 'request_total_run_seconds')
|
2018-12-11 09:58:11 -08:00
|
|
|
|
|
|
|
server = OmbiServer(server_id, scheme + url, apikey, verify_ssl, request_type_counts,
|
|
|
|
request_type_run_seconds, request_total_counts, request_total_run_seconds)
|
|
|
|
self.ombi_servers.append(server)
|
|
|
|
except configparser.NoOptionError as e:
|
|
|
|
self.ombi_enabled = False
|
2018-12-11 21:37:49 -08:00
|
|
|
self.logger.error(
|
2018-12-15 22:55:59 -08:00
|
|
|
'%s disabled. Error: %s', section, e)
|
|
|
|
|
|
|
|
# Parse SickChill options
|
|
|
|
self.sickchill_enabled = self.enable_check('sickchill_server_ids')
|
|
|
|
|
|
|
|
if self.sickchill_enabled:
|
|
|
|
for server_id in self.sickchill_enabled:
|
|
|
|
section = 'sickchill-' + str(server_id)
|
|
|
|
try:
|
|
|
|
url = self.url_check(self.config.get(section, 'url'))
|
|
|
|
|
|
|
|
apikey = self.config.get(section, 'apikey')
|
|
|
|
|
|
|
|
scheme = 'https://' if self.config.getboolean(section, 'ssl') else 'http://'
|
|
|
|
|
|
|
|
verify_ssl = self.config.getboolean(section, 'verify_ssl')
|
|
|
|
|
|
|
|
if scheme != 'https://':
|
|
|
|
verify_ssl = False
|
|
|
|
|
|
|
|
get_missing = self.config.getboolean(section, 'get_missing')
|
|
|
|
|
|
|
|
get_missing_run_seconds = self.config.getint(section, 'get_missing_run_seconds')
|
|
|
|
|
|
|
|
server = SickChillServer(server_id, scheme + url, apikey, verify_ssl,
|
|
|
|
get_missing, get_missing_run_seconds)
|
|
|
|
self.sickchill_servers.append(server)
|
|
|
|
except configparser.NoOptionError as e:
|
|
|
|
self.sickchill_enabled = False
|
|
|
|
self.logger.error(
|
|
|
|
'%s disabled. Error: %s', section, e)
|
2018-12-04 08:45:18 -08:00
|
|
|
|
|
|
|
# Parse ASA opts
|
2018-12-09 19:41:38 -08:00
|
|
|
self.ciscoasa_enabled = self.enable_check('ciscoasa_firewall_ids')
|
|
|
|
|
|
|
|
if self.ciscoasa_enabled:
|
2018-12-11 09:45:43 -08:00
|
|
|
for firewall_id in self.ciscoasa_enabled:
|
2018-12-15 22:55:59 -08:00
|
|
|
section = 'ciscoasa-' + str(firewall_id)
|
2018-12-11 09:58:11 -08:00
|
|
|
try:
|
2018-12-15 22:55:59 -08:00
|
|
|
url = self.url_check(self.config.get(section, 'url'))
|
2018-12-13 11:55:07 -08:00
|
|
|
|
2018-12-15 22:55:59 -08:00
|
|
|
username = self.config.get(section, 'username')
|
2018-12-13 11:55:07 -08:00
|
|
|
|
2018-12-15 22:55:59 -08:00
|
|
|
password = self.config.get(section, 'password')
|
2018-12-13 11:55:07 -08:00
|
|
|
|
2018-12-15 22:55:59 -08:00
|
|
|
scheme = 'https://' if self.config.getboolean(section, 'ssl') else 'http://'
|
2018-12-13 11:55:07 -08:00
|
|
|
|
2018-12-15 22:55:59 -08:00
|
|
|
verify_ssl = self.config.getboolean(section, 'verify_ssl')
|
2018-12-13 11:55:07 -08:00
|
|
|
|
2018-12-11 09:58:11 -08:00
|
|
|
if scheme != 'https://':
|
|
|
|
verify_ssl = False
|
2018-12-13 11:55:07 -08:00
|
|
|
|
2018-12-15 22:55:59 -08:00
|
|
|
outside_interface = self.config.get(section, 'outside_interface')
|
2018-12-13 11:55:07 -08:00
|
|
|
|
2018-12-15 22:55:59 -08:00
|
|
|
get_bandwidth_run_seconds = self.config.getint(section, 'get_bandwidth_run_seconds')
|
2018-12-11 09:58:11 -08:00
|
|
|
|
|
|
|
firewall = CiscoASAFirewall(firewall_id, scheme + url, username, password, outside_interface,
|
|
|
|
verify_ssl, get_bandwidth_run_seconds)
|
|
|
|
self.ciscoasa_firewalls.append(firewall)
|
|
|
|
except configparser.NoOptionError as e:
|
|
|
|
self.ciscoasa_enabled = False
|
2018-12-11 21:37:49 -08:00
|
|
|
self.logger.error(
|
2018-12-15 22:55:59 -08:00
|
|
|
'%s disabled. Error: %s', section, e)
|