-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnagios-utils.ps1
75 lines (64 loc) · 1.73 KB
/
nagios-utils.ps1
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
$NagiosOk = 0
$NagiosWarning = 1
$NagiosCritical = 2
$NagiosUnknown = 3
$ErrorActionPreference = "Stop"
$error.clear()
# Return human readable state text
function Plugin-State-Text ([int] $code) {
switch ($code) {
$NagiosOk {"OK"}
$NagiosWarning {"WARNING"}
$NagiosCritical {"CRITICAL"}
$NagiosUnknown {"UNKNOWN"}
}
}
function Perfdata-Label ([string] $name) {
$name = $name -replace '[\s=\\]+', '_'
$name = $name -replace "'", ''
$name
}
function Plugin-Output ([int] $code, [string] $output) {
if ($code -gt $NagiosUnknown) { $code = $NagiosUnknown }
$state = Plugin-State-Text $code
Write-Host "${state}: $output"
foreach ($t in $args) {
if ($t -isnot [array]) {
$t = @($t)
}
foreach ($l in $t) {
Write-Host $l
}
}
}
function Plugin-Performance-Output ($perfdata, [string] $prefix = '') {
#if ($perfdata.Count -eq 0) { return }
$text = ""
if ($prefix -eq '') { $text += "|" }
foreach ($key in $perfdata.Keys) {
$label = Perfdata-Label $key
$value = $perfdata[$key]
if ($prefix) {
$label = "${prefix}::${label}"
}
if ($value -is [string] -or $value -is [int]) {
$text += " '${label}'=${value}"
} elseif ($value -is [hashtable]) {
$text += Plugin-Performance-Output $value $label
} else {
# ignoring invalid perfvalue
}
}
if ($prefix) {
return $text
} else {
Write-Host $text
}
}
# Exit the plugin in a Nagios Plugin way
function Plugin-Exit ([int] $code, [string] $output) {
if ($output) {
Plugin-Output $code $output @args
}
exit $code
}