Skip to content

Commit a1fdb85

Browse files
committed
Adds support to read EventLog for logs
1 parent d759a4d commit a1fdb85

File tree

2 files changed

+50
-6
lines changed

2 files changed

+50
-6
lines changed

doc/100-General/10-Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic
2424
### Enhancements
2525

2626
* [#383](https://github.com/Icinga/icinga-powershell-framework/pull/383) Moves the components REST-Api [icinga-powershell-restapi](https://icinga.com/docs/icinga-for-windows/latest/restapi/doc/01-Introduction/) and API-Checks [icinga-powershell-apichecks](https://icinga.com/docs/icinga-for-windows/latest/apichecks/doc/01-Introduction/) directly into the Framework
27+
* [#392](https://github.com/Icinga/icinga-powershell-framework/pull/392) Adds support to read logs from Windows EventLog while using `Read-IcingaAgentLogFile`
2728

2829
## 1.6.1 (2021-09-15)
2930

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,53 @@
11
function Read-IcingaAgentLogFile()
22
{
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+
}
823

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+
}
1053
}

0 commit comments

Comments
 (0)