Operating System - HP-UX
1823085 Members
3412 Online
109646 Solutions
New Discussion юеВ

Shell Script to report total print jobs for each que

 
Mike Kapsak
Advisor

Shell Script to report total print jobs for each que

Hello,

I would like to create a script to run periodically that gives me the total number of print jobs on an HP-UX server, and also the total number of print jobs by printq. There would be a threshold for the total number as well as a threshold for each queue. If a threshold is reached, I want to send an e-mail. If it is exceeded, I want to send a page. Any assistance is appreciated. Thanks for your time.

Mike Kapsak
12 REPLIES 12
Mark Greene_1
Honored Contributor

Re: Shell Script to report total print jobs for each que

lpstat -t will show you what's sitting in each printq. You could just pipe that to wc -l, and send e-mail if it's greater than a certain number. However, if you have a lot a queues, a little activity in each could trigger a bogus mail. Conversely, a down printer (no paper, out of toner, switched off) will do the same. Also, legit network problems may go undetected if a single queue gets jobs stacked-up, but below the threashold.

As a compromise, you may want to lpstat -a [queue] for each print queue. You'll want to grep for "down" as well as doing the wc -l to check the number of enqueued jobs.

HTH
mark
the future will be a lot like now, only later
Bill Hassell
Honored Contributor

Re: Shell Script to report total print jobs for each que

lpstat -t can be a very painful command to use if you have remote printers. The reason is that -t polls any remote printers and responses may take many seconds to minutes to respond. Since all you need is a count of the print jobs, just look at the print queues in /var/spool/lp/request. Each directory is a print queue and there will be 2 files per print job, a short control file with print options, and the data file. Count the number of files that start with c (the control file). You can even determine the size of the print jobs by adding the byte count for each d file (files that start with d).


Bill Hassell, sysadmin
twang
Honored Contributor

Re: Shell Script to report total print jobs for each que

The following is a simple script i write to check the printer's status and reset failed printers:

#! /usr/bin/sh
program="usr_prt"
# purpose: Interface for retrieving HPUX Online Printers Info
# creator: tommy
# date: 14/11/2000
ver="1.0.1"

MESS1="Insert a printer_name (q/Q to quit): \c"
MESS2="Insert an option (q/Q to quit): \c"
while : ; do
clear
echo "#########################################

HPUX Online Printers Info

`date |awk '{print $3"-"$2"-"$6}'` version:$ver
Anytime enter Q/q to quit!!
#########################################
Current Online Printers:
"
lpstat -a |sort
echo ""
echo $MESS1
read PRTID
if test "$PRTID" = "q"
# or "$PRTID" = "Q"]
then
echo ""
exit_code=$?
echo "$program: exiting with status $exit_code"
exit 1
else
if test "$PRTID" = "Q"
then
echo ""
exit_code=$?
echo "$program: exiting with status $exit_code"
echo ""
exit 1
else
RETURN=""
while [ test $RETURN != "yes" ]
do
clear
lpstat -d $PRTID
exit_code=$?
echo "-------SubMenu-------"
echo "T/t : print testprt"
echo "C/c : cancel all jobs"
echo "R/r : reset this printer"
echo "M/m : return to Main Menu"
resp=*
echo $MESS1
read resp
case $resp in
[T,t])
lp -d $PRTID testprt
exit_code=$?
;;
[C,c])
cancel -e $PRTID
exit_code=$?
;;
[R,r])
disable $PRTID
sleep 1
enable $PRTID
sleep 1
lpstat -d $PRTID
exit_code=$?
;;
[M,m])
RETURN="yes"
;;

[q,Q])
echo ""
echo "$program: exiting with status $exit_code"
echo ""
exit 0
;;

*)
echo Invalid Responce
sleep 1
;;
esac
done
fi
fi
done
Andreas Voss
Honored Contributor

Re: Shell Script to report total print jobs for each que

Hi,

lpsched writes a log file:
/var/adm/lp/log

For reporting copy the file to another place and truncate the logfile to 0 for further reporting.
With a wc -l of the logfile you will have the total amount of print jobs since last truncate and with a little bit of awk ... you can sort out the print job counts by queue name.

Regards
Mike Kapsak
Advisor

Re: Shell Script to report total print jobs for each que

Thanks for the replies. I'm a script writing newbie and was hoping someone had already created a script to do so. I'm able to get some of the information using lpstat and awk with the following:

#!/usr/bin/ksh
# Variables
TOTALPRTLIST="/home/mkapsak/prtlist"
PRINTQUEUES= "/home/mkapsak/prtqueues"

# List all print queues
ll /var/spool/lp/request > $TOTALPRTLIST

# Create printer list
awk '{print $9}' /home/mkapsak/prtlist > $PRINTQUEUES

I'm not very familiar with variables, looping and reading the printer file using a while stmt.

Mike Kapsak
Bill Hassell
Honored Contributor

Re: Shell Script to report total print jobs for each que

Here's a simpler script that will count the number of queued requests per printer and then show a summary:

#!/usr/bin/sh
# Count print requests by printer

export PATH=/usr/bin
ACTIVE=0
JOBS=0
for PRINTQ in $(echo /var/spool/lp/request/*)
do
QUEUENAME=$(basename $PRINTQ)
QTY=$(ls $PRINTQ/d* 2>/dev/null | wc -l | cut -d \ -f 1)
if [ $QTY -gt 0 ]
then
let ACTIVE=$ACTIVE+1
print "Printer $QUEUENAME = \c"
print "$QTY request\c"
[[ $QTY -ne 1 ]] && print "s\c"
let JOBS=$JOBS+$QTY
print ""
fi
done
print "\nTotal jobs=$JOBS, printers with a queue=$ACTIVE"


Bill Hassell, sysadmin
Mike Kapsak
Advisor

Re: Shell Script to report total print jobs for each que

Hi Bill,

I tried running the script you supplied but I'm geting a usage message from the cut command:

Usage: cut -b List [-n] [File...]
or: cut -c List [File...]
or: cut -f List [-d Character] [-s] [File...]
itrcscript[11]: test: Specify a parameter with this command.

Mike Kapsak
Daniel Neeves
Advisor

Re: Shell Script to report total print jobs for each que

Hi Mike,

Use a single quote '\' around the backslash after the cut command.

QTY=$(ls $PRINTQ/d* 2>/dev/null | wc -l | cut -d '\' -f 1)

Cheers
Dan
Like Ya Know !!
Geoff Wild
Honored Contributor

Re: Shell Script to report total print jobs for each que

Here's another script for you - called lpst

#!/bin/sh
#
# check printer status
# Geoff Wild

if [ $# -lt 1 -o \( $# -gt 1 -a $# -lt 4 \) ]
then
echo "Usage:"
echo "lpst \"printer\""
echo "Example:"
echo "lpst W052"
exit 1
fi
echo " "
/usr/sbin/ping $1 -n 2
echo " "
lpstat -p$1 -v$1
echo " "
echo "Output Requests"
echo "-----------------------------------------------------------"
lpstat -o$1
echo " "
lpstat -r
echo " "


Example:

lpst W052

PING myprinter.mydomain.com: 64 byte packets
64 bytes from 192.168.2.101: icmp_seq=0. time=7. ms
64 bytes from 192.168.2.101: icmp_seq=1. time=7. ms

----myprinter.mydomain.com PING Statistics----
2 packets transmitted, 2 packets received, 0% packet loss
round-trip (ms) min/avg/max = 7/7/7

printer W052 is idle. enabled since May 14 10:00
fence priority : 0
device for W052: /dev/null

Output Requests
-----------------------------------------------------------
no entries

scheduler is running


Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Bill Hassell
Honored Contributor

Re: Shell Script to report total print jobs for each que

Sorry I did not see the question earlier. The cut -d \ -f .. is a way to specify a single space as a delimiter. Unfortunately, the forums code reduces multiple spaces to just one. So when you cut-n-paste the text, it will produce the error you see. The text should read:

cut -d \space space -f ...

so leave out the apostrophes and just add an extra space after the \ char. The \ escapes the special meaning of the first space and the missing second space separates -d char from the -f option. Another way is:

cut -d ' ' -f ...

which is probably more intuitive and translates cleanly:

#!/usr/bin/sh
# Count print requests by printer

export PATH=/usr/bin
ACTIVE=0
JOBS=0
for PRINTQ in $(echo /var/spool/lp/request/*)
do
QUEUENAME=$(basename $PRINTQ)
QTY=$(ls $PRINTQ/d* 2>/dev/null | wc -l | cut -d ' ' -f 1)
if [ $QTY -gt 0 ]
then
let ACTIVE=$ACTIVE+1
print "Printer $QUEUENAME = \c"
print "$QTY request\c"
[[ $QTY -ne 1 ]] && print "s\c"
let JOBS=$JOBS+$QTY
print ""
fi
done
print "\nTotal jobs=$JOBS, printers with a queue=$ACTIVE"

(I checked a cut-n-paste from the above and it works OK)


Bill Hassell, sysadmin
Tracy Force
Occasional Contributor

Re: Shell Script to report total print jobs for each que

can someone explain what this line is doing?.
[[ $QTY -ne 1 ]] && print "s\c"
thanks
Bill Hassell
Honored Contributor

Re: Shell Script to report total print jobs for each que

The [[ $QTY -ne 1 ]] && print "s\c" line is a quick way to improve spelling. Instead of always saying "1 requests or "2 requests", the letter s is appended only when the quantity of requests is not 1. The 2 square brackets are used as an alternate way to test a condition, and the double ampersands is a shorthand way to say: do this if true. If two vertical bars were used, it would signify: do this if false. Both constructs can be used on the same line. The "s\c" means print "s" but do not add a new line, just leave the cursor where it is.


Bill Hassell, sysadmin