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

Badges
MCSE
Community

Cozumpark Bilisim Portali
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.