-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitorui.py
211 lines (183 loc) · 9.04 KB
/
monitorui.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
"""
monitorui.py is the main script which runs the MONITORUI
"""
from sys import platform
from time import sleep, time
from random import randint
from pathlib import Path
from datetime import datetime
from os import chdir, path, mkdir, remove
import concurrent.futures
from colorama import Fore, Style, init
from modules.monitored_site import MonitoredSite
from modules.functions import common_func as cf
init()
def go_monitor(site, system):
try:
# set site config path for current minotred site
siteconfig = cf.read_json(sitesfolder + "//" + system + "//" + site + '//configsite.json')
# set site folder for current minotred site
sitefolder = sitesfolder + "//" + system + "//" + site
# set log daily feed folder
logdailyfeedfolder = './/logs/log_daily_feed'
if 'monitoringstart' not in siteconfig:
siteconfig['monitoringstart'] = '000000'
if 'monitoringend' not in siteconfig:
siteconfig['monitoringend'] = '235959'
if 'monitoringdays' not in siteconfig:
siteconfig['monitoringdays'] = [0, 1, 2, 3, 4, 5, 6]
if 'checkhostping' not in siteconfig:
siteconfig['checkhostping'] = False
if 'checksiteresponsecode' not in siteconfig:
siteconfig['checksiteresponsecode'] = False
if 'checksitecontent' not in siteconfig:
siteconfig['checksitecontent'] = False
if 'checkcertificateexpiration' not in siteconfig:
siteconfig['checkcertificateexpiration'] = False
if 'checkwmiprocesses' not in siteconfig:
siteconfig['checkwmiprocesses'] = False
# WMI need support of Win32, if monitoring runs on non Win32 compatible system,
# WMI is set to disabled
if platform != "win32":
siteconfig['checkwmiprocesses'] = False
if 'checksqllitescript' not in siteconfig:
siteconfig['checksqllitescript'] = False
if 'checksqloraclescript' not in siteconfig:
siteconfig['checksqloraclescript'] = False
# Check if monitored site is alowed for current dayd
if datetime.today().weekday() in siteconfig['monitoringdays']:
if datetime.now().strftime("%H%M%S") >= siteconfig['monitoringstart']\
and datetime.now().strftime("%H%M%S") <= siteconfig['monitoringend']:
# Crete instance of class Monitored Site
workingsite = MonitoredSite(config, siteconfig, system, site, sitefolder, logdailyfeedfolder)
if siteconfig['checkhostping']:
workingsite.site_check_ping()
if siteconfig['checkhostport']:
workingsite.site_check_port()
if siteconfig['checksiteresponsecode']:
workingsite.site_check_response_code()
if siteconfig['checkcertificateexpiration']:
workingsite.site_certificate_expiration_check()
if siteconfig['checksitecontent']:
workingsite.site_check_site_content()
if siteconfig['checkwmiprocesses']:
workingsite.site_check_wmni_process()
if siteconfig['checksqllitescript']:
workingsite.site_check_sqlite_script()
if siteconfig['checksqloraclescript']:
workingsite.site_check_oracle_script()
workingsite.copy_log_for_agregation()
# Remove logs in Monitored Site - Log Retention
workingsite.removesite_logs()
# Remove instance of MonitoredSite Class
try:
del workingsite
except Exception as exep:
message = f'{site} Failed removing object: \r\n {exep}'
print(Fore.RED + message + Style.RESET_ALL)
cf.write_file_append(logpath, f'{message}')
else:
print(f'{datetime.now()}|MONITORING_EXECUTION|INFO|{site} skipped, out of working hours- \r\n')
else:
print(f'{datetime.now()}|MONITORING_EXECUTION|INFO|{site} skipped, not sheduled for today \r\n')
except Exception as exep:
message = f'{datetime.now()}|MONITORING_EXECUTION|ERROR|Skipped on Exception, issues found: {exep} \r\n'
print(Fore.RED + message + Style.RESET_ALL)
cf.write_file_append(logpath, f'{message}')
# Change dir to actual script location (useful on Windows), global settings
chdir(path.dirname(path.abspath(__file__)))
# get actual time and write script start date in to script log
scriptstarttime = datetime.now()
if not path.exists('.//logs'):
mkdir('.//logs')
logpath = f'.//logs//monitoring_{datetime.now().strftime("%d%m%Y")}.log'
if not path.exists('.//logs/log_daily_feed'):
mkdir('.//logs/log_daily_feed')
LOG_DIR_PATH = './/logs//'
message = f'{datetime.now()}|MONITORING_EXECUTION|INFO|Monitoring Start\r\n'
print(message)
cf.write_file_append(logpath, message)
# Read config file and set password in to config list
config = cf.read_json('.//config//config.json')
# sitesfolder = config['sitesfolder']
if 'sitesfolder' in config:
sitesfolder = config['sitesfolder']
else:
sitesfolder = './/sites'
# Look somewhere else, there is nothing to do
if 'smtppassfilelocation' in config:
smtppassfilelocation = config['smtppassfilelocation']
if path.isfile(config['smtppassfilelocation']):
config['smtppass'] = str(cf.read_file(smtppassfilelocation)[0])
if 'workinloop' not in config:
config['workinloop'] = False
if 'loopintervallmin' not in config:
config['loopintervallmin'] = 300
if 'loopintervallmax' not in config:
config['loopintervallmax'] = 500
if 'logsretention' not in config:
config['logsretention'] = None
if 'parallel_checks' not in config:
config['parallel_checks'] = False
if 'max_workers' not in config:
config['max_workers'] = 5
SCRIPT_LOOP = True
# TODO: In future make paralel for not only sites, but systems, paralel of paralel
while SCRIPT_LOOP:
listofsystems = cf.list_directories(sitesfolder + "//")
# print(listofsystems)
for system in listofsystems:
# Read sites in to list
listofsites = cf.list_directories(sitesfolder + "//" + system + "//")
if config['parallel_checks']:
if __name__ == '__main__':
# with multiprocessing.Pool() as pool:
# pool.map(go, listofsites)
with concurrent.futures.ThreadPoolExecutor(config['max_workers']) as executor:
futures = [executor.submit(go_monitor, site, system) for site in listofsites]
concurrent.futures.wait(futures)
else:
# for cycle for each site from list, invoke class end perfom monitoring actions
for site in listofsites:
go_monitor(site, system)
# Clean up log files
# Log retetion for MonitoringUI
if config['logsretention'] is not None:
try:
for item in Path(LOG_DIR_PATH).glob('*.log'):
if item.is_file():
try:
if (Path.stat(item).st_mtime) < time() - config['logsretention'] * 86400:
remove(item)
except Exception as remove_e:
message = f'{datetime.now()}|MONITORING_EXECUTION|ERROR|Failed to remove log file: {remove_e}\r\n'
print(Fore.RED + message + Style.RESET_ALL)
cf.write_file_append(logpath, f'{message}')
except Exception as e:
message = f'{datetime.now()}|MONITORING_EXECUTION|ERROR|Failed to oparate with log folder during celaning log files: {e}\r\n'
print(Fore.RED + message + Style.RESET_ALL)
cf.write_file_append(logpath, f'{message}')
try:
for item in Path(LOG_DIR_PATH + 'log_daily_feed//').glob('*.log'):
if item.is_file():
try:
if (Path.stat(item).st_mtime) < time() - config['logsretention'] * 86400:
remove(item)
except Exception as remove_e:
message = f'{datetime.now()}|MONITORING_EXECUTION|ERROR|Failed to remove log file: {remove_e}\r\n'
print(Fore.RED + message + Style.RESET_ALL)
cf.write_file_append(logpath, f'{message}')
except Exception as e:
message = f'{datetime.now()}|MONITORING_EXECUTION|ERROR|Failed to oparate with log folder during celaning log files: {e}\r\n'
print(Fore.RED + message + Style.RESET_ALL)
cf.write_file_append(logpath, f'{message}')
if config['workinloop'] is False:
SCRIPT_LOOP = False
if config['workinloop'] is True:
# Work in loop condition
sleep(randint(config['loopintervallmin'], config['loopintervallmax']))
# Calculte running time of script
message = f'{datetime.now()}|MONITORING_EXECUTION|INFO|Monitoring End|Monitoring execution time = {str(datetime.now() - scriptstarttime)}\r\n'
print(message)
cf.write_file_append(logpath, message)
# Work in loop condition