This file is indexed.

/usr/share/munin/plugins/cupsys_pages is in munin-node 1.4.6-3ubuntu3.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  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
#!/usr/bin/perl -w
# -*- perl -*-

=head1 NAME

cupsys_pages - Plugin to monitor number of pages pr printer printed by
CUPS

=head1 CONFIGURATION

The following environment variables are used

 logdir   - Which logfile to use
 logfile  - What file to read in logdir
 maxlabel - Maximum printers to plot

=head1 AUTHOR

Unknown author

=head1 LICENSE

Unknown license

=head1 MAGIC MARKERS

 #%# family=auto
 #%# capabilities=autoconf

=cut

use strict;
use Munin::Plugin;

my $statefile = "/var/lib/munin/plugin-state/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 0;
}

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 die "Could not open $statefile for reading: $!\n";
    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
  die "Could not open $statefile for writing: $!\n";
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";
    foreach my $printer (sort(keys %printers)) {
	# Convert printername to something legal
	my $p = $printer;
	$printer = clean_fieldname($printer);
	print "$printer.label $p\n";
	print "$printer.type DERIVE\n";
	print "$printer.min 0\n";
    }
    exit 0;
} else {
    foreach my $printer (sort(keys %printers)) {
	my $p = $printer;
	$printer = clean_fieldname($printer);
	print "$printer.value ".$printers{$p}."\n";
    }
    exit 0;
}

sub parselogfile {
    my ($fname, $start, $stop) = @_;
    open (LOGFILE, $fname) or die "No logfile $fname: $!\n";
    seek (LOGFILE, $start, 0) or die "Could not seek in logfile $fname: $!\n";
    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);
}