On Sat, 9 Feb 2002, Kacper Kruszewski wrote:
> hi.
>
> I have 3 HP and one Xerox network printers.
> I'd like to start accounting connected to mysql database.
> Is there any howto?
> I haven't tried to start it yet,
> but I'd like to do it as easy as it is only possible so I'm
> looking for a howto or somebody who already did that.
Hi,
I'm doing the same thing right now. Below is my accounting script that
talks to mysql. It's only been tested for a couple of days on one printer.
#!/opt/gnu/bin/perl -Tw
# This script updates the printer quota database.
# It will abort jobs and send mail to users if the their quota is too low.
# Johan Bengtsson DD 2002
$ENV{"PATH"} = "";
$ENV{"IFS"} = "";
# Check the first word in input.
$state = shift(@ARGV);
# Quit if it's not "jobstart" or "fileend".
if (($state ne "jobstart") && ($state ne "fileend")) {
exit;
}
# Put database name, user and password into variables.
$dbuser = "pqupdate";
$dbpasswd = "somepasswd";
$dbname = "pquota";
$dbhost = "foo.dd.chalmers.se";
$mysql = "/opt/gnu/bin/mysql";
$pagecount = "0";
$login = "foo";
$printer = "none";
$userstatus = "1";
$freeridestatus = "";
@freeride = "";
# Extract username of the person who submits the printjob, printername and pagecount
from input.
$arg = shift(@ARGV);
while (defined $arg) {
for ($arg) {
if (/^-n/) {$login = substr("$arg", 2,)}
elsif (/^-P/) {$printer = substr("$arg", 2,)}
elsif (/^-b/) {$pagecount = substr("$arg", 2,)}
else {};
}
$arg = shift(@ARGV);
}
# Untaint variables.
if ($login =~ /^([-\@\w.]+)$/) {
$login = $1;
}
else {
die "Bad data in $login";
}
if ($printer =~ /^([-\@\w.]+)$/) {
$printer = $1;
}
else {
die "Bad data in $printer";
}
if ($pagecount =~ /^([-\@\w.]+)$/) {
$pagecount = $1;
}
else {
die "Bad data in $pagecount";
}
# Some privileged users who don't have printerquota.
@freeride = ("iko", "sbl", "stoffe", "pw", "appro", "stedt");
$freeridestatus = grep { $_ eq $login } @freeride;
if ($freeridestatus == 1) {
exit 0;
}
# Pagecount = Sheetcount on some printers, so adjustments are necessary.
if ($printer eq "problem") {
$pagecount = $pagecount / 2;
}
# People printing in colour must be punished...
if ($printer eq "saruman") {
$pagecount = $pagecount * 8;
}
# Check if user exists in pquota database.
$chkuserstatus = `$mysql -u$dbuser -hfoo.chalmers.se -p$dbpasswd -D$dbname
-e"SELECT COUNT(login) FROM pquota WHERE login='$login'"`;
$userstatus = substr("$chkuserstatus", 12,);
# Create a new entry if user doesn't exist in pquota database.
if ($userstatus == "0") {
`$mysql -u$dbuser -hfoo.dd.chalmers.se -p$dbpasswd -Dpquota -e"INSERT INTO pquota
SET login='$login',pages='300'"`;
}
# Check if user has printerquota left if at the start of a job.
$pgleft = `$mysql -u$dbuser -heru.dd.chalmers.se -p$dbpasswd -D$dbname -e"SELECT
pages FROM pquota WHERE login='$login'"`;
$pages_left_before_print = substr("$pgleft", 6,);
# At jobstart send user mail if quota isn't enough.
if ("$state" eq "jobstart") {
if ($pages_left_before_print < "1") {
open(MAIL, "| /usr/bin/mailx -s 'Printerquota' foo\@foo.se");
print MAIL <<EOF;
Printerquota systemet �r under uppbyggnad. Kontakta
foo\@somewhere.chalmers.se
om du f�r detta brev.
Thanks,
BOFH
EOF
close(MAIL);
# Abort print job.
exit 34;
}
# When status = jobstart and user has enough quota, exit script for now and let user
print.
exit;
}
# Calculate the users quota after the job is finished.
$pages_left_after_print = $pages_left_before_print - $pagecount;
# Update printerquota.
system("$mysql", "-h$dbhost", "-u$dbuser", "-p$dbpasswd", "-D$dbname", "-eUPDATE
pquota SET pages = $pages_left_after_print WHERE login = '$login'");
# Update printerquota log.
system("$mysql", "-h$dbhost", "-u$dbuser", "-p$dbpasswd", "-D$dbname", "-eINSERT INTO
pquotalog VALUES('$login', '$printer', NOW(),$pagecount, $pages_left_after_print)");
exit 0;
--
http://www.dd.chalmers.se/~elijah/
-----------------------------------------------------------------------------
YOU MUST BE A LIST MEMBER IN ORDER TO POST TO THE LPRNG MAILING LIST
The address you post from MUST be your subscription address
If you need help, send email to [EMAIL PROTECTED] (or lprng-requests
or lprng-digest-requests) with the word 'help' in the body. For the impatient,
to subscribe to a list with name LIST, send mail to [EMAIL PROTECTED]
with: | example:
subscribe LIST <mailaddr> | subscribe lprng-digest [EMAIL PROTECTED]
unsubscribe LIST <mailaddr> | unsubscribe lprng [EMAIL PROTECTED]
If you have major problems, send email to [EMAIL PROTECTED] with the word
LPRNGLIST in the SUBJECT line.
-----------------------------------------------------------------------------