From 40d9bb964e8d8a34167faaf0a79bc8ac573a4820 Mon Sep 17 00:00:00 2001 From: Gab Date: Tue, 20 Oct 2020 02:05:17 -0400 Subject: [PATCH 1/7] Added scraping for uncategorized torrents --- README.md | 1 + qbittorrent_exporter/exporter.py | 12 +++++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d56daec..1fa1428 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,7 @@ The application reads configuration using environment variables: | `QBITTORRENT_PORT` | | qbittorrent server port | | `QBITTORRENT_USER` | `""` | qbittorrent username | | `QBITTORRENT_PASS` | `""` | qbittorrent password | +| `QBITTORRENT_INCLUDE_UNCATEGORIZED` | `"false"` | Include uncategorized torrents | | `EXPORTER_PORT` | `8000` | Exporter listening port | | `EXPORTER_LOG_LEVEL` | `INFO` | Log level. One of: `DEBUG`, `INFO`, `WARNING`, `ERROR`, `CRITICAL` | diff --git a/qbittorrent_exporter/exporter.py b/qbittorrent_exporter/exporter.py index e16e64c..75ed8e7 100644 --- a/qbittorrent_exporter/exporter.py +++ b/qbittorrent_exporter/exporter.py @@ -3,6 +3,7 @@ import os import sys import signal import faulthandler +from attrdict import AttrDict from qbittorrentapi import Client, TorrentStates from prometheus_client import start_http_server from prometheus_client.core import GaugeMetricFamily, CounterMetricFamily, REGISTRY @@ -24,6 +25,7 @@ class QbittorrentMetricsCollector(): "errored", "paused", ] + INCLUDE_UNCATEGORIZED = False def __init__(self, config): self.config = config @@ -34,6 +36,7 @@ class QbittorrentMetricsCollector(): username=config["username"], password=config["password"], ) + self.INCLUDE_UNCATEGORIZED = config["include_uncategorized"] == 'true' def collect(self): try: @@ -122,6 +125,8 @@ class QbittorrentMetricsCollector(): return [] metrics = [] + if self.INCLUDE_UNCATEGORIZED: + categories.Uncategorized = AttrDict({'name': 'Uncategorized', 'savePath': ''}) for category in categories: category_torrents = [t for t in self.torrents if t['category'] == category] @@ -166,7 +171,8 @@ def main(): "username": os.environ.get("QBITTORRENT_USER", ""), "password": os.environ.get("QBITTORRENT_PASS", ""), "exporter_port": int(os.environ.get("EXPORTER_PORT", "8000")), - "log_level": os.environ.get("EXPORTER_LOG_LEVEL", "INFO") + "log_level": os.environ.get("EXPORTER_LOG_LEVEL", "INFO"), + "include_uncategorized": os.environ.get("QBITTORRENT_INCLUDE_UNCATEGORIZED", "false"), } # Register signal handler @@ -194,6 +200,10 @@ def main(): logger.info("Exporter is starting up") REGISTRY.register(QbittorrentMetricsCollector(config)) + logger.info( + f"Including uncategorized torrents: {config['include_uncategorized']}" + ) + # Start server start_http_server(config["exporter_port"]) logger.info( From 9e9f4a9ce406dfdde21bffc8b3d753f8bceceac4 Mon Sep 17 00:00:00 2001 From: Gab Date: Tue, 20 Oct 2020 02:10:04 -0400 Subject: [PATCH 2/7] Added scraping for uncategorized torrents --- qbittorrent_exporter/exporter.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/qbittorrent_exporter/exporter.py b/qbittorrent_exporter/exporter.py index 75ed8e7..dbb495b 100644 --- a/qbittorrent_exporter/exporter.py +++ b/qbittorrent_exporter/exporter.py @@ -128,7 +128,7 @@ class QbittorrentMetricsCollector(): if self.INCLUDE_UNCATEGORIZED: categories.Uncategorized = AttrDict({'name': 'Uncategorized', 'savePath': ''}) for category in categories: - category_torrents = [t for t in self.torrents if t['category'] == category] +category_torrents = [t for t in self.torrents if t['category'] == category or (self.INCLUDE_UNCATEGORIZED and category == "Uncategorized" and t['category'] == "")] for status in self.TORRENT_STATUSES: status_prop = f"is_{status}" @@ -203,7 +203,7 @@ def main(): logger.info( f"Including uncategorized torrents: {config['include_uncategorized']}" ) - + # Start server start_http_server(config["exporter_port"]) logger.info( From 2497249b8cecaf5c701b0dc65060633fefe5a124 Mon Sep 17 00:00:00 2001 From: Gab Date: Tue, 20 Oct 2020 02:10:39 -0400 Subject: [PATCH 3/7] Fixed indentation --- qbittorrent_exporter/exporter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qbittorrent_exporter/exporter.py b/qbittorrent_exporter/exporter.py index dbb495b..94d924c 100644 --- a/qbittorrent_exporter/exporter.py +++ b/qbittorrent_exporter/exporter.py @@ -128,7 +128,7 @@ class QbittorrentMetricsCollector(): if self.INCLUDE_UNCATEGORIZED: categories.Uncategorized = AttrDict({'name': 'Uncategorized', 'savePath': ''}) for category in categories: -category_torrents = [t for t in self.torrents if t['category'] == category or (self.INCLUDE_UNCATEGORIZED and category == "Uncategorized" and t['category'] == "")] + category_torrents = [t for t in self.torrents if t['category'] == category or (self.INCLUDE_UNCATEGORIZED and category == "Uncategorized" and t['category'] == "")] for status in self.TORRENT_STATUSES: status_prop = f"is_{status}" From 003297ecfd2e054c64173336ce4586464896589c Mon Sep 17 00:00:00 2001 From: Gab Date: Tue, 20 Oct 2020 11:45:48 -0400 Subject: [PATCH 4/7] Removed uncategorized from config --- qbittorrent_exporter/exporter.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/qbittorrent_exporter/exporter.py b/qbittorrent_exporter/exporter.py index 94d924c..2023ab7 100644 --- a/qbittorrent_exporter/exporter.py +++ b/qbittorrent_exporter/exporter.py @@ -25,7 +25,6 @@ class QbittorrentMetricsCollector(): "errored", "paused", ] - INCLUDE_UNCATEGORIZED = False def __init__(self, config): self.config = config @@ -36,7 +35,6 @@ class QbittorrentMetricsCollector(): username=config["username"], password=config["password"], ) - self.INCLUDE_UNCATEGORIZED = config["include_uncategorized"] == 'true' def collect(self): try: @@ -125,10 +123,9 @@ class QbittorrentMetricsCollector(): return [] metrics = [] - if self.INCLUDE_UNCATEGORIZED: - categories.Uncategorized = AttrDict({'name': 'Uncategorized', 'savePath': ''}) + categories.Uncategorized = AttrDict({'name': 'Uncategorized', 'savePath': ''}) for category in categories: - category_torrents = [t for t in self.torrents if t['category'] == category or (self.INCLUDE_UNCATEGORIZED and category == "Uncategorized" and t['category'] == "")] + category_torrents = [t for t in self.torrents if t['category'] == category or (category == "Uncategorized" and t['category'] == "")] for status in self.TORRENT_STATUSES: status_prop = f"is_{status}" From baf14d495cfa0b3433d2b4313db7b24987fe6a75 Mon Sep 17 00:00:00 2001 From: Gab Date: Tue, 20 Oct 2020 11:46:50 -0400 Subject: [PATCH 5/7] Removed now useless logging message --- qbittorrent_exporter/exporter.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/qbittorrent_exporter/exporter.py b/qbittorrent_exporter/exporter.py index 2023ab7..5b9c23f 100644 --- a/qbittorrent_exporter/exporter.py +++ b/qbittorrent_exporter/exporter.py @@ -197,10 +197,6 @@ def main(): logger.info("Exporter is starting up") REGISTRY.register(QbittorrentMetricsCollector(config)) - logger.info( - f"Including uncategorized torrents: {config['include_uncategorized']}" - ) - # Start server start_http_server(config["exporter_port"]) logger.info( From b4aefff06df2181ffd90e45c0190368852a7bed1 Mon Sep 17 00:00:00 2001 From: Gab Date: Tue, 20 Oct 2020 11:57:33 -0400 Subject: [PATCH 6/7] Changed readme --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 1fa1428..d56daec 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,6 @@ The application reads configuration using environment variables: | `QBITTORRENT_PORT` | | qbittorrent server port | | `QBITTORRENT_USER` | `""` | qbittorrent username | | `QBITTORRENT_PASS` | `""` | qbittorrent password | -| `QBITTORRENT_INCLUDE_UNCATEGORIZED` | `"false"` | Include uncategorized torrents | | `EXPORTER_PORT` | `8000` | Exporter listening port | | `EXPORTER_LOG_LEVEL` | `INFO` | Log level. One of: `DEBUG`, `INFO`, `WARNING`, `ERROR`, `CRITICAL` | From 3bfc3f06738a18752bbe9c9e130817780bd5f6aa Mon Sep 17 00:00:00 2001 From: Gab Date: Wed, 21 Oct 2020 18:58:17 -0400 Subject: [PATCH 7/7] Removed unused config --- qbittorrent_exporter/exporter.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/qbittorrent_exporter/exporter.py b/qbittorrent_exporter/exporter.py index 5b9c23f..4e742a1 100644 --- a/qbittorrent_exporter/exporter.py +++ b/qbittorrent_exporter/exporter.py @@ -168,8 +168,7 @@ def main(): "username": os.environ.get("QBITTORRENT_USER", ""), "password": os.environ.get("QBITTORRENT_PASS", ""), "exporter_port": int(os.environ.get("EXPORTER_PORT", "8000")), - "log_level": os.environ.get("EXPORTER_LOG_LEVEL", "INFO"), - "include_uncategorized": os.environ.get("QBITTORRENT_INCLUDE_UNCATEGORIZED", "false"), + "log_level": os.environ.get("EXPORTER_LOG_LEVEL", "INFO") } # Register signal handler