-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
80 lines (67 loc) · 2.88 KB
/
Copy pathmain.py
File metadata and controls
80 lines (67 loc) · 2.88 KB
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
import log.log as log
from service.servicestatus import *
from config.config import configuration, EMAIL_SENT_HISTORY, CHECK_RESULTS
import json
import datetime
from emailworker.email import *
import time
import threading
from server.server import *
import os
_root_logger = log.setup_custom_logger("root")
CHECK_RESULTS_FILE = configuration.get_property("jsonfile")
SERVICES = configuration.get_property("services")
monitoring_interval = configuration.get_property("monitoring_interval")
def load_existing_checks():
global CHECK_RESULTS
if os.path.exists(CHECK_RESULTS_FILE):
with open(CHECK_RESULTS_FILE, 'r') as file:
CHECK_RESULTS = json.load(file)
# print("Loaded existing checks:", CHECK_RESULTS)
else:
print("No existing checks file found.")
def load_email_sent_history():
global EMAIL_SENT_HISTORY
# Load the last email sent times from the checks if available
for check in CHECK_RESULTS.get('checks', []):
for service in check.get('services', []):
servicename = service.get('Servicename')
last_email_sent = service.get('lastemailsent_time')
if last_email_sent:
EMAIL_SENT_HISTORY[servicename] = datetime.datetime.strptime(last_email_sent, "%Y-%m-%d %H:%M:%S")
def check_services():
now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
check_result = {"check_id": f"Check {len(CHECK_RESULTS['checks']) + 1}", "services": []}
for service_name in SERVICES:
status = get_service_status(service_name)
email_sent = send_email(service_name, status)
if email_sent:
last_email_sent_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
else:
last_email_sent_time = EMAIL_SENT_HISTORY.get(service_name, "")
if last_email_sent_time:
last_email_sent_time = last_email_sent_time.strftime("%Y-%m-%d %H:%M:%S")
service_result = {
"Servicename": service_name,
"Status": status,
"lastchecked_time": now,
"lastemailsent_time": last_email_sent_time
}
check_result["services"].append(service_result)
CHECK_RESULTS["checks"].append(check_result)
with open(CHECK_RESULTS_FILE, "w") as file:
json.dump(CHECK_RESULTS, file, indent=4)
next_check_time = datetime.datetime.now() + datetime.timedelta(seconds=30)
_root_logger.info(f'Service checks done. Next check in {monitoring_interval} seconds, at {next_check_time.strftime("%Y-%m-%d %H:%M:%S")}')
def main():
_root_logger.info(f"Process Execution Started.\n")
while True:
load_existing_checks()
load_email_sent_history()
check_services()
time.sleep(monitoring_interval)
_root_logger.info(f"Process Execution Ended.")
if __name__ == "__main__":
thread = threading.Thread(target=main)
thread.start()
start_server()