summaryrefslogtreecommitdiff
path: root/tools/auth_changetrack/nagios_check.py
blob: a9977f8be87de9248d7b945e71903caa0da01462 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python3

import sys
import psycopg2
from datetime import timedelta

# Up to 5 minutes delay is ok
WARNING_THRESHOLD = timedelta(minutes=5)
# More than 15 minutes something is definitely wrong
CRITICAL_THRESHOLD = timedelta(minutes=15)


def check_queue(curs):
    # Get the oldest entry that has not been completed, if any
    curs.execute("SELECT COALESCE(now()-changedat) FROM account_communityauthchangelog")
    rows = curs.fetchall()

    if len(rows) == 0:
        return "queue is empty"
        sys.exit(0)

    age = rows[0][0]

    if age < WARNING_THRESHOLD:
        return "queue age is %s" % age
    elif age < CRITICAL_THRESHOLD:
        print("WARNING, queue age is %s" % age)
        sys.exit(1)
    else:
        print("CRITICAL, queue age is %s" % age)
        sys.exit(2)


def check_mail(curs):
    curs.execute("SELECT count(*) FROM (SELECT 1 FROM all_user_email_addresses GROUP BY email HAVING count(*) > 1) x")
    num, = curs.fetchone()
    if num > 0:
        print("CRITICAL, {} email addresses have duplicate entries!".format(num))
        sys.exit(2)
    return ""


if __name__ == "__main__":
    if len(sys.argv) != 2:
        print("Usage: nagios_check.py <dsn>")
        sys.exit(1)

    conn = psycopg2.connect(sys.argv[1])
    curs = conn.cursor()

    status = []
    status.append(check_queue(curs))
    status.append(check_mail(curs))

    print("OK: {}".format('; '.join([s for s in status if s])))