2
0
Fork 0
mirror of https://github.com/munin-monitoring/contrib.git synced 2018-11-08 00:59:34 +01:00
contrib-munin/plugins/nginx/nginx-cache-hit-rate

97 lines
2.4 KiB
Perl
Executable file

#!/usr/bin/perl -w
=head1 NAME
nginx_hitmiss - Munin plugin to show hit rate of nginx proxy cache
=head1 APPLICABLE SYSTEMS
nginx caching proxy
=head1 CONFIGURATION
File::ReadBackwards is used and must be installed.
You can override the log file location.
[nginx*]
env.logfile /var/log/nginx/cache-access.log
Nginx must be configured to include "cs=$upstream_cache_status" in the
log file. Example format:
log_format cache '$remote_addr - $host [$time_local] "$request" $status '
'$body_bytes_sent "$http_referer" '
'rt=$request_time ut="$upstream_response_time" '
'cs=$upstream_cache_status';
By default the last 1000 log lines are read, you may want to change this.
=head1 MAGIC MARKERS
#%# family=auto
#%# capabilities=autoconf
=head1 LICENSE
BSD
=cut
use File::ReadBackwards;
my $line_counter=1000;
my $LOG = exists $ENV{'logfile'} ? $ENV{'logfile'}
: "/var/log/nginx/cache-access.log";
if ( exists $ARGV[0] and $ARGV[0] eq "autoconf" ) {
unless (-r $LOG) {
print "$LOG not readable\n";
exit 0;
} else {
print "yes\n";
exit 0;
}
}
if ( exists $ARGV[0] and $ARGV[0] eq "config" ) {
print "graph_title NGINX hit rates\n";
print "graph_args -l 0 -u 100 --rigid\n";
print "graph_category webserver\n";
print "graph_vlabel %\n";
print "hit.label Hits\n";
print "hit.draw AREA\n";
print "hit.min 0\n";
print "hit.cdef hit,$line_counter,/,100,*\n";
print "miss.label Misses\n";
print "miss.draw STACK\n";
print "miss.min 0\n";
print "miss.cdef miss,$line_counter,/,100,*\n";
print "expired.label Expired Objects\n";
print "expired.draw STACK\n";
print "expired.min 0\n";
print "expired.cdef expired,$line_counter,/,100,*\n";
exit 0;
}
my ($e,$h,$m) = (0,0,0);
my $file_counter=0;
FILE: while ($line_counter > 0) {
my $file_extension = $file_counter==0? "" : ".$file_counter";
my $lh= File::ReadBackwards->new( "$LOG$file_extension" )
or ( warn "$line_counter lines to read, but $LOG$file_extension: $!"
and last FILE);
$file_counter++;
while (defined ($_= $lh->readline)) {
$line_counter--;
/cs=HIT/ and $h++;
/cs=MISS/ and $m++;
/cs=EXPIRED/ and $e++;
last FILE if $line_counter==0;
}
$lh->close();
}
print "hit.value $h\n";
print "miss.value $m\n";
print "expired.value $e\n";