1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
#!@@PERL@@ -w
#
# Plugin to monitor number of pages pr printer printed by CUPS
#
# Usage: copy or link into /etc/munin/plugins
#
# Parameters:
#
# config (required)
# autoconf (optional - used by munin-config)
#
# Config variables:
#
# logdir - Which logfile to use
# logfile - What file to read in logdir
# maxlabel - Maximum printers to plot
#
# $Log$
# Revision 1.5 2004/12/10 18:51:43 jimmyo
# linux/apt* has been forced to LANG=C, to get predictable output.
#
# Revision 1.4 2004/12/10 10:47:47 jimmyo
# Change name from ${scale} to ${graph_period}, to be more consistent.
#
# Revision 1.3 2004/12/09 22:12:54 jimmyo
# Added "graph_period" option, to make "graph_sums" usable.
#
# Revision 1.2 2004/11/21 00:16:56 jimmyo
# Changed a lot of plugins so they use DERIVE instead of COUNTER.
#
# Revision 1.1 2004/06/08 14:12:19 jimmyo
# Added contributed plugin generic/cupsys_pages, contributed by Rune N. Skillingstad.
#
#
#
# Magic markers (optinal - used by munin-config and some installation
# scripts):
#
#%# family=auto
#%# capabilities=autoconf
use strict;
my $statefile = "@@PLUGSTATE@@/munin-cupsys-pages.state";
my $pos = undef;
my %printers = ();
my $LOGDIR = $ENV{logdir} || "/var/log/cups/";
my $LOGFILE = $ENV{logfile} || "page_log";
my $MAXLABEL = $ENV{maxlabel} || 20;
if($ARGV[0] and $ARGV[0] eq "autoconf" ) {
my $logfile;
if(-d $LOGDIR) {
$logfile = "$LOGDIR/$LOGFILE";
if(-f $logfile) {
if(-r $logfile) {
print "yes\n";
exit 0;
} else {
print "no (logfile not readable)\n";
}
} else {
print "no (logfile not found)\n";
}
} else {
print "no (could not find logdir)\n";
}
exit 1;
}
my $logfile = "$LOGDIR/$LOGFILE";
my $rotlogfile = $logfile . ".0";
if (-f "$logfile.0") {
$rotlogfile = $logfile . ".0";
} elsif (-f "$logfile.1") {
$rotlogfile = $logfile . ".1";
} elsif (-f "$logfile.01") {
$rotlogfile = $logfile . ".01";
}
if (! -f $logfile and ! -f $rotlogfile) {
print "pages.value $logfile $rotlogfile U\n";
exit 0;
}
if (-f "$statefile") {
open(IN, "$statefile") or exit 4;
if(<IN> =~ /^(\d+)$/) {
$pos = $1;
}
LINE: while(<IN>) {
if(/^([^:]+):(\d+)$/) {
$printers{$1} = $2;
}
if(keys(%printers) >= $MAXLABEL) {
last LINE;
}
}
close(IN);
}
my $startsize = (stat $logfile)[7];
if (!defined $pos) {
# Initial run.
$pos = $startsize;
$pos = 0;
}
if ($startsize < $pos) {
# Log rotated
parselogfile ($rotlogfile, $pos, (stat $rotlogfile)[7]);
$pos = 0;
}
parselogfile($logfile, $pos, $startsize);
$pos = $startsize;
# Write back to state file
open (STATFILE, ">$statefile") or exit 4;
print STATFILE "$pos\n";
foreach my $printer (sort(keys %printers)) {
print STATFILE "$printer:".$printers{$printer}."\n";
}
close(STATFILE);
if($ARGV[0] and $ARGV[0] eq "config") {
print "host_name $ENV{FQDN}\n";
print "graph_title CUPS pages printed\n";
print "graph_args --base 1000 -l 0\n";
print "graph_vlabel pages/\${graph_period}\n";
print "graph_order ".join(" ",sort(keys(%printers)))."\n";
foreach my $printer (sort(keys %printers)) {
print "$printer.label $printer\n";
print "$printer.type DERIVE\n";
print "$printer.min 0\n";
}
exit 0;
} else {
foreach my $printer (sort(keys %printers)) {
print "$printer.value ".$printers{$printer}."\n";
}
}
sub parselogfile {
my ($fname, $start, $stop) = @_;
open (LOGFILE, $fname) or exit 3;
seek (LOGFILE, $start, 0) or exit 2;
while (tell(LOGFILE) < $stop) {
my $line = <LOGFILE>;
chomp ($line);
if ($line =~ /^(\S+)\s+\S+\s+\d+\s+\[[^\]]+\]\s+(\d+)\s+(\d+)\s+(\S+)\s+(\S+)$/) {
if(!defined($printers{$1}) && keys(%printers) < $MAXLABEL) {
$printers{$1} += int($2)*int($3);
} elsif(defined($printers{$1})) {
$printers{$1} += int($2)*int($3);
}
}
}
close(LOGFILE);
}
|