job-page-limit
zakmck at NOSPAMvirgilio.it
zakmck at NOSPAMvirgilio.it
Tue Jun 22 09:12:45 PDT 2004
Hi all,
the lpadmin quotas option doesn't seem to work. For instance the command:
lpadmin -p printer -o job-quota-period=60 -o job-page-limit=2
actually produces a change in /etc/cups/printers.conf, but any control is indeed applies, you may continue to print many more than 2 ppm.
Does anyone know something about?
For one interested, I've just finished (aka: NOT so much tested!) a PHP script, which reaches same goal by looking at page_log file (and invoking lprm). Please Find Attached.
Marco Brandizi
==========================================================
<?php
/**
* (C) 2004 Marco Brandizi
* This code is free and may be distributed under terms of GPL licence
*
* Controls CUPS printing, using page log.
* Allow to stop all jobs such that printed pages per minute, from a single
* host are over a given limit.
*
* REQUIRES access to lprm command and page log file
*
* INSTALLATION
* You can start once (for ex. sudden after cups) and will loop forever, waiting
* for page log lines
*
* BUGS
* - Works looking in log file and using lprm to remove
* jobs, too simple to notify back to sender a refuse
* - Still not so much tested, for ex. I'm not sure about CUPS page log
* format
*
*/
define ( 'CUPS_PAGE_LOG', '/var/log/cups/page_log' );
define ( 'PAGES_PER_MINUTE', 4 );
$logfd = popen ( "tail -n 1 -f " . CUPS_PAGE_LOG, 'r' );
// DEBUG $logfd = fopen ( CUPS_PAGE_LOG, 'r' );
$pages = array();
while ( !feof ( $logfd ) ) // should be forever
{
$time = time ();
// Cleans jobs older than a minute
foreach ( $pages as $host => $entry )
if ( $time - $entry [ 'tstart' ] > 60 ) {
// DEBUG echo "host $host removed from check cache, start = {$entry [ 'tstart' ]}, time = $time, diff = " . ( $time - $entry [ 'tstart' ] ) . "\n";
unset ( $pages [ $host ] );
}
// Parse it
$line = cfgets ( $logfd );
$flds = null;
// printer user job-id date-time page-number num-copies job-billing hostname
// XRX_0000AA7068DF brandizi 3 [22/Jun/2004:12:28:02 +0200] 1 1 - stayton.btbs.unimib.it
if ( ! preg_match ( '/(.*) .* (.*) \[(.*) .*\] ([0-9]*) ([0-9]*) - (.*)/', $line, $flds ) ) {
echo "Warning! Invalid line:\n$line\n";
continue;
}
$printer = $flds [ 1 ];
$jobid = $flds [ 2 ];
$date = $flds [ 3 ];
$page = $flds [ 4 ];
$copies = $flds [ 5 ]; if ( $copies == 0 ) $copies = 1;
$host = $flds [ 6 ];
$tstamp = -1;
// Normalized date 1D 2M 3Y 4H 5m 6s
if ( !preg_match ( '/([0-9]{1,2})\/(.{1,3})\/([0-9]{4}):([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})/', $date, $dtItms ) )
echo "Warning! Invalid date: $date\n";
else
$tstamp = strtotime ( "{$dtItms [ 2 ]} {$dtItms [ 1 ]} {$dtItms [ 3 ]} {$dtItms [ 4 ]}:{$dtItms [ 5 ]}:{$dtItms [ 6 ]}" );
// DEBUG echo "printer: $printer, job id: $jobid, date: $date ($tstamp), page: $page, host: $host\n";
// Per host page counters
if ( !$pages [ $host ] ) {
$pages [ $host ] [ 'tstart' ] = $tstamp;
}
$pages [ $host ] [ 'pages' ] += $copies;
// echo "$host $host has printed {$pages [ $host ]['pages']} pages since {$pages [ $host ]['tstart']}\n";
// No more than x pages from a single host
if ( $pages [ $host ] [ 'pages' ] > PAGES_PER_MINUTE )
{
$cmd = "lprm -P $printer $jobid";
if ( !system ( $cmd, $err ) )
echo "Job $jobid from host $host killed, too many pages\n";
else
echo "Warning! Could not issue: $cmd, error $err\n";
//DEBUG $time = $tstamp;
}
}
/** Returns the line without endings */
function cfgets ( $fd, $len = 1024 )
{
$line = fgets ( $fd, $len );
if ( !$line ) return '';
$len = strlen ( $line );
$c = $line { $len };
return $c == '\n' || $c == EOF ? substr ( $line, 0, $len - 1 ) : $line;
}
?>
More information about the cups
mailing list