Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions chatmaild/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ chatmail-metadata = "chatmaild.metadata:main"
chatmail-expire = "chatmaild.expire:daily_expire_main"
chatmail-quota-expire = "chatmaild.expire:quota_expire_main"
chatmail-fsreport = "chatmaild.fsreport:main"
chatmail-deferred = "chatmaild.deferred:main"
lastlogin = "chatmaild.lastlogin:main"
turnserver = "chatmaild.turnserver:main"

Expand Down
39 changes: 39 additions & 0 deletions chatmaild/src/chatmaild/deferred.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""
Analyze deferred mails and print most common failing destinations.

Example:

python -m chatmaild.deferred
"""

import json
import subprocess
from collections import Counter, defaultdict


def main():
p = subprocess.Popen(["postqueue", "-j"], text=True, stdout=subprocess.PIPE)
domain_reasons = defaultdict(Counter)
domain_total = Counter()

for line in p.stdout:
item = json.loads(line)
if item["queue_name"] != "deferred":
continue

for recipient in item["recipients"]:
_, domain = recipient["address"].rsplit("@", 1)
reason = recipient["delay_reason"].removeprefix(
"host 127.0.0.1[127.0.0.1] said: "
)
domain_total[domain] += 1
domain_reasons[domain][reason] += 1

for domain, total in reversed(domain_total.most_common()):
print(f"{domain} ({total} recipients)")
for reason, count in domain_reasons[domain].most_common():
print(f" {count}: {reason}")


if __name__ == "__main__":
main()
Loading