-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPRTG-VMWare-Alerts.ps1
298 lines (242 loc) · 9.14 KB
/
PRTG-VMWare-Alerts.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
<#
.SYNOPSIS
Monitors VMWare Alarms and Warnings
.DESCRIPTION
Using VMware PowerCLI this Script checks VMware Alerts and Warnings
Exceptions can be made within this script by changing the variable $AlarmIgnoreScript or $VMIgnoreScript. This way, the change applies to all PRTG sensors
based on this script. If exceptions have to be made on a per sensor level, the script parameter $VMIgnorePattern or $AlarmIgnorePattern can be used.
Copy this script to the PRTG probe EXEXML scripts folder (${env:ProgramFiles(x86)}\PRTG Network Monitor\Custom Sensors\EXEXML)
and create a "EXE/Script Advanced. Choose this script from the dropdown and set at least:
+ Parameters: VCenter, Username, Password
+ Scanning Interval: minimum 5 minutes
.PARAMETER ViServer
The Hostname of the VCenter Server
.PARAMETER User
Provide the VCenter Username
.PARAMETER Password
Provide the VCenter Password
.PARAMETER VMIgnorePattern
Regular expression to describe the VMs to Ignore Alerts and Warnings
.PARAMETER AlarmIgnorePattern
Regular expression to describe the Alert to Ignore
Example: ^(DemoTestServer|DemoAusname2)$
Example2: ^(Test123.*|Test555)$ excludes Test123, Test1234, Test12345 and Test555
#https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_regular_expressions?view=powershell-7.1
.EXAMPLE
Sample call from PRTG EXE/Script Advanced
PRTG-VMware-Alerts.ps1 -ViServer "%VCenterName%" -User "%Username%" -Password "%PW%" -AlarmIgnorePattern '(vSphere Health detected new issues in your environment)'
.NOTES
This script is based on the sample by Paessler (https://kb.paessler.com/en/topic/70174-monitor-vcenter)
Author: Jannos-443
https://github.com/Jannos-443/PRTG-VMware-Alerts
#>
param(
[string] $ViServer = "",
[string] $User = "",
[string] $Password = "",
[string] $VMIgnorePattern = "", #VM Objekt to ignore
[string] $AlarmIgnorePattern = "" #Alarm Message to ignore
)
#Catch all unhandled Errors
trap{
if($connected)
{
$null = Disconnect-VIServer -Server $ViServer -Confirm:$false -ErrorAction SilentlyContinue
}
$Output = "line:$($_.InvocationInfo.ScriptLineNumber.ToString()) char:$($_.InvocationInfo.OffsetInLine.ToString()) --- message: $($_.Exception.Message.ToString()) --- line: $($_.InvocationInfo.Line.ToString()) "
$Output = $Output.Replace("<","")
$Output = $Output.Replace(">","")
$Output = $Output.Replace("#","")
Write-Output "<prtg>"
Write-Output "<error>1</error>"
Write-Output "<text>$Output</text>"
Write-Output "</prtg>"
Exit
}
[int] $Warnings = 0
[int] $WarningsNotAck = 0
[int] $WarningsAck = 0
[int] $Alerts = 0
[int] $AlertsNotAck = 0
[int] $AlertsAck = 0
[String] $WarningsText = ""
[String] $AlertsText = ""
#https://stackoverflow.com/questions/19055924/how-to-launch-64-bit-powershell-from-32-bit-cmd-exe
#############################################################################
#If Powershell is running the 32-bit version on a 64-bit machine, we
#need to force powershell to run in 64-bit mode .
#############################################################################
if ($env:PROCESSOR_ARCHITEW6432 -eq "AMD64")
{
if ($myInvocation.Line)
{
[string]$output = &"$env:WINDIR\sysnative\windowspowershell\v1.0\powershell.exe" -NonInteractive -NoProfile $myInvocation.Line
}
else
{
[string]$output = &"$env:WINDIR\sysnative\windowspowershell\v1.0\powershell.exe" -NonInteractive -NoProfile -file "$($myInvocation.InvocationName)" $args
}
#Remove any text after </prtg>
try{
$output = $output.Substring(0,$output.LastIndexOf("</prtg>")+7)
}
catch
{
}
Write-Output $output
exit
}
#############################################################################
#End
#############################################################################
$connected = $false
# Import VMware PowerCLI module
$ViModule = "VMware.VimAutomation.Core"
try {
Import-Module $ViModule -ErrorAction Stop
} catch {
Write-Output "<prtg>"
Write-Output " <error>1</error>"
Write-Output " <text>Error Loading VMware Powershell Module ($($_.Exception.Message))</text>"
Write-Output "</prtg>"
Exit
}
# PowerCLI Configuration Settings
try
{
#Ignore certificate warnings
Set-PowerCLIConfiguration -InvalidCertificateAction ignore -Scope User -Confirm:$false | Out-Null
#Disable CEIP
Set-PowerCLIConfiguration -ParticipateInCeip $false -Scope User -Confirm:$false | Out-Null
}
catch
{
Write-Host "Error in Set-PowerCLIConfiguration but we will ignore it." #Error when another Script is currently accessing it.
}
# Connect to vCenter
try {
Connect-VIServer -Server $ViServer -User $User -Password $Password -ErrorAction Stop | Out-Null
$connected = $true
}
catch
{
Write-Output "<prtg>"
Write-Output " <error>1</error>"
Write-Output " <text>Could not connect to vCenter server $ViServer. Error: $($_.Exception.Message)</text>"
Write-Output "</prtg>"
Exit
}
# Get a list of all Alarms
try {
#$Alarms = (Get-Inventory -Server $ViServer -ErrorAction Stop).ExtensionData.TriggeredAlarmState
$Alarms = (Get-Folder -Type "Datacenter" -Server $ViServer -ErrorAction Stop).ExtensionData.TriggeredAlarmState
} catch {
Write-Output "<prtg>"
Write-Output " <error>1</error>"
Write-Output " <text>Could not Get-Inventory. Error: $($_.Exception.Message)</text>"
Write-Output "</prtg>"
Exit
}
#Filter Alarms
# hardcoded list that applies to all hosts
$AlarmIgnoreScript = '^(TestIgnore)$'
$VMIgnoreScript = '^(TestIgnore)$'
if($VMIgnorePattern -eq "")
{
$VMIgnorePattern = '^()$'
}
if($AlarmIgnorePattern -eq "")
{
$AlarmIgnorePattern = '^()$'
}
$xmlOutput = '<prtg>'
if ($Alarms.Count -gt 0) {
foreach ($Alarm in $Alarms) {
$path = (get-view $alarm.Entity -Server $ViServer).Name #Alarm Objekt
$name = ((get-view $alarm.Alarm -Server $ViServer).info).name #Alarm Name
#check ignore variables
if(($name -match $AlarmIgnoreScript) -or ($name -match $AlarmIgnorePattern) -or ($path -match $VMIgnoreScript) -or ($path -match $VMIgnorePattern))
{
#ignored
}
#if not ignored
else
{
if ($Alarm.OverallStatus -eq "yellow") {
$Warnings += 1
if ($Alarm.Acknowledged)
{
$WarningsAck += 1
}
else
{
$WarningsNotAck +=1
$WarningsText += "$($path) - $($name) # "
}
}
elseif ($Alarm.OverallStatus -eq "red") {
$Alerts += 1
if ($Alarm.Acknowledged)
{
$AlertsAck += 1
}
else
{
$AlertsNotAck += 1
$AlertsText += "$($path) - $($name) # "
}
}
}
}
}
# Output Text
$OutputText =""
if($AlertsNotAck -gt 0)
{
$OutputText += "Alerts: $($AlertsText)"
}
if($WarningsNotAck -gt 0)
{
$OutputText += "Warnings: $($WarningsText)"
}
if(($WarningsNotAck -gt 0) -or ($AlertsNotAck -gt 0))
{
$OutputText = $OutputText.Replace("<","")
$OutputText = $OutputText.Replace(">","")
$OutputText = $OutputText.Replace("#","")
#The number sign (#) is not supported in sensor messages. If a message contains a number sign, the message is clipped at this point - https://www.paessler.com/manuals/prtg/custom_sensors
$xmlOutput = $xmlOutput + "<text>$OutputText</text>"
}
else
{
$xmlOutput = $xmlOutput + "<text>No not Acknowledged Alarms or Warnings</text>"
}
# Disconnect from vCenter
Disconnect-VIServer -Server $ViServer -Confirm:$false
$connected = $false
$xmlOutput = $xmlOutput + "<result>
<channel>Total Alerts - NOT Acknowledged</channel>
<value>$AlertsNotAck</value>
<unit>Count</unit>
<limitmode>1</limitmode>
<LimitMaxError>0.1</LimitMaxError>
</result>
<result>
<channel>Total Warnings - NOT Acknowledged</channel>
<value>$WarningsNotAck</value>
<unit>Count</unit>
<limitmode>1</limitmode>
<LimitMaxWarning>0.1</LimitMaxWarning>
</result>
<result>
<channel>Total Alerts - Acknowledged</channel>
<value>$AlertsAck</value>
<unit>Count</unit>
</result>
<result>
<channel>Total Warnings - Acknowledged</channel>
<value>$WarningsAck</value>
<unit>Count</unit>
</result>"
$xmlOutput = $xmlOutput + "</prtg>"
Write-Output $xmlOutput