search
Categories
Sponsors
VirtualMetric Hyper-V Monitoring, Hyper-V Reporting
Archive
Blogroll

Badges
MCSE
Community

Cozumpark Bilisim Portali
Monitoring CSV Free Disk Space with PRTG
Posted in Virtual Machine Manager, Windows Powershell | 4 Comments | 17,055 views | 08/03/2011 15:04

You can not monitor CSV Free Disk Space with PRTG by default. You should use custom scripts to do that. So I decided to write a Powershell script for that. But instead of using WMI in Powershell, using Powershell commands with Remote Powershell is a lot easier. Also this is an example for you about how to execute your own Powershell scripts on remote servers. Also this is a great example for Remote Powershell functionality if you are not familiar with it.

First of all, you need my PS1 script for monitoring:

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
$HostIP = $args[0]     # 192.168.2.1
$CSVName = $args[1]    # Cluster Disk 1
 
Function Get-RemoteState
{
$RemoteState = (Get-PSSession -ComputerName $HostIP).State
}
 
Function Remove-RemoteState
{
Remove-PSSession * -EA SilentlyContinue
}
 
Function Get-PercentFree
{
Param ($Session, $CSVName)
 
	Invoke-Command -Session $Session -ArgumentList $CSVName -ScriptBlock {
	param($CSVName)
	Import-Module FailoverClusters
	$PercentFree = ((Get-ClusterSharedVolume -Name "$CSVName").SharedVolumeInfo | select -Expand Partition).PercentFree
	$PercentFree
	}
}
 
Function Get-FreeSpace
{
Param ($Session, $CSVName)
 
	Invoke-Command -Session $Session -ArgumentList $CSVName -ScriptBlock {
	param($CSVName) 
	Import-Module FailoverClusters
	$FreeSpace = ((Get-ClusterSharedVolume -Name "$CSVName").SharedVolumeInfo | select -Expand Partition).FreeSpace
	$FreeSpace
	}
}
 
If ($RemoteState -eq "Opened")
{
$Session = Get-PSSession -ComputerName $HostIP
}
else
{
Remove-RemoteState
#$Code= Get-Content C:\prtgpass.txt
#$SecurePassword = Convertto-Securestring $Code
$SecurePassword = ConvertTo-SecureString "PASSWORD_HERE" -AsPlainText -Force
$Credentials = new-object -typename System.Management.Automation.PSCredential -argumentlist "DOMAIN\Administrator", $SecurePassword
$Session = New-PSSession -Computername $HostIP -Credential $Credentials
}
 
Get-RemoteState
$PercentFree = Get-PercentFree -Session $Session -CSVName $CSVName
$PercentFree = "$PercentFree"
$PercentFree = ($PercentFree.Split(".")[0])
$PercentFree = 100 - "$PercentFree"
 
if ($PercentFree -gt "90")
{
write-host $PercentFree,":Critical"
}
Else
{
write-host $PercentFree,":OK"
}
Exit-PSSession

Now save as this script as “CSV Free Disk Space.ps1” and copy it to the “C:\Program Files (x86)\PRTG Network Monitor\Custom Sensors\EXE” folder on the PRTG -Probe- server. Only one note, you should change Domain and Password info in the script. You should write your Administrator name as “CONTOSO\Administrator”. You should write your password as clean text but I’ll work about that to find a way to encrypt it. Just change “PASSWORD_HERE” line and “DOMAN\Administrator” line in the script.

Before adding sensor on to PRTG, you should configure Remote Powershell functionality on PRTG server and on one of the Hyper-V Cluster Host.

First, go to PRTG host and execute on Powershell:

1
2
3
4
Set-ExecutionPolicy Unrestricted
Set-Item WSMan:\localhost\Client\TrustedHosts *
Restart-Service winrm
Enable-PSRemoting

One important note. If you use Windows Server 2008 R2 or Windows x64 versions, you should execute commands on both x86 version of Powershell and x64 version of Powershell.

x86 path: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
x64 path: C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe

Also you may need to disable UAC to execute Remote Powershell commands!

Then go to Hyper-V Cluster Host and execute:

Enable-PSRemoting

Now go to the PRTG and create a custom sensor for Hyper-V Cluster Host.

Only one important thing is “Parameters”. You should write exactly same things there:

'%host' 'Cluster Disk 1'

If you have more than one CSV disk, just add them as a new sensor and parameters should be:

'%host' 'Cluster Disk 2'
'%host' 'Cluster Disk 3'

After a few minutes, PRTG will start showing Free Space information.

Actually I added “Used Space” to the PRTG. You can change it as “Free Space” if you want.

There are lines in the script:

1
2
3
$PercentFree = "$PercentFree"
$PercentFree = ($PercentFree.Split(".")[0])
$PercentFree = 100 - "$PercentFree"

Just remove the last line like here:

1
2
$PercentFree = "$PercentFree"
$PercentFree = ($PercentFree.Split(".")[0])

I hope this will help you to monitor your environment.


Comments (4)

Rob

April 14th, 2011
19:24:21

Hello Yusuf,

Thanks for posting this script…I used some of the ideas from it to write my own sensor for PRTG, although not for a cluster shared volume, just a single drive. I’ve been having trouble with a particular server responding to the built-in WMI sensor. I solved the problem of authentication by changing the service that PRTG runs under to a domain account with permissions on the host, and I do not opena session with the host, I use this command to get the info I need into the variable $d: $d = Invoke-Command -computername $hostname -ScriptBlock {param($drive)New-Object -typename System.IO.DriveInfo -argumentlist $drive} -argumentlist $DLetter

Also the $hostname var should be the host name and not IP address…you can use the PRTG variable “%host” if you use DNS names for your hosts and not IP addresses.

Again, thanks for the post,

-Rob


admin

April 14th, 2011
22:33:39

Hello Rob,

I’m glad to hear that. I’m sure your comment will help PRTG users who uses PRTG server in a domain environment.

My PRTG server is a standalone server, so I don’t have that chance to connect without session :)

Thanks.


Germano

November 30th, 2011
16:48:46

Hello Yusuf,

One thing that i noticed in your script, is that when you change from Used Space to Free Space, you need to change the output too, because in the unmodified version, you´re monitoring the Used Space, but when u wip the line to change to Free Space, you must change the Output to get the value lower than an specified value, or you´ll never got to monitor when the volume run out of space.

The second thing is if you found already some way to mask the password.

Best Regards,

Germano.


dp

March 27th, 2012
01:01:06

Please show a command line example for testing.
Thanks



Leave a Reply