[sysadmin] cupsquota.sh
Sometimes I want to know how many pages some user printed on some printer on
the network from some host on the network. This script tells me just that.
#!/bin/sh
# script to calculate some statistics from a cups page_log
#
# takes two (optional) arguments: a cups page_log and a username
#
# page_log is expected to be in the following format:
#
# samsung sven 279 [23/Nov/2007:18:44:01 +0100] 1 1 - localhost
# samsung sven 280 [23/Nov/2007:20:09:55 +0100] 1 1 - localhost
# HP loes 281 [25/Nov/2007:16:21:18 +0100] 1 1 - localhost
# HP loes 281 [25/Nov/2007:16:21:25 +0100] 2 1 - localhost
# HP loes 281 [25/Nov/2007:16:21:30 +0100] 3 1 - localhost
# HP loes 281 [25/Nov/2007:16:21:34 +0100] 4 1 - localhost
# HP loes 281 [25/Nov/2007:16:21:41 +0100] 5 1 - localhost
# HP loes 281 [25/Nov/2007:16:21:46 +0100] 6 1 - localhost
# HP loes 281 [25/Nov/2007:16:21:49 +0100] 7 1 - localhost
# samsung loes 282 [25/Nov/2007:23:43:22 +0100] 1 1 - localhost
# samsung sven 283 [28/Nov/2007:18:15:30 +0100] 1 1 - localhost
# samsung sven 283 [28/Nov/2007:18:15:32 +0100] 2 1 - localhost
# samsung sven 284 [28/Nov/2007:18:17:42 +0100] 1 1 - localhost
# samsung sven 284 [28/Nov/2007:18:17:46 +0100] 2 1 - localhost
# samsung loes 285 [30/Nov/2007:09:02:14 +0100] 1 1 - localhost
# samsung sven 286 [30/Nov/2007:14:29:33 +0100] 1 1 - localhost
# samsung sven 286 [30/Nov/2007:14:29:36 +0100] 2 1 - localhost
# samsung loes 287 [30/Nov/2007:18:24:37 +0100] 1 1 - localhost
if [ $# -eq 0 ]
then
USERNAME=${USER}
PAGELOG=/var/log/cups/page_log
elif [ $# -eq 1 ]
then
if [ -f $1 ]
then
USERNAME=${USER}
PAGELOG=$1
else
USERNAME=$1
PAGELOG=/var/log/cups/page_log
fi
elif [ $# -eq 2 ]
then
if [ -f $1 ]
then
PAGELOG=$1
USERNAME=$2
else
PAGELOG=$2
USERNAME=$1
fi
else
echo "Usage:"
echo "$0 [
exit 1
fi
/usr/bin/awk -v username=${USERNAME} '
BEGIN {
pagecount = 0
pagenum = 0
if (!username) {
username = ENVIRON["USER"]
}
}
$2 ~ username {
if ( $6 !~ "total" ) { # HACK :-(
if ( $6 == 1 ) {
pagecount += pagenum
}
pagenum = $6
}
}
END {
printf "%s has printed %d pages in total\n", username, pagecount
}
' ${PAGELOG}
It’s an awk
program with a bourne shell wrapper to calculate the total amount of pages a
user has printed on a host using cups.