Monitoring with PowerShell: Conditional Disk Space monitoring

A couple of weeks ago I was talking to someone in one of the many MSP discords, and he was struggling with disk space monitoring. His RMM system is designed in such a way that whenever he added a disk space monitor to a machine it adds an entirely new component to the monitoring list. He also could not filter out if the disk is present or not. So if the machine does not have a D:\ Drive that component would fail.

Of course this was easily solved by creating a monitor in his RMM system that said “For all drives available” but then he bumped into the next issue; he could not exclude disks such as a TempDB disks that are meant to be near capacity, or disks that are very large and thus 10% could be hundreds of GB or even a couple of TB. he also had to disable monitoring for all disks if he just didn’t want to alert on a single disk. Quite annoying of course, so I figured I’d create a quick and dirty blog to show how to monitor disk space, and build in some conditions in the monitoring component itself.

The Script

So the script has some variables to enter for your own settings, and the ones I’m giving are just an example. You can feed the “Excluded drives” from your monitoring system if it supports that. We’re also only grabbing disks that have a drive letter, change line 16 if you also want to include drives without.

####### Disk settings
$DiskSettings = [PSCustomObject]@{
    LargeDiskPercent     = 1
    LargeDiskAbsolute    = 100GB
    LargeDisksThreshold  = 10TB
    MediumDiskPercent    = 5
    MediumDiskAbsolute   = 50GB
    MediumDiskThreshold  = 3TB
    SmallDiskThreshold   = 200GB
    SmallDiskPercent     = 10
    SmallDiskAbsolute    = 2GB
    ExcludedDriveLetters = "A", "B"
}
####### Disk settings
try {
    $Volumes = get-volume | Where-Object { $_.DriveLetter -ne $null -and $_.DriveLetter -notin $ExcludedDriveLetters }
}
catch {
    write-host "Could not get volumes: $($_.Exception.Message)"
    exit 1
}
$DisksOutOfSpace = Foreach ($Volume in $Volumes) {
    if ($volume.size -gt $DiskSettings.LargeDisksThreshold) { $percent = $DiskSettings.LargeDiskPercent; $absolute = $DiskSettings.LargeDiskAbsolute }
    if ($volume.size -lt $DiskSettings.LargeDisksThreshold) { $percent = $DiskSettings.MediumDiskPercent; $absolute = $DiskSettings.MediumDiskAbsolute }
    if ($volume.size -lt $DiskSettings.MediumDiskThreshold) { $percent = $DiskSettings.MediumDiskPercent; $absolute = $DiskSettings.MediumDiskAbsolute }
    if ($volume.size -lt $DiskSettings.SmallDiskThreshold) { $percent = $DiskSettings.SmallDiskPercent; $absolute = $DiskSettings.SmallDiskAbsolute }

    if ($volume.SizeRemaining -lt $absolute -or ([Math]::Round(($volume.SizeRemaining / $volume.Size * 100), 0)) -lt $percent ) {
        [PSCustomObject]@{
            DiskName            = $Volume.FileSystemLabel
            DriveLetter         = $volume.DriveLetter
            Size                = $volume.Size
            SizeRemaining       = $volume.SizeRemaining
            PercentageRemaining = [Math]::Round(($volume.SizeRemaining / $volume.Size * 100), 0)
        } 
    }
}

if ($DisksOutOfSpace) {
    "Some disks are running low on space. Please investigate"
    $DisksOutOfSpace
}
else {
    "Healthy - No disks running out of space"
}

So the benefits of approaching it this way are that he is able to have just 1 disk monitoring component in his RMM system, instead of a list of 10, he is able to make conditional changes, and the alerting even offers some more insights such as the friendly name.

As a final note, I wanted to let you all know that you can now buy me a cup of coffee on Github. Click that link if you feel like I helped you out at all once, it helps support the hosting of the blog, the CTF resources, but also Azure Functions I create and keep updated for you all. I won’t make a habit of putting this under my posts and my content will always remain free. 🙂

And that’s it! as always, Happy PowerShelling.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.