Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import os
- import time
- import requests
- import sys
- import threading
- # Discord Webhook URL
- DISCORD_WEBHOOK = "https://discord.com/api/webhooks/xxxx"
- # Log file paths
- LOG_FILES = {
- "sonarr": "/home/user/docker/sonarr/config/logs/sonarr.debug.txt",
- "sonarr_4k": "/home/user/docker/sonarr_4k/config/logs/sonarr.debug.txt",
- "radarr": "/home/user/docker/radarr/config/logs/radarr.debug.txt",
- "radarr_4k": "/home/user/docker/radarr_4k/config/logs/radarr.debug.txt",
- }
- # The search string we care about
- SEARCH_STRING = "ReleaseSearchService|Total of 0 reports were found"
- # Enable debug mode (set False to disable)
- DEBUG = False
- def send_to_discord(instance_name, line):
- """Send the matched log line to Discord"""
- try:
- payload = {
- "content": f"**[{instance_name}] ⚠️ No torrents found in indexers:**\n```{line.strip()}```"
- }
- requests.post(DISCORD_WEBHOOK, json=payload, timeout=5)
- except Exception as e:
- print(f"Error sending to Discord: {e}", file=sys.stderr)
- def tail_file(instance_name, filepath):
- """Continuously follow a log file for matching lines, handling rotation"""
- f = open(filepath, "r", encoding="utf-8", errors="ignore")
- f.seek(0, os.SEEK_END)
- current_inode = os.fstat(f.fileno()).st_ino
- while True:
- line = f.readline()
- if line:
- if SEARCH_STRING in line:
- send_to_discord(instance_name, line)
- else:
- time.sleep(0.1)
- try:
- new_inode = os.stat(filepath).st_ino
- if new_inode != current_inode:
- # File was rotated -> reopen
- f.close()
- f = open(filepath, "r", encoding="utf-8", errors="ignore")
- current_inode = os.fstat(f.fileno()).st_ino
- print(f"[{instance_name}] log rotated, reopened {filepath}")
- except FileNotFoundError:
- # File temporarily missing during rotation, retry next loop
- pass
- def main():
- threads = []
- for instance_name, filepath in LOG_FILES.items():
- if os.path.exists(filepath):
- print(f"Starting to monitor: {instance_name} ({filepath})")
- t = threading.Thread(target=tail_file, args=(instance_name, filepath), daemon=True)
- threads.append(t)
- t.start()
- else:
- print(f"Warning: {filepath} does not exist", file=sys.stderr)
- # Keep main thread alive
- try:
- while True:
- time.sleep(1)
- except KeyboardInterrupt:
- print("Stopping monitor...")
- if __name__ == "__main__":
- main()
Add Comment
Please, Sign In to add comment