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

Revision 3133, 4.0 kB (checked in by jerome, 17 years ago)

Changed copyright years.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
Line 
1#! /bin/sh
2#
3# PyKota - Print Quotas for CUPS and LPRng
4#
5# (c) 2003, 2004, 2005, 2006, 2007 Jerome Alet <alet@librelogiciel.com>
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#
16# First version by Matt Hyclak & Jerome Alet
17# Rewritten from scratch, and contributed to PyKota, by Christian Andreetta
18# and Paolo Nobili
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.
23#--------------------------------------------------------------------------
24PATH=$PATH:/bin:/usr/bin:/usr/local/bin:/opt/bin:/sbin:/usr/sbin
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
29SNMP_DELAY=2    #seconds between snmp reads
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"
41        local comm="$2"
42        local version="$3"
43        PRINTER_STATUS=`snmpget -v${version} -c ${comm} -Ov ${printer} HOST-RESOURCES-MIB::hrPrinterStatus.1`
44}
45#--------------------------------------------------------------------------
46function printing_wait() {
47        local printer="$1"
48        local comm="$2"
49        local version="$3"
50        log_msg "CYCLE: printing_wait"
51        while [ 1 ]; do
52                printer_status_get "${printer}" "${comm}" "${version}"
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
61        local comm="$2"
62        local version="$3"
63        log_msg "CYCLE: idle_wait"
64        while [ ${idle_num} -lt ${IDLE_WAIT_NUM} ]; do
65                printer_status_get "${printer}" "${comm}" "${version}"
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"
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
88        log_msg "BEGIN"
89        ##log_msg "`ls -la /var/spool/{cups,samba}`"
90        log_msg "Pykota Phase: ${PYKOTAPHASE}"
91        if [ x$PYKOTASTATUS != "xCANCELLED" ] && [ x$PYKOTAACTION = "xALLOW" ] && [ x$PYKOTAPHASE = "xAFTER" ] ; then
92                printing_wait "${printer}" "${comm}" "${version}"
93        fi
94        idle_wait "${printer}" "${comm}" "${version}" #in any case
95        ##log_msg "`ls -la /var/spool/{cups,samba}`"
96        log_msg "END"
97}
98#==========================================================================
99# MAIN
100#==========================================================================
101PRINTER_STATUS=""
102main "$1" "$2" "$3"
103exit $?
Note: See TracBrowser for help on using the browser.