Small fixes

This commit is contained in:
Esteban Sánchez 2021-07-02 16:07:53 +02:00
parent 03d332979b
commit de00864f11

View file

@ -5,6 +5,7 @@ import signal
import faulthandler import faulthandler
from attrdict import AttrDict from attrdict import AttrDict
from qbittorrentapi import Client, TorrentStates from qbittorrentapi import Client, TorrentStates
from qbittorrentapi.exceptions import APIConnectionError
from prometheus_client import start_http_server from prometheus_client import start_http_server
from prometheus_client.core import GaugeMetricFamily, CounterMetricFamily, REGISTRY from prometheus_client.core import GaugeMetricFamily, CounterMetricFamily, REGISTRY
import logging import logging
@ -41,6 +42,7 @@ class QbittorrentMetricsCollector():
self.torrents = self.client.torrents.info() self.torrents = self.client.torrents.info()
except Exception as e: except Exception as e:
logger.error(f"Couldn't get server info: {e}") logger.error(f"Couldn't get server info: {e}")
return None
metrics = self.get_qbittorrent_metrics() metrics = self.get_qbittorrent_metrics()
@ -66,20 +68,23 @@ class QbittorrentMetricsCollector():
return metrics return metrics
def get_qbittorrent_status_metrics(self): def get_qbittorrent_status_metrics(self):
response = {}
version = ""
# Fetch data from API # Fetch data from API
try: try:
response = self.client.transfer.info response = self.client.transfer.info
version = self.client.app.version version = self.client.app.version
self.torrents = self.client.torrents.info() self.torrents = self.client.torrents.info()
except Exception as e: except APIConnectionError as e:
logger.error(f"Couldn't get server info: {e}") logger.error(f"Couldn't get server info: {e.error_message}")
response = None except Exception:
version = "" logger.error(f"Couldn't get server info")
return [ return [
{ {
"name": f"{self.config['metrics_prefix']}_up", "name": f"{self.config['metrics_prefix']}_up",
"value": response is not None, "value": bool(response),
"labels": {"version": version}, "labels": {"version": version},
"help": "Whether if server is alive or not", "help": "Whether if server is alive or not",
}, },