-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathInvoke-NagiosRequest.ps1
314 lines (267 loc) · 12.7 KB
/
Invoke-NagiosRequest.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
<#
.SYNOPSIS
This script allows the user execute cgi commands to a Nagios site by using
the Invoke-WebRequest cmdlet.
.DESCRIPTION
The purpose of the script is to automate the process of disabling/enabling
nagios notifications and/or checks for Nagios hosts and their services.
The script utilizes Invoke-WebRequest to post to the nagios cmd.cgi.
.PARAMETER ComputerName
what nagios refers to as host for which you wish to
enable/disable checks and notifications. Hosts are case-sensitive, but the
code actually changes the host to uppercase.
.PARAMETER Action
Integer number of nagios cmd.cgi for the action you wish to take.
Disable checks of all services on this host
$action=16
Enable checks of all services on this host
$action=15
Disable notifications for all services on this host
$action=29
Enable notifications for all services on this host
$action=28
Acknowledge service problem
$action=34
Force service check
$action=7
.PARAMETER NagiosCoreUrl
The base url of your nagios installation (i.e. http://nagios.domain.com/nagios)
.PARAMETER Credential
Provide a PSCredential object containing a valid username and password to perform the requested action.
.NOTES
Author: Jason Wasser @wasserja
Modified: 6/20/2017
Version: 1.9
Currently the script only supports enabling/disabling of active checks and
notifications.
Changelog:
version 1.8
* Added Disable-NagiosServiceNotifications and Enable-NagiosServiceNotifications
* Added Disable-NagiosGlobalNotifications and Enable-NagiosGlobalNotifications
version 1.7
* Added logic for hostgroups and service groups
version 1.6
* Converted to Functions and script module
version 1.52
* Added host problem acknowledgement.
version 1.51
* Added logic for user not entering a password.
version 1.5
* Added service problem acknowledgement.
* Added force service check.
* Added disabling/enabling service checks for host groups
* Added disabling/enabling service checks for service groups.
version 1.0
* Initial re-write of Set-NagiosCLI.ps1 now using Invoke-WebRequest.
Future developments could include scheduled downtimes.
Known Issues:
* To run the script as a scheduled task as a service account will require running
Internet Explorer once as the user.
.EXAMPLE
Invoke-NagiosRequest.ps1 -computername server1 -action 29 -NagiosCoreUrl http://nagios.domain.com/nagios -username nagiosadmin
This will disable notifications for all services on host server1 including the host.
.EXAMPLE
Invoke-NagiosRequest.ps1 -computername server1 -action 28 -NagiosCoreUrl http://nagios.domain.com/nagios -username nagiosadmin
This will enable notifications for all services on host server1 including the host.
.EXAMPLE
Invoke-NagiosRequest.ps1 -computername server1 -action 16 -NagiosCoreUrl http://nagios.domain.com/nagios -username nagiosadmin
This will disable checks for all services on host server1 including the host.
.EXAMPLE
Invoke-NagiosRequest.ps1 -computername server1 -action 15 -NagiosCoreUrl http://nagios.domain.com/nagios -username nagiosadmin
This will enable checks for all services on host server1 including the host.
.EXAMPLE
Invoke-NagiosRequest.ps1 -computername (get-content c:\temp\computerlist.txt) -action 29 -NagiosCoreUrl http://nagios.domain.com/nagios -username nagiosadmin
This will disable notifications for a list of computers found in the c:\temp\computerlist.txt file.
#>
#requires -version 3.0
Function Invoke-NagiosRequest {
[CmdletBinding(DefaultParameterSetName='ByHost')]
Param
(
# Nagios Host
[Parameter(Mandatory=$false,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Position=0,
HelpMessage = "What nagios refers to host(s) for which you wish to enable/disable checks and notifications.
Nagios is case-sensitive for hosts (i.e. server01 != SERVER01).",
ParameterSetName='ByHost')]
[alias('host')]
[string[]]$ComputerName,
# Nagios cmd.cgi action by number
[Parameter(Mandatory=$true,Position=1,
HelpMessage = "Integer of nagios cmd.cgi for the action you wish to take
Disable checks of all services on this host
action=16
Enable checks of all services on this host
action=15
Disable notifications for a service on this host
action=23
Enable notifications for a service on this host
action=22
Disable notifications for all services on this host
action=29
Enable notifications for all services on this host
action=28
Acknowledge host alert
action=33
Acknowledge service alert
action=34
Force service check
action=7
Disable checks of all services on host group
action=68
Enable checks of all services on host group
action=67
Disable checks of all services in a service group
action=114
Enable checks of all services in a service group
action=113
")]
[ValidateSet(7,11,12,15,16,22,23,28,29,33,34,67,68,113,114)]
[int]$Action,
[Parameter(Mandatory=$false)]
[string]$Service,
[Parameter(Mandatory=$false)]
[string]$ServiceGroup,
[Parameter(Mandatory=$false)]
[string]$Comment,
[Parameter(Mandatory=$false)]
[string]$HostGroup,
# Nagios base url
[Parameter(Mandatory=$false,Position=2,HelpMessage="The base url of your nagios installation (i.e. http://nagios.domain.com/nagios)")]
[string]$NagiosCoreUrl,
# Nagios Credential
[Parameter(Mandatory=$true)]
[System.Management.Automation.PSCredential]$Credential
)
Begin
{
# Function for making the actual CGI POST command.
Function Submit-NagiosRequest {
Param (
[string]$NagiosObject
)
Write-Output "###########################################################################"
Write-Output "Submitting cgi command to Nagios for $NagiosObject"
$WebRequest = Invoke-WebRequest -Uri $uri -Credential $Credential -Body $formFields -Method POST -ContentType 'application/x-www-form-urlencoded'
# If there was a problem with the hostname or other problem the errorMessage DIV field will be displayed. If not display the infoMessage of success.
$Message = $WebRequest.ParsedHtml.getElementsByTagName("div") | Where-Object "classname" -Match "errorMessage|infoMessage" | Select-Object -ExpandProperty innerText
if ($Message) {
$Message
}
Write-Output "###########################################################################"
}
# Building URI for Nagios CGI
$cgiurl="/cgi-bin/cmd.cgi"
$uri = $NagiosCoreUrl + $cgiurl
}
Process
{
# Here we need to separate out the Nagios commands that potential
# loop through a list of nagios hosts for the enabling/disabling
# of checks and notifications from the other commands such as host
# groups, service groups, acknowledgements, and future development.
switch -Regex ($action) {
# List of action integers that are not computer/host based
"67|68" {
foreach ($hg in $hostgroup) {
switch ($action) {
# Enable checks of all services on host group
67 {
if (!$hg) {
$hg = Read-Host "Please enter the hostgroup (case-sensitive)"
}
$formFields = @{cmd_typ=$action;hostgroup=$hg;ahas=$true;cmd_mod=2}
}
# Disable checks of all services on host group
68 {
if (!$hg) {
$hg = Read-Host "Please enter the hostgroup (case-sensitive)"
}
$formFields = @{cmd_typ=$action;hostgroup=$hg;ahas=$true;cmd_mod=2}
}
}
Submit-NagiosRequest -NagiosObject $hg
}
}
"^113$|^114$" {
foreach ($sg in $servicegroup) {
switch ($action) {
# Enable checks of all services in a service group
113 {
if (!$sg) {
$sg = Read-Host "Please enter the service group (case-sensitive)"
}
$formFields = @{cmd_typ=$action;servicegroup=$sg;ahas=$false;cmd_mod=2}
}
# Disable checks of all services in a service group
114 {
if (!$sg) {
$sg = Read-Host "Please enter the service group (case-sensitive)"
}
$formFields = @{cmd_typ=$action;servicegroup=$sg;ahas=$false;cmd_mod=2}
}
}
Submit-NagiosRequest -NagiosObject $sg
}
}
"^11$|^12$" {
Write-Verbose "Enabling/Disabling Global Nagios Notifications"
$formFields = @{cmd_typ=$action;cmd_mod=2}
Submit-NagiosRequest -NagiosObject 'GlobalNotifications'
}
default {
# For all other host/computer commands.
if (!$ComputerName) {
$ComputerName = Read-Host "Please enter a Nagios host name (case-sensitive)"
}
foreach ($computer in $ComputerName) {
$computer = $computer.ToUpper()
switch -Regex ($action) {
# Acknowledge Host Problesm
33 {
if (!$comment){
$comment = Read-Host "Please enter a comment for acknowledgement"
}
$formFields = @{cmd_typ=$action;host=$computer;service=$service;cmd_mod=2;com_data=$comment;sticky_ack=$true;send_notification=$true}
}
# Acknowledge Service Problem
34 {
if (!$service) {
$service = Read-Host "Please enter the service name (case-sensitive)"
}
if (!$comment){
$comment = Read-Host "Please enter a comment for acknowledgement"
}
$formFields = @{cmd_typ=$action;host=$computer;service=$service;cmd_mod=2;com_data=$comment;sticky_ack=$true;send_notification=$true}
}
# Force service check
7 {
if (!$service) {
$service = Read-Host "Please enter the service name (case-sensitive)"
}
$formFields = @{cmd_typ=$action;host=$computer;service=$service;cmd_mod=2;start_time=(Get-Date -Format "MM-dd-yyyy HH:mm:ss");force_check=$true}
}
"22|23" {
if (!$service) {
$service = Read-Host "Please enter the service name (case-sensitive)"
}
$formFields = @{cmd_typ=$action;host=$computer;service=$service;cmd_mod=2}
}
# All other commands for enabling/disabling checks or notifications for hosts
default {
$formFields = @{cmd_typ=$action;host=$computer;ahas=$true;cmd_mod=2}
}
}
Submit-NagiosRequest -NagiosObject $computer
}
}
}
}
End
{
}
}
# End of Invoke-NagiosRequest Function
########################################################################################################