root / plugins / nginx / nginx-cache-hit-rate @ ef960abc
Historique | Voir | Annoter | Télécharger (2,41 ko)
| 1 | 67f09e9c | Andi H | #!/usr/bin/perl -w |
|---|---|---|---|
| 2 | |||
| 3 | =head1 NAME |
||
| 4 | |||
| 5 | nginx_hitmiss - Munin plugin to show hit rate of nginx proxy cache |
||
| 6 | |||
| 7 | =head1 APPLICABLE SYSTEMS |
||
| 8 | |||
| 9 | nginx caching proxy |
||
| 10 | |||
| 11 | =head1 CONFIGURATION |
||
| 12 | |||
| 13 | File::ReadBackwards is used and must be installed. |
||
| 14 | |||
| 15 | You can override the log file location. |
||
| 16 | |||
| 17 | [nginx*] |
||
| 18 | env.logfile /var/log/nginx/cache-access.log |
||
| 19 | |||
| 20 | Nginx must be configured to include "cs=$upstream_cache_status" in the |
||
| 21 | log file. Example format: |
||
| 22 | |||
| 23 | log_format cache '$remote_addr - $host [$time_local] "$request" $status ' |
||
| 24 | '$body_bytes_sent "$http_referer" ' |
||
| 25 | 'rt=$request_time ut="$upstream_response_time" ' |
||
| 26 | 'cs=$upstream_cache_status'; |
||
| 27 | |||
| 28 | By default the last 1000 log lines are read, you may want to change this. |
||
| 29 | |||
| 30 | =head1 MAGIC MARKERS |
||
| 31 | |||
| 32 | #%# family=auto |
||
| 33 | #%# capabilities=autoconf |
||
| 34 | |||
| 35 | =head1 LICENSE |
||
| 36 | |||
| 37 | BSD |
||
| 38 | |||
| 39 | =cut |
||
| 40 | |||
| 41 | use File::ReadBackwards; |
||
| 42 | my $line_counter=1000; |
||
| 43 | |||
| 44 | my $LOG = exists $ENV{'logfile'} ? $ENV{'logfile'}
|
||
| 45 | : "/var/log/nginx/cache-access.log"; |
||
| 46 | |||
| 47 | if ( exists $ARGV[0] and $ARGV[0] eq "autoconf" ) {
|
||
| 48 | unless (-r $LOG) {
|
||
| 49 | print "$LOG not readable\n"; |
||
| 50 | exit 0; |
||
| 51 | } else {
|
||
| 52 | print "yes\n"; |
||
| 53 | exit 0; |
||
| 54 | } |
||
| 55 | } |
||
| 56 | |||
| 57 | if ( exists $ARGV[0] and $ARGV[0] eq "config" ) {
|
||
| 58 | print "graph_title NGINX hit rates\n"; |
||
| 59 | print "graph_args -l 0 -u 100 --rigid\n"; |
||
| 60 | print "graph_category nginx\n"; |
||
| 61 | print "graph_vlabel %\n"; |
||
| 62 | print "hit.label Hits\n"; |
||
| 63 | print "hit.draw AREA\n"; |
||
| 64 | print "hit.min 0\n"; |
||
| 65 | print "hit.cdef hit,$line_counter,/,100,*\n"; |
||
| 66 | print "miss.label Misses\n"; |
||
| 67 | print "miss.draw STACK\n"; |
||
| 68 | print "miss.min 0\n"; |
||
| 69 | print "miss.cdef miss,$line_counter,/,100,*\n"; |
||
| 70 | print "expired.label Expired Objects\n"; |
||
| 71 | print "expired.draw STACK\n"; |
||
| 72 | print "expired.min 0\n"; |
||
| 73 | print "expired.cdef expired,$line_counter,/,100,*\n"; |
||
| 74 | exit 0; |
||
| 75 | } |
||
| 76 | |||
| 77 | my ($e,$h,$m) = (0,0,0); |
||
| 78 | my $file_counter=0; |
||
| 79 | |||
| 80 | FILE: while ($line_counter > 0) {
|
||
| 81 | my $file_extension = $file_counter==0? "" : ".$file_counter"; |
||
| 82 | my $lh= File::ReadBackwards->new( "$LOG$file_extension" ) |
||
| 83 | or ( warn "$line_counter lines to read, but $LOG$file_extension: $!" |
||
| 84 | and last FILE); |
||
| 85 | $file_counter++; |
||
| 86 | while (defined ($_= $lh->readline)) {
|
||
| 87 | $line_counter--; |
||
| 88 | /cs=HIT/ and $h++; |
||
| 89 | /cs=MISS/ and $m++; |
||
| 90 | /cs=EXPIRED/ and $e++; |
||
| 91 | last FILE if $line_counter==0; |
||
| 92 | } |
||
| 93 | $lh->close(); |
||
| 94 | } |
||
| 95 | print "hit.value $h\n"; |
||
| 96 | print "miss.value $m\n"; |
||
| 97 | print "expired.value $e\n"; |
