Added scraping for uncategorized torrents

This commit is contained in:
Gab 2020-10-20 02:05:17 -04:00
parent 0b3fb04728
commit 40d9bb964e
2 changed files with 12 additions and 1 deletions

View file

@ -31,6 +31,7 @@ The application reads configuration using environment variables:
| `QBITTORRENT_PORT` | | qbittorrent server port | | `QBITTORRENT_PORT` | | qbittorrent server port |
| `QBITTORRENT_USER` | `""` | qbittorrent username | | `QBITTORRENT_USER` | `""` | qbittorrent username |
| `QBITTORRENT_PASS` | `""` | qbittorrent password | | `QBITTORRENT_PASS` | `""` | qbittorrent password |
| `QBITTORRENT_INCLUDE_UNCATEGORIZED` | `"false"` | Include uncategorized torrents |
| `EXPORTER_PORT` | `8000` | Exporter listening port | | `EXPORTER_PORT` | `8000` | Exporter listening port |
| `EXPORTER_LOG_LEVEL` | `INFO` | Log level. One of: `DEBUG`, `INFO`, `WARNING`, `ERROR`, `CRITICAL` | | `EXPORTER_LOG_LEVEL` | `INFO` | Log level. One of: `DEBUG`, `INFO`, `WARNING`, `ERROR`, `CRITICAL` |

View file

@ -3,6 +3,7 @@ import os
import sys import sys
import signal import signal
import faulthandler import faulthandler
from attrdict import AttrDict
from qbittorrentapi import Client, TorrentStates from qbittorrentapi import Client, TorrentStates
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
@ -24,6 +25,7 @@ class QbittorrentMetricsCollector():
"errored", "errored",
"paused", "paused",
] ]
INCLUDE_UNCATEGORIZED = False
def __init__(self, config): def __init__(self, config):
self.config = config self.config = config
@ -34,6 +36,7 @@ class QbittorrentMetricsCollector():
username=config["username"], username=config["username"],
password=config["password"], password=config["password"],
) )
self.INCLUDE_UNCATEGORIZED = config["include_uncategorized"] == 'true'
def collect(self): def collect(self):
try: try:
@ -122,6 +125,8 @@ class QbittorrentMetricsCollector():
return [] return []
metrics = [] metrics = []
if self.INCLUDE_UNCATEGORIZED:
categories.Uncategorized = AttrDict({'name': 'Uncategorized', 'savePath': ''})
for category in categories: 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]
@ -166,7 +171,8 @@ def main():
"username": os.environ.get("QBITTORRENT_USER", ""), "username": os.environ.get("QBITTORRENT_USER", ""),
"password": os.environ.get("QBITTORRENT_PASS", ""), "password": os.environ.get("QBITTORRENT_PASS", ""),
"exporter_port": int(os.environ.get("EXPORTER_PORT", "8000")), "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 # Register signal handler
@ -194,6 +200,10 @@ def main():
logger.info("Exporter is starting up") logger.info("Exporter is starting up")
REGISTRY.register(QbittorrentMetricsCollector(config)) REGISTRY.register(QbittorrentMetricsCollector(config))
logger.info(
f"Including uncategorized torrents: {config['include_uncategorized']}"
)
# Start server # Start server
start_http_server(config["exporter_port"]) start_http_server(config["exporter_port"])
logger.info( logger.info(