Create a Drive Space Monitor That Sends Push Notifications in Delphi
By: Chris Mcclenny
Tags: Push Notification, Delphi

Things you will need

A free pushover account: https://pushover.net/ . Setup your account and create a new application token to use for this Drive Space monitor. Its super easy and they walk you through the process. The free SSL library files: https://indy.fulgan.com/SSL/ Know how to use Delphi and create projects

Create the service

First we need to create a Delphi Service Application and get the project setup and saved.

Service applications can be tricky if done incorrectly. Adjusting the service execution sleep time too long and Windows Service control module will think the service has locked up and will report “Not responding”. Adjust it too short and it will eat up way too much CPU time. We need to find the sweet spot.

Service Methodologies

There are two main methodologies when writing Windows services.

Spin up a thread from ServiceStart() function to do the work

Do the work from the ServiceExecute() function

If your workload is short lived, meaning it will not take more than a few seconds to process, its fine doing all of the work inside of ServiceExecute(). This is much simpler to code and less prone to technical debt later on. Just always remember, all of the code in ServiceExecute() MUST execute fast. If you have a workload which is more CPU intensive and executes much slower, the best option is to spin up a thread. But for this project, the code runs so fast, we can just go the simpler route and use ServiceExecute().

ServiceExecute Function

The ServiceExecute() core is a simple infinite loop which only breaks on the terminated service signal.

The end of the loop simply sleeps for a short time then we tell Windows to process service messages. This will allow the service to respond when Start/Shutdown/Restart are called from the Windows service control panel.

The code in between is the good stuff. We start by reading in our Settings from an INI file. Instead of reading them only when the service starts, why not read them on every loop iteration. This way, we can control how the service operates by making changes to just this file without needing to restart the service for every change which increases our uptime.

What is in the INI settings file?

Drives: Comma separated list of drives to monitor PercentFullTrigger: Percent Full trigger point. Set it to 70 to get notifications when drives hit 70% full CheckIntervalSeconds: How fast the ServiceExecute loop runs LastSent: Last date it sent a notification MachineName: Name this machine for the Pushover notification MessageTitle: Title for the Pushover Notification UserKey: Provided by Pushover APITokenKey: Provided by Pushover DeviceName: Provided by Pushover

Core code

Then comes the core of the code. This is the real work. We get the LastSentDate from the INI file from above, and make sure its not todays date. The reason for this is so that it will fire only one Pushover notification per day. Then we loop through each drive specified in the settings file and check its drive space using GetDiskFreeSpaceEx. We do some basic drive space math to convert free space to a percentage of full drive space. This percent full is then stored in the CriticalDrivesList as a name/value pair matched up with its drive letter. The last part of this core code is where it sends the CriticalDrivesList to the SendPushoverMsg function.

Send the Pushover Message

So now we need to move on to the actual Pushover code. This will do an Http POST to the Pushover API servers. And of course, we use Indy for Http. The Http client setup is quite simple. Just a few basic settings and its done. The critical thing to get correct is the SSL setup. It can be a bit tricky. Also, we will need the SSL DLL library files (ssleay32.dll, libeay32.dll). We simply drop these in our service executables path.

Further down in the code, we setup the message layout, then add POST data to conform to the Pushover API specs. This is where we add our APIToken, Device Name, and UserKey provided by our Pushover account. This is also where we add the title, notification sound to play on the phone, priority and many others. Then we simply post it to Pushover and DONE! Scroll down to see the notification.

The Pushover Notification

This is what one looks like on your phone. Nice!

The completed DriveSpaceSvc.pas service file

The completed project

The completed project is hosted on my GitHub page at https://github.com/squashbrain/DriveSpaceMonitor