refactor: merge Report into Expiry class and shorten prom output code

This commit is contained in:
holger krekel
2026-11-24 18:10:05 +01:00
parent 416fbbf512
commit 64c8bde5de
2 changed files with 65 additions and 123 deletions
+64 -122
View File
@@ -7,7 +7,6 @@ import os
import re
import shutil
import sys
import tempfile
import time
from argparse import ArgumentParser
from collections import namedtuple
@@ -125,7 +124,23 @@ def print_info(msg):
print(msg, file=sys.stderr)
PROM_HELP = {
"storage_bytes": "Mailbox storage in bytes.",
"messages_bytes": "Sum of msg bytes >= threshold.",
"messages_count": "Number of msgs >= size threshold.",
"accounts": "Number of accounts.",
"accounts_active": "Non-CI accounts active within N days.",
"expire_removed_mailboxes": "Mailboxes removed by the last run.",
"expire_removed_files": "Files removed by the last run.",
"expire_duration_seconds": "Duration of the last run.",
"expire_last_run_timestamp_seconds": "End of the last run.",
}
class Expiry:
login_days = (1, 10, 30, 40, 80, 100, 150)
size_thresholds = (0, 100 * 1024, 512 * 1024, 1 << 20, 2 << 20, 5 << 20, 10 << 20)
def __init__(self, config, dry, now, verbose):
self.config = config
self.dry = dry
@@ -137,6 +152,11 @@ class Expiry:
self.all_files = 0
self.removed_paths = set()
self.start = time.time()
self.size_messages = self.size_extra = 0
self.num_ci_logins = self.num_all_logins = 0
self.login_buckets = dict.fromkeys(self.login_days, 0)
self.msg_bytes = dict.fromkeys(self.size_thresholds, 0)
self.msg_counts = dict.fromkeys(self.size_thresholds, 0)
def remove_mailbox(self, mboxdir):
if self.verbose:
@@ -171,7 +191,7 @@ class Expiry:
changed = False
if mbox.last_login and mbox.last_login < cutoff_without_login:
self.remove_mailbox(mbox.basedir)
return False
return
elif mbox.last_login is None:
try:
if not self.dry:
@@ -181,7 +201,7 @@ class Expiry:
print_info(
f"Skipped deleting {mbox.basedir}, doesn't have last_login but isn't empty"
)
return False
return
mboxname = os.path.basename(mbox.basedir)
if self.verbose:
@@ -219,8 +239,22 @@ class Expiry:
if changed:
self.remove_file(f"{mbox.basedir}/maildirsize")
mbox.messages = [m for m in mbox.messages if m.path not in self.removed_paths]
return True
self.num_all_logins += 1
if mboxname.startswith("ci-"):
self.num_ci_logins += 1
else:
for days in self.login_buckets:
if mbox.last_login >= self.now - days * 86400:
self.login_buckets[days] += 1
kept = [m for m in mbox.messages if m.path not in self.removed_paths]
for size in self.msg_bytes:
for msg in kept:
if msg.size >= size:
self.msg_bytes[size] += msg.size
self.msg_counts[size] += 1
self.size_messages += sum(m.size for m in kept)
self.size_extra += sum(entry.size for entry in mbox.extrafiles)
def get_summary(self):
return (
@@ -229,122 +263,32 @@ class Expiry:
f"in {time.time() - self.start:2.2f} seconds"
)
def prom_lines(self):
def write_prom(self, path):
end = time.time()
return [
"# HELP chatmail_expire_removed_mailboxes Mailboxes removed by the last run.",
"# TYPE chatmail_expire_removed_mailboxes gauge",
f"chatmail_expire_removed_mailboxes {self.del_mboxes}",
"# HELP chatmail_expire_removed_files Files removed by the last run.",
"# TYPE chatmail_expire_removed_files gauge",
f"chatmail_expire_removed_files {self.del_files}",
"# HELP chatmail_expire_duration_seconds Duration of the last run.",
"# TYPE chatmail_expire_duration_seconds gauge",
f"chatmail_expire_duration_seconds {end - self.start:.2f}",
"# HELP chatmail_expire_last_run_timestamp_seconds End of the last run.",
"# TYPE chatmail_expire_last_run_timestamp_seconds gauge",
f"chatmail_expire_last_run_timestamp_seconds {end:.0f}",
]
storage = dict(messages=self.size_messages, extra=self.size_extra)
storage["total"] = self.size_messages + self.size_extra
accounts = dict(all=self.num_all_logins, ci=self.num_ci_logins)
accounts["user"] = accounts["all"] - accounts["ci"]
tmp = f"{path}.tmp"
with open(tmp, "w") as f:
def gauge(name, values, label=None):
print(f"# HELP chatmail_{name} {PROM_HELP[name]}", file=f)
print(f"# TYPE chatmail_{name} gauge", file=f)
for key, value in values.items() if label else [(None, values)]:
labels = f'{{{label}="{key}"}}' if label else ""
print(f"chatmail_{name}{labels} {value}", file=f)
class Report:
def __init__(self, now):
self.size_extra = 0
self.size_messages = 0
self.now = now
self.num_ci_logins = self.num_all_logins = 0
self.login_buckets = {x: 0 for x in (1, 10, 30, 40, 80, 100, 150)}
KiB = 1024
MiB = 1024 * KiB
self.message_size_thresholds = (
0,
100 * KiB,
MiB // 2,
1 * MiB,
2 * MiB,
5 * MiB,
10 * MiB,
)
self.message_buckets = {x: 0 for x in self.message_size_thresholds}
self.message_count_buckets = {x: 0 for x in self.message_size_thresholds}
def process_mailbox_stat(self, mailbox):
# categorize login times
last_login = mailbox.last_login
if last_login:
self.num_all_logins += 1
if os.path.basename(mailbox.basedir)[:3] == "ci-":
self.num_ci_logins += 1
else:
for days in self.login_buckets:
if last_login >= self.now - days * 86400:
self.login_buckets[days] += 1
# categorize message sizes
for size in self.message_buckets:
for msg in mailbox.messages:
if msg.size >= size:
self.message_buckets[size] += msg.size
self.message_count_buckets[size] += 1
self.size_messages += sum(entry.size for entry in mailbox.messages)
self.size_extra += sum(entry.size for entry in mailbox.extrafiles)
def _write_atomic(self, filepath, content):
"""Atomically write content to filepath via tmp+rename."""
dirpath = os.path.dirname(os.path.abspath(filepath))
fd, tmppath = tempfile.mkstemp(dir=dirpath, suffix=".tmp")
try:
with os.fdopen(fd, "w") as f:
f.write(content)
os.chmod(tmppath, 0o644)
os.rename(tmppath, filepath)
except BaseException:
try:
os.unlink(tmppath)
except OSError:
pass
raise
def dump_textfile(self, filepath, extra_lines=()):
"""Dump metrics in Prometheus exposition format."""
lines = []
lines.append("# HELP chatmail_storage_bytes Mailbox storage in bytes.")
lines.append("# TYPE chatmail_storage_bytes gauge")
lines.append(f'chatmail_storage_bytes{{kind="messages"}} {self.size_messages}')
lines.append(f'chatmail_storage_bytes{{kind="extra"}} {self.size_extra}')
total = self.size_extra + self.size_messages
lines.append(f'chatmail_storage_bytes{{kind="total"}} {total}')
lines.append("# HELP chatmail_messages_bytes Sum of msg bytes >= threshold.")
lines.append("# TYPE chatmail_messages_bytes gauge")
for minsize, sumsize in self.message_buckets.items():
lines.append(f'chatmail_messages_bytes{{min_size="{minsize}"}} {sumsize}')
lines.append("# HELP chatmail_messages_count Number of msgs >= size threshold.")
lines.append("# TYPE chatmail_messages_count gauge")
for minsize, count in self.message_count_buckets.items():
lines.append(f'chatmail_messages_count{{min_size="{minsize}"}} {count}')
lines.append("# HELP chatmail_accounts Number of accounts.")
lines.append("# TYPE chatmail_accounts gauge")
user_logins = self.num_all_logins - self.num_ci_logins
lines.append(f'chatmail_accounts{{kind="all"}} {self.num_all_logins}')
lines.append(f'chatmail_accounts{{kind="ci"}} {self.num_ci_logins}')
lines.append(f'chatmail_accounts{{kind="user"}} {user_logins}')
lines.append(
"# HELP chatmail_accounts_active Non-CI accounts active within N days."
)
lines.append("# TYPE chatmail_accounts_active gauge")
for days, active in self.login_buckets.items():
lines.append(f'chatmail_accounts_active{{days="{days}"}} {active}')
lines.extend(extra_lines)
self._write_atomic(filepath, "\n".join(lines) + "\n")
gauge("storage_bytes", storage, "kind")
gauge("messages_bytes", self.msg_bytes, "min_size")
gauge("messages_count", self.msg_counts, "min_size")
gauge("accounts", accounts, "kind")
gauge("accounts_active", self.login_buckets, "days")
gauge("expire_removed_mailboxes", self.del_mboxes)
gauge("expire_removed_files", self.del_files)
gauge("expire_duration_seconds", f"{end - self.start:.2f}")
gauge("expire_last_run_timestamp_seconds", f"{end:.0f}")
os.replace(tmp, path)
def daily_expire_main(args=None):
@@ -395,13 +339,11 @@ def daily_expire_main(args=None):
maxnum = int(args.maxnum) if args.maxnum else None
exp = Expiry(config, dry=not args.remove, now=now, verbose=args.verbose)
rep = Report(now=now)
for mailbox in iter_mailboxes(str(config.mailboxes_dir), maxnum=maxnum):
if exp.process_mailbox_stat(mailbox):
rep.process_mailbox_stat(mailbox)
exp.process_mailbox_stat(mailbox)
print(exp.get_summary())
if args.prom:
rep.dump_textfile(args.prom, extra_lines=exp.prom_lines())
exp.write_prom(args.prom)
def quota_expire_main(args=None):
+1 -1
View File
@@ -288,7 +288,7 @@ class LegacyRemoveDeployer(Deployer):
enabled=False,
)
# chatmail-expire writes the storage statistics chatmail-fsreport collected
# chatmail-fsreport was folded into chatmail-expiry
timer = "chatmail-fsreport.timer"
if has_systemd() and host.get_fact(SystemdEnabled).get(timer):
systemd.service(