Script in vbs that will check if a printer is working correctly and is processing its Documents.
In case the same number of documents are in the Queue it will alert, this can be used in nagios
with a retry of 3 times each 2 min.

'Verifiy if stuck doc's in printer queue and printer status
'by Felipe Ferreira August 2012
'For Nagios
'Version 1
'TODO:
'Check if printer is responding to network.
Option Explicit
Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
DIM INT_QUEUE, INT_ERROR,STR_PRINTER_SHARE
Dim outputfile,strScriptPath, strScriptFile,ofile, oFSO
Dim argcountcommand, arg(10)
Dim INT_WARN,INT_CRIT,BLN_PERF,INT_ERR_CHK
Dim outputmsg,intExit,INT_QUEUE_BEFORE,STR_STATUS
strScriptFile = WScript.ScriptFullname
strScriptPath = Left(strScriptFile, Len(strScriptFile) - Len(WScript.Scriptname)) 'used to define the path from where the script file is located
INT_ERR_CHK=0
INT_QUEUE_BEFORE=""
' GET ARGUMENTS FROM CMD LINE
GetArgs()
if argcountcommand = 0 then
wscript.echo "Usage " & strScriptFile & "-p "
wscript.quit(1)
elseif ((UCase(wscript.arguments(0))="-H") Or (UCase(wscript.arguments(0))="--HELP")) then
wscript.echo "Usage " & strScriptFile & "-p "
wscript.quit(1)
elseif(argcountcommand = 2) then
STR_PRINTER_SHARE = GetOneArg("-p")
end if
'EDIT
'------------------------------------------------------------------------------------
Dim debug:debug="true" ' false for no output
'------------------------------------------------------------------------------------
'Arguemnt from Script
call check_queue(STR_PRINTER_SHARE)
if (INT_ERR_CHK <> 0 or INT_ERROR = 1) then
outputmsg = "CRITICAL - Printer " & STR_PRINTER_SHARE & "has problems. Status = " & STR_STATUS & " .Queue = " & INT_QUEUE & "|queue="& INT_QUEUE
intExit = 2
elseif INT_ERROR = 5 then
outputmsg = "WARNING - Printer " & STR_PRINTER_SHARE & " has Lower Tonner! Status = " & STR_STATUS & " .Queue = " & INT_QUEUE & "|queue="& INT_QUEUE
intExit = 2
elseif INT_ERROR = 4 then
outputmsg = "CRITICAL - Printer " & STR_PRINTER_SHARE & " is Out of Paper!. Status = " & STR_STATUS & " .Queue = " & INT_QUEUE & "|queue="& INT_QUEUE
intExit = 2
elseif INT_ERR_CHK = 0 then
outputmsg = "OK - Printer " & STR_PRINTER_SHARE & " is working fine, queue = " & INT_QUEUE & "|queue="& INT_QUEUE
intExit = 0
end if
wscript.echo outputmsg
wscript.quit(intExit)
'########################## FUNCTIONS ANS SCRIPTS ###############################
function check_queue(str_prnt)
'Function to check printer queue
Dim objItem,STR_FILE
Dim objWMIService : Set objWMIService = GetObject("winmgmts:localhostrootCIMV2")
Dim colItems : Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Printer where ShareName= '" & str_prnt & "'", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
For Each objItem In colItems
INT_ERROR=objItem.DetectedErrorState
INT_QUEUE=objItem.JobCountSinceLastReset
STR_STATUS=objItem.Status
pt objItem.Name
pt "Status: " & objItem.Status
'pt "JobCountSinceLastReset: " & INT_QUEUE
pt "Location: " & objItem.Location
pt "DetectedErrorState: " & INT_ERROR '0 = Unkown '1 = Error '2 = OK '4=nopaper '5 = Degraded ( Tonner Low )
'INT_QUEUE=
if (INT_QUEUE <> 0 ) then
pt "QUEUE is not EMPTY!"
Set oFSO = CreateObject("Scripting.FilesyStemObject")
outputfile = strScriptPath & "log_" & str_prnt & ".txt"
'Check if file exists if it dos CHECK, if same value
INT_QUEUE_BEFORE=ReadFileText(outputfile)
if (INT_QUEUE_BEFORE <> "" ) then
INT_QUEUE_BEFORE=cint(INT_QUEUE_BEFORE)
end if
INT_QUEUE=cint(INT_QUEUE)
if (INT_QUEUE > INT_QUEUE_BEFORE ) then
pt vbcrlf & "Queue was at: " & INT_QUEUE_BEFORE
pt "Queue is now: " & INT_QUEUE & vbcrlf
INT_ERR_CHK = INT_QUEUE
end if
'Compare INT_QUEUE vefore with current, if same or higher INT_ERROR!
'CREATE FILE AND WRITE IN IT, ONLY IF IT DOES NOT EXISTS YET.
if (INT_QUEUE_BEFORE = "" or INT_QUEUE > INT_QUEUE_BEFORE ) then
Set ofile = oFSo.OpenTextFile(Outputfile,2,TRUE) 'DO NOT APPEND, CREATE AND WRITE ONE LINE
ofile.writeline INT_QUEUE
ofile.close
end if
else
'DELETE FILE IF EXISTS
Set oFSO = CreateObject("Scripting.FilesyStemObject")
outputfile = strScriptPath & "log_" & str_prnt & ".txt"
If oFSO.FileExists(outputfile) Then
oFSO.DeleteFile outputfile
pt"File " & outputfile & " deleted"
end if
Set oFSO = nothing
end if
Next
end function
Function ReadFileText(sFile)
'Will read file and return all file txt if it exists
'on error resume next
Dim objFSO 'As FileSystemObject
dim oTS
dim sText,FileSIze
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(sFile) Then
Set oFile = objFSO.GetFile(sFile)
FileSize = oFile.Size
set oFile = nothing
Set oTS = objFSO.OpenTextFile(sFile)
if ( FileSize <> 0 ) then
sText = oTS.ReadAll
'pt "Current known errors are " & vbcrlf & sText
ReadFileText = sText
end if
oTS.close
else
ReadFileText = ""
end if
set oTS = nothing
Set objFSO = nothing
end function
Function GetArgs()
'Get ALL arguments passed to the script
On Error Resume Next
Dim i
argcountcommand=WScript.Arguments.Count
for i=0 to argcountcommand-1
arg(i)=WScript.Arguments(i)
pt i & " - " & arg(i)
next
End Function
Function GetOneArg(strName)
On Error Resume Next
Dim i
for i=0 to argcountcommand-1
if (Ucase(arg(i))=Ucase(strName)) then
GetOneArg=arg(i+1)
Exit Function
end if
next
End Function
function pt(txt)
if(debug="true" ) then
wscript.echo txt
end if
end function

Tags: , , , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *