Guest User

Untitled

a guest
Aug 24th, 2025
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. import os
  3. import time
  4. import requests
  5. import sys
  6. import threading
  7.  
  8. # Discord Webhook URL
  9. DISCORD_WEBHOOK = "https://discord.com/api/webhooks/xxxx"
  10.  
  11. # Log file paths
  12. LOG_FILES = {
  13. "sonarr": "/home/user/docker/sonarr/config/logs/sonarr.debug.txt",
  14. "sonarr_4k": "/home/user/docker/sonarr_4k/config/logs/sonarr.debug.txt",
  15. "radarr": "/home/user/docker/radarr/config/logs/radarr.debug.txt",
  16. "radarr_4k": "/home/user/docker/radarr_4k/config/logs/radarr.debug.txt",
  17. }
  18.  
  19. # The search string we care about
  20. SEARCH_STRING = "ReleaseSearchService|Total of 0 reports were found"
  21.  
  22. # Enable debug mode (set False to disable)
  23. DEBUG = False
  24.  
  25.  
  26. def send_to_discord(instance_name, line):
  27. """Send the matched log line to Discord"""
  28. try:
  29. payload = {
  30. "content": f"**[{instance_name}] ⚠️ No torrents found in indexers:**\n```{line.strip()}```"
  31. }
  32. requests.post(DISCORD_WEBHOOK, json=payload, timeout=5)
  33. except Exception as e:
  34. print(f"Error sending to Discord: {e}", file=sys.stderr)
  35.  
  36.  
  37. def tail_file(instance_name, filepath):
  38. """Continuously follow a log file for matching lines, handling rotation"""
  39. f = open(filepath, "r", encoding="utf-8", errors="ignore")
  40. f.seek(0, os.SEEK_END)
  41. current_inode = os.fstat(f.fileno()).st_ino
  42.  
  43. while True:
  44. line = f.readline()
  45. if line:
  46. if SEARCH_STRING in line:
  47. send_to_discord(instance_name, line)
  48. else:
  49. time.sleep(0.1)
  50.  
  51. try:
  52. new_inode = os.stat(filepath).st_ino
  53. if new_inode != current_inode:
  54. # File was rotated -> reopen
  55. f.close()
  56. f = open(filepath, "r", encoding="utf-8", errors="ignore")
  57. current_inode = os.fstat(f.fileno()).st_ino
  58. print(f"[{instance_name}] log rotated, reopened {filepath}")
  59. except FileNotFoundError:
  60. # File temporarily missing during rotation, retry next loop
  61. pass
  62.  
  63. def main():
  64. threads = []
  65. for instance_name, filepath in LOG_FILES.items():
  66. if os.path.exists(filepath):
  67. print(f"Starting to monitor: {instance_name} ({filepath})")
  68. t = threading.Thread(target=tail_file, args=(instance_name, filepath), daemon=True)
  69. threads.append(t)
  70. t.start()
  71. else:
  72. print(f"Warning: {filepath} does not exist", file=sys.stderr)
  73.  
  74. # Keep main thread alive
  75. try:
  76. while True:
  77. time.sleep(1)
  78. except KeyboardInterrupt:
  79. print("Stopping monitor...")
  80.  
  81.  
  82. if __name__ == "__main__":
  83. main()
  84.  
Add Comment
Please, Sign In to add comment