From 67f09e9ca1b84e2aa0fbd27122db26cc7a8baa05 Mon Sep 17 00:00:00 2001 From: Andi H Date: Mon, 9 May 2011 14:40:56 +0200 Subject: [PATCH] Initial version --- plugins/other/nginx-cache-hit-rate | 97 ++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100755 plugins/other/nginx-cache-hit-rate diff --git a/plugins/other/nginx-cache-hit-rate b/plugins/other/nginx-cache-hit-rate new file mode 100755 index 00000000..a241a8c4 --- /dev/null +++ b/plugins/other/nginx-cache-hit-rate @@ -0,0 +1,97 @@ +#!/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 nginx\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";