-
Notifications
You must be signed in to change notification settings - Fork 2
/
Get-NagiosXiServiceStatus.ps1
120 lines (100 loc) · 3.47 KB
/
Get-NagiosXiServiceStatus.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
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
117
118
119
120
<#
.SYNOPSIS
Get a list of service status from Nagios XI.
.DESCRIPTION
Get a list of service status from Nagios XI using Invoke-NagiosXiApi.
All parameters have default values, but you should change your NagiosXiApiUrl and NagiosXiApiKey to match
your environment. See the documentation for Invoke-NagiosXiApi.
.PARAMETER HostName
Provide the name of a monitored service. You can also provide multiple host names. Additionally
you can use a matching keyword "lk" to search for hosts matching a specific string.
.EXAMPLE
Get-NagiosXiHostStatus
Returns the status for all hosts in Nagios.
.EXAMPLE
Get-NagiosXiHostStatus -HostName SERVER01
Returns the host status for server named server01.
.EXAMPLE
Get-NagiosXiHostStatus -HostName SERVER01,SERVER02
Returns the host status for server named server01 and server02.
.EXAMPLE
Get-NagiosXiHostStatus -Query 'lk:server'
Returns the host status for any Nagios hosts with the string server in the name.
#>
function Get-NagiosXiServiceStatus {
[CmdletBinding()]
[Alias()]
Param
(
[string]$NagiosXiApiUrl,
[string]$NagiosXiApiKey,
[string]$Resource = 'objects/servicestatus',
[string]$Method = 'Get',
[string[]]$HostName,
[string[]]$ServiceName,
[switch]$Summary
)
Begin {}
Process {
# Build Hostname list into Query
if ($HostName) {
if ($HostName.Count -gt 1) {
$Query = "host_name=in:"
foreach ($Host in $HostName) {
$Query += "$Host,"
Write-Verbose "Query $Query"
}
}
else {
$Query = "host_name=$HostName"
Write-Verbose "Query $Query"
}
}
else {
Write-Verbose 'No host name entered.'
$Query = $null
Write-Verbose "Query $Query"
}
Write-Verbose "Query $Query"
# Build ServiceName list into Query
if ($ServiceName) {
if ($ServiceName.Count -gt 1) {
# More than one service name entered.
Write-Verbose 'More than one service name entered.'
$Query += "&name=in:"
Write-Verbose "Query $Query"
# Loop through each service name to add to query.
foreach ($Service in $ServiceName) {
$Query += "$Service,"
Write-Verbose "Query $Query"
}
}
else {
# Only one service name entered.
Write-Verbose "Only one server named entered."
$Query += "&name=$ServiceName"
Write-Verbose "Query $Query"
}
}
else {
# No service name entered
Write-Verbose 'No Service name entered.'
if (!$Query) {
$Query = $null
Write-Verbose "Query $Query"
}
else {
Write-Verbose "Query $Query"
}
}
Write-Verbose "Query $Query"
$ServiceStatus = Invoke-NagiosXIApi -NagiosXiApiUrl $NagiosXiApiUrl -Resource $Resource -Method $Method -Query $Query -NagiosXiApiKey $NagiosXiApiKey
if ($Summary) {
$ServiceStatus.servicestatus | Select-Object -Property host_name, name, status_text
}
else {
$ServiceStatus.servicestatus
}
}
End {}
}