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.

Recent Articles

The return of CyberDrain CTF

CyberDrain CTF returns! (and so do I!)

It’s been since september that I actually picked up a digital pen equivalent and wrote anything down. This was due to me being busy with life but also my side projects like CIPP. I’m trying to get back into the game of scripting and blogging about these scripts. There’s still so much to automate and so little time, right? ;)

Monitoring with PowerShell: Monitoring Acronis Backups

Intro

This is a monitoring script requested via Reddit, One of the reddit r/msp users wondered how they can monitor Acronis a little bit easier. I jumped on this because it happened pretty much at the same time that I was asked to speak at the Acronis CyberSummit so it kinda made sense to script this so I have something to demonstrate at my session there.

Monitoring with PowerShell: Monitoring VSS Snapshots

Intro

Wow! It’s been a while since I’ve blogged. I’ve just been so swamped with CIPP that I’ve just let the blogging go entirely. It’s a shame because I think out of all my hobbies it’s one I enjoy the most. It’s always nice helping others achieve their scripting target. I even got a couple of LinkedIn questions asking if I was done with blogging but I’m not. Writing always gives me some more piece of mind so I’ll try to catch up again. I know I’ve said that before but this time I’ll follow through. I’m sitting down right now and scheduling the release of 5 blogs in one go. No more whining and no more waiting.