check_service 3.87 KB
Newer Older
Hiro's avatar
Hiro committed
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
56
57
58
59
60
61
62
63
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# This file is part of GetTor, a Tor Browser distribution system.
#
# :authors: hiro <hiro@torproject.org>
#           see also AUTHORS file
#
# :license: This is Free Software. See LICENSE for license information.

import sys
import smtplib
import time
import imaplib
import email
import time

# Standard Nagios return codes
OK, WARNING, CRITICAL, UNKNOWN = range(4)

ORG_EMAIL   = "@gmail.com"
FROM_EMAIL  = "test.gettor.browser" + ORG_EMAIL
SMTP_SERVER = "imap.gmail.com"
SMTP_PORT   = 993

MESSAGE_FROM = "gettor@torproject.org"
MESSAGE_SUBJECT = "[GetTor] Links for your request"
MESSAGE_BODY = "https://gitlab.com/thetorproject/"

STATUS_FILE = "/srv/gettor.torproject.org/check/status"

# -------------------------------------------------
#
# Utility to read email from Gmail Using Python
#
# ------------------------------------------------

def test_email_from_gmail(password):
    try:
        mail = imaplib.IMAP4_SSL(SMTP_SERVER)
        mail.login(FROM_EMAIL, password)
        mail.select('INBOX')

        type, data = mail.search(None, 'ALL')
        mail_ids = data[0]

        id_list = mail_ids.split()
        first_email_id = int(str(id_list[0], 'utf-8'))
        latest_email_id = int(str(id_list[-1], 'utf-8'))

        for i in range(int(latest_email_id), int(first_email_id), -1):
            typ, data = mail.fetch(str(i), '(RFC822)')

            for response_part in data:
                if isinstance(response_part, tuple):
                    m = str(response_part[1], 'utf-8')
                    msg = email.message_from_string(m)
                    email_subject = "{}".format(msg['subject'])
                    email_from = "{}".format(msg['from'])
                    email_body = "{}".format(msg.as_string())

                    if (MESSAGE_FROM == email_from) and (MESSAGE_SUBJECT == email_subject) and (MESSAGE_BODY in email_body):
                        mail.store(str(i), '+FLAGS', '\\Deleted')
Hiro's avatar
Hiro committed
64
                        mail.close()
Hiro's avatar
Hiro committed
65
                        return OK, "GetTor is good and sending emails with working links"
Hiro's avatar
Hiro committed
66
67
68
69
70
                    else:
                        mail.store(str(i), '+FLAGS', '\\Deleted')



Hiro's avatar
Hiro committed
71
        mail.close()
Hiro's avatar
Hiro committed
72
73
74
75
76
77
78
        return WARNING, "No emails from gettor found"

    except Exception as e:
        return CRITICAL, str(e)

def send_email_from_gmail(password):
    sent_from = FROM_EMAIL
Hiro's avatar
Hiro committed
79
    sent_to = ["{}".format(MESSAGE_FROM)]
Hiro's avatar
Hiro committed
80
81
82
    subject = 'windows en'
    body = 'windows en'

Hiro's avatar
Hiro committed
83
    email_text = """From: %s\nTo: %s\nSubject: %s\n\n%s""" % (sent_from, ", ".join(sent_to), subject, body)
Hiro's avatar
Hiro committed
84
85

    try:
Hiro's avatar
Hiro committed
86
87
88
89
90
        mail = smtplib.SMTP_SSL('smtp.gmail.com', 465)
        mail.ehlo()
        mail.login(sent_from, password)
        mail.sendmail(sent_from, sent_to, email_text)
        mail.close()
Hiro's avatar
Hiro committed
91
        return OK, "Test email sent"
Hiro's avatar
Hiro committed
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
    except Exception as e:
        return UNKNOWN, str(e)

if __name__ == "__main__":
    status, message = None, None

    if len(sys.argv) == 2:
        password = sys.argv[1]
    else:
        password = "yourPassword"

    status_file = open(STATUS_FILE,'w')

    try:
        status, message = send_email_from_gmail(password)
    except Exception as e:
Hiro's avatar
Hiro committed
108
        status = UNKNOWN
Hiro's avatar
Hiro committed
109
        message = repr(e)
Hiro's avatar
Hiro committed
110
        status_file.write("UNKNOWN: %s" % message)
Hiro's avatar
Hiro committed
111

Hiro's avatar
Hiro committed
112
    time.sleep(600)
Hiro's avatar
Hiro committed
113
114
115
116
117
118
119
120
121
122

    try:
        status, message = test_email_from_gmail(password)
    except KeyboardInterrupt:
        status, message = CRITICAL, "Caught Control-C..."
    except Exception as e:
        status = CRITICAL
        message = repr(e)
    finally:
        if status == OK:
Hiro's avatar
Hiro committed
123
            status_file.write("OK\n0: %s" % message)
Hiro's avatar
Hiro committed
124
        elif status == WARNING:
Hiro's avatar
Hiro committed
125
            status_file.write("WARNING\n1: %s" % message)
Hiro's avatar
Hiro committed
126
        elif status == CRITICAL:
Hiro's avatar
Hiro committed
127
            status_file.write("CRITICAL\n2: %s" % message)
Hiro's avatar
Hiro committed
128
        else:
Hiro's avatar
Hiro committed
129
            status_file.write("UNKNOWN\n3: %s" % message)
Hiro's avatar
Hiro committed
130
131
132
            status = UNKNOWN

        sys.exit(status)