mirror of
https://github.com/simple-login/app.git
synced 2025-09-24 15:35:49 +08:00
add monitoring cronjob that monitors how many emails in Postfix queues
This commit is contained in:
parent
c0f263ee70
commit
4bbb07c3ce
2 changed files with 43 additions and 1 deletions
38
cron.py
38
cron.py
|
@ -1,4 +1,5 @@
|
||||||
import argparse
|
import argparse
|
||||||
|
import os
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from time import sleep
|
from time import sleep
|
||||||
|
|
||||||
|
@ -12,6 +13,7 @@ from app.config import (
|
||||||
ADMIN_EMAIL,
|
ADMIN_EMAIL,
|
||||||
MACAPP_APPLE_API_SECRET,
|
MACAPP_APPLE_API_SECRET,
|
||||||
APPLE_API_SECRET,
|
APPLE_API_SECRET,
|
||||||
|
HOST,
|
||||||
)
|
)
|
||||||
from app.email_utils import (
|
from app.email_utils import (
|
||||||
send_email,
|
send_email,
|
||||||
|
@ -26,13 +28,13 @@ from app.models import (
|
||||||
User,
|
User,
|
||||||
Alias,
|
Alias,
|
||||||
EmailLog,
|
EmailLog,
|
||||||
Contact,
|
|
||||||
CustomDomain,
|
CustomDomain,
|
||||||
Client,
|
Client,
|
||||||
ManualSubscription,
|
ManualSubscription,
|
||||||
RefusedEmail,
|
RefusedEmail,
|
||||||
AppleSubscription,
|
AppleSubscription,
|
||||||
Mailbox,
|
Mailbox,
|
||||||
|
Monitoring,
|
||||||
)
|
)
|
||||||
from server import create_app
|
from server import create_app
|
||||||
|
|
||||||
|
@ -313,6 +315,36 @@ def sanity_check():
|
||||||
LOG.d("Finish sanity check")
|
LOG.d("Finish sanity check")
|
||||||
|
|
||||||
|
|
||||||
|
def monitoring():
|
||||||
|
"""Look at different metrics and alert appropriately"""
|
||||||
|
incoming_queue = nb_files("/var/spool/postfix/incoming")
|
||||||
|
active_queue = nb_files("/var/spool/postfix/active")
|
||||||
|
deferred_queue = nb_files("/var/spool/postfix/deferred")
|
||||||
|
LOG.d("postfix queue sizes %s %s %s", incoming_queue, active_queue, deferred_queue)
|
||||||
|
|
||||||
|
Monitoring.create(
|
||||||
|
host=HOST,
|
||||||
|
incoming_queue=incoming_queue,
|
||||||
|
active_queue=active_queue,
|
||||||
|
deferred_queue=deferred_queue,
|
||||||
|
)
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
|
# alert when too many emails in incoming + active queue
|
||||||
|
# 20 is an arbitrary number here
|
||||||
|
if incoming_queue + active_queue > 20:
|
||||||
|
LOG.exception(
|
||||||
|
"Too many emails in incoming & active queue %s %s",
|
||||||
|
incoming_queue,
|
||||||
|
active_queue,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def nb_files(directory) -> int:
|
||||||
|
"""return the number of files in directory and its sub-directories"""
|
||||||
|
return sum(len(files) for _, _, files in os.walk(directory))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
LOG.d("Start running cronjob")
|
LOG.d("Start running cronjob")
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
|
@ -329,6 +361,7 @@ if __name__ == "__main__":
|
||||||
"delete_refused_emails",
|
"delete_refused_emails",
|
||||||
"poll_apple_subscription",
|
"poll_apple_subscription",
|
||||||
"sanity_check",
|
"sanity_check",
|
||||||
|
"monitoring",
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
@ -357,3 +390,6 @@ if __name__ == "__main__":
|
||||||
elif args.job == "sanity_check":
|
elif args.job == "sanity_check":
|
||||||
LOG.d("Check data consistency")
|
LOG.d("Check data consistency")
|
||||||
sanity_check()
|
sanity_check()
|
||||||
|
elif args.job == "monitoring":
|
||||||
|
LOG.d("Collect and monitor system heath")
|
||||||
|
monitoring()
|
||||||
|
|
|
@ -40,3 +40,9 @@ jobs:
|
||||||
shell: /bin/bash
|
shell: /bin/bash
|
||||||
schedule: "0 2 * * *"
|
schedule: "0 2 * * *"
|
||||||
captureStderr: true
|
captureStderr: true
|
||||||
|
|
||||||
|
- name: SimpleLogin System Monitoring
|
||||||
|
command: python /code/cron.py -j monitor
|
||||||
|
shell: /bin/bash
|
||||||
|
schedule: "*/5 * * * *"
|
||||||
|
captureStderr: true
|
||||||
|
|
Loading…
Add table
Reference in a new issue