|
| 1 | +function Show-IcingaEventLogAnalysis() |
| 2 | +{ |
| 3 | + param ( |
| 4 | + [string]$LogName = 'Application' |
| 5 | + ); |
| 6 | + |
| 7 | + Write-IcingaConsoleNotice 'Analysing EventLog "{0}"...' -Objects $LogName; |
| 8 | + |
| 9 | + Start-IcingaTimer 'EventLog Analyser'; |
| 10 | + |
| 11 | + try { |
| 12 | + $BasicLogData = Get-WinEvent -ListLog $LogName -ErrorAction Stop; |
| 13 | + } catch { |
| 14 | + Write-IcingaConsoleError 'Failed to fetch data for EventLog "{0}". Probably this log does not exist.' -Objects $LogName; |
| 15 | + return; |
| 16 | + } |
| 17 | + |
| 18 | + Write-IcingaConsoleNotice 'Logging Mode: {0}' -Objects $BasicLogData.LogMode; |
| 19 | + Write-IcingaConsoleNotice 'Maximum Size: {0} GB' -Objects ([math]::Round((Convert-Bytes -Value $BasicLogData.MaximumSizeInBytes -Unit 'GB').value, 2)); |
| 20 | + Write-IcingaConsoleNotice 'Current Entries: {0}' -Objects $BasicLogData.RecordCount; |
| 21 | + |
| 22 | + [hashtable]$LogAnalysis = @{ |
| 23 | + 'Day' = @{ |
| 24 | + 'Entries' = @{ }; |
| 25 | + 'Count' = 0; |
| 26 | + 'Average' = 0; |
| 27 | + 'Maximum' = 0; |
| 28 | + }; |
| 29 | + 'Hour' = @{ |
| 30 | + 'Entries' = @{ }; |
| 31 | + 'Count' = 0; |
| 32 | + 'Average' = 0; |
| 33 | + 'Maximum' = 0; |
| 34 | + }; |
| 35 | + 'Minute' = @{ |
| 36 | + 'Entries' = @{ }; |
| 37 | + 'Count' = 0; |
| 38 | + 'Average' = 0; |
| 39 | + 'Maximum' = 0; |
| 40 | + }; |
| 41 | + }; |
| 42 | + |
| 43 | + $LogData = Get-WinEvent -LogName $LogName; |
| 44 | + |
| 45 | + foreach ($entry in $LogData) { |
| 46 | + [string]$DayOfLogging = $entry.TimeCreated.ToString('yyyy\/MM\/dd'); |
| 47 | + [string]$HourOfLogging = $entry.TimeCreated.ToString('yyyy\/MM\/dd-HH'); |
| 48 | + [string]$MinuteOfLogging = $entry.TimeCreated.ToString('yyyy\/MM\/dd-HH-mm'); |
| 49 | + |
| 50 | + if ($LogAnalysis.Day.Entries.ContainsKey($DayOfLogging) -eq $FALSE) { |
| 51 | + $LogAnalysis.Day.Entries.Add($DayOfLogging, 0); |
| 52 | + } |
| 53 | + |
| 54 | + if ($LogAnalysis.Hour.Entries.ContainsKey($HourOfLogging) -eq $FALSE) { |
| 55 | + $LogAnalysis.Hour.Entries.Add($HourOfLogging, 0); |
| 56 | + } |
| 57 | + |
| 58 | + if ($LogAnalysis.Minute.Entries.ContainsKey($MinuteOfLogging) -eq $FALSE) { |
| 59 | + $LogAnalysis.Minute.Entries.Add($MinuteOfLogging, 0); |
| 60 | + } |
| 61 | + |
| 62 | + $LogAnalysis.Day.Entries[$DayOfLogging] += 1; |
| 63 | + $LogAnalysis.Hour.Entries[$HourOfLogging] += 1; |
| 64 | + $LogAnalysis.Minute.Entries[$MinuteOfLogging] += 1; |
| 65 | + |
| 66 | + $LogAnalysis.Day.Count += 1; |
| 67 | + $LogAnalysis.Hour.Count += 1; |
| 68 | + $LogAnalysis.Minute.Count += 1; |
| 69 | + |
| 70 | + $LogAnalysis.Day.Average = [math]::Ceiling($LogAnalysis.Day.Count / $LogAnalysis.Day.Entries.Count); |
| 71 | + $LogAnalysis.Hour.Average = [math]::Ceiling($LogAnalysis.Hour.Count / $LogAnalysis.Hour.Entries.Count); |
| 72 | + $LogAnalysis.Minute.Average = [math]::Ceiling($LogAnalysis.Minute.Count / $LogAnalysis.Minute.Entries.Count); |
| 73 | + } |
| 74 | + |
| 75 | + foreach ($value in $LogAnalysis.Day.Entries.Values) { |
| 76 | + $LogAnalysis.Day.Maximum = Get-IcingaValue -Value $value -Compare $LogAnalysis.Day.Maximum -Maximum; |
| 77 | + } |
| 78 | + foreach ($value in $LogAnalysis.Hour.Entries.Values) { |
| 79 | + $LogAnalysis.Hour.Maximum = Get-IcingaValue -Value $value -Compare $LogAnalysis.Hour.Maximum -Maximum; |
| 80 | + } |
| 81 | + foreach ($value in $LogAnalysis.Minute.Entries.Values) { |
| 82 | + $LogAnalysis.Minute.Maximum = Get-IcingaValue -Value $value -Compare $LogAnalysis.Minute.Maximum -Maximum; |
| 83 | + } |
| 84 | + Stop-IcingaTimer 'EventLog Analyser'; |
| 85 | + |
| 86 | + Write-IcingaConsoleNotice 'Average Logs per Day: {0}' -Objects $LogAnalysis.Day.Average; |
| 87 | + Write-IcingaConsoleNotice 'Average Logs per Hour: {0}' -Objects $LogAnalysis.Hour.Average; |
| 88 | + Write-IcingaConsoleNotice 'Average Logs per Minute: {0}' -Objects $LogAnalysis.Minute.Average; |
| 89 | + Write-IcingaConsoleNotice 'Maximum Logs per Day: {0}' -Objects $LogAnalysis.Day.Maximum; |
| 90 | + Write-IcingaConsoleNotice 'Maximum Logs per Hour: {0}' -Objects $LogAnalysis.Hour.Maximum; |
| 91 | + Write-IcingaConsoleNotice 'Maximum Logs per Minute: {0}' -Objects $LogAnalysis.Minute.Maximum; |
| 92 | + Write-IcingaConsoleNotice 'Analysing Time: {0}s' -Objects ([math]::Round((Get-IcingaTimer 'EventLog Analyser').Elapsed.TotalSeconds, 2)); |
| 93 | +} |
0 commit comments