[3389] | 1 | #! /bin/bash |
---|
[1095] | 2 | # |
---|
[3260] | 3 | # PyKota : Print Quotas for CUPS |
---|
[1095] | 4 | # |
---|
[3561] | 5 | # (c) 2003-2013 Jerome Alet <alet@librelogiciel.com> |
---|
[3260] | 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. |
---|
[3413] | 10 | # |
---|
[3260] | 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. |
---|
[3413] | 15 | # |
---|
[3260] | 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/>. |
---|
[1095] | 18 | # |
---|
| 19 | # $Id$ |
---|
| 20 | # |
---|
[1723] | 21 | # First version by Matt Hyclak & Jerome Alet |
---|
| 22 | # Rewritten from scratch, and contributed to PyKota, by Christian Andreetta |
---|
| 23 | # and Paolo Nobili |
---|
[1398] | 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. |
---|
[1723] | 28 | #-------------------------------------------------------------------------- |
---|
[1814] | 29 | PATH=$PATH:/bin:/usr/bin:/usr/local/bin:/opt/bin:/sbin:/usr/sbin |
---|
[1723] | 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 |
---|
[1731] | 34 | SNMP_DELAY=2 #seconds between snmp reads |
---|
[1723] | 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" |
---|
[2614] | 46 | local comm="$2" |
---|
| 47 | local version="$3" |
---|
| 48 | PRINTER_STATUS=`snmpget -v${version} -c ${comm} -Ov ${printer} HOST-RESOURCES-MIB::hrPrinterStatus.1` |
---|
[1723] | 49 | } |
---|
| 50 | #-------------------------------------------------------------------------- |
---|
| 51 | function printing_wait() { |
---|
| 52 | local printer="$1" |
---|
[2614] | 53 | local comm="$2" |
---|
| 54 | local version="$3" |
---|
| 55 | log_msg "CYCLE: printing_wait" |
---|
[1723] | 56 | while [ 1 ]; do |
---|
[2614] | 57 | printer_status_get "${printer}" "${comm}" "${version}" |
---|
[1723] | 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 |
---|
[2614] | 66 | local comm="$2" |
---|
| 67 | local version="$3" |
---|
| 68 | log_msg "CYCLE: idle_wait" |
---|
[1723] | 69 | while [ ${idle_num} -lt ${IDLE_WAIT_NUM} ]; do |
---|
[2614] | 70 | printer_status_get "${printer}" "${comm}" "${version}" |
---|
[1723] | 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" |
---|
[2614] | 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 |
---|
[1723] | 93 | log_msg "BEGIN" |
---|
| 94 | ##log_msg "`ls -la /var/spool/{cups,samba}`" |
---|
| 95 | log_msg "Pykota Phase: ${PYKOTAPHASE}" |
---|
[2584] | 96 | if [ x$PYKOTASTATUS != "xCANCELLED" ] && [ x$PYKOTAACTION = "xALLOW" ] && [ x$PYKOTAPHASE = "xAFTER" ] ; then |
---|
[2614] | 97 | printing_wait "${printer}" "${comm}" "${version}" |
---|
[1723] | 98 | fi |
---|
[2614] | 99 | idle_wait "${printer}" "${comm}" "${version}" #in any case |
---|
[1723] | 100 | ##log_msg "`ls -la /var/spool/{cups,samba}`" |
---|
| 101 | log_msg "END" |
---|
| 102 | } |
---|
| 103 | #========================================================================== |
---|
| 104 | # MAIN |
---|
| 105 | #========================================================================== |
---|
| 106 | PRINTER_STATUS="" |
---|
[2614] | 107 | main "$1" "$2" "$3" |
---|
[1723] | 108 | exit $? |
---|