Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Print Queue Auto-email help

Status
Not open for further replies.
May 5, 2007
2
CA
Hello all,

So here is a little background information as to my troubles. A previous Unix admin I worked with wrote a script for me to notify me via email if any of the print queues were down on a specific Unix box.

The script works great... it tries to re-enable the queue and emails me the results. Problem is, I get over 150 emails a day and a good 99% of them were all able to successfully reenable all 'downed' queues.

My problem is that my current Unix admin is either swamped with work, or is inept, (I'd vouch for the latter) as my ongoing request for months has been to modify this script to ONLY email me if the re-enable of a particular queue was unsuccessful... I figure it will eliminate about 140 or so of those emails from my inbox.

the script is as follows:

Code:
#!/bin/ksh
#-----------------------------------------------------------------------------#
# SCRIPT NAME:
#
# CREATED BY:           [edited]
#
# PURPOSE:              Check for DOWN print queues
#
# VERSION:              1.00
#
# DEPENDANCIES:         None.
#------------------------------------------------------------------------------#

# MODIFICATION HISTORY:
#
# DATE(YYYY-MM-DD)      NAME            DESCRIPTION
# 2006-08-24            JP              Initial
#------------------------------------------------------------------------------#

# VARIABLES:

HOSTNAME=$(uname -n)                    # <Host Name>
PROG_NAME=$0                            # <Program Name>
#------------------------------------------------------------------------------#


# FUNCTIONS:

function readDATA
{
echo $data
    QUEUE=`echo $data | awk '{print $1}'`
    STATE=`echo $data | awk '{print $3}'`
    if [[ $STATE != "DEV_BUSY" ]]
    then
        enable $QUEUE
        if [[ $? = 0 ]]
        then
            echo "$?"
            echo "enable $QUEUE command successful"     >> /tmp/lpstat_down.txt.
$TS
        else
            echo "enable $QUEUE command failed"         >> /tmp/lpstat_down.txt.
$TS
        fi
    fi
}
#------------------------------------------------------------------------------#


#---------------------------------------------------------------------

## Begin script.

TS=`date +"%H%M`
/usr/bin/lpstat -W | grep -E "DOWN|DEV_BUSY" > /tmp/lpstat.out.$TS

SIZE=`ls -l /tmp/lpstat.out.$TS | awk '{print $5}'`

if [[ $SIZE != "0" ]]
then
    cp /tmp/lpstat.out.$TS /tmp/lpstat.txt.$TS

    banner "printer" issues             > /tmp/lpstat_down.txt.$TS

    echo "Host ==>         $HOSTNAME"   >> /tmp/lpstat_down.txt.$TS
    echo "GENERATED BY ==> $PROG_NAME"  >> /tmp/lpstat_down.txt.$TS
    echo ""                             >> /tmp/lpstat_down.txt.$TS
    echo "Queue   Dev   Status   "      >> /tmp/lpstat_down.txt.$TS
    echo "------- ----- ---------"      >> /tmp/lpstat_down.txt.$TS
    cat /tmp/lpstat.out.$TS                 >> /tmp/lpstat_down.txt.$TS
    echo ""                             >> /tmp/lpstat_down.txt.$TS
    echo "Will enable DOWNed queues"    >> /tmp/lpstat_down.txt.$TS
    echo ""                             >> /tmp/lpstat_down.txt.$TS

    while read -r data
    do
        readDATA
    done < /tmp/lpstat.txt.$TS

    MAIL_SUBJ='Print Queue(s) Problems'
    mail -s "$MAIL_SUBJ" username@domain.com < /tmp/lpstat_down.txt.$TS
fi

rm /tmp/lpstat.out.$TS
rm /tmp/lpstat_down.txt.$TS
rm /tmp/lpstat.txt.$TS

exit

I realise you guys/gals aren't paid to do the unix admin that i work with's job, however, if you could point me in the right direction so I can *try* and edit this myself I would appreciate it. I just wanna rid my inbox of a whole bunch of non-necessary mail.

I have a feeling that it will only be a few simple GREP commands to looks for 'unsuccessful' or something, but i'm not sure how to integrate it.
 
Note: This suggested fix has not been tested - so your results may vary...

See the red sections for the suggested changes.

One flaw I see with these changes - if you have a successful and a failed queue restart, the last change (success or failure) will determine if an email is sent.

Code:
#!/bin/ksh
#-----------------------------------------------------------------------------#
# SCRIPT NAME:
#
# CREATED BY:           [edited]
#
# PURPOSE:              Check for DOWN print queues
#
# VERSION:              1.00
#
# DEPENDANCIES:         None.
#------------------------------------------------------------------------------#

# MODIFICATION HISTORY:
#
# DATE(YYYY-MM-DD)      NAME            DESCRIPTION
# 2006-08-24            JP              Initial
#------------------------------------------------------------------------------#

# VARIABLES:

HOSTNAME=$(uname -n)                    # <Host Name>
PROG_NAME=$0                            # <Program Name>
[COLOR=#ff0000]MAIL_FLAG="SEND"			# default is to send[/color]
#------------------------------------------------------------------------------#


# FUNCTIONS:

function readDATA
{
echo $data
    QUEUE=`echo $data | awk '{print $1}'`
    STATE=`echo $data | awk '{print $3}'`
    if [[ $STATE != "DEV_BUSY" ]]
    then
        enable $QUEUE
        if [[ $? = 0 ]]
        then
            echo "$?"
            echo "enable $QUEUE command successful"     >> /tmp/lpstat_down.txt.$TS
          [COLOR=#ff0000]  MAIL_FLAG="NOMAIL"[/color]
        else
            echo "enable $QUEUE command failed"         >> /tmp/lpstat_down.txt.$TS
 [COLOR=#ff0000]           MAIL_FLAG="SEND"[/color]
        fi
    fi
}
#------------------------------------------------------------------------------#


#---------------------------------------------------------------------

## Begin script.

TS=`date +"%H%M`
/usr/bin/lpstat -W | grep -E "DOWN|DEV_BUSY" > /tmp/lpstat.out.$TS

SIZE=`ls -l /tmp/lpstat.out.$TS | awk '{print $5}'`

if [[ $SIZE != "0" ]]
then
    cp /tmp/lpstat.out.$TS /tmp/lpstat.txt.$TS

    banner "printer" issues             > /tmp/lpstat_down.txt.$TS

    echo "Host ==>         $HOSTNAME"   >> /tmp/lpstat_down.txt.$TS
    echo "GENERATED BY ==> $PROG_NAME"  >> /tmp/lpstat_down.txt.$TS
    echo ""                             >> /tmp/lpstat_down.txt.$TS
    echo "Queue   Dev   Status   "      >> /tmp/lpstat_down.txt.$TS
    echo "------- ----- ---------"      >> /tmp/lpstat_down.txt.$TS
    cat /tmp/lpstat.out.$TS                 >> /tmp/lpstat_down.txt.$TS
    echo ""                             >> /tmp/lpstat_down.txt.$TS
    echo "Will enable DOWNed queues"    >> /tmp/lpstat_down.txt.$TS
    echo ""                             >> /tmp/lpstat_down.txt.$TS

    while read -r data
    do
        readDATA
    done < /tmp/lpstat.txt.$TS

[COLOR=#ff0000]    if [[ $MAIL_FLAG = "SEND" ]]
    then
      MAIL_SUBJ='Print Queue(s) Problems'
      mail -s "$MAIL_SUBJ" username@domain.com < /tmp/lpstat_down.txt.$TS
    fi[/color]
fi

rm /tmp/lpstat.out.$TS
rm /tmp/lpstat_down.txt.$TS
rm /tmp/lpstat.txt.$TS

exit
 
Ignore my previous suggested changes, this one will only send email if a queue restart has failed. See code in red for the changes.

Code:
#!/bin/ksh
#-----------------------------------------------------------------------------#
# SCRIPT NAME:
#
# CREATED BY:           [edited]
#
# PURPOSE:              Check for DOWN print queues
#
# VERSION:              1.00
#
# DEPENDANCIES:         None.
#------------------------------------------------------------------------------#

# MODIFICATION HISTORY:
#
# DATE(YYYY-MM-DD)      NAME            DESCRIPTION
# 2006-08-24            JP              Initial
#------------------------------------------------------------------------------#

# VARIABLES:

HOSTNAME=$(uname -n)                    # <Host Name>
PROG_NAME=$0                            # <Program Name>
#------------------------------------------------------------------------------#


# FUNCTIONS:

function readDATA
{
echo $data
    QUEUE=`echo $data | awk '{print $1}'`
    STATE=`echo $data | awk '{print $3}'`
    if [[ $STATE != "DEV_BUSY" ]]
    then
        enable $QUEUE
        if [[ $? = 0 ]]
        then
            echo "$?"
            echo "enable $QUEUE command successful"     >> /tmp/lpstat_down.txt.$TS
        else
            echo "enable $QUEUE command failed"         >> /tmp/lpstat_down.txt.$TS
            MAIL_FLAG="SEND"
        fi
    fi
}
#------------------------------------------------------------------------------#


#---------------------------------------------------------------------

## Begin script.

TS=`date +"%H%M`
/usr/bin/lpstat -W | grep -E "DOWN|DEV_BUSY" > /tmp/lpstat.out.$TS

SIZE=`ls -l /tmp/lpstat.out.$TS | awk '{print $5}'`

if [[ $SIZE != "0" ]]
then
    cp /tmp/lpstat.out.$TS /tmp/lpstat.txt.$TS

    banner "printer" issues             > /tmp/lpstat_down.txt.$TS

    echo "Host ==>         $HOSTNAME"   >> /tmp/lpstat_down.txt.$TS
    echo "GENERATED BY ==> $PROG_NAME"  >> /tmp/lpstat_down.txt.$TS
    echo ""                             >> /tmp/lpstat_down.txt.$TS
    echo "Queue   Dev   Status   "      >> /tmp/lpstat_down.txt.$TS
    echo "------- ----- ---------"      >> /tmp/lpstat_down.txt.$TS
    cat /tmp/lpstat.out.$TS                 >> /tmp/lpstat_down.txt.$TS
    echo ""                             >> /tmp/lpstat_down.txt.$TS
    echo "Will enable DOWNed queues"    >> /tmp/lpstat_down.txt.$TS
    echo ""                             >> /tmp/lpstat_down.txt.$TS

    while read -r data
    do
        readDATA
    done < /tmp/lpstat.txt.$TS

[COLOR=#ff0000]    grep -i failed /tmp/lpstat_down.txt.$TS > /dev/null
    if [[ $? = 0 ]]
    then
      MAIL_SUBJ='Print Queue(s) Problems'
      mail -s "$MAIL_SUBJ" username@domain.com < /tmp/lpstat_down.txt.$TS
    fi[/color]
fi

rm /tmp/lpstat.out.$TS
rm /tmp/lpstat_down.txt.$TS
rm /tmp/lpstat.txt.$TS

exit
 
eek - forgot the closing [/code] in my last post. Oh well - the important part is in red anyway. I really should be sleeping and not hacking on the computer at 0-dark-30.
 
thanks.. i made the suggested changes and ran the script:

-----

results

-----

Code:
 #####   #####      #    #    #   #####  ######  #####
 #    #  #    #     #    ##   #     #    #       #    #
 #    #  #    #     #    # #  #     #    #####   #    #
 #####   #####      #    #  # #     #    #       #####
 #       #   #      #    #   ##     #    #       #   #
 #       #    #     #    #    #     #    ######  #    #


    #     ####    ####   #    #  ######   ####
    #    #       #       #    #  #       #
    #     ####    ####   #    #  #####    ####
    #         #       #  #    #  #            #
    #    #    #  #    #  #    #  #       #    #
    #     ####    ####    ####   ######   ####

Host ==>         server1
GENERATED BY ==> /home/sysadmin/lpstat_query_li.ksh

Queue   Dev   Status   
------- ----- ---------
np_kb404             @shnp1         DOWN     
np_kb404             PORT1          HOST_DOWN
q14c                 hp@pqjd9       DOWN     
zu3c                 hp@zumjd3      DEV_BUSY  103025 /wmstor/proda/p23/ wmsprod                 2   1   1
zu3p                 hp@zumjd3      DEV_BUSY  151528 /wmstor/proda/p23/ wmsprod                 2   1   1
dg3                  PORT1          HOST_DOWN
dg3c                 PORT1          HOST_DOWN
dg3d                 PORT1          HOST_DOWN
o6c                  hp@onjd5       DEV_BUSY  135804 /wmstor/proda/p15/ csr15                   4   1   1
o6d                  hp@onjd5       DEV_BUSY  156901 rpt_route_status.l csr11                  17   1   1
o6p                  hp@onjd5       DEV_BUSY  139500 /wmstor/proda/p12/ sal004                 29   1   1
mx2                  PORT1          HOST_DOWN
mx2c                 PORT1          HOST_DOWN
mx2d                 PORT1          HOST_DOWN
hptestp              PORT1          HOST_DOWN
hptest               PORT1          HOST_DOWN
hptestc              PORT1          HOST_DOWN
hptestd              PORT1          HOST_DOWN
72md14               @mcdri1        DOWN     
72md14               PORT1          HOST_DOWN
72md16               @mcdri2        DOWN     
72md16               PORT1          HOST_DOWN

Will enable DOWNed queues

enable np_kb404 command successful
enable np_kb404 command successful
enable q14c command successful
enable dg3 command successful
enable dg3c command successful
enable dg3d command successful
enable mx2 command successful
enable mx2c command successful
enable mx2d command successful
enable hptestp command successful
enable hptest command successful
enable hptestc command successful
enable hptestd command successful
enable 72md14 command successful
enable 72md14 command successful
enable 72md16 command successful
enable 72md16 command successful
--------

it still emailed me even tho all commands were successful...

i made the following change to the script:

Code:
    MAIL_SUBJ='Print Queue(s) Problems'

    mail -s "$MAIL_SUBJ" username@domain.com < /tmp/lpstat_down.txt.$TS

TO

Code:
    grep -i failed /tmp/lpstat_down.txt.$TS > /dev/null

    if [[ $? = 0 ]]

    then

      MAIL_SUBJ='Print Queue(s) Problems'

      mail -s "$MAIL_SUBJ" username@domain.com < /tmp/lpstat_down.txt.$TS

    fi
 
I have checked this section of code with a script on my system:

Code:
  grep -i failed /tmp/lpstat_down.txt.$TS > /dev/null

    if [[ $? = 0 ]]

    then

      MAIL_SUBJ='Print Queue(s) Problems'

      mail -s "$MAIL_SUBJ" username@domain.com < /tmp/lpstat_down.txt.$TS

    fi

and it works as expected. If it finds "failed" in the lpstat_down... file, it emails, otherwise it doesnt.

Can you provide some more info:

1.) What flavor *nix?
2.) Comment out the rm commands at the end
run the script
manually issue the grep command (as it appears in the script)
then do an "echo $?" immediately after the grep - what is the result?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top