root / plugins / varnish / varnish_cachehitratio @ 942bda31
Historique | Voir | Annoter | Télécharger (4,54 ko)
| 1 | 7d58463d | Anders Nordby | #! /usr/bin/perl |
|---|---|---|---|
| 2 | # Varnish cache hit ratio logger/plugin |
||
| 3 | # anders@fupp.net, 2007-09-19 |
||
| 4 | |||
| 5 | # Log/data file |
||
| 6 | # These must have write permission to the user the plugin runs as |
||
| 7 | # On FreeBSD, that is nobody |
||
| 8 | # Comment $mylog out to skip logging |
||
| 9 | |||
| 10 | # Set to 1 if you want to show unknown requsts (client requests which are |
||
| 11 | # neither hits nor misses): |
||
| 12 | $showunknown = 1; |
||
| 13 | |||
| 14 | $mydat = "/var/tmp/varnish_cachehitratio.dat"; |
||
| 15 | #$mylog = "/var/log/varnish_cachehitratio.log"; |
||
| 16 | |||
| 17 | %stat = (); |
||
| 18 | $ENV{PATH} = "/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin";
|
||
| 19 | open(VV, "varnishstat -V 2>&1 |"); |
||
| 20 | while (<VV>) {
|
||
| 21 | if (/^varnishstat/) { $vversion = $_; }
|
||
| 22 | } |
||
| 23 | close(VV); |
||
| 24 | chomp($vversion); |
||
| 25 | $vversion =~ s@varnishstat\s*@@; |
||
| 26 | $vversion =~ s@\(@@; |
||
| 27 | $vversion =~ s@\)@@; |
||
| 28 | $vversion =~ s@varnish-@@; |
||
| 29 | |||
| 30 | use Date::Format; |
||
| 31 | |||
| 32 | sub popstat10 {
|
||
| 33 | foreach $line (`varnishstat -1`) {
|
||
| 34 | chomp($line); |
||
| 35 | if ($line =~ /^\s+(\d+)\s+(.*)$/) {
|
||
| 36 | $val = $1; |
||
| 37 | $key = $2; |
||
| 38 | $key =~ s@\s@_@g; |
||
| 39 | $key =~ tr@A-Z@a-z@; |
||
| 40 | |||
| 41 | $stat{"$key"} = $val;
|
||
| 42 | } |
||
| 43 | } |
||
| 44 | } |
||
| 45 | |||
| 46 | sub popstat {
|
||
| 47 | foreach $line (`varnishstat -1`) {
|
||
| 48 | chomp($line); |
||
| 49 | if ($line =~ /^\w+\s+(\d+)\s+[\d\.]+\s+(.*)$/) {
|
||
| 50 | $val = $1; |
||
| 51 | $key = $2; |
||
| 52 | $key =~ s@\s@_@g; |
||
| 53 | $key =~ tr@A-Z@a-z@; |
||
| 54 | |||
| 55 | $stat{"$key"} = $val;
|
||
| 56 | } |
||
| 57 | } |
||
| 58 | } |
||
| 59 | |||
| 60 | sub printconfig {
|
||
| 61 | print "graph_title Cache hit/miss ratio\n"; |
||
| 62 | print "graph_args --upper-limit 100 -l 0\n"; |
||
| 63 | print "graph_vlabel % of requests\n"; |
||
| 64 | print "graph_category varnish\n"; |
||
| 65 | print "graph_info This graph shows the ratio of requests found in the cache and not\n"; |
||
| 66 | if ($showunknown) {
|
||
| 67 | print "graph_order hitratio missratio unknownratio\n"; |
||
| 68 | } else {
|
||
| 69 | print "graph_order hitratio missratio\n"; |
||
| 70 | } |
||
| 71 | print "graph_scale no\n"; |
||
| 72 | |||
| 73 | print "hitratio.label hits\n"; |
||
| 74 | print "hitratio.type GAUGE\n"; |
||
| 75 | print "hitratio.graph yes\n"; |
||
| 76 | print "hitratio.min 0\n"; |
||
| 77 | print "hitratio.max 100\n"; |
||
| 78 | print "hitratio.draw AREA\n"; |
||
| 79 | |||
| 80 | print "missratio.label misses\n"; |
||
| 81 | print "missratio.type GAUGE\n"; |
||
| 82 | print "missratio.graph yes\n"; |
||
| 83 | print "missratio.min 0\n"; |
||
| 84 | print "missratio.max 100\n"; |
||
| 85 | print "missratio.draw STACK\n"; |
||
| 86 | |||
| 87 | if ($showunknown) {
|
||
| 88 | print "unknownratio.label unknown\n"; |
||
| 89 | print "unknownratio.type GAUGE\n"; |
||
| 90 | print "unknownratio.graph yes\n"; |
||
| 91 | print "unknownratio.min 0\n"; |
||
| 92 | print "unknownratio.max 100\n"; |
||
| 93 | print "unknownratio.draw STACK\n"; |
||
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 97 | sub findvalues {
|
||
| 98 | $nrequests = (defined $stat{"client_requests_received"}) ? $stat{"client_requests_received"} : 0;
|
||
| 99 | $nhits = (defined $stat{"cache_hits"}) ? $stat{"cache_hits"} : 0;
|
||
| 100 | $nmisses = (defined $stat{"cache_misses"}) ? $stat{"cache_misses"} : 0;
|
||
| 101 | |||
| 102 | open(OVAL, $mydat); |
||
| 103 | $tmpstr = <OVAL>; |
||
| 104 | close(OVAL); |
||
| 105 | chomp($tmpstr); |
||
| 106 | |||
| 107 | ($orequests,$ohits,$omisses) = split(/ /, $tmpstr, 3); |
||
| 108 | |||
| 109 | $hits = $nhits - $ohits; |
||
| 110 | $requests = $nrequests - $orequests; |
||
| 111 | $misses = $nmisses - $omisses; |
||
| 112 | } |
||
| 113 | |||
| 114 | sub printvalues {
|
||
| 115 | if ($requests > 0) {
|
||
| 116 | $hitratio = sprintf("%.2f", $hits / $requests * 100);
|
||
| 117 | $missratio = sprintf("%.2f", $misses / $requests * 100);
|
||
| 118 | } else {
|
||
| 119 | # Assume cache hit ratio = 100% if requests < 0 |
||
| 120 | $hitratio = sprintf("%.2f", 100);
|
||
| 121 | $missratio = sprintf("%.2f", 0);
|
||
| 122 | } |
||
| 123 | |||
| 124 | if ($hits > 0 || $misses > 0) {
|
||
| 125 | $xhitratio = sprintf("%.2f", $hits / ($hits+$misses)*100);
|
||
| 126 | $xmissratio = sprintf("%.2f", $misses / ($hits+$misses)*100);
|
||
| 127 | } else {
|
||
| 128 | $xhitratio = sprintf("%.2f", 100);
|
||
| 129 | $xmissratio = sprintf("%.2f", 0);
|
||
| 130 | } |
||
| 131 | |||
| 132 | if (($hitratio + $missratio) > 100) {
|
||
| 133 | # Rounding foo, hit+miss ratio is higher than 100 |
||
| 134 | $missratio = sprintf("%.2f", 100 - $hitratio);
|
||
| 135 | $unknownratio = sprintf("%.2f", 0);
|
||
| 136 | } else {
|
||
| 137 | # Unknown = rest, hit+miss ratio is upto or 100 |
||
| 138 | $unknownratio = sprintf("%.2f", 100 - ($hitratio + $missratio));
|
||
| 139 | } |
||
| 140 | |||
| 141 | if ($showunknown) {
|
||
| 142 | print "hitratio.value $hitratio\n"; |
||
| 143 | } else {
|
||
| 144 | print "hitratio.value $xhitratio\n"; |
||
| 145 | } |
||
| 146 | print "missratio.value $missratio\n"; |
||
| 147 | |||
| 148 | if ($showunknown) {
|
||
| 149 | print "unknownratio.value $unknownratio\n"; |
||
| 150 | } |
||
| 151 | if ($mylog ne "") {
|
||
| 152 | open(LOG, ">>$mylog"); |
||
| 153 | if ($showunknown) {
|
||
| 154 | print LOG "hitratio=$hitratio missratio=$missratio unknown=$unknownratio hits=$hits misses=$misses requests=$requests [" . time2str("%Y-%m-%d %H:%M:%S", time) . "]\n";
|
||
| 155 | } else {
|
||
| 156 | print LOG "hitratio=$hitratio missratio=$missratio hits=$hits misses=$misses requests=$requests [" . time2str("%Y-%m-%d %H:%M:%S", time) . "]\n";
|
||
| 157 | } |
||
| 158 | close(LOG); |
||
| 159 | } |
||
| 160 | } |
||
| 161 | |||
| 162 | sub writevalues {
|
||
| 163 | open(OVAL, ">$mydat"); |
||
| 164 | # xhitratio is hitratio considering only hits and misses, not client |
||
| 165 | # requests |
||
| 166 | print OVAL "$nrequests $nhits $nmisses $hitratio $xhitratio\n"; |
||
| 167 | close(OVAL); |
||
| 168 | } |
||
| 169 | |||
| 170 | if ($ARGV[0] eq "autoconf") {
|
||
| 171 | print "yes\n"; |
||
| 172 | } elsif ($ARGV[0] eq "config") {
|
||
| 173 | printconfig; |
||
| 174 | } else {
|
||
| 175 | if ($vversion =~ /^1\.0/) {
|
||
| 176 | popstat10; |
||
| 177 | } else {
|
||
| 178 | popstat; |
||
| 179 | } |
||
| 180 | findvalues; |
||
| 181 | printvalues; |
||
| 182 | writevalues; |
||
| 183 | } |
