-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGet-ResourceInfoPRTG.ps1
435 lines (317 loc) · 14.7 KB
/
Get-ResourceInfoPRTG.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#Requires -Version 3.0
#Requires -PSEdition Desktop
<#
.SYNOPSIS
This script is a custom PRTG sensor. It can be used to return information for a devices Disk, Processor/CPU, and Memory information
.DESCRIPTION
This script returns information for a devices disk, memory and processor/CPU and uses the defined threshold values to create PRTG notifications for each result
.PARAMETER ComputerName
Specifies the name of the computer to which to create the CIM session. Specify either a single computer name, or multiple computer names separated by a comma.
If ComputerName is not specified, a CIM session to the local computer is created.
You can specify the value for computer name in one of the following formats:
- One or more NetBIOS names
- One or more IP addresses
- One or more fully qualified domain names.
If the computer is in a different domain than the user, you must specify the fully qualified domain name.
You can also pass a computer name (in quotes) to `New-CimSession` by using the pipeline.
.PARAMETER ResourceMonitors
Specify all of the resource monitors you wish to monitor with this sensor separating multiple values with a comma
.PARAMETER LowDiskSpaceWarningThreshold
Define the Warning Threshold for your Low Disk Space monitor
.PARAMETER LowDiskSpaceCriticalThreshold
Define the Critical Threshold for your Low Disk Space monitor
.PARAMETER CpuUsageWarningThreshold
Define the Warning Threshold for your CPU Utilization monitor
.PARAMETER CpuUsageCriticalThreshold
Define the Critical Threshold for your CPU Utilization monitor
.PARAMETER MemoryWarningThreshold
Define the Warning Threshold for your Memory Usage monitor
.PARAMETER MemoryCriticalThreshold
Define the Critical Threshold for your Memory Usage monitor
.PARAMETER UseSSL
Specify the use of WinRM over HTTPS (port 5986) to build CIM sessions instead of WinRM (port 5985)
.PARAMETER SkipCACheck
Indicates that when connecting over HTTPS, the client does not validate that the server certificate is signed by a trusted certification authority (CA).
Use this parameter only when the remote computer is trusted using another mechanism, such as when the remote computer is part of a network that is physically secure and isolated, or when the remote computer is listed as a trusted host
in a WinRM configuration.
.PARAMETER SkipCNCheck
Indicates that the certificate common name (CN) of the server does not need to match the hostname of the server.
Use this parameter only for remote operations that use the HTTPS protocol.
Note: use this parameter only for trusted computers.
.PARAMETER SkipRevocationCheck
Indicates that the revocation check for server certificates is skipped.
Note: use this parameter only for trusted computers.
.NOTES
Author: Robert H. Osborne
Alias: tobor
Contact: [email protected]
.LINK
https://www.paessler.com/prtg
https://www.paessler.com/manuals/prtg/custom_sensors
https://osbornepro.com
https://writeups.osbornepro.com
https://btpssecpack.osbornepro.com
https://github.com/tobor88
https://gitlab.com/tobor88
https://www.powershellgallery.com/profiles/tobor
https://www.linkedin.com/in/roberthosborne/
https://www.credly.com/users/roberthosborne/badges
https://www.hackthebox.eu/profile/52286
.INPUTS
System.String
.OUTPUTS
System.String
#>
[OutputType([System.String])]
[CmdletBinding(DefaultParameterSetName="WinRM")]
param (
[Parameter(
Position=0,
Mandatory=$True,
ValueFromPipeline=$True,
ValueFromPipeLineByPropertyName=$False,
HelpMessage="[H] Enter the name of the device you are reaching out too `n[E] EXAMPLE: servername.domainname.com "
)] # End Parameter
[Alias("Device","Server")] # <---- The value 'Device' is required for PRTG to pass variable to cmdlet. Using -ComputerName is considered a best practice
[String]$ComputerName,
[Parameter(
Mandatory=$False,
ValueFromPipeline=$False,
ValueFromPipeLineByPropertyName=$False
)] # End Parameter
# OPTIONS: "CPULoad", "CPUCount", "DiskFreePercentage", "DiskFreeGigabytes", "TotalDisks", "MemoryFreePercentage", "MemoryFreeGigaByte"
[String[]]$ResourceMonitors = @("CPULoad", "CPUCount", "DiskFreePercentage", "DiskFreeGigabytes", "TotalDisks", "MemoryFreePercentage", "MemoryFreeGigaByte"),
[Parameter(
Mandatory=$False,
ValueFromPipeline=$False,
ValueFromPipeLineByPropertyName=$False
)] # End Parameter
[ValidateRange(1,100)]
[Int]$LowDiskSpaceWarningThreshold = 20,
[Parameter(
Mandatory=$False,
ValueFromPipeline=$False,
ValueFromPipeLineByPropertyName=$False
)] # End Parameter
[ValidateRange(1,100)]
[Int]$LowDiskSpaceCriticalThreshold = 10,
[Parameter(
Mandatory=$False,
ValueFromPipeline=$False,
ValueFromPipeLineByPropertyName=$False
)] # End Parameter
[ValidateRange(1,100)]
[Int]$CpuUsageWarningThreshold = 85,
[Parameter(
Mandatory=$False,
ValueFromPipeline=$False,
ValueFromPipeLineByPropertyName=$False
)] # End Parameter
[ValidateRange(1,100)]
[Int]$CpuUsageCriticalThreshold = 95,
[Parameter(
Mandatory=$False,
ValueFromPipeline=$False,
ValueFromPipeLineByPropertyName=$False
)] # End Parameter
[ValidateRange(1,100)]
[Int]$MemoryWarningThreshold = 85,
[Parameter(
Mandatory=$False,
ValueFromPipeline=$False,
ValueFromPipeLineByPropertyName=$False
)] # End Parameter
[ValidateRange(1,100)]
[Int]$MemoryCriticalThreshold = 95,
[Parameter(
ParameterSetName="WinRMoverHTTPS",
Mandatory=$False
)] # End Parameter
[Switch]$UseSSL,
[Parameter(
ParameterSetName="WinRMoverHTTPS",
Mandatory=$False
)] # End Parameter
[Switch]$SkipCACheck,
[Parameter(
ParameterSetName="WinRMoverHTTPS",
Mandatory=$False
)] # End Parameter
[Switch]$SkipCNCheck,
[Parameter(
ParameterSetName="WinRMoverHTTPS",
Mandatory=$False
)] # End Parameter
[Switch]$SkipRevocationCheck,
[Parameter(
Mandatory=$False
)] # End Parameter
[Switch]$IgnoreWarningMessage
) # End param
BEGIN {
If ($ResourceMonitors.Count -gt 5 -and (!($IgnoreWarningMessage.IsPresent))) {
Write-Warning -Message "[!] PRTG states that the fewer XML results returned, the better the chance they do not get convoluted during data transfer back to PRTG. PRTG claims a max of 50 results but they do not gurantee them"
} # End If
} PROCESS {
Write-Verbose -Message "[v] Evaluating whether to create a CIM session"
If ($ComputerName.Split('.')[0].ToUpper() -like $Using:Env:COMPUTERNAME) {
Write-Verbose -Message "[v] Returing information from local device"
$OS = Get-CimInstance -ClassName "Win32_OperatingSystem"
$CPUs = Get-CimInstance -ClassName "Win32_Processor"
$Disks = Get-CimInstance -ClassName "Win32_LogicalDisk" -Filter "DriveType=3"
} Else {
Write-Verbose -Message "[v] Defining CIM Session options"
$CimSessionOptions = New-CimSessionOption -UseSsl:$UseSSL.IsPresent -SkipRevocationCheck:$SkipRevocationCheck.IsPresent -SkipCACheck:$SkipCACheck.IsPresent -SkipCNCheck:$SkipCNCheck.IsPresent
Write-Verbose -Message "[v] Attempting to establish the CIM session for $ComputerName"
$CIMSession = New-CimSession -ComputerName $ComputerName -SessionOption $CimSessionOptions
Write-Verbose -Message "[v] Using CIM Session to return information"
$OS = Get-CimInstance -CimSession $CIMSession -ClassName "Win32_OperatingSystem"
$CPUs = Get-CimInstance -CimSession $CIMSession -ClassName "Win32_Processor"
$Disks = Get-CimInstance -CimSession $CIMSession -ClassName "Win32_LogicalDisk" -Filter "DriveType=3"
} # End If Else
Write-Verbose -Message "[v] Building XML formatted values using the returned resource information"
If ($ResourceMonitors -contains "CPULoad") {
ForEach ($CPU in $CPUs) {
$CPUsTotal += 1
$CpuLoadXml += "<Result>
<Channel>CPU Load #$CPUsTotal</Channel>
<Value>" + $CPU.LoadPercentage + "</Value>
<Unit>Percent</Unit>
<LimitWarningMsg>WARNING: CPU is at or above $CpuUsageWarningThreshold% capacity</LimitWarningMsg>
<LimitErrorMsg>CRITICAL: CPU is at or above $CpuUsageCriticalThreshold% capacity</LimitErrorMsg>
<LimitMaxError>$CpuUsageCriticalThreshold.00</LimitMaxError>
<LimitMaxWarning>$CpuUsageWarningThreshold.00</LimitMaxWarning>
<LimitMode>1</LimitMode>
</Result>"
} # End ForEach
} Else {
Write-Verbose -Message "[v] CPU Load monitoring was not specified"
} # End If Else
If ($ResourceMonitors -contains "CPUCount") {
$CpuTotalXml ="<Result>
<Channel>Total CPUs</Channel>
<Value>" + $CPUsTotal + "</Value>
<Unit>Count</Unit>
</Result>"
} Else {
Write-Verbose -Message "[v] CPU Total Count monitor was not specified"
} # End If Else
If ($ResourceMonitors -contains "DiskFreePercentage" -or $ResourceMonitors -contains "DiskFreeGigabytes") {
Write-Verbose -Message "[v] Building XML for the returned disk usage information"
ForEach ($Disk in $Disks) {
$TotalDriveSpace = $Disk.Size
$FreeDriveSpace = $Disk.FreeSpace
$DrivePercentageFree = [Math]::Round(($Disk.FreeSpace/$Disk.Size)*100,2).ToString("###")
$DisksTotal += 1
If ($ResourceMonitors -contains "DiskFreeGigabytes") {
$DiskUsageXml += "<Result>
<Channel>Total Space Drive " + $Disk.DeviceID + "</Channel>
<Value>" + $TotalDriveSpace + "</Value>
<VolumeSize>GigaByte</VolumeSize>
</Result>"
$DiskUsageXml += "<Result>
<Channel>Free Space Drive " + $Disk.DeviceID + "</Channel>
<Value>" + $FreeDriveSpace + "</Value>
<VolumeSize>GigaByte</VolumeSize>
</Result>"
} Else {
Write-Verbose -Message "[v] Disk Free in Gigabytes monitor was not specified"
} # End If Else
If ($ResourceMonitors -contains "DiskFreePercentage") {
If ($Disk.DeviceID -like 'C:') {
$DiskUsageXml += "<Result>
<Channel>Percentage Free on Drive " + $Disk.DeviceID + "</Channel>
<Value>" + $DrivePercentageFree + "</Value>
<Unit>Percent</Unit>
<LimitWarningMsg>WARNING: Disk Space Free is at or below $LowDiskSpaceWarningThreshold% capacity</LimitWarningMsg>
<LimitErrorMsg>CRITICAL: Available Disk space is at or has fallen below $LowDiskSpaceCriticalThreshold% availability</LimitErrorMsg>
<LimitMinError>$LowDiskSpaceCriticalThreshold.00</LimitMinError>
<LimitMinWarning>$LowDiskSpaceWarningThreshold.00</LimitMinWarning>
<LimitMode>1</LimitMode>
</Result>"
} Else {
$DiskUsageXml += "<Result>
<Channel>Percentage Free on Drive " + $Disk.DeviceID + "</Channel>
<Value>" + $DrivePercentageFree + "</Value>
<Unit>Percent</Unit>
</Result>"
} # End Else
} Else {
Write-Verbose -Message "[v] Disk Space Percentage monitor was not specified"
} # End If Else
} # End ForEach
If ($ResourceMonitors -contains "TotalDisks") {
$DiskTotalToXml ="<Result>
<Channel>Total Disks</Channel>
<Value>" + $DisksTotal + "</Value>
<Unit>Count</Unit>
</Result>"
} Else {
Write-Verbose -Message "[v] Total Disk monitor was not specified"
} # End If Else
} Else {
Write-Verbose -Message "[v] No Disk Monitoring options were selected"
} # End If Else
Write-Verbose -Message "[v] Building XML values for returned Memory usage information"
$TotalMemory = ($OS.TotalVisibleMemorySize * 1KB)
If ($ResourceMonitors -contains "MemoryFreePercentage") {
$MemoryFreePercentage = [Math]::Round(($OS.FreePhysicalMemory/$OS.TotalVisibleMemorySize)*100,2)
$MemoryInUsePercentage = [Math]::Round(100-(($OS.FreePhysicalMemory/$OS.TotalVisibleMemorySize)*100),2)
$MemoryPercentageXml = "<Result>
<Channel>Total Memory</Channel>
<Value>" + $TotalMemory + "</Value>
<VolumeSize>GigaByte</VolumeSize>
<LimitErrorMsg>ERROR: Connection to $ComputerName could not be established</LimitErrorMsg>
<LimitMinError>1</LimitMinError>
<LimitMode>1</LimitMode>
</Result>
<Result>
<Channel>Memory Free Percentage</Channel>
<Value>" + $MemoryFreePercentage + "</Value>
<Float>1</Float>
<Unit>Percent</Unit>
</Result>
<Result>
<Channel>Memory In Use Percentage</Channel>
<Value>" + $MemoryInUsePercentage + "</Value>
<Float>1</Float>
<Unit>Percent</Unit>
<LimitWarningMsg>WARNING: CPU is at or above $MemoryWarningThreshold% capacity</LimitWarningMsg>
<LimitErrorMsg>CRITICAL: CPU is at or above $MemoryCriticalThreshold% capacity</LimitErrorMsg>
<LimitMaxError>$MemoryCriticalThreshold.00</LimitMaxError>
<LimitMaxWarning>$MemoryWarningThreshold.00</LimitMaxWarning>
<LimitMode>1</LimitMode>
</Result>"
} Else {
Write-Verbose -Message "[v] Memory Free Percentage monitor was not specified"
} # End If Else
If ($ResourceMonitors -contains "MemoryFreeGigaByte") {
$MemoryFree = [Math]::Round($OS.FreeVirtualMemory / 1MB, 3)
$MemoryInUse = [Math]::Round(($OS.TotalVisibleMemorySize - $OS.FreeVirtualMemory) / 1MB, 3)
$MemoryFreeGbXml = "<Result>
<Channel>Memory Free</Channel>
<Value>" + $MemoryFree + "</Value>
<VolumeSize>GigaByte</VolumeSize>
<Float>1</Float>
</Result>
<Result>
<Channel>Memory In Use</Channel>
<Value>" + $MemoryInUse + "</Value>
<VolumeSize>GigaByte</VolumeSize>
<Float>1</Float>
</Result>"
} Else {
Write-Verbose -Message "[v] Memory Usage in Gigabytes monitor was not specified"
} # End If Else
$Xml="<PRTG>
$CpuTotalXml
$CpuLoadXml
$DiskTotalToXml
$DiskUsageXml
$MemoryFreeGbXml
$MemoryPercentageXml
</PRTG>
"
} END {
Return $Xml
} # End BPE