feat: unread mail counter

This commit is contained in:
Strix 2025-04-10 01:51:02 +02:00
parent d4496a2eda
commit cdbb47f6b9
5 changed files with 54 additions and 7 deletions

View file

@ -1,7 +1,13 @@
EXPORTER_PORT=8000 EXPORTER_PORT=8000
SCRAPE_INTERVAL=30 UPDATE_INTERVAL=30
# twitch # twitch
TWITCH_CLIENT_ID= TWITCH_CLIENT_ID=
TWITCH_CLIENT_SECRET= TWITCH_CLIENT_SECRET=
TWITCH_CHANNELS=ikiznear TWITCH_CHANNELS=ikiznear
# mail
MAIL_SERVER=
MAIL_PORT=993
MAIL_USER=
MAIL_PASS=

View file

@ -1,4 +1,4 @@
FROM python:3.11-slim FROM python:latest
WORKDIR /app WORKDIR /app
COPY . /app COPY . /app

View file

@ -4,7 +4,7 @@ import time
from prometheus_client import start_http_server from prometheus_client import start_http_server
from dotenv import load_dotenv from dotenv import load_dotenv
load_dotenv() load_dotenv('.env', verbose=True)
def load_stat_modules(path='stats'): def load_stat_modules(path='stats'):
modules = [] modules = []
@ -23,10 +23,10 @@ def load_stat_modules(path='stats'):
if __name__ == '__main__': if __name__ == '__main__':
port = int(os.getenv("EXPORTER_PORT", "8000")) port = int(os.getenv("EXPORTER_PORT", "8000"))
modules = load_stat_modules()
print(f"Starting exporter on port {port}...") print(f"Starting exporter on port {port}...")
start_http_server(port) start_http_server(port)
modules = load_stat_modules()
while True: while True:
for mod in modules: for mod in modules:
@ -34,4 +34,4 @@ if __name__ == '__main__':
mod.update() mod.update()
except Exception as e: except Exception as e:
print(f"Error in {mod.__name__}: {e}") print(f"Error in {mod.__name__}: {e}")
time.sleep(int(os.getenv("SCRAPE_INTERVAL", "30"))) time.sleep(int(os.getenv("UPDATE_INTERVAL", "30")))

42
stats/mail.py Normal file
View file

@ -0,0 +1,42 @@
from prometheus_client import Gauge
import os
import imaplib
unread_mails = Gauge('unread_mails', 'Number of unread mails', ['mailbox'])
MAIL_SERVER = os.getenv("MAIL_SERVER")
MAIL_USER = os.getenv("MAIL_USER")
MAIL_PASS = os.getenv("MAIL_PASS")
MAIL_PORT = int(os.getenv("MAIL_PORT", 993))
print("[mail] Mail server:", MAIL_SERVER)
print("[mail] Mail user:", MAIL_USER)
print("[mail] Mail port:", MAIL_PORT)
def update():
try:
# handle secure differently, detect with port
if MAIL_PORT == 993:
mail = imaplib.IMAP4_SSL(MAIL_SERVER, MAIL_PORT)
else:
mail = imaplib.IMAP4(MAIL_SERVER, MAIL_PORT)
mail.login(MAIL_USER, MAIL_PASS)
mail.select("inbox")
result, data = mail.search(None, 'UNSEEN')
if result == 'OK':
unread_count = len(data[0].split())
unread_mails.labels(mailbox="inbox").set(unread_count)
else:
print(f"[mail] Error searching inbox: {data}")
unread_mails.labels(mailbox="inbox").set(0)
mail.close()
mail.logout()
except imaplib.IMAP4.error as e:
print(f"[mail] IMAP error: {e}")
unread_mails.labels(mailbox="inbox").set(0)
except ConnectionRefusedError:
print("[mail] Connection refused. Check your IMAP server settings.")
unread_mails.labels(mailbox="inbox").set(0)
except Exception as e:
print(f"[mail] Error checking unread mails: {e}")
unread_mails.labels(mailbox="inbox").set(0)

View file

@ -17,7 +17,6 @@ _token_cache = {
} }
print("[twitch] Client ID:", TWITCH_CLIENT_ID) print("[twitch] Client ID:", TWITCH_CLIENT_ID)
print("[twitch] Client Secret:", TWITCH_CLIENT_SECRET)
print("[twitch] Channels:", CHANNELS) print("[twitch] Channels:", CHANNELS)
def get_token(): def get_token():