Skip to content

Commit 9d426fe

Browse files
authored
Merge pull request #136 from Icinga:fix/eventlog_plugin_time_filter
Fix: Improves EventLog time filter and fixes issue on filtering Improves the general filtering for `-After` and `-Before` values by using our generic plugin threshold method. Instead of having to write `2021/01/30` (which is still possible) you can now use `-After 5h` to go back 5 hours within the event log. In addition the explanation of the `-After` and `-Before` argument is now more clear.
2 parents e697b52 + 7c8b850 commit 9d426fe

File tree

9 files changed

+58
-24
lines changed

9 files changed

+58
-24
lines changed

config/director/Invoke-IcingaCheckEventlog.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

config/director/Plugins_Bundle.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

config/icinga/Invoke-IcingaCheckEventlog.conf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ object CheckCommand "Invoke-IcingaCheckEventlog" {
33

44
arguments += {
55
"-Before" = {
6-
description = "Used to specify a date like dd.mm.yyyy and every eventlog entry before that date will be considered."
6+
description = "Used to specify time data of which point the plugin should stop considering event logs. You can either use a fixed date and time like '2021/01/30 12:00:00', a fixed day '2021/01/30' or use more dynamic approaches like '1d', '10h' and so on. By using '2h' for example, log files of the last 2 hours will be ignored. Please ensure to manually set the `-After` argument and ensure you go back further in time with the `-After` argument than the `-Before` argument. Allowed units: ms, s, m, h, d, w, M, y"
77
value = "$IcingaCheckEventlog_Object_Before$"
88
order = 16
99
}
@@ -34,7 +34,7 @@ object CheckCommand "Invoke-IcingaCheckEventlog" {
3434
order = 13
3535
}
3636
"-After" = {
37-
description = "Used to specify a date like dd.mm.yyyy and every eventlog entry after that date will be considered."
37+
description = "Used to specify time data of which point the plugin should start to read event logs from. You can either use a fixed date and time like '2021/01/30 12:00:00', a fixed day '2021/01/30' or use more dynamic approaches like '1d', '10h' and so on. Allowed units: ms, s, m, h, d, w, M, y"
3838
value = "$IcingaCheckEventlog_Object_After$"
3939
order = 15
4040
}

config/icinga/Plugins_Bundle.conf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,7 +1235,7 @@ object CheckCommand "Invoke-IcingaCheckEventlog" {
12351235

12361236
arguments += {
12371237
"-Before" = {
1238-
description = "Used to specify a date like dd.mm.yyyy and every eventlog entry before that date will be considered."
1238+
description = "Used to specify time data of which point the plugin should stop considering event logs. You can either use a fixed date and time like '2021/01/30 12:00:00', a fixed day '2021/01/30' or use more dynamic approaches like '1d', '10h' and so on. By using '2h' for example, log files of the last 2 hours will be ignored. Please ensure to manually set the `-After` argument and ensure you go back further in time with the `-After` argument than the `-Before` argument. Allowed units: ms, s, m, h, d, w, M, y"
12391239
value = "$IcingaCheckEventlog_Object_Before$"
12401240
order = 16
12411241
}
@@ -1266,7 +1266,7 @@ object CheckCommand "Invoke-IcingaCheckEventlog" {
12661266
order = 13
12671267
}
12681268
"-After" = {
1269-
description = "Used to specify a date like dd.mm.yyyy and every eventlog entry after that date will be considered."
1269+
description = "Used to specify time data of which point the plugin should start to read event logs from. You can either use a fixed date and time like '2021/01/30 12:00:00', a fixed day '2021/01/30' or use more dynamic approaches like '1d', '10h' and so on. Allowed units: ms, s, m, h, d, w, M, y"
12701270
value = "$IcingaCheckEventlog_Object_After$"
12711271
order = 15
12721272
}

doc/30-Upgrading-Plugins.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ Specific version upgrades are described below. Please note that version updates
66

77
For upgrading plugins, please have a look on the [installation docs](02-Installation.md).
88

9-
## Upgrading to v1.4.0 (pending)
9+
## Upgrading to v1.4.0 (2021-03-02)
1010

11-
*No special steps required*
11+
### Invoke-IcingaCheckEventLog
12+
13+
We made some slight adjustments to `Invoke-IcingaCheckEventLog` for the `-After` and `-Before` argument, which will now not only allow a fixed time stamp, like `2021/01/30`, but also threshold inputs like `5h` (to go back 5 hours), `1d` (to go back 1 day), and so on. There should be no impact on current implementations but you might wanna have a look on your checks and results after uprading.
1214

1315
## Upgrading to v1.3.0 (2020-12-01)
1416

doc/31-Changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic
1818

1919
### Bugfixes
2020

21+
* [#136](https://github.com/Icinga/icinga-powershell-plugins/pull/136) Improves `Invoke-IcingaCheckEventLog` time filter by allowing simple input like `-After 5h` and fixes issue on filtering by formatting the time data into required format `yyyy/MM/dd HH:mm:ss`
22+
2123
## 1.3.1 (2021-02-04)
2224

2325
[Issue and PRs](https://github.com/Icinga/icinga-powershell-plugins/milestone/6?closed=1)

doc/plugins/06-Invoke-IcingaCheckEventlog.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ To execute this plugin you will require to grant the following user permissions.
3939
| ExcludeMessage | Array | false | | Used to specify an array of messages within the eventlog to be excluded. |
4040
| IncludeSource | Array | false | | Used to specify an array of message sources within the eventlog to be included. |
4141
| ExcludeSource | Array | false | | Used to specify an array of message sources within the eventlog to be excluded. |
42-
| After | Object | false | | Used to specify a date like dd.mm.yyyy and every eventlog entry after that date will be considered. |
43-
| Before | Object | false | | Used to specify a date like dd.mm.yyyy and every eventlog entry before that date will be considered. |
42+
| After | Object | false | | Used to specify time data of which point the plugin should start to read event logs from. You can either use a fixed date and time like "2021/01/30 12:00:00", a fixed day "2021/01/30" or use more dynamic approaches like "1d", "10h" and so on. Allowed units: ms, s, m, h, d, w, M, y |
43+
| Before | Object | false | | Used to specify time data of which point the plugin should stop considering event logs. You can either use a fixed date and time like "2021/01/30 12:00:00", a fixed day "2021/01/30" or use more dynamic approaches like "1d", "10h" and so on. By using "2h" for example, log files of the last 2 hours will be ignored. Please ensure to manually set the `-After` argument and ensure you go back further in time with the `-After` argument than the `-Before` argument. Allowed units: ms, s, m, h, d, w, M, y |
4444
| DisableTimeCache | SwitchParameter | false | False | Switch to disable the time cache on a check. If this parameter is set the time cache is disabled. After the check has been run once, the next check instance will filter through the eventlog from the point the last check ended. This is due to the time cache, when disabled the whole eventlog is checked instead. |
4545
| NoPerfData | SwitchParameter | false | False | Used to disable PerfData. |
4646
| Verbosity | Int32 | false | 0 | |
@@ -56,7 +56,7 @@ Invoke-IcingaCheckEventlog -LogName Application -IncludeEntryType Warning -Warni
5656
### Example Output 1
5757

5858
```powershell
59-
[CRITICAL] Check package "EventLog" - [CRITICAL] EventId 642 [WARNING] EventId 1008, EventId 2002, EventId 642\_ [WARNING] Check package "Between: [16.08.2020 09:31:35] - [27.08.2020 22:57:39] there occured 242 event(s)." \_ [WARNING] EventId 2002: Value "242" is greater than threshold "100"\_ [WARNING] Check package "Between: [16.08.2020 09:31:35] - [27.08.2020 22:57:39] there occured 242 event(s)." \_ [WARNING] EventId 1008: Value "242" is greater than threshold "100"\_ [WARNING] Check package "Between: [16.08.2020 09:31:35] - [27.08.2020 22:57:39] there occured 242 event(s)." \_ [WARNING] EventId 1008: Value "242" is greater than threshold "100"\_ [WARNING] Check package "Between: [16.08.2020 09:31:36] - [27.08.2020 22:57:40] there occured 242 event(s)." \_ [WARNING] EventId 1008: Value "242" is greater than threshold "100"\_ [WARNING] Check package "Between: [16.08.2020 14:50:37] - [17.08.2020 19:41:00] there occured 391 event(s)." \_ [WARNING] EventId 642: Value "391" is greater than threshold "100"\_ [WARNING] Check package "Between: [16.08.2020 14:50:37] - [17.08.2020 19:41:00] there occured 391 event(s)." \_ [WARNING] EventId 642: Value "391" is greater than threshold "100"\_ [WARNING] Check package "Between: [16.08.2020 15:51:00] - [17.08.2020 19:41:00] there occured 298 event(s)." \_ [WARNING] EventId 642: Value "298" is greater than threshold "100"\_ [CRITICAL] Check package "Between: [17.08.2020 19:56:38] - [27.08.2020 09:56:35] there occured 3539 event(s)." \_ [CRITICAL] EventId 642: Value "3539" is greater than threshold "1000"\_ [CRITICAL] Check package "Between: [17.08.2020 19:56:38] - [27.08.2020 09:56:35] there occured 3539 event(s)." \_ [CRITICAL] EventId 642: Value "3539" is greater than threshold "1000"\_ [CRITICAL] Check package "Between: [17.08.2020 20:03:07] - [27.08.2020 09:56:35] there occured 2757 event(s)." \_ [CRITICAL] EventId 642: Value "2757" is greater than threshold "1000"
59+
[CRITICAL] Check package "EventLog" - [CRITICAL] EventId 642 [WARNING] EventId 1008, EventId 2002, EventId 642\_ [WARNING] Check package "Between: [16.08.2020 09:31:35] - [27.08.2020 22:57:39] there occurred 242 event(s)." \_ [WARNING] EventId 2002: Value "242" is greater than threshold "100"\_ [WARNING] Check package "Between: [16.08.2020 09:31:35] - [27.08.2020 22:57:39] there occurred 242 event(s)." \_ [WARNING] EventId 1008: Value "242" is greater than threshold "100"\_ [WARNING] Check package "Between: [16.08.2020 09:31:35] - [27.08.2020 22:57:39] there occurred 242 event(s)." \_ [WARNING] EventId 1008: Value "242" is greater than threshold "100"\_ [WARNING] Check package "Between: [16.08.2020 09:31:36] - [27.08.2020 22:57:40] there occurred 242 event(s)." \_ [WARNING] EventId 1008: Value "242" is greater than threshold "100"\_ [WARNING] Check package "Between: [16.08.2020 14:50:37] - [17.08.2020 19:41:00] there occurred 391 event(s)." \_ [WARNING] EventId 642: Value "391" is greater than threshold "100"\_ [WARNING] Check package "Between: [16.08.2020 14:50:37] - [17.08.2020 19:41:00] there occurred 391 event(s)." \_ [WARNING] EventId 642: Value "391" is greater than threshold "100"\_ [WARNING] Check package "Between: [16.08.2020 15:51:00] - [17.08.2020 19:41:00] there occurred 298 event(s)." \_ [WARNING] EventId 642: Value "298" is greater than threshold "100"\_ [CRITICAL] Check package "Between: [17.08.2020 19:56:38] - [27.08.2020 09:56:35] there occurred 3539 event(s)." \_ [CRITICAL] EventId 642: Value "3539" is greater than threshold "1000"\_ [CRITICAL] Check package "Between: [17.08.2020 19:56:38] - [27.08.2020 09:56:35] there occurred 3539 event(s)." \_ [CRITICAL] EventId 642: Value "3539" is greater than threshold "1000"\_ [CRITICAL] Check package "Between: [17.08.2020 20:03:07] - [27.08.2020 09:56:35] there occurred 2757 event(s)." \_ [CRITICAL] EventId 642: Value "2757" is greater than threshold "1000"
6060
```
6161

6262
### Example Command 2
@@ -68,5 +68,5 @@ Invoke-IcingaCheckEventlog -LogName Application -IncludeEntryType Warning -Warni
6868
### Example Output 2
6969

7070
```powershell
71-
[WARNING] Check package "EventLog" - [WARNING] EventId 1008\_ [WARNING] Check package "Between: [16.08.2020 09:31:35] - [27.08.2020 22:57:39] there occured 242 event(s)." \_ [WARNING] EventId 1008: Value "242" is greater than threshold "100"\_ [WARNING] Check package "Between: [16.08.2020 09:31:35] - [27.08.2020 22:57:39] there occured 242 event(s)." \_ [WARNING] EventId 1008: Value "242" is greater than threshold "100"\_ [WARNING] Check package "Between: [16.08.2020 09:31:36] - [27.08.2020 22:57:40] there occured 242 event(s)." \_ [WARNING] EventId 1008: Value "242" is greater than threshold "100"| 'eventid_1008'=726c;;
71+
[WARNING] Check package "EventLog" - [WARNING] EventId 1008\_ [WARNING] Check package "Between: [16.08.2020 09:31:35] - [27.08.2020 22:57:39] there occurred 242 event(s)." \_ [WARNING] EventId 1008: Value "242" is greater than threshold "100"\_ [WARNING] Check package "Between: [16.08.2020 09:31:35] - [27.08.2020 22:57:39] there occurred 242 event(s)." \_ [WARNING] EventId 1008: Value "242" is greater than threshold "100"\_ [WARNING] Check package "Between: [16.08.2020 09:31:36] - [27.08.2020 22:57:40] there occurred 242 event(s)." \_ [WARNING] EventId 1008: Value "242" is greater than threshold "100"| 'eventid_1008'=726c;;
7272
```

plugins/Invoke-IcingaCheckEventlog.psm1

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,18 @@
7777
.PARAMETER ExcludeSource
7878
Used to specify an array of message sources within the eventlog to be excluded.
7979
.PARAMETER After
80-
Used to specify a date like dd.mm.yyyy and every eventlog entry after that date will be considered.
80+
Used to specify time data of which point the plugin should start to read event logs from.
81+
You can either use a fixed date and time like "2021/01/30 12:00:00", a fixed day "2021/01/30" or use more dynamic approaches like "1d", "10h" and so on.
82+
83+
Allowed units: ms, s, m, h, d, w, M, y
8184
.PARAMETER Before
82-
Used to specify a date like dd.mm.yyyy and every eventlog entry before that date will be considered.
85+
Used to specify time data of which point the plugin should stop considering event logs.
86+
You can either use a fixed date and time like "2021/01/30 12:00:00", a fixed day "2021/01/30" or use more dynamic approaches like "1d", "10h" and so on.
87+
88+
By using "2h" for example, log files of the last 2 hours will be ignored. Please ensure to manually set the `-After` argument and ensure you go back
89+
further in time with the `-After` argument than the `-Before` argument.
90+
91+
Allowed units: ms, s, m, h, d, w, M, y
8392
.PARAMETER DisableTimeCache
8493
Switch to disable the time cache on a check. If this parameter is set the time cache is disabled.
8594
After the check has been run once, the next check instance will filter through the eventlog from the point the last check ended.
@@ -119,10 +128,13 @@ function Invoke-IcingaCheckEventlog()
119128
[int]$Verbosity = 0
120129
);
121130

131+
$After = Convert-IcingaPluginThresholds $After;
132+
$Before = Convert-IcingaPluginThresholds $Before;
133+
122134
$EventLogPackage = New-IcingaCheckPackage -Name 'EventLog' -OperatorAnd -Verbose $Verbosity;
123135
$EventLogData = Get-IcingaEventLog -LogName $LogName -IncludeEventId $IncludeEventId -ExcludeEventId $ExcludeEventId -IncludeUsername $IncludeUsername -ExcludeUsername $ExcludeUsername `
124136
-IncludeEntryType $IncludeEntryType -ExcludeEntryType $ExcludeEntryType -IncludeMessage $IncludeMessage -ExcludeMessage $ExcludeMessage `
125-
-IncludeSource $IncludeSource -ExcludeSource $ExcludeSource -After $After -Before $Before -DisableTimeCache $DisableTimeCache;
137+
-IncludeSource $IncludeSource -ExcludeSource $ExcludeSource -After $After.Value -Before $Before.Value -DisableTimeCache $DisableTimeCache;
126138

127139
[hashtable]$EventLogSource = @{};
128140

provider/eventlog/Get-IcingaEventLog.psm1

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,38 @@ function Get-IcingaEventLog()
2828
};
2929

3030
# This will generate a unique hash for each possible configured EventLog check to store the last check time for each of these checks
31-
[string]$CheckHash = (Get-StringSha1 ($LogName + $IncludeEventId + $ExcludeEventId + $IncludeUsername + $ExcludeUsername + $IncludeEntryType + $ExcludeEntryType + $IncludeMessage + $ExcludeMessage)) + '.lastcheck';
31+
[string]$CheckHash = (Get-StringSha1 ($LogName + $IncludeEventId + $ExcludeEventId + $IncludeUsername + $ExcludeUsername + $IncludeEntryType + $ExcludeEntryType + $IncludeMessage + $ExcludeMessage)) + '.lastcheck';
32+
[string]$EventsAfter = $null;
33+
[string]$EventsBefore = $null;
3234

33-
if ($null -eq $After -and $DisableTimeCache -eq $FALSE) {
35+
if ([string]::IsNullOrEmpty($After) -and $DisableTimeCache -eq $FALSE) {
3436
$time = Get-IcingaCacheData -Space 'provider' -CacheStore 'eventlog' -KeyName $CheckHash;
3537
Set-IcingaCacheData -Space 'provider' -CacheStore 'eventlog' -KeyName $CheckHash -Value ((Get-Date).ToFileTime());
3638

3739
if ($null -ne $time) {
38-
$After = [datetime]::FromFileTime($time);
40+
$EventsAfter = ([datetime]::FromFileTime($time)).ToString('yyyy\/MM\/dd HH:mm:ss');
3941
}
4042
}
4143

4244
# In case we are not having cached time execution and not have not overwritten the timestamp, only fetch values from 2 hours in the past
43-
if ($null -eq $After) {
44-
$After = [datetime]::Now.Subtract([TimeSpan]::FromHours(2));
45+
if ([string]::IsNullOrEmpty($EventsAfter)) {
46+
if ([string]::IsNullOrEmpty($After)) {
47+
[string]$EventsAfter = ([datetime]::Now.Subtract([TimeSpan]::FromHours(2))).ToString('yyyy\/MM\/dd HH:mm:ss');
48+
} else {
49+
if ((Test-Numeric $After)) {
50+
$EventsAfter = ([datetime]::Now.Subtract([TimeSpan]::FromSeconds($After))).ToString('yyyy\/MM\/dd HH:mm:ss');
51+
} else {
52+
$EventsAfter = $After;
53+
}
54+
}
55+
}
56+
57+
if ([string]::IsNullOrEmpty($Before) -eq $FALSE) {
58+
if ((Test-Numeric $Before)) {
59+
$EventsBefore = ([datetime]::Now.Subtract([TimeSpan]::FromSeconds($Before))).ToString('yyyy\/MM\/dd HH:mm:ss');
60+
} else {
61+
$EventsBefore = $Before;
62+
}
4563
}
4664

4765
if ($null -ne $IncludeUsername -And $IncludeUsername.Count -ne 0) {
@@ -50,11 +68,11 @@ function Get-IcingaEventLog()
5068
if ($null -ne $IncludeEntryType -And $IncludeEntryType.Count -ne 0) {
5169
$EventLogArguments.Add('EntryType', $IncludeEntryType);
5270
}
53-
if ($null -ne $After) {
54-
$EventLogArguments.Add('After', $After);
71+
if ([string]::IsNullOrEmpty($EventsAfter) -eq $FALSE) {
72+
$EventLogArguments.Add('After', $EventsAfter);
5573
}
56-
if ($null -ne $Before) {
57-
$EventLogArguments.Add('Before', $Before);
74+
if ([string]::IsNullOrEmpty($EventsBefore) -eq $FALSE) {
75+
$EventLogArguments.Add('Before', $EventsBefore);
5876
}
5977

6078
try {

0 commit comments

Comments
 (0)