-
Notifications
You must be signed in to change notification settings - Fork 2
/
Invoke-NagiosXiApi.ps1
152 lines (123 loc) · 4.57 KB
/
Invoke-NagiosXiApi.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<#
.SYNOPSIS
Invoke-NagiosXiApi is a wrapper script for interacting with the Nagios XI
REST API.
.DESCRIPTION
Invoke-NagiosXiApi is a wrapper script for interacting with the Nagios XI
REST API. With the Nagios XI API you can get a host and service status as
well as a number of other data. If your Nagios XI account has admin access
you can perform other actions through the API such as configuration management
and reading the Nagios XI system status.
Requirements:
* Nagios XI 5 or higher
* Nagios XI Url
* Nagios XI account with API access
* Nagios XI API key - Each user is generated a unique API key in the Admin,
Manage Users section of Nagios XI.
.NOTES
Created by: Jason Wasser @wasserja
Modified: 4/11/2017 09:07:56 AM
Version 0.5
.PARAMETER NagiosXiApiUrl
The Url of the Nagios XI API. Example: https://nagiosxi.domain.com/nagiosxi/api/v1/
.PARAMETER NagiosXiApiKey
Each Nagios XI user is generated a unique API key which can be used with the
API. Only API keys associated with admin accounts are allowed to perform system and
configuration actions. Read-only accounts can read object status.
It is recommended that you create a read-only account to pre-populate the NagiosXiApiKey
default value in the MrANagios.psm1 script module.
.PARAMETER Resource
The resource is the specific resource and action to take. See the Nagios XI API
documentation for valid resources.
.PARAMETER Method
The REST method you wish to perform (i.e. GET, POST, DELETE). See Nagios XI API
documentation.
.PARAMETER Query
Use the Query parameter to get specific hosts or services.
.EXAMPLE
Invoke-NagiosXiApi -Resource objects/host
hostlist
--------
@{recordcount=331; host=System.Object[]}
Return a list of all host objects for Nagios XI in JSON format.
.EXAMPLE
Invoke-NagiosXiApi -Resource objects/servicestatus
servicestatuslist
-----------------
@{recordcount=2389; servicestatus=System.Object[]}
Return a list of all services and their status in JSON format.
.EXAMPLE
Invoke-NagiosXiApi -NagiosXiApiKey 'j2k35j123k5j1k351' -Query 'host_name=SERVER01'
servicestatuslist
-----------------
@{recordcount=10; servicestatus=System.Object[]}
Return the status of the services on host named SERVER01 with a specified API key.
.EXAMPLE
Invoke-NagiosXiApi -Resource objects/servicestatus -Query 'host_name=in:SERVER01,SERVER02'
servicestatuslist
-----------------
@{recordcount=20; servicestatus=System.Object[]}
Return the status of services on hosts SERVER01 and SERVER02.
.EXAMPLE
Invoke-NagiosXiApi -Resource objects/servicestatus -Query 'host_name=lk:SERVER'
servicestatuslist
-----------------
@{recordcount=2389; servicestatus=System.Object[]}
Return the status of services of hosts with SERVER in the name.
#>
function Invoke-NagiosXiApi {
[CmdletBinding()]
[Alias()]
Param
(
[string]$NagiosXiApiUrl,
[string]$NagiosXiApiKey,
[string]$Resource = 'objects/host',
[string]$Method = 'Get',
[string]$Query
)
Begin {
$ValidResources = @(
'system/applyconfig'
'system/importconfig'
'system/status'
'system/user'
'config/host'
'config/service'
'config/hostgroup'
'config/servicegroup'
'objects/hoststatus'
'objects/servicestatus'
'objects/logentries'
'objects/statehistory'
'objects/comment'
'objects/downtime'
'objects/contact'
'objects/host'
'objects/service'
'objects/hostgroup'
'objects/servicegroup'
'objects/contactgroup'
'objects/hostgroupmembers'
'objects/servicegroupmembers'
'objects/contactgroupmembers'
'objects/rrdexport'
'objects/cpexport'
)
if ($ValidResources -contains $Resource) {
Write-Verbose "Valid Resource, continuing."
}
else {
Write-Verbose 'invalid resource'
return
}
}
Process {
Write-Verbose 'Invoking Nagios XI REST API'
$Uri = $NagiosXiApiUrl + $Resource + '/?apikey=' + $NagiosXiApiKey + '&' + $Query
Write-Verbose "Uri $Uri"
Invoke-RestMethod -Method $Method -Uri $Uri
}
End {
}
}