-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheckServer.py
128 lines (106 loc) · 4.26 KB
/
CheckServer.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
import socket
import ssl
from datetime import datetime
import pickle
import subprocess
import platform
from gmail import email_alert
class Server():
def __init__(self, name, port, connection, priority):
self.name = name
self.port = port
self.connection = connection.lower()
self.priority = priority.lower()
self.history = []
self.alert = False
def check_connection(self):
msg = ""
success = False
now = datetime.now()
try:
if self.connection == "plain":
socket.create_connection((self.name, self.port), timeout=10)
msg = f"{self.name} is up. On port {self.port} with {self.connection}"
success = True
self.alert = False
elif self.connection == "ssl":
ssl.wrap_socket(socket.create_connection((self.name, self.port), timeout=10))
msg = f"{self.name} is up. On port {self.port} with {self.connection}"
success = True
self.alert = False
else:
if self.ping():
msg = f"{self.name} is up. On port {self.port} with {self.connection}"
success = True
self.alert = False
except socket.timeout:
msg = f"server: {self.name} timeout. On port {self.port}"
except (ConnectionRefusedError, ConnectionResetError) as e:
msg = f"server: {self.name} {e}"
except Exception as e:
msg = f"No Clue??: {e}"
def check_connection(self):
msg = ""
success = False
now = datetime.now()
try:
if self.connection == "plain":
socket.create_connection((self.name, self.port), timeout = 10)
msg = f"{self.name} is up on port on {self.port} with {self.connection}"
success = True
self.alert = False
elif self.connection == "ssl":
ssl.wrap_socket(socket.create_connection((self.name, self.port), timeout = 10))
msg = f"{self.name} is up on port on {self.port} with {self.connection}"
success = True
self.alert = False
else:
if self.ping() :
msg = f"{self.name} is up on port on {self.port} with {self.connection}"
success = True
self.alert = False
except socket.timeout:
msg = f"{self.name} timeout. On port {self.port}"
except (ConnectionRefusedError, ConnectionResetError) as e :
msg = f"server : {self.name} {e}"
except Exception as e :
msg = f"No Clue?? : {e}"
if success == False and self.alert == False:
# Send Alert
self.alert = True
# Uncomment this once you have setup gmail alerts!
# Check out video if you need help!
# https://youtu.be/B1IsCbXp0uE
email_alert(self.name,f"{msg}\n{now}","[email protected]")
self.create_history(msg,success,now)
def ping(self):
try :
output = subprocess.check_output("ping -{} 1 {}".format('n' if platform.system().lower() == 'windows'
else 'c', self.name), shell=True, universal_newlines=True)
if 'unreachable' in output:
return False
else :
return True
except Exception:
return False
def create_history(self, msg, success, now):
history_max = 100
self.history.append((msg, success, now))
while(len(self.history) > history_max):
self.history.pop(0)
if __name__ == '__main__':
try:
servers = pickle.load(open("servers.pickle", "rb"))
except:
servers = [
Server("reddit.com", 80, "plain", "high"),
Server("msn.com", 80, "plain", "high"),
Server("smtp.gmail.com", 465, "ssl", "high"),
Server("192.168.1.164", 80, "ping", "high"),
Server("yahoo.com", 80, "plain", "high")
]
for server in servers:
server.check_connection()
print(len(server.history))
# print(server.history[-1])
pickle.dump(servers, open("servers.pickle", "wb"))