Monitor Everything with Monitis – And do it easily with PowerShell – Part 13

Monitoring out of Paper with Monitis (Printer Monitoring)

Last time, we scratched the surface of what you can do with performance counters and Monitis.  Today, we’ll show you a fun performance counter to help automate your office, and also show you how you can use the Monitis module to make and updating custom monitors a lot simpler.

One of the fun sets of performance counters are the printer counters:

Get-Counter -ListSet *print*queue* | Select-Object -ExpandProperty Paths

You can see all of the out of paper errors, for all printers on a machine, with this line:

Get-Counter "\Print Queue(*)\Out of Paper Errors"

This is a perfect candidate for notifications, because it lets you automatically inform the printer lackey that he needs to get paper, and which printer needs the paper.

We’re going to make a custom monitor and a notification rule for this one, but we’re going to use a fun little feature of the monitis module you can use to create a command to update a monitor.

Let’s get started by importing the module and connecting to monitis


The command is Add-MonitisCustomMonitorUpdater.  What it does is create a custom monitor, and a command to collect and push values into that custom monitor.  The command stores your apikey and secretkey, so you can schedule the update.  Here’s the out-of-paper monitor:

Add-MonitisCustomMonitorUpdater -Name OutOfPaper -ScriptBlock {
    Get-Counter "\Print Queue(*)\Out of Paper Errors" |
        Select-Object -ExpandProperty CounterSamples |
        Where-Object { $_.InstanceName -ne '_total' -and $_.CookedValue -gt 0} |
        Select-Object @{
            Label = "Printer"
        }, @{
            Label = "OutOfPaperErrors"
} -Property Printer, OutOfPaperErrors

Once you’ve run this, you can run Update-MonitisOutOfPaper to update your monitor in monitis.

Add-MonitisCustomMonitor takes a name of a monitor, and a script block of an check and the name of properties to get from that check.  A script block is simply a bunch of PowerShell commands, like we’ve been running throughout the series.

This script block starts off like the other performance counter script.  Get-Counter gets the counters, and Select-Object –ExpandProperty picks out all of the samples from those counters.  We want only samples that are not the built in total counter and have at least one page out of paper, so that’s Where-Object { $_.InstanceName -ne ‘_total’ -and $_.CookedValue -gt 0}.  The last lines are the trickiest: Select-Object can take a list of properties, or a list of labels and expressions (like what happens above).  By using this trick, I can rename the property InstanceName to be Printer, and CookedValue can be OutOfPaperErrors.

Update-MonitisOutOfPaper will always be imported whenever you import the monitis module, and you can define as many monitor updater commands as you’d like.

To add the notification rule, we need to add a contact and add the rule, and then we’re done:

Add-MonitisNotificationRule -TestId $test.MonitisTestId -ContactId $contact.ContactId -TriggerParameter OutOfPaperErrors -TriggerValue 0 -TriggerOnGreaterThan
Add-MonitisContact -FirstName Printer -LastName Lackey -Account -AccountType Email
$contact= Get-MonitisContact -Name "Printer Lackey"
$test = Get-MonitisCustomMonitor OutOfPaper
Add-MonitisNotificationRule -TestId $test.MonitisTestId -ContactId $contact.ContactId -TriggerParameter OutOfPaperErrors -TriggerValue 0 -TriggerOnGreaterThan

We’re almost done showing you all of the cool things you can monitor with Monitis.  Check back tomorrow to learn how you can use a custom updater function to schedule your script.

See also:

Monitor Everything with Monitis – And do it easily with PowerShell – Part 1

Part 2: Managing External Monitors with Monitis and PowerShell

Part 3: Mining External Monitor Results with Monitis and PowerShell

Part 4: Monitoring Web Applications with Monitis

Part 5: Testing Web Content with Monitis, Excel, and PowerShell

Part 6: Monitoring Anything with a Custom Monitor

Part 7:  Hardware Inventory with Monitis Custom Monitors

Part 8: Monitoring Logons with Monitis

Part 9: Monitoring Connections to Shared Folders with Monitis and Custom Monitors

Part 10: Inventory Windows Installations with Monitis and PowerShell

Part 11: Monitoring Removable Disks on Many Computers with Monitis and PowerShell

Part 12: Monitoring Event Logs and Using Monitis Notifications