-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathInvoke-IcingaCheckTimeSync.psm1
116 lines (105 loc) · 4.58 KB
/
Invoke-IcingaCheckTimeSync.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<#
.SYNOPSIS
Gets Network Time Protocol time(SMTP/NTP) from a specified server
.DESCRIPTION
Invoke-IcingaCheckTimeSync connects to an NTP server on UDP default port 123 and retrieves the current NTP time.
Selected components of the returned time information are decoded and returned in a hashtable.
.PARAMETER Server
The NTP Server you want to connect to.
.PARAMETER Port
Port number (default: 123)
.PARAMETER TimeOffset
The maximum acceptable offset between the local clock and the NTP Server, in seconds e.g. if you allow up to 0.5s timeoffset you can also enter 500ms.
Invoke-IcingaCheckTimeSync will return OK, if there is no difference between them,
WARNING, if the time difference exceeds the Warning threshold,
CRITICAL, if the time difference exceeds the Critical threshold.
.PARAMETER Warning
Used to specify a offset Warning threshold e.g 10ms or 0.01s
.PARAMETER Critical
Used to specify a offset Critical threshold e.g 20ms or 0.02s.
.PARAMETER Timeout
Seconds before connection times out (default: 10)
.PARAMETER IPV4
Use IPV4 connection. Default $FALSE
.PARAMETER IgnoreService
Ignores the W32Time service during check execution and will not throw warning or critical in case the service is not running
.PARAMETER Verbosity
Changes the behavior of the plugin output which check states are printed:
0 (default): Only service checks/packages with state not OK will be printed
1: Only services with not OK will be printed including OK checks of affected check packages including Package config
2: Everything will be printed regardless of the check state
3: Identical to Verbose 2, but prints in addition the check package configuration e.g (All must be [OK])
.EXAMPLE
PS> Invoke-IcingaCheckTimeSync -Server '0.pool.ntp.org' -TimeOffset 10ms -Warning 10ms -Critical 20ms -Verbosity 2
\_ [OK] Check package "Time Package" (Match All)
\_ [OK] Sync Status: NoLeapWarning
\_ [WARNING] Time Offset: Value "0.02s" is greater than threshold "0.01s"
\_ [OK] Time Service: Running
| 'time_offset'=0.02s;0.01;0.02 'time_service'=4;;4
0
.EXAMPLE
PS> Invoke-IcingaCheckTimeSync -Server 'time.versatel.de' -TimeOffset 50ms -Warning 10ms -Critical 20ms -Verbosity 2
\_ [OK] Sync Status: NoLeapWarning
\_ [OK] Time Offset: 0s
\_ [OK] Time Service: Running
| 'time_offset'=0s;0.01;0.02 'time_service'=4;;4
1
.LINK
https://github.com/Icinga/icinga-powershell-plugins
.NOTES
The values you enter here are all converted into seconds and if you enter only values,
without type measurement behind like (s, ms, h, d etc.) the values are interpreted as seconds.
#>
function Invoke-IcingaCheckTimeSync()
{
param(
[string]$Server,
$TimeOffset = 0,
$Warning = $null,
$Critical = $null,
[int]$Timeout = 10,
[switch]$IPV4 = $FALSE,
[int]$Port = 123,
[switch]$IgnoreService = $FALSE,
[switch]$NoPerfData = $FALSE,
[ValidateSet(0, 1, 2, 3)]
[int]$Verbosity = 0
);
$TimeData = Get-IcingaNtpData -Server $Server -Port $Port -TimeOffset $TimeOffset -Timeout $Timeout -IPV4:$IPV4;
$TimeService = (Get-IcingaServices 'W32Time').W32time;
$OffsetCheck = New-IcingaCheck `
-Name 'Time Offset' `
-Value (
$TimeData.CalculatedOffset
) `
-Unit 's' `
-MetricIndex $Server `
-MetricName 'offset';
$OffsetCheck.WarnOutOfRange($Warning).CritOutOfRange($Critical) | Out-Null;
$TimeCheck = New-IcingaCheck `
-Name 'Time Service' `
-Value $TimeService.configuration.Status.raw `
-ObjectExists $TimeService `
-Translation $ProviderEnums.ServiceStatusName `
-MetricIndex $Server `
-MetricName 'service';
$TimeCheck.CritIfNotMatch($ProviderEnums.ServiceStatus.Running) | Out-Null;
$SyncStatus = New-IcingaCheck `
-Name 'Sync Status' `
-Value $TimeData.SyncStatus `
-Translation $ProviderEnums.TimeSyncStatusName `
-NoPerfData;
$SyncStatus.CritIfMatch($ProviderEnums.TimeSyncStatus.ClockNotSynchronized) | Out-Null;
$checks = @()
if ($IgnoreService) {
$checks = @($OffsetCheck, $SyncStatus)
} else {
$checks = @($OffsetCheck, $TimeCheck, $SyncStatus)
}
$CheckPackage = New-IcingaCheckPackage `
-Name 'Time Package' `
-Checks $checks `
-OperatorAnd `
-Verbose $Verbosity;
return (New-IcingaCheckResult -Check $CheckPackage -NoPerfData $NoPerfData -Compile);
}