2020-08-15 19:55:56 +02:00
|
|
|
import os
|
|
|
|
from time import sleep
|
|
|
|
|
|
|
|
from app.config import HOST
|
|
|
|
from app.extensions import db
|
|
|
|
from app.log import LOG
|
|
|
|
from app.models import Monitoring
|
|
|
|
from server import create_app
|
|
|
|
|
2020-08-16 10:27:35 +02:00
|
|
|
# the number of consecutive fails
|
2020-10-03 10:34:07 +02:00
|
|
|
# if more than _max_nb_fails, alert
|
2020-08-16 10:27:35 +02:00
|
|
|
# reset whenever the system comes back to normal
|
2020-09-08 18:52:36 +02:00
|
|
|
# a system is considered fail if incoming_queue + active_queue > 50
|
2020-08-16 10:27:35 +02:00
|
|
|
_nb_failed = 0
|
|
|
|
|
2020-10-03 10:34:07 +02:00
|
|
|
_max_nb_fails = 10
|
|
|
|
|
2020-10-11 18:11:49 +02:00
|
|
|
# the maximum number of emails in incoming & active queue
|
2020-10-11 18:13:17 +02:00
|
|
|
_max_incoming = 50
|
2020-10-11 18:11:49 +02:00
|
|
|
|
2020-08-15 19:55:56 +02:00
|
|
|
|
|
|
|
def get_stats():
|
|
|
|
"""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()
|
|
|
|
|
2020-08-16 10:27:35 +02:00
|
|
|
global _nb_failed
|
2020-08-15 19:55:56 +02:00
|
|
|
# alert when too many emails in incoming + active queue
|
2020-10-11 18:13:17 +02:00
|
|
|
if incoming_queue > _max_incoming:
|
2020-08-16 10:27:35 +02:00
|
|
|
_nb_failed += 1
|
|
|
|
|
2020-10-03 10:34:07 +02:00
|
|
|
if _nb_failed > _max_nb_fails:
|
2020-08-16 10:27:35 +02:00
|
|
|
# reset
|
|
|
|
_nb_failed = 0
|
|
|
|
|
2021-09-08 11:29:55 +02:00
|
|
|
LOG.e(
|
2020-08-16 10:27:35 +02:00
|
|
|
"Too many emails in incoming & active queue %s %s",
|
|
|
|
incoming_queue,
|
|
|
|
active_queue,
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
_nb_failed = 0
|
2020-08-15 19:55:56 +02:00
|
|
|
|
|
|
|
|
|
|
|
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__":
|
|
|
|
while True:
|
|
|
|
app = create_app()
|
|
|
|
with app.app_context():
|
|
|
|
get_stats()
|
|
|
|
|
2020-10-03 10:34:07 +02:00
|
|
|
# 1 min
|
|
|
|
sleep(60)
|