Table of Contents

Printer Monitoring

Author Michael Fisher
Compatibility Xymon 4.2+
Requirements Windows Print Services
Download Printer.vbs
Last Update 2009-04-09

Description

Script to Monitor and Report on Status of Windows Print Services

Installation

Run the VBS Script at any desired Interval

Client side

Place the VBS into the EXT folder and configure BBWin.cfg to load the external at any given interval

Server side

N/A

Source

Printer.vbs

Show Code ⇲

Hide Code ⇱

Option Explicit
On Error Resume Next

Dim colour, bbdelay, strDetail, strDetailAll
Dim WindowsShell, objWMIService, colInstalledPrinters, BBConfigExtPath, objPrinter

Const BBTestName = "print"

strDetail = ""
strDetailAll = ""
colour = "green"
bbdelay = "+27"

BBConfigExtPath = "c:\Program Files\BBWin\tmp"

'=============================================================================


Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colInstalledPrinters = objWMIService.ExecQuery("Select PortName,Location,Status,DetectedErrorState,Name from Win32_Printer where Name != 'Microsoft XPS Document Writer'")

For Each objPrinter in colInstalledPrinters

If objPrinter.DetectedErrorState = "9" Then
strDetail = "&red" & " " & objPrinter.Name & " is OFFLINE" & vbCrLf & strDetail & vbCrLf
strDetailAll = "<tr><td>&red " & objPrinter.Name & "</td><td>" & objPrinter.PortName & "</td><td>" & objPrinter.Location & "</td></tr>" & vbCrLf & strDetailAll
colour = "red"
ElseIf objPrinter.DetectedErrorState = "6" Then
strDetail = "&red" & " " & objPrinter.Name & " has NO TONER" & vbCrLf & strDetail & vbCrLf
strDetailAll = "<tr><td>&red " & objPrinter.Name & "</td><td>" & objPrinter.PortName & "</td><td>" & objPrinter.Location & "</td></tr>" & vbCrLf & strDetailAll
colour = "red"
ElseIf objPrinter.DetectedErrorState = "1" Then
strDetail = "&red" & " " & objPrinter.Name & " is in ERROR" & vbCrLf & strDetail & vbCrLf
strDetailAll = "<tr><td>&red " & objPrinter.Name & "</td><td>" & objPrinter.PortName & "</td><td>" & objPrinter.Location & "</td></tr>" & vbCrLf & strDetailAll
colour = "red"
ElseIf objPrinter.DetectedErrorState = "4" Then
strDetail = "&red" & " " & objPrinter.Name & " has NO PAPER" & vbCrLf & strDetail & vbCrLf
strDetailAll = "<tr><td>&red " & objPrinter.Name & "</td><td>" & objPrinter.PortName & "</td><td>" & objPrinter.Location & "</td></tr>" & vbCrLf & strDetailAll
colour = "red"
ElseIf objPrinter.DetectedErrorState = "8" Then
strDetail = "&red" & " " & objPrinter.Name & " is JAMMED" & vbCrLf & strDetail & vbCrLf
strDetailAll = "<tr><td>&red " & objPrinter.Name & "</td><td>" & objPrinter.PortName & "</td><td>" & objPrinter.Location & "</td></tr>" & vbCrLf & strDetailAll
colour = "red"
ElseIf objPrinter.DetectedErrorState = "10" Then
strDetail = "&red" & " " & objPrinter.Name & " is Requesting Service" & vbCrLf & strDetail & vbCrLf
strDetailAll = "<tr><td>&red " & objPrinter.Name & "</td><td>" & objPrinter.PortName & "</td><td>" & objPrinter.Location & "</td></tr>" & vbCrLf & strDetailAll
colour = "red"
ElseIf objPrinter.DetectedErrorState = "11" Then
strDetail = "&red" & " " & objPrinter.Name & " shows OUTPUT BIN IS FULL" & vbCrLf & strDetail & vbCrLf
strDetailAll = "<tr><td>&red " & objPrinter.Name & "</td><td>" & objPrinter.PortName & "</td><td>" & objPrinter.Location & "</td></tr>" & vbCrLf & strDetailAll
colour = "red"
ElseIf objPrinter.DetectedErrorState = "0" Then
strDetailAll = "<tr><td>&green " & objPrinter.Name & "</td><td>" & objPrinter.PortName & "</td><td>" & objPrinter.Location & "</td></tr>" & vbCrLf & strDetailAll
End If

Next


Set colInstalledPrinters = Nothing
Set objWMIService = Nothing

StartDetailBuild()
SetPrintFinalStatus()
GetBBPath()
WriteFile()

Sub StartDetailBuild()
	IF colour = "red" then 
	strDetail = "&red Printer Status:" & vbCrLf & vbCrLf & strDetail & vbCrLf
        ElseIf colour = "green" then
        strDetail = "&green Printer Status: All Printers are Online and Operational" & vbCrLf & vbCrLf & strDetail & vbCrLf
        End If
End Sub

Sub SetPrintFinalStatus()
	Select Case colour
		Case "red"
			strDetail = "red" & bbdelay & " " & Date & " " & Time & vbCrLf & vbCrLf & strDetail & vbCrLf
		Case "yellow"
			strDetail = "yellow" & bbdelay & " " & Date & " " & Time & vbCrLf & vbCrLf & strDetail & vbCrLf
		Case "green"
			strDetail = "green" & bbdelay & " " & Date & " " & Time & vbCrLf & vbCrLf & strDetail & vbCrLf
	End Select
End Sub

Sub GetBBPath()
	Set WindowsShell = WScript.CreateObject("WScript.Shell")
	If IsObject(WindowsShell) Then Set WindowsShell = Nothing
End Sub

Sub WriteFile()
    Dim oFSO, oFile
    Set oFSO = CreateObject("Scripting.FileSystemObject")	
    Set oFile = oFSO.OpenTextFile(BBConfigExtPath & "\" & BBTestName , 8 , TRUE)
    oFile.Write strDetail
    oFile.Write "<table>" & vbCrLf & strDetailAll & vbCrLf & "</table>"
    oFile.Close
    If IsObject(oFSO) Then Set oFSO = Nothing
End Sub

Known Bugs and Issues

None

To Do

Provided by ““Malcolm Hunter” malcolm.hunter@gmx.co.uk

To create the output so that email alerts do not contain HTML code, incorporate the following into the above script:

' Get max lengths
For Each objPrinter in colInstalledPrinters
If Len(objPrinter.Name) > iMaxNameLen Then iMaxNameLen = Len(objPrinter.Name)
If Len(objPrinter.PortName) > iMaxPortLen Then iMaxPortLen = Len(objPrinter.PortName)
If Len(objPrinter.Location) > iMaxLocnLen Then iMaxLocnLen = Len(objPrinter.Location)
Next

' Use tabs and padding to format each line
strDetailAll = "&red " & Pad(objPrinter.Name, iMaxNameLen) & vbTab & Pad(objPrinter.PortName, iMaxPortLen) & vbTab & Pad(objPrinter.Location, iMaxLocnLen)

' Returns text padded to iLength
Function Pad(sText, iLength)
Dim iPadBy, sTemp
sTemp = sText
iPadding = iLength - Len(sText)
If iPadding > 0 Then sTemp = sTemp & Space(iPadding)
Pad = sTemp
End Function

Credits

Changelog