aboutsummaryrefslogtreecommitdiff
path: root/jobs/PeerChecker.py
blob: 52969b7b2da5ca9f3bfe10a211adb73ced504ac4 (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python3

from __future__ import absolute_import
from builtins import str
import os
import base64
import logging
import datetime 

import imaplib
import requests

from . import JobBase
from . import JobSpawner

class PeerChecker(JobSpawner.JobSpawner):
    class IndividualPeerChecker(JobBase.JobBase):
        def __init__(self, config, checkurl, notificationAddress):
            self.checkurl = checkurl
            self.notificationAddress = notificationAddress
            super(PeerChecker.IndividualPeerChecker, self).__init__(config, checkurl, notificationAddress)

        def getName(self):
            return str(self.__class__) + " for " + self.checkurl
        def executeEvery(self):
            return JobBase.JobFrequency.HOUR
        def notifyOnFailureEvery(self):
            return JobBase.JobFailureNotificationFrequency.EVERYTIME
        def numberFailuresBeforeNotification(self):
            return JobBase.JobFailureCountMinimumBeforeNotification.ONE
        def execute(self):
            peerOK = False

            self.subject = ""
            self.body = ""

            try:
                response = requests.get(self.checkurl, timeout=5)
                if response.status_code != 200:
                    peerOK = False
                    self.subject = self.checkurl + " returned a non-standard status code."
                    self.body = str(response.status_code) + "\n" + response.content
                else:
                    content = response.content.decode("utf-8")
                    if "True" in content:
                        peerOK = True
                    elif "MailProblem" in content:
                        peerOK = False
                        self.subject = self.checkurl + " reports it cannot send email."
                        self.body = str(response.status_code) + "\n" + content
                    elif "JobProblem" in content:
                        peerOK = False
                        self.subject = self.checkurl + " reports its jobs are not running."
                        self.body = str(response.status_code) + "\n" + content
                    else:
                        peerOK = False
                        self.subject = self.checkurl + " had an unexpected response."
                        self.body = str(response.status_code) + "\n" + content
            except Exception as e:
                peerOK = False
                self.subject = self.checkurl + " is not responding."
                self.body = repr(e) + "\n"
                self.body += logging.traceback.format_exc()
            return peerOK

        def onFailure(self):
            return self.sendEmail(self.subject, self.body, self.notificationAddress)
        def onStateChangeSuccess(self):
            return self.sendEmail("Successfully hit " + self.checkurl, "", self.notificationAddress)

    def get_sub_jobs(self, config):
        peers = config.items('peers')
        for p in peers:
            (address, email) = p[1].split(',')
            yield self.IndividualPeerChecker(config, address, email)