-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathservices.py
183 lines (140 loc) · 4.12 KB
/
services.py
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import requests
import smtplib
import subprocess
import socket
class Service(object):
up = False
error = None
def __str__(self):
raise NotImplementedError()
def online(self):
raise NotImplementedError()
def status(self):
if self.up:
return str(self) + " is up"
else:
if self.error:
return str(self) + " is down: " + self.error
else:
return str(self) + " is down."
class Http(Service):
def __init__(self, url):
self.url = url
self.error = None
def __str__(self):
return "HTTP check for " + self.url
def online(self):
try:
resp = requests.get(self.url)
if resp.status_code == 200:
self.up = True
else:
self.up = False
self.error = "Status code is: " + str(resp.status_code)
except Exception as e:
self.up = False
self.error = str(e)
return self.up
class Port(Service):
def __init__(self, host, port):
self.host = host
self.port = port
def __str__(self):
return "PORT check of " + self.host + " " + str(self.port)
def online(self):
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex((self.host, self.port))
if result == 0:
self.up = True
else:
raise Exception
except:
self.up = False
return self.up
class Ping(Service):
def __init__(self, host):
self.host = host
def __str__(self):
return "PING " + self.host
def online(self):
try:
subprocess.check_call(["ping", "-c", "2", self.host], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
self.up = True
except:
self.up = False
return self.up
class Ping6(Service):
def __init__(self, host):
self.host = host
def __str__(self):
return "PING6 " + self.host
def online(self):
try:
subprocess.check_call(["ping6", "-c", "2", self.host], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
self.up = True
except:
self.up = False
return self.up
class Smtp(Service):
"""
SMTP can test a simple connection, or a connection with user if you specify one.
"""
def __init__(self, server, port=25, username=None, password=None, ssl=False, starttls=False):
self.server = server
self.port = port
self.username = username
self.password = password
self.ssl = ssl
self.starttls = starttls
def __str__(self):
return "SMTP check for " + self.server + ":" + str(self.port)
def online(self):
try:
if self.ssl:
srv = smtplib.SMTP_SSL(self.server, self.port)
else:
srv = smtplib.SMTP(self.server, self.port)
srv.ehlo()
if self.starttls:
srv.starttls()
srv.ehlo()
if self.username:
srv.login(self.username, self.password)
srv.quit()
self.up = True
except Exception as e:
self.error = str(e)
self.up = False
return self.up
class Fake(Service):
"""
Useful for debugging
fails it fail == True
"""
def __init__(self, fail, name):
self.name = name
self.fail = fail
def __str__(self):
return "FAKE " + self.name
def online(self):
self.up = not self.fail
return self.up
class FakeThree(Service):
"""
Fails every three iterations, starting with the first one.
Useful to test state.
"""
def __init__(self, name):
self.name = name
self.iteration = 0
def __str__(self):
return "FAKETHREE " + self.name
def online(self):
if self.iteration % 3 == 0:
self.iteration += 1
self.up = False
else:
self.iteration += 1
self.up = True
return self.up