aboutsummaryrefslogtreecommitdiff
path: root/jobs/TCPServerChecker.py
blob: fb6e9c137211a162b2af0900731e66e29e0a8ebd (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
#!/usr/bin/env python3

from __future__ import absolute_import
from builtins import str
import os
import socket
import logging

from . import JobBase
from . import JobSpawner

class TCPServerChecker(JobSpawner.JobSpawner):
    servers = [ 
                #("example.com", 53, "example.com:tcpdns", JobBase.JobFrequency.MINUTE, JobBase.JobFailureNotificationFrequency.EVERYTIME, JobBase.JobFailureCountMinimumBeforeNotification.ONE)
              ]

    class ServerChecker(JobBase.JobBase):
        def __init__(self, config, ip, port, friendlyName, frequency, failureNotificationFrequency, failuresBeforeNotification):
            self.config = config
            self.ip = ip
            self.port = port
            self.friendlyName = friendlyName + "(" + self.ip + ":" + str(self.port) + ")"
            self.frequency = frequency
            self.failureNotificationFrequency = failureNotificationFrequency
            self.failuresBeforeNotification = failuresBeforeNotification
            super(TCPServerChecker.ServerChecker, self).__init__(config, ip, port)

        def getName(self):
            return str(self.__class__) + " for " + self.friendlyName
        def executeEvery(self):
            return self.frequency
        def notifyOnFailureEvery(self):
            return self.failureNotificationFrequency
        def numberFailuresBeforeNotification(self):
            return self.failuresBeforeNotification
        def execute(self):
            try:
                s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                s.settimeout(10)
                s.connect((self.ip, self.port))
                s.close()
                return True
            except:
                self.failuremsg = "Could not hit server " + self.friendlyName
                logging.warn(self.failuremsg)
                return False
        def onFailure(self):
            return self.sendEmail(self.failuremsg, "")
        def onStateChangeSuccess(self):
            return self.sendEmail("Successfully hit " + self.friendlyName, "")

    def get_sub_jobs(self, config):
        for s in self.servers:
            yield self.ServerChecker(config, s[0], s[1], s[2], s[3], s[4], s[5])