Skip to content

Commit 3fdd13d

Browse files
authored
Merge pull request #393 from Icinga:feature/generic_eventlog_reader_and_ifw_reader
Feature: Adds generic EventLog reader and IfW reader Adds generic reader function `Read-IcingaWindowsEventLog`, allowing to read any EventLog as stream on the console and adds in addition `Read-IcingaForWindowsLog` for reading Icinga for Windows specific logs
2 parents b4ced77 + f4dd594 commit 3fdd13d

File tree

4 files changed

+76
-38
lines changed

4 files changed

+76
-38
lines changed

Diff for: doc/100-General/10-Changelog.md

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic
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
2727
* [#392](https://github.com/Icinga/icinga-powershell-framework/pull/392) Adds support to read logs from Windows EventLog while using `Read-IcingaAgentLogFile`
28+
* [#393](https://github.com/Icinga/icinga-powershell-framework/pull/393) Adds generic reader function `Read-IcingaWindowsEventLog`, allowing to read any EventLog as stream on the console and adds in addition `Read-IcingaForWindowsLog` for reading Icinga for Windows specific logs
2829

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

Diff for: lib/core/framework/Read-IcingaForWindowsLog.psm1

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
function Read-IcingaForWindowsLog()
2+
{
3+
Read-IcingaWindowsEventLog -LogName 'Application' -Source 'Icinga for Windows' -MaxEntries 500;
4+
}

Diff for: lib/core/framework/Read-IcingaWindowsEventLog.psm1

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
function Read-IcingaWindowsEventLog()
2+
{
3+
param (
4+
[string]$LogName = 'Application',
5+
[array]$Source = @(),
6+
[int]$MaxEntries = 500
7+
);
8+
9+
if ([string]::IsNullOrEmpty($LogName)) {
10+
Write-IcingaConsoleError 'You have to specify a log to read from';
11+
return;
12+
}
13+
14+
$LastEvent = $null;
15+
$LastMessage = $null;
16+
$LastId = $null;
17+
$MaxEvents = 40000;
18+
19+
while ($TRUE) {
20+
[array]$IcingaEvents = Get-WinEvent -LogName $LogName -MaxEvents $MaxEvents -ErrorAction Stop;
21+
[int]$CurrentIndex = $MaxEntries;
22+
[array]$CollectedEvents = @();
23+
24+
foreach ($event in $IcingaEvents) {
25+
26+
if ($CurrentIndex -eq 0) {
27+
break;
28+
}
29+
30+
if ($Source.Count -ne 0 -And $Source -NotContains $event.ProviderName) {
31+
continue;
32+
}
33+
34+
$CurrentIndex -= 1;
35+
36+
if ($null -ne $LastEvent -And $event.TimeCreated -lt $LastEvent) {
37+
$MaxEvents = 500;
38+
break;
39+
}
40+
41+
if ($event.TimeCreated -eq $LastEvent -And (Get-StringSha1 -Content $event.Message) -eq $LastMessage -And $event.Id -eq $LastId) {
42+
$MaxEvents = 500;
43+
break;
44+
}
45+
46+
$CollectedEvents += $event;
47+
}
48+
49+
$CollectedEvents = $CollectedEvents | Sort-Object { $_.TimeCreated };
50+
51+
foreach ($event in $CollectedEvents) {
52+
53+
$ForeColor = 'White';
54+
55+
if ($event.Level -eq 3) { # Warning
56+
$ForeColor = 'DarkYellow';
57+
} elseif ($event.Level -eq 2) { # Error
58+
$ForeColor = 'Red';
59+
}
60+
61+
$LastMessage = (Get-StringSha1 -Content $event.Message);
62+
$LastId = $event.Id;
63+
$LastEvent = [DateTime]$event.TimeCreated;
64+
65+
Write-IcingaConsolePlain -Message '[{0}] {1}' -Objects $event.TimeCreated, $event.Message -ForeColor $ForeColor;
66+
}
67+
68+
Start-Sleep -Seconds 1;
69+
}
70+
}

Diff for: lib/core/icingaagent/readers/Read-IcingaAgentLogFile.psm1

+1-38
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,7 @@ function Read-IcingaAgentLogFile()
33
if ((Test-IcingaAgentFeatureEnabled -Feature 'windowseventlog') -And ([version](Get-IcingaAgentVersion).Full) -ge (New-IcingaVersionObject -Version '2.13.0')) {
44

55
# 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-
}
23-
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-
}
6+
Read-IcingaWindowsEventLog -LogName 'Application' -Source 'Icinga 2' -MaxEntries 500;
447
} else {
458
$Logfile = Join-Path -Path (Get-IcingaAgentLogDirectory) -ChildPath 'icinga2.log';
469
if ((Test-Path $Logfile) -eq $FALSE) {

0 commit comments

Comments
 (0)