Varken/varken/iniparser.py

312 lines
12 KiB
Python
Raw Normal View History

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
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):
self.config = configparser.ConfigParser(interpolation=None)
2018-12-04 08:45:18 -08:00
self.data_folder = data_folder
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 = []
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
self.filtered_strings = None
def config_blacklist(self):
filtered_strings = [section.get(k) for key, section in self.config.items()
for k in section if k in BlacklistFilter.blacklisted_strings]
self.filtered_strings = list(filter(None, filtered_strings))
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-10 16:49:22 -08:00
try:
global_server_ids = self.config.get('global', t)
if global_server_ids.lower() in ['false', 'no', '0']:
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:
self.logger.error(e)
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)
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-14 12:25:17 -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
)
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:
self.logger.error('%s is invalid! URL must host/IP and port if not 80 or 443. ie. localhost:8080', url_check)
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-14 12:25:17 -08:00
self.logger.debug('%s is a vlaid 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-11 11:30:54 -08:00
sonarr_section = 'sonarr-' + str(server_id)
2018-12-11 11:33:08 -08:00
try:
2018-12-13 11:55:07 -08:00
url = self.url_check(self.config.get(sonarr_section, 'url'))
2018-12-11 11:33:08 -08:00
apikey = self.config.get(sonarr_section, 'apikey')
2018-12-13 11:55:07 -08:00
scheme = 'https://' if self.config.getboolean(
sonarr_section, 'ssl') else 'http://'
2018-12-13 11:55:07 -08:00
verify_ssl = self.config.getboolean(
sonarr_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-11 11:33:08 -08:00
queue = self.config.getboolean(sonarr_section, 'queue')
2018-12-13 11:55:07 -08:00
missing_days = self.config.getint(
sonarr_section, 'missing_days')
2018-12-13 11:55:07 -08:00
future_days = self.config.getint(
sonarr_section, 'future_days')
2018-12-13 11:55:07 -08:00
missing_days_run_seconds = self.config.getint(
sonarr_section, 'missing_days_run_seconds')
2018-12-13 11:55:07 -08:00
future_days_run_seconds = self.config.getint(
sonarr_section, 'future_days_run_seconds')
2018-12-13 11:55:07 -08:00
queue_run_seconds = self.config.getint(
sonarr_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:
self.radarr_enabled = False
self.logger.error(
'%s disabled. Error: %s', sonarr_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-11 11:30:54 -08:00
radarr_section = 'radarr-' + str(server_id)
try:
2018-12-13 11:55:07 -08:00
url = self.url_check(self.config.get(radarr_section, 'url'))
apikey = self.config.get(radarr_section, 'apikey')
2018-12-13 11:55:07 -08:00
scheme = 'https://' if self.config.getboolean(
radarr_section, 'ssl') else 'http://'
2018-12-13 11:55:07 -08:00
verify_ssl = self.config.getboolean(
radarr_section, 'verify_ssl')
2018-12-13 11:55:07 -08:00
if scheme != 'https://':
verify_ssl = False
2018-12-13 11:55:07 -08:00
queue = self.config.getboolean(radarr_section, 'queue')
2018-12-13 11:55:07 -08:00
queue_run_seconds = self.config.getint(
radarr_section, 'queue_run_seconds')
2018-12-13 11:55:07 -08:00
get_missing = self.config.getboolean(
radarr_section, 'get_missing')
2018-12-13 11:55:07 -08:00
get_missing_run_seconds = self.config.getint(
radarr_section, 'get_missing_run_seconds')
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
self.logger.error(
'%s disabled. Error: %s', radarr_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-11 11:30:54 -08:00
tautulli_section = 'tautulli-' + str(server_id)
try:
2018-12-13 11:55:07 -08:00
url = self.url_check(self.config.get(tautulli_section, 'url'))
fallback_ip = self.config.get(
tautulli_section, 'fallback_ip')
2018-12-13 11:55:07 -08:00
apikey = self.config.get(tautulli_section, 'apikey')
2018-12-13 11:55:07 -08:00
scheme = 'https://' if self.config.getboolean(
tautulli_section, 'ssl') else 'http://'
2018-12-13 11:55:07 -08:00
verify_ssl = self.config.getboolean(
tautulli_section, 'verify_ssl')
2018-12-13 11:55:07 -08:00
if scheme != 'https://':
verify_ssl = False
2018-12-13 11:55:07 -08:00
get_activity = self.config.getboolean(
tautulli_section, 'get_activity')
2018-12-13 11:55:07 -08:00
get_activity_run_seconds = self.config.getint(
tautulli_section, 'get_activity_run_seconds')
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
self.logger.error(
'%s disabled. Error: %s', tautulli_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-11 11:30:54 -08:00
ombi_section = 'ombi-' + str(server_id)
try:
2018-12-13 11:55:07 -08:00
url = self.url_check(self.config.get(ombi_section, 'url'))
apikey = self.config.get(ombi_section, 'apikey')
2018-12-13 11:55:07 -08:00
scheme = 'https://' if self.config.getboolean(
ombi_section, 'ssl') else 'http://'
2018-12-13 11:55:07 -08:00
verify_ssl = self.config.getboolean(
ombi_section, 'verify_ssl')
2018-12-13 11:55:07 -08:00
if scheme != 'https://':
verify_ssl = False
2018-12-13 11:55:07 -08:00
request_type_counts = self.config.getboolean(
ombi_section, 'get_request_type_counts')
2018-12-13 11:55:07 -08:00
request_type_run_seconds = self.config.getint(
ombi_section, 'request_type_run_seconds')
2018-12-13 11:55:07 -08:00
request_total_counts = self.config.getboolean(
ombi_section, 'get_request_total_counts')
2018-12-13 11:55:07 -08:00
request_total_run_seconds = self.config.getint(
ombi_section, 'request_total_run_seconds')
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
self.logger.error(
'%s disabled. Error: %s', ombi_section, e)
2018-12-04 08:45:18 -08:00
# Parse ASA opts
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-11 11:30:54 -08:00
ciscoasa_section = 'ciscoasa-' + str(firewall_id)
try:
2018-12-13 11:55:07 -08:00
url = self.url_check(self.config.get(ciscoasa_section, 'url'))
username = self.config.get(ciscoasa_section, 'username')
2018-12-13 11:55:07 -08:00
password = self.config.get(ciscoasa_section, 'password')
2018-12-13 11:55:07 -08:00
scheme = 'https://' if self.config.getboolean(
ciscoasa_section, 'ssl') else 'http://'
2018-12-13 11:55:07 -08:00
verify_ssl = self.config.getboolean(
ciscoasa_section, 'verify_ssl')
2018-12-13 11:55:07 -08:00
if scheme != 'https://':
verify_ssl = False
2018-12-13 11:55:07 -08:00
outside_interface = self.config.get(
ciscoasa_section, 'outside_interface')
2018-12-13 11:55:07 -08:00
get_bandwidth_run_seconds = self.config.getint(
ciscoasa_section, 'get_bandwidth_run_seconds')
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
self.logger.error(
'%s disabled. Error: %s', ciscoasa_section, e)