-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProcess_Monitor.ps1
319 lines (258 loc) · 11.7 KB
/
Process_Monitor.ps1
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
param(
[string]$config = "C:\InstallationFolder\ProcessMonitor\config_file.txt",
[string]$mode = "normal"
)
# Email configuration
$smtpServer = "MAILSERVER.com"
$clientname = "CLIENTNAME"
$emailFrom = "EMAILFROM@DOMAIN.com"
$emailTo = "RECIPIENTS HERE"
$debug = $False
$now = @(Get-Date)
$successBody = ""
$success = 0
$failBody = ""
$fail = 0
$skippedBody = ""
$skip
$emailBody = "<html><body>"
$emailBody += "Checks run at $now<br>`n"
$checks = Get-Content $config
ForEach($line in $checks)
{
if(($line.StartsWith("#")) -or ($line.length -lt 2))
{
Continue
}
$settings = $line.split(",")
if(!($settings.length -eq 4))
{
Write-Host "Error: Config must have 4 entries per row"
Continue
}
$startTime = $settings[0]
$endTime = $settings[1]
$type = $settings[2]
$details = $settings[3]
$statusMessage = ""
if($now -ge $startTime -le $endTime)
{
Write-Host "Info: Running $type check for $details @$now (between $startTime-$endTime)"
switch($type)
{
checkinfile {
# This expects a path and search term separated by a ^
# i.e. c:\progra~1\InstalDir\LogFiles\*.log^failure
if (($details.split("^").length) -eq 2){
Write-Host "Info: CheckInFile check"
$date = Get-Date -Format "yyyyMMdd"
$filename = $details.split("^")[0]
$filename = $filename.replace("<date>",$date)
$pattern = $details.split("^")[1]
$results = @()
$FreshError = 0
$files = Get-ChildItem $filename # filename can include a wildcard, needs to handle _1, _2 etc. suffixes
foreach($file in $files)
{
Write-Host "Info: Searching `"$file`" for `"$pattern`""
$results += Select-String $file -Pattern $pattern -SimpleMatch
#the following cycle checks chekinfile errors
foreach($i in $results) #loops through all occurances of the error
{
$timing = $i.Line.substring(0,8)
$current_hour = Get-Date -format "HH:mm:ss"
$TimeDiff = New-TimeSpan $timing $current_hour # solution from https://community.spiceworks.com/topic/436406-time-difference-in-powershell
if (($TimeDiff.Hours -eq 0) -and ($TimeDiff.Minute -lt 18)) #only report an error that have occured in the last 18 minutes
{
Write-Host "recent error"
#This is ad-hoc solution for specific string pattern in file
if ($pattern = "QUANTITY ERROR")#if this bug check if happened for a new order.
{
Write-Host "FOUND QTY ERROR"
$nextline = $i.Context.PostContext|Select-String -Pattern "is not equal to allocated quantity"#gets the next line after the error
foreach($j in $nextline)#there is only one line picked after the error line, but have to loop throught it anyway
{
$orderID = $j.Line.Substring($j.Line.IndexOf("for Order")+10,4) #get the errored order ID
Write-Host "Order id is:"$orderID
$OrderTime = $j.Line.Substring(0,8) #get the exact time when the error was logged
$orderstring = " for Order "+$orderID
Write-Host "String to search for: "$orderstring
$olderrors = Select-String $filename -Pattern $orderstring #check the logfile for all occurances of this order
$FreshError=1 # assume it is a new problem unless proven otherwise
$numoferrors = $olderrors.Matches.count
Write-Host "number of errors: " $numoferrors
if ($olderrors.Matches.count -gt 1){#only check for old occurances of the same error if the total number of errors is more than one
Write-Host "Subloop"
foreach($k in $olderrors) #loops through all occurances of the order
{
$oldtime = $k.Line.substring(0,8) #check the time of the occurance
Write-Host "oldtime "+$oldtime
$TimeDiff2 = New-TimeSpan $oldtime $OrderTime
Write-Host "Timediff2" $TimeDiff2
if (([System.Math]::Abs($TimeDiff2.Hours) -gt 0) -or ([System.Math]::Abs($TimeDiff2.Minutes) -ge 18))
#if the occurance was more than or exactly 18 munutes ago then it's a repeat error
{
$FreshError =0
Write-Host "Will not report this order"
}
}
}
}
}#end qunatity error bug ad-hoc check
else #if general checkinfile error happened within 18 minutes - report it
{
$FreshError=1
}
}
}
}
if(($results.Count -gt 0) -and $FreshError ) #uncomment the 2nd variable if using the check for the last hour
{
$failBody += "Fail (checkinfile):`"$pattern`" found in `"$file`"<br>`n"
$fail += 1
}
else
{
$successBody += "Success (checkinfile):`"$pattern`" not found in `"$file`"<br>`n"
$success += 1
}
}
}
checknotinfile {
# As above, but alerts when not in file
if (($details.split("^").length) -eq 2){
Write-Host "Info: CheckNotInFile check"
$date = Get-Date -Format "yyyyMMdd"
$filename = $details.split("^")[0]
$filename = $filename.replace("<date>",$date)
$pattern = $details.split("^")[1]
$results = @()
$files = Get-ChildItem $filename # filename can include a wildcard, needs to handle _1, _2 etc. suffixes
foreach($file in $files)
{
Write-Host "Info: Searching `"$file`" for `"$pattern`""
$results += Select-String $file -Pattern $pattern -SimpleMatch
}
if($results -eq $null)
{
$failBody += "Fail (checknotinfile):`"$pattern`" not found in `"$file`"<br>`n"
$fail += 1
}
else
{
$successBody += "Success (checknotinfile):`"$pattern`" found in `"$file`"<br>`n"
$success += 1
}
}
}
checkFIX2mins {
# Alerts when the last FIX message was logged more than 2 minutes ago (i.e. FIX Server is down, no heartbeats)
if ($type -eq "checkFIX2mins"){
Write-Host "Info: CheckFIX2mins check"
$date = Get-Date -Format "yyyyMMdd"
$filename = $details.replace("<date>",$date)
Write-Host "Info: Searching $filename for time of last FIX message"
$lastFIXline = Get-Content $filename | Select -Last 1
$lastFIXtime = $lastFIXline.substring(0,8)
$lastFIXtime = [DateTime]::ParseExact($lastFIXtime,"HH:mm:ss",$null)
Write-Host "Comparing time of last FIX message ($lastFIXtime) to current time ($currenttime)"
$currenttime = Get-Date -format HH:mm:ss
$FIXtime = New-TimeSpan $lastFIXtime $currenttime
$FIXsecs = $FIXtime.TotalSeconds
Write-Host "Seconds since last FIX message: "$FIXsecs
if($FIXsecs -gt 120)
{
$failBody += "Fail (checkFIX2mins): last FIX message ($lastFIXtime) received more than 2 mins ago (checked @$currenttime) - $filename<br>`n"
$fail += 1
}
else
{
$successBody += "Success (checkFIX2mins): last FIX message ($lastFIXtime) received within the last 2 mins (checked @$currenttime) - $filename<br>`n"
$success += 1
}
}
}
checkprinter { #check that Bullzip is the default printer to output reports
$defaultprinter = Get-WmiObject -Query "SELECT * from Win32_Printer WHERE Default=$true"
if ($defaultprinter.Name -ne 'Bullzip PDF Printer')
{
$failBody += "Fail (checkprinter): Bullzip PDF is not currently the default printer - Resetting Automatically`n"
$fail += 1
$newprinter = Get-WmiObject -Query "Select * from Win32_Printer Where Name = 'Bullzip PDF Printer'"
$newprinter.SetDefaultPrinter()
}
else
{
$successBody += "Success (checkprinter): Bullzip PDF is currently the default printer`n"
$success += 1
}
}
default {
Write-Host "Error:Check type not supported"
}
}
}
else
{
Write-Host "Info: Skipping $type check for $details @ $now (outside of $startTime-$endTime)"
$skippedBody += "Skip: Skipping $type check for $details @ $now (outside of $startTime-$endTime)<br>`n"
$skip += 1
}
}
if($fail -gt 0)
{
$emailBody += "         Failed checks!<br>********************************************************************************<br>"
$emailBody += "<br>`n" + $failBody
$emailBody += "********************************************************************************<br>"
}
if($success -gt 0)
{
$emailBody += "        Successful checks<br>********************************************************************************<br>"
$emailBody += "<br>`n" + $successBody
$emailBody += "********************************************************************************<br>"
}
if($skip -gt 0)
{
$emailBody += "         Skipped checks<br>********************************************************************************<br>"
$emailBody += "<br>`n" + $skippedBody
$emailBody += "********************************************************************************<br>"
}
$emailBody += "</body></html>"
$sendEmail = $False
switch($mode)
{
normal
{
if($fail -gt 0)
{
$emailSubject = "$clientname`: Minor Fails $fail/$($fail+$success) checks $now"
$sendEmail = $True
}
}
status
{
$emailSubject = "$clientname`: Status email $now"
$sendEmail = $True
}
}
if($sendEmail)
{
if($debug)
{
Write-Host "Debug: Email from: $emailFrom"
Write-Host "Debug: Email to: $emailTo"
Write-Host "Debug: Email subject: $emailSubject"
Write-Host "Debug: Email body: $emailBody"
}
else
{
Write-Host "Info: Sending email"
$msg = New-Object System.Net.Mail.MailMessage $emailFrom, $emailTo
$msg.Subject = $emailSubject
$msg.IsBodyHtml = $True
$msg.Body = $emailBody
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25)
$SMTPClient.EnableSsl = $False
$SMTPClient.UseDefaultCredentials = $False
}
}