/usr/share/munin/plugins/loggrep 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 159 160 161 162 163 164 165 166 167 168 169 | #!/usr/bin/perl -w
# -*- perl -*-
=head1 NAME
loggrep - Counts the number of matching log lines
=head1 CONFIGURATION
The following environment variables are used by this plugin:
logfile - Files to grep (shellglob) (required)
regex - Regex to look for (required)
label - Label
counter - If set the value captured by the first paren
in the regex is summed instead of coutning lines.
The value is used for the vertical label.
regex_<key> - Additional regexes
label_<key> - Additional labels
counter_<key> - Additional counters (the value is ignored).
title - Graph title
=head1 NOTES
TODO: Handle rotated logs
=head1 AUTHOR
Copyright (C) 2004 Dagfinn Ilmari Mannsaaker
=head1 LICENSE
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 dated June, 1991.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
USA.
=head1 MAGIC MARKERS
#%# family=manual
=cut
use strict;
my %regex;
my $logfile = $ENV{logfile};
my $vlabel = defined $ENV{'counter'} ? $ENV{'counter'} : 'entries';
(my $name = $0) =~ s|.*/||;
(my $host_name = $0) =~ s|.*_([^_]+)_.*|$1|;
die("No log file specified") unless defined $logfile;
if (exists $ENV{'regex'}) {
$regex{'count'}{'regex'} = qr/$ENV{'regex'}/;
$regex{'count'}{'label'} = $ENV{'label'} || $ENV{'regex'};
$regex{'count'}{'value'} = 0;
$regex{'count'}{'counter'} = defined $ENV{'counter'};
}
for my $key (map {/^regex_(.+)/} keys %ENV) {
$regex{$key}{'regex'} = qr/$ENV{"regex_$key"}/;
$regex{$key}{'label'} = $ENV{"label_$key"} || $ENV{"regex_$key"};
$regex{$key}{'value'} = 0;
$regex{$key}{'counter'} = defined $ENV{"counter_$key"};
}
die("No regexes specified") unless keys %regex;
my $statefile = "/var/lib/munin/plugin-state/$name.state";
if ($ARGV[0] and $ARGV[0] eq 'config') {
my $title = $ENV{title} || "Entries in $logfile";
print "graph_title $title\n";
print "graph_args --base 1000 -l 0\n";
print "graph_vlabel $vlabel / \${graph_period}\n";
print "graph_category other\n";
if ($host_name && $host_name ne $name) {
print "host_name $host_name\n";
}
for my $key (keys %regex) {
print "$key.label $regex{$key}{'label'}\n";
print "$key.type DERIVE\n";
print "$key.min 0\n";
print "$key.draw LINE2\n";
}
exit 0;
}
my %pos = map { $_ => undef } glob($logfile);
if (-f $statefile) {
open(my $in, '<', $statefile) or die("Can't read $statefile: $!\n");
while (<$in>) {
chomp;
if (/^([^=]+)=(\d+)$/ && exists $regex{$1}) {
$regex{$1}{'value'} = $2;
} else {
last;
}
}
do {
chomp;
if (<$in> =~ /^(\d+)$/ and exists $pos{$_}) {
$pos{$_} = $1
}
} while (<$in>);
close $in;
}
for my $log (keys %pos) {
$pos{$log} = parse_log($log, $pos{$log});
}
for my $key (keys %regex) {
print "$key.value $regex{$key}{'value'}\n";
}
if(-l $statefile) {
die("$statefile is a symbolic link, refusing to touch it.\n");
}
open(my $out, '>', $statefile) or die("Can't write $statefile: $!\n");
for my $key (keys %regex) {
print $out "$key=$regex{$key}{'value'}\n";
}
for my $log (keys %pos) {
print $out "$log\n$pos{$log}\n";
}
close $out;
sub parse_log {
my ($fname, $start) = @_;
open(my $logfh, $fname) or die ("Can't open log file $fname: $!\n");
my $size = (stat $logfh)[7];
# First encounter
$start = $size unless defined $start;
# The log has been rotated, start over
$start = 0 if $size < $start;
seek($logfh, $start, 0) or die ("Can't seek to $fname:$start: $!\n");
while (tell($logfh) < $size) {
my $line = <$logfh>;
for my $match (values %regex) {
if ($line =~ $match->{'regex'}) {
$match->{'value'} += $match->{'counter'} ? $1 : 1;
}
}
}
close($logfh);
return $size;
}
# vim:syntax=perl
|