#!/usr/bin/perl # # catridge_usage.pl # This Perl script parses the page_log file generated by CUPS # generating page statistics by printer, user and dates, and by # using a catridge log (in the format shown below) summarizes # pages print by cartridge. # # Usage: # # The supported command line parameters are: # # -P printer: chooses the printer (defaults to any) # -u user: chooses the user (defaults to any) # -c clog: chooses the cartridge log (defaults to /var/log/cups/cart_log) # -p cupslog: chooses the cups page log (defaults to /var/log/cups/page_log) # # The cartridge log should be as follows: # # 01/Jan/2003:12:00:00 - Other # 24/Mar/2004:13:00:00 - HP original # 16/Apr/2004:14:34:00 - MaxPrint (1) # 26/Apr/2004:18:00:00 - MaxPrint (2) # # Check http://www.feferraz.net/en/cartusage.html for updates or mail # pessoal at feferraz.net . # # History # - 2006-01 - Created script # - 2008-02 - Incorporated bug-fix and suggestions as per # e-mail from Max-Gerd Retzlaff m.retzlaff at gmx.net # use Time::Local; use Getopt::Std; getopt("Pcup"); if (defined($opt_P)) { $printer = $opt_P; } else { $printer = '.*'; } if (defined($opt_c)) { $cartlog = $opt_c; } else { $cartlog = '/var/log/cups/cart_log'; } if (defined($opt_p)) { $cupslog = $opt_p; } else { $cupslog = '/var/log/cups/page_log'; } if (defined($opt_u)) { $user = $opt_u; } else { $user = '\w+' } open(PAGELOG,$cupslog) || die("Not able to read cups' page log: $!\n"); open(CLOG,$cartlog) || die("Impossible to open cartridge's log: $!\n"); %months_hash = ( Jan => 0, Feb => 1, Mar => 2, Apr => 3, May => 4, Jun => 5, Jul => 6, Aug => 7, Sep => 8, Oct => 9, Nov => 10, Dec => 11 ); $c = -1; while ($inp = ) { if ($inp =~ m/(\d\d)\/(\w\w\w)\/(\d\d\d\d):(\d\d):(\d\d):(\d\d)\s-\s(.*)$/) { $c++; ($day,$month,$year,$H,$M,$S,$cartucho) = ($1,$months_hash{$2},$3,$4,$5,$6,$7); $cart_install[$c] = timegm($S,$M,$H,$day,$month,$year); $cart_names[$c] = $cartucho; } } if ($c == -1) { die("Couldn't find any useful information on $cartlog.\n"); } close(CLOG); while ($inp = ) { if ($inp =~ m/^$printer\s$user\s.*\[(\d\d)\/(\w\w\w)\/(\d\d\d\d):(\d\d):(\d\d):(\d\d).*\]\s\d+\s(\d+)/) { ($day,$month,$year,$H,$M,$S,$pages) = ($1,$months_hash{$2},$3,$4,$5,$6,$7); $tot += $pages; $time = timegm($S,$M,$H,$day,$month,$year); $CAUGHT=0; for ($i = 0;$i <= $c && $CAUGHT==0;$i++) { if ($time < $cart_install[$i]) { $cart_pages[$i-1]+= $pages; $CAUGHT=1; } } if ($time > $cart_install[$c]) { $cart_pages[$c]+=$pages; $CAUGHT=1; } if ($CAUGHT == 1) { $totc+=$pages; } } } close(PAGELOG); unless (defined($tot)) { die("No pages printed with the specified printer by the specified user.\n") } print "Num \t Cartridge\n"; for ($i =0;$i <= $c;$i++) { if (defined($cart_pages[$i])) { print "$cart_pages[$i] \t $cart_names[$i]\n"; } } print "Total: $tot pages.\n "; if ($tot != $totc) { print "Problems. Individual totals do not add up to nominal total. Verify possible inconsistence in your cartdrige log.\n"; }