diff --git a/clients/deluge.py b/clients/deluge.py index a6afdb5a706009f4cd63f745d254204506684122..8918ec2b548b940a099d389d89a335dd13afabb4 100644 --- a/clients/deluge.py +++ b/clients/deluge.py @@ -22,7 +22,7 @@ class DelugeClient(TorrentClient): :return: request """ - self.send_log('Adding headers to request', 'info') + self.send_log('Adding headers to request', 'debug') headers = { 'Content-Type': 'application/json', @@ -99,7 +99,7 @@ class DelugeClient(TorrentClient): json_output = json.loads(raw_output) - return json_output if json_output['result'] else None + return json_output def _authenticate(self): """ @@ -120,15 +120,19 @@ class DelugeClient(TorrentClient): print(e) sys.exit(1) - # We need the session ID to send with future requests - self.session_id = res.headers['Set-Cookie'].split(';')[0] - output = self._process_response(res) - if output and not output['result']: - msg = 'Failed to authenticate to {} API. Aborting'.format(self.torrent_client) - self.send_log(msg, 'error') - print(msg) + # If response has result but it's None than the login failed + if 'result' in output and not output['result']: + msg = 'Failed to authenticate to {} API. Check your password and try again.'.format(self.torrent_client) + self.send_log(msg, 'critical') + sys.exit(1) + + # We need the session ID to send with future requests + if 'Set-Cookie' in res.headers: + self.session_id = res.headers['Set-Cookie'].split(';')[0] + else: + self.send_log('No authentication cookie in response. Aborting', 'critical') sys.exit(1) msg = 'Successfully Authenticated With {} API'.format(self.torrent_client) @@ -150,7 +154,7 @@ class DelugeClient(TorrentClient): self.torrent_list[hash]['progress'] = round(data['progress'], 2) self.torrent_list[hash]['total_downloaded'] = data['all_time_download'] self.torrent_list[hash]['total_uploaded'] = data['total_uploaded'] - self.torrent_list[hash]['ratio'] = round(data['ratio'], 2) + self.torrent_list[hash]['ratio'] = data['ratio'] self.torrent_list[hash]['total_seeds'] = data['total_seeds'] self.torrent_list[hash]['state'] = data['state'] self.torrent_list[hash]['tracker'] = data['tracker_host'] diff --git a/clients/torrentclient.py b/clients/torrentclient.py index babd109c7958c7e0173d0e58a7852a59f2c0a62d..79926492391872555318a4c0c51c508b163b34e7 100644 --- a/clients/torrentclient.py +++ b/clients/torrentclient.py @@ -116,16 +116,17 @@ class TorrentClient: trackers[data['tracker']]['total_uploaded'] += data['total_uploaded'] trackers[data['tracker']]['total_downloaded'] += data['total_downloaded'] trackers[data['tracker']]['total_size'] += data['total_size'] + trackers[data['tracker']]['total_ratio'] += data['ratio'] else: trackers[data['tracker']] = {} trackers[data['tracker']]['total_torrents'] = 1 trackers[data['tracker']]['total_uploaded'] = data['total_uploaded'] trackers[data['tracker']]['total_downloaded'] = data['total_downloaded'] trackers[data['tracker']]['total_size'] = data['total_size'] + trackers[data['tracker']]['total_ratio'] = data['ratio'] for k, v in trackers.items(): - total_ratio = round(v['total_uploaded'] / v['total_size'], 3) tracker_json = [ { 'measurement': 'trackers', @@ -133,7 +134,8 @@ class TorrentClient: 'total_torrents': v['total_torrents'], 'total_upload': v['total_uploaded'], 'total_download': v['total_downloaded'], - 'total_ratio': total_ratio + 'total_ratio': v['total_ratio'], + 'tracker': k, }, 'tags': { 'host': self.hostname, diff --git a/influxdbSeedbox.py b/influxdbSeedbox.py index 7ee02b5066475b0620205a5c936e463bd97434ee..dd7492856a29eb6650dfbe8f08c41ddc557de6b5 100644 --- a/influxdbSeedbox.py +++ b/influxdbSeedbox.py @@ -20,7 +20,7 @@ class configManager(): #TODO Validate given client url - def __init__(self, config): + def __init__(self, silent, config): self.valid_torrent_clients = ['deluge', 'utorrent', 'rtorrent'] self.valid_log_levels = { @@ -30,9 +30,11 @@ class configManager(): 'ERROR': 3, 'CRITICAL': 4 } + self.silent = silent + if not self.silent: + print('Loading Configuration File {}'.format(config)) - print('Loading Configuration File {}'.format(config)) config_file = os.path.join(os.getcwd(), config) if os.path.isfile(config_file): self.config = configparser.ConfigParser() @@ -44,7 +46,8 @@ class configManager(): self._load_config_values() self._validate_logging_level() self._validate_torrent_client() - print('Configuration Successfully Loaded') + if not self.silent: + print('Configuration Successfully Loaded') def _load_config_values(self): @@ -95,17 +98,17 @@ class configManager(): self.logging_level = self.logging_level.upper() return else: - print('Invalid logging level provided. {}'.format(self.logging_level)) - print('Logging will be disabled') - print('Valid options are: {}'.format(', '.join(self.valid_log_levels))) + if not self.silent: + print('Invalid logging level provided. {}'.format(self.logging_level)) + print('Logging will be disabled') self.logging = None class influxdbSeedbox(): - def __init__(self, config=None): + def __init__(self, config=None, silent=None): - self.config = configManager(config=config) + self.config = configManager(silent, config=config) self.output = self.config.output self.logger = None @@ -122,7 +125,8 @@ class influxdbSeedbox(): if self.config.tor_client == 'deluge': from clients.deluge import DelugeClient - print('Generating Deluge Client') + if self.output: + print('Generating Deluge Client') self.tor_client = DelugeClient(self.send_log, username=self.config.tor_client_user, password=self.config.tor_client_password, @@ -131,16 +135,18 @@ class influxdbSeedbox(): elif self.config.tor_client == 'utorrent': from clients.utorrent import UTorrentClient - print('Generating uTorrent Client') + if self.output: + print('Generating uTorrent Client') self.tor_client = UTorrentClient(self.send_log, - username=self.config.tor_client_user, - password=self.config.tor_client_password, - url=self.config.tor_client_url, - hostname=self.config.hostname) + username=self.config.tor_client_user, + password=self.config.tor_client_password, + url=self.config.tor_client_url, + hostname=self.config.hostname) elif self.config.tor_client == 'rtorrent': from clients.rtorrent import rTorrentClient - print('Generating rTorrent Client') + if self.output: + print('Generating rTorrent Client') self.tor_client = rTorrentClient(self.send_log, username=None, password=None, @@ -154,7 +160,8 @@ class influxdbSeedbox(): """ if self.config.logging: - print('Logging is enabled. Log output will be sent to {}'.format(self.config.logging_file)) + if self.output: + print('Logging is enabled. Log output will be sent to {}'.format(self.config.logging_file)) self.logger = logging.getLogger(__name__) self.logger.setLevel(self.config.logging_level) formatter = logging.Formatter('%(asctime)s %(levelname)s: %(message)s') @@ -211,7 +218,7 @@ class influxdbSeedbox(): :param json_data: :return: """ - self.send_log(json_data, 'debug') + self.send_log(json_data, 'info') # TODO This bit of fuckery may turn out to not be a good idea. """ @@ -268,9 +275,10 @@ def main(): parser = argparse.ArgumentParser(description="A tool to send Torrent Client statistics to InfluxDB") parser.add_argument('--config', default='config.ini', dest='config', help='Specify a custom location for the config file') - parser.add_argument('--silent', default=False, dest='config', help='Surpress All Output') + # Silent flag allows output prior to the config being loaded to also be suppressed + parser.add_argument('--silent', action='store_true', help='Surpress All Output, regardless of config settings') args = parser.parse_args() - monitor = influxdbSeedbox(config=args.config) + monitor = influxdbSeedbox(silent=args.silent, config=args.config) monitor.run()