2020-08-16 01:55:56 +08:00
|
|
|
import os
|
|
|
|
from time import sleep
|
|
|
|
|
2022-01-13 17:39:22 +08:00
|
|
|
import newrelic.agent
|
|
|
|
|
2021-10-12 20:36:47 +08:00
|
|
|
from app.db import Session
|
2020-08-16 01:55:56 +08:00
|
|
|
from app.log import LOG
|
|
|
|
|
2020-08-16 16:27:35 +08:00
|
|
|
# the number of consecutive fails
|
2020-10-03 16:34:07 +08:00
|
|
|
# if more than _max_nb_fails, alert
|
2020-08-16 16:27:35 +08:00
|
|
|
# reset whenever the system comes back to normal
|
2020-09-09 00:52:36 +08:00
|
|
|
# a system is considered fail if incoming_queue + active_queue > 50
|
2020-08-16 16:27:35 +08:00
|
|
|
_nb_failed = 0
|
|
|
|
|
2020-10-03 16:34:07 +08:00
|
|
|
_max_nb_fails = 10
|
|
|
|
|
2020-10-12 00:11:49 +08:00
|
|
|
# the maximum number of emails in incoming & active queue
|
2020-10-12 00:13:17 +08:00
|
|
|
_max_incoming = 50
|
2020-10-12 00:11:49 +08:00
|
|
|
|
2020-08-16 01:55:56 +08:00
|
|
|
|
2022-01-13 17:39:22 +08:00
|
|
|
@newrelic.agent.background_task()
|
|
|
|
def log_postfix_metrics():
|
2020-08-16 01:55:56 +08:00
|
|
|
"""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)
|
|
|
|
|
2022-01-13 17:39:22 +08:00
|
|
|
newrelic.agent.record_custom_metric("Custom/postfix_incoming_queue", incoming_queue)
|
|
|
|
newrelic.agent.record_custom_metric("Custom/postfix_active_queue", active_queue)
|
|
|
|
newrelic.agent.record_custom_metric("Custom/postfix_deferred_queue", deferred_queue)
|
2020-08-16 01:55:56 +08: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))
|
|
|
|
|
|
|
|
|
2022-01-13 17:39:22 +08:00
|
|
|
@newrelic.agent.background_task()
|
|
|
|
def log_nb_db_connection():
|
|
|
|
# get the number of connections to the DB
|
|
|
|
r = Session.execute("select count(*) from pg_stat_activity;")
|
|
|
|
nb_connection = list(r)[0][0]
|
|
|
|
|
|
|
|
LOG.d("number of db connections %s", nb_connection)
|
|
|
|
newrelic.agent.record_custom_metric("Custom/nb_db_connections", nb_connection)
|
|
|
|
|
|
|
|
|
2020-08-16 01:55:56 +08:00
|
|
|
if __name__ == "__main__":
|
|
|
|
while True:
|
2022-01-13 17:39:22 +08:00
|
|
|
log_postfix_metrics()
|
|
|
|
log_nb_db_connection()
|
2020-08-16 01:55:56 +08:00
|
|
|
|
2020-10-03 16:34:07 +08:00
|
|
|
# 1 min
|
|
|
|
sleep(60)
|