Microsoft Windows print spooler and any other Windows service health check

Microsoft Windows print spooler and any other Windows service health check

Print Published on 4 mins Last updated

[Editor – Originally published in 2019, the links in this post have since been updated]

Over the past year I've written a few blogs about load balancing print servers and print managed solutions. If you haven’t already deployed print servers in HA, please don't fret. It is a relatively simple and straightforward process, as you can see in our blog load balancing Microsoft Print Server.

During the course of our customer engagements, however, one question has always come up: ‘What is the best way to check the health of the print server?’

Our previous recommendation would have been to use the connect to port health check, which is fine for checking that the load balancer can communicate with the print server. But what happens if the print server service (the print spooler) goes offline? Well, if you are using the connect to port health check, you will get disgruntled users when their print jobs are not being fed to the printers.

What other options do we have to intelligently check the port as well as the status of the print spooler service?

Enter the WMI command! This enables the load balancer to query Windows system services from a Linux system.

That’s correct - you read that right! Where can you get this package from? Take a look here.

Once you’ve added the RPM and installed the package you will be able to use the WMIC command as per the examples below:

Example: wmic -U [domain/]adminuser%password //host "select * from Win32_ComputerSystem"`

[root@lbmaster ~]# wmic -U wmic -U ${USER}${DOMAIN}%${PASS} //${REALIP} "Select CommandLine,Name,Processid from Win32_Process"

[root@lbmaster ~]# wmic -U spcheck\@\printlab\.\com //192.168.86.136 "select CommandLine,Name,Processid from Win32_Process"`
Password for [spcheck@printlab.com]:

You’re probably thinking, ‘That’s useful, but what about the health check?’

Fret no further - it's here for all to see.

#!/bin/bash

#Print Spooler and Port Health Check script v1
#Created by IGraham 02/09/19

## This script uses the nmap command piped the WMI package (to probe the Windows print spooler service status) obtained from https://downloads.loadbalancer.org/support/rpms/wmi-1.3.14-27.3.x86_64.rpm
#  piped to grep to confirm that both the SMB port 445 and spoolsv service is running. Login using username@domain.com if the print servers belong to an Active Directory Domain.
#  Please be aware if the print servers reside within a domain the DOMAIN variable needs to be in @DOMAIN.COM format

#Variables passed by ldirectord.
# $1 = VIP Address
# $2 = VIP Port
# $3 = Real Server IP
# $4 = Real Server Port


#Script Variables for check port.
CHECK_IP="$3"           #Should be $3 so it picks up the IP from ldirectord.
CHECK_PORT="$2"         #Should be $4 so it picks up the Port from ldirectord, otherwise for a port list use a space seperated format "80 443 8080"
TIMEOUT="8"            #Port timeout value. 8 seconds should be the minimum timeout set to ensure the nmap command executes in an adequate time.
USER="spcheck"
PASS="Open123!"
DOMAIN="printlab.com"
PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin"

NMAP_OUTPUT=$(/usr/bin/nmap -sV -p $CHECK_PORT --host-timeout $TIMEOUT $CHECK_IP | /bin/wmic -U "${DOMAIN}\\${USER}%${PASS}" //${CHECK_IP} "SELECT CommandLine,Name,Processid FROM Win32_Process" | /bin/grep -i "spoolsv")
# echo $NMAP_OUTPUT


if [[ "$NMAP_OUTPUT" == *spoolsv* ]];
then
    	EXIT_CODE=0
else
    	EXIT_CODE=100
fi

exit $EXIT_CODE

What does this health check do and how does it work?

Thanks to our good old friend the NMAP command we can perform a port check of the SMB port 445.

Next we then pipe the WMIC command, log in to the print server using our credentials and list the current running processes.

With the processes listed we then pipe through to grep and search for the print spooler service spoolsv to confirm that it is running.

If the grep finds the spoolsv service then the health check is marked as pass. However if the spoolsv is not listed then the health check is marked as failed.

The beauty of this script is that it is not limited to the print spooler service. In theory you can grep for any Windows operating system service and choose the appropriate port to check using the NMAP command.

So go forth and enjoy having a little more confidence in the load balancer health check, whether it be for a print server cluster or whatever Microsoft Windows service you are looking to make highly available.

Download the Spoolercheck script here.

Note:
To apply the health check script to your load balancer configuration please upload it to the load balancer in the following directory:

/var/lib/loadbalancer.org/check

Next make it executable using:

chmod +x spoolercheck.sh

Next modify your VIP and in the Health Checks field select External

Then select spoolercheck.sh from the drop down list and click update to apply.

External-Check-1

If you are using a layer 7 VIP configuration don't forget to reload HAproxy when prompted.

If you have any questions about the above or need help, don't hesitate to get in touch or leave a comment below.

Want a load balancer that simply works?

See what all the fuss is about