root / pykota / trunk / bin / waitprinter.sh @ 2622

Revision 2622, 4.0 kB (checked in by jerome, 18 years ago)

Added 2006 to the copyright's years.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
RevLine 
[1095]1#! /bin/sh
2#
3# PyKota - Print Quotas for CUPS and LPRng
4#
[2622]5# (c) 2003, 2004, 2005, 2006 Jerome Alet <alet@librelogiciel.com>
[1095]6# You're welcome to redistribute this software under the
7# terms of the GNU General Public Licence version 2.0
8# or, at your option, any higher version.
9#
10# You can read the complete GNU GPL in the file COPYING
11# which should come along with this software, or visit
12# the Free Software Foundation's WEB site http://www.fsf.org
13#
14# $Id$
15#
[1723]16# First version by Matt Hyclak & Jerome Alet
17# Rewritten from scratch, and contributed to PyKota, by Christian Andreetta
18# and Paolo Nobili
[1398]19
20# If ending phase, after the job has been fully transmitted to the printer
21# we have to wait for the printer being in printing mode before checking
22# if it is idle, otherwise we could have problems with slow printers.
[1723]23#--------------------------------------------------------------------------
[1814]24PATH=$PATH:/bin:/usr/bin:/usr/local/bin:/opt/bin:/sbin:/usr/sbin
[1723]25#--------------------------------------------------------------------------
26LOG_FILE=/var/log/pykota-waitprinter-snmpread.log
27LOG_DO_FLAG=""  #set different from null for file logging
28IDLE_WAIT_NUM=3 #number of snmp reads to consider the printer _really_ idle
[1731]29SNMP_DELAY=2    #seconds between snmp reads
[1723]30#--------------------------------------------------------------------------
31# END OF CONF
32#--------------------------------------------------------------------------
33function log_msg() {
34        if [ -n "${LOG_DO_FLAG}" ]; then
35                echo `date` "$*" >> ${LOG_FILE}
36        fi
37}
38#--------------------------------------------------------------------------
39function printer_status_get() {
40        local printer="$1"
[2614]41        local comm="$2"
42        local version="$3"
43        PRINTER_STATUS=`snmpget -v${version} -c ${comm} -Ov ${printer} HOST-RESOURCES-MIB::hrPrinterStatus.1`
[1723]44}
45#--------------------------------------------------------------------------
46function printing_wait() {
47        local printer="$1"
[2614]48        local comm="$2"
49        local version="$3"
50        log_msg "CYCLE: printing_wait"
[1723]51        while [ 1 ]; do
[2614]52                printer_status_get "${printer}" "${comm}" "${version}"
[1723]53                log_msg "${PRINTER_STATUS}"
54                echo "${PRINTER_STATUS}" | grep -iE '(printing)|(idle)' >/dev/null && break
55                sleep ${SNMP_DELAY}
56        done
57}
58#--------------------------------------------------------------------------
59function idle_wait() {
60        local printer="$1" idle_num=0 idle_flag=0
[2614]61        local comm="$2"
62        local version="$3"
63        log_msg "CYCLE: idle_wait"
[1723]64        while [ ${idle_num} -lt ${IDLE_WAIT_NUM} ]; do
[2614]65                printer_status_get "${printer}" "${comm}" "${version}"
[1723]66                log_msg "${PRINTER_STATUS}"
67                idle_flag=0
68                echo "${PRINTER_STATUS}" | grep -iE '(idle)' >/dev/null && idle_flag=1
69                if [ ${idle_flag} -gt 0 ]; then
70                        idle_num=$((idle_num+1))
71                else
72                        idle_num=0
73                fi
74                sleep ${SNMP_DELAY}
75        done
76}
77#--------------------------------------------------------------------------
78function main() {
79        local printer="$1"
[2614]80        local comm="$2"
81        local version="$3"
82        if [ x${comm} == "x" ]; then
83                comm="public"
84        fi
85        if [ x${version} == "x" ]; then
86                version="1"
87        fi
[1723]88        log_msg "BEGIN"
89        ##log_msg "`ls -la /var/spool/{cups,samba}`"
90        log_msg "Pykota Phase: ${PYKOTAPHASE}"
[2584]91        if [ x$PYKOTASTATUS != "xCANCELLED" ] && [ x$PYKOTAACTION = "xALLOW" ] && [ x$PYKOTAPHASE = "xAFTER" ] ; then
[2614]92                printing_wait "${printer}" "${comm}" "${version}"
[1723]93        fi
[2614]94        idle_wait "${printer}" "${comm}" "${version}" #in any case
[1723]95        ##log_msg "`ls -la /var/spool/{cups,samba}`"
96        log_msg "END"
97}
98#==========================================================================
99# MAIN
100#==========================================================================
101PRINTER_STATUS=""
[2614]102main "$1" "$2" "$3"
[1723]103exit $?
Note: See TracBrowser for help on using the browser.