|
1 | 1 | function Read-IcingaAgentLogFile()
|
2 | 2 | {
|
3 |
| - $Logfile = Join-Path -Path (Get-IcingaAgentLogDirectory) -ChildPath 'icinga2.log'; |
4 |
| - if ((Test-Path $Logfile) -eq $FALSE) { |
5 |
| - Write-IcingaConsoleError 'Icinga 2 logfile not present. Unable to load it'; |
6 |
| - return; |
7 |
| - } |
| 3 | + if ((Test-IcingaAgentFeatureEnabled -Feature 'windowseventlog') -And ([version](Get-IcingaAgentVersion).Full) -ge (New-IcingaVersionObject -Version '2.13.0')) { |
| 4 | + |
| 5 | + # Icinga 2.13.0 and beyond will log directly into the EventLog |
| 6 | + |
| 7 | + $LastEvent = $null; |
| 8 | + $LastMessage = $null; |
| 9 | + $LastId = $null; |
| 10 | + |
| 11 | + while ($TRUE) { |
| 12 | + $IcingaEvents = Get-WinEvent -LogName Application -MaxEvents 500 -ErrorAction Stop | Sort-Object { $_.TimeCreated }; |
| 13 | + |
| 14 | + foreach ($event in $IcingaEvents) { |
| 15 | + |
| 16 | + if ($event.ProviderName -ne 'Icinga 2') { |
| 17 | + continue; |
| 18 | + } |
| 19 | + |
| 20 | + if ($null -ne $LastEvent -And $event.TimeCreated -lt $LastEvent) { |
| 21 | + continue; |
| 22 | + } |
8 | 23 |
|
9 |
| - Get-Content -Path $Logfile -Tail 20 -Wait; |
| 24 | + if ($event.TimeCreated -eq $LastEvent -And (Get-StringSha1 -Content $event.Message) -eq $LastMessage -And $event.Id -eq $LastId) { |
| 25 | + continue; |
| 26 | + } |
| 27 | + |
| 28 | + $LastEvent = [DateTime]$event.TimeCreated; |
| 29 | + $LastMessage = (Get-StringSha1 -Content $event.Message); |
| 30 | + $LastId = $event.Id; |
| 31 | + $ForeColor = 'White'; |
| 32 | + |
| 33 | + if ($event.Level -eq 3) { # Warning |
| 34 | + $ForeColor = 'DarkYellow'; |
| 35 | + } elseif ($event.Level -eq 2) { # Error |
| 36 | + $ForeColor = 'Red'; |
| 37 | + } |
| 38 | + |
| 39 | + Write-IcingaConsolePlain -Message '[{0}] {1}' -Objects $event.TimeCreated, $event.Message -ForeColor $ForeColor; |
| 40 | + } |
| 41 | + |
| 42 | + Start-Sleep -Seconds 1; |
| 43 | + } |
| 44 | + } else { |
| 45 | + $Logfile = Join-Path -Path (Get-IcingaAgentLogDirectory) -ChildPath 'icinga2.log'; |
| 46 | + if ((Test-Path $Logfile) -eq $FALSE) { |
| 47 | + Write-IcingaConsoleError 'Icinga 2 logfile not present. Unable to load it'; |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + Get-Content -Path $Logfile -Tail 20 -Wait; |
| 52 | + } |
10 | 53 | }
|
0 commit comments