24 | | if [ x$PYKOTAACTION != "xDENY" ] && [ x$PYKOTAPHASE = "xAFTER" ] ; then |
25 | | until snmpget -v1 -c public -Ov $1 HOST-RESOURCES-MIB::hrPrinterStatus.1 | grep -i printing >/dev/null; do |
26 | | if [ "$KILLED" = 1 ]; then |
27 | | break ; |
28 | | fi |
29 | | sleep 1 ; |
30 | | done |
31 | | fi |
32 | | |
33 | | # In any case, wait until the printer is idle again. |
34 | | until snmpget -v1 -c public -Ov $1 HOST-RESOURCES-MIB::hrPrinterStatus.1 | grep -i idle >/dev/null ; do |
35 | | if [ "$KILLED" = 1 ]; then |
36 | | break ; |
37 | | fi |
38 | | sleep 1 ; |
39 | | done |
| 25 | #-------------------------------------------------------------------------- |
| 26 | LOG_FILE=/var/log/pykota-waitprinter-snmpread.log |
| 27 | LOG_DO_FLAG="" #set different from null for file logging |
| 28 | IDLE_WAIT_NUM=3 #number of snmp reads to consider the printer _really_ idle |
| 29 | SNMP_DELAY=1 #seconds between snmp reads |
| 30 | #-------------------------------------------------------------------------- |
| 31 | # END OF CONF |
| 32 | #-------------------------------------------------------------------------- |
| 33 | function log_msg() { |
| 34 | if [ -n "${LOG_DO_FLAG}" ]; then |
| 35 | echo `date` "$*" >> ${LOG_FILE} |
| 36 | fi |
| 37 | } |
| 38 | #-------------------------------------------------------------------------- |
| 39 | function printer_status_get() { |
| 40 | local printer="$1" |
| 41 | PRINTER_STATUS=`snmpget -v1 -c public -Ov ${printer} HOST-RESOURCES-MIB::hrPrinterStatus.1` |
| 42 | } |
| 43 | #-------------------------------------------------------------------------- |
| 44 | function printing_wait() { |
| 45 | local printer="$1" |
| 46 | log_msg "CICLE: printing_wait" |
| 47 | while [ 1 ]; do |
| 48 | printer_status_get "${printer}" |
| 49 | log_msg "${PRINTER_STATUS}" |
| 50 | echo "${PRINTER_STATUS}" | grep -iE '(printing)|(idle)' >/dev/null && break |
| 51 | sleep ${SNMP_DELAY} |
| 52 | done |
| 53 | } |
| 54 | #-------------------------------------------------------------------------- |
| 55 | function idle_wait() { |
| 56 | local printer="$1" idle_num=0 idle_flag=0 |
| 57 | log_msg "CICLE: idle_wait" |
| 58 | while [ ${idle_num} -lt ${IDLE_WAIT_NUM} ]; do |
| 59 | printer_status_get "${printer}" |
| 60 | log_msg "${PRINTER_STATUS}" |
| 61 | idle_flag=0 |
| 62 | echo "${PRINTER_STATUS}" | grep -iE '(idle)' >/dev/null && idle_flag=1 |
| 63 | if [ ${idle_flag} -gt 0 ]; then |
| 64 | idle_num=$((idle_num+1)) |
| 65 | else |
| 66 | idle_num=0 |
| 67 | fi |
| 68 | sleep ${SNMP_DELAY} |
| 69 | done |
| 70 | } |
| 71 | #-------------------------------------------------------------------------- |
| 72 | function main() { |
| 73 | local printer="$1" |
| 74 | log_msg "BEGIN" |
| 75 | ##log_msg "`ls -la /var/spool/{cups,samba}`" |
| 76 | log_msg "Pykota Phase: ${PYKOTAPHASE}" |
| 77 | if [ x$PYKOTAACTION != "xDENY" ] && [ x$PYKOTAPHASE = "xAFTER" ] ; then |
| 78 | printing_wait "${printer}" |
| 79 | fi |
| 80 | idle_wait "${printer}" #in any case |
| 81 | ##log_msg "`ls -la /var/spool/{cups,samba}`" |
| 82 | log_msg "END" |
| 83 | } |
| 84 | #========================================================================== |
| 85 | # MAIN |
| 86 | #========================================================================== |
| 87 | PRINTER_STATUS="" |
| 88 | main "$1" |
| 89 | exit $? |