1 | #! /bin/bash |
---|
2 | # |
---|
3 | # PyKota : Print Quotas for CUPS |
---|
4 | # |
---|
5 | # (c) 2003-2009 Jerome Alet <alet@librelogiciel.com> |
---|
6 | # This program is free software: you can redistribute it and/or modify |
---|
7 | # it under the terms of the GNU General Public License as published by |
---|
8 | # the Free Software Foundation, either version 3 of the License, or |
---|
9 | # (at your option) any later version. |
---|
10 | # |
---|
11 | # This program is distributed in the hope that it will be useful, |
---|
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | # GNU General Public License for more details. |
---|
15 | # |
---|
16 | # You should have received a copy of the GNU General Public License |
---|
17 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
18 | # |
---|
19 | # $Id$ |
---|
20 | # |
---|
21 | # First version by Matt Hyclak & Jerome Alet |
---|
22 | # Rewritten from scratch, and contributed to PyKota, by Christian Andreetta |
---|
23 | # and Paolo Nobili |
---|
24 | |
---|
25 | # If ending phase, after the job has been fully transmitted to the printer |
---|
26 | # we have to wait for the printer being in printing mode before checking |
---|
27 | # if it is idle, otherwise we could have problems with slow printers. |
---|
28 | #-------------------------------------------------------------------------- |
---|
29 | PATH=$PATH:/bin:/usr/bin:/usr/local/bin:/opt/bin:/sbin:/usr/sbin |
---|
30 | #-------------------------------------------------------------------------- |
---|
31 | LOG_FILE=/var/log/pykota-waitprinter-snmpread.log |
---|
32 | LOG_DO_FLAG="" #set different from null for file logging |
---|
33 | IDLE_WAIT_NUM=3 #number of snmp reads to consider the printer _really_ idle |
---|
34 | SNMP_DELAY=2 #seconds between snmp reads |
---|
35 | #-------------------------------------------------------------------------- |
---|
36 | # END OF CONF |
---|
37 | #-------------------------------------------------------------------------- |
---|
38 | function log_msg() { |
---|
39 | if [ -n "${LOG_DO_FLAG}" ]; then |
---|
40 | echo `date` "$*" >> ${LOG_FILE} |
---|
41 | fi |
---|
42 | } |
---|
43 | #-------------------------------------------------------------------------- |
---|
44 | function printer_status_get() { |
---|
45 | local printer="$1" |
---|
46 | local comm="$2" |
---|
47 | local version="$3" |
---|
48 | PRINTER_STATUS=`snmpget -v${version} -c ${comm} -Ov ${printer} HOST-RESOURCES-MIB::hrPrinterStatus.1` |
---|
49 | } |
---|
50 | #-------------------------------------------------------------------------- |
---|
51 | function printing_wait() { |
---|
52 | local printer="$1" |
---|
53 | local comm="$2" |
---|
54 | local version="$3" |
---|
55 | log_msg "CYCLE: printing_wait" |
---|
56 | while [ 1 ]; do |
---|
57 | printer_status_get "${printer}" "${comm}" "${version}" |
---|
58 | log_msg "${PRINTER_STATUS}" |
---|
59 | echo "${PRINTER_STATUS}" | grep -iE '(printing)|(idle)' >/dev/null && break |
---|
60 | sleep ${SNMP_DELAY} |
---|
61 | done |
---|
62 | } |
---|
63 | #-------------------------------------------------------------------------- |
---|
64 | function idle_wait() { |
---|
65 | local printer="$1" idle_num=0 idle_flag=0 |
---|
66 | local comm="$2" |
---|
67 | local version="$3" |
---|
68 | log_msg "CYCLE: idle_wait" |
---|
69 | while [ ${idle_num} -lt ${IDLE_WAIT_NUM} ]; do |
---|
70 | printer_status_get "${printer}" "${comm}" "${version}" |
---|
71 | log_msg "${PRINTER_STATUS}" |
---|
72 | idle_flag=0 |
---|
73 | echo "${PRINTER_STATUS}" | grep -iE '(idle)' >/dev/null && idle_flag=1 |
---|
74 | if [ ${idle_flag} -gt 0 ]; then |
---|
75 | idle_num=$((idle_num+1)) |
---|
76 | else |
---|
77 | idle_num=0 |
---|
78 | fi |
---|
79 | sleep ${SNMP_DELAY} |
---|
80 | done |
---|
81 | } |
---|
82 | #-------------------------------------------------------------------------- |
---|
83 | function main() { |
---|
84 | local printer="$1" |
---|
85 | local comm="$2" |
---|
86 | local version="$3" |
---|
87 | if [ x${comm} == "x" ]; then |
---|
88 | comm="public" |
---|
89 | fi |
---|
90 | if [ x${version} == "x" ]; then |
---|
91 | version="1" |
---|
92 | fi |
---|
93 | log_msg "BEGIN" |
---|
94 | ##log_msg "`ls -la /var/spool/{cups,samba}`" |
---|
95 | log_msg "Pykota Phase: ${PYKOTAPHASE}" |
---|
96 | if [ x$PYKOTASTATUS != "xCANCELLED" ] && [ x$PYKOTAACTION = "xALLOW" ] && [ x$PYKOTAPHASE = "xAFTER" ] ; then |
---|
97 | printing_wait "${printer}" "${comm}" "${version}" |
---|
98 | fi |
---|
99 | idle_wait "${printer}" "${comm}" "${version}" #in any case |
---|
100 | ##log_msg "`ls -la /var/spool/{cups,samba}`" |
---|
101 | log_msg "END" |
---|
102 | } |
---|
103 | #========================================================================== |
---|
104 | # MAIN |
---|
105 | #========================================================================== |
---|
106 | PRINTER_STATUS="" |
---|
107 | main "$1" "$2" "$3" |
---|
108 | exit $? |
---|