diff --git a/lib/Core/Varnish.pm b/lib/Core/Varnish.pm new file mode 100644 index 0000000..0e13a21 --- /dev/null +++ b/lib/Core/Varnish.pm @@ -0,0 +1,98 @@ +package Core::Varnish; + +use strict; +use warnings; + +use base 'Resmon::Module'; + +use Resmon::ExtComm qw(run_command cache_command); + +=pod + +=head1 NAME + +Core::Varnish - a varnish stats module + +=head1 SYNOPSIS + + Core::Varnish { + local : noop + } + +=head1 CONFIGURATION + +=over + +=item check_name + +The check name is descriptive only in this check. It is not used for anything. + +=back + +=head1 METRICS + +Processes all metrics from the output of the varnishstat command. Returns both +the current metric value, and the average per second if there is one. + +=over + +=back + +=cut + +sub handler { + + my %metrics; + my $self = shift; + my $config = $self->{config}; + my $check_name = $self->{check_name}; + + my $stat_cmd = 'varnishstat -1'; + + my $varnish_stats = run_command($stat_cmd); + my @varnish_stats = split("\n", $varnish_stats); + my $metric_name; + my $metric_value; + my $metric_ps_avg; + my $metric_desc; + + foreach my $value (@varnish_stats) { + + $value =~ s/\s+/ /g; + # If there is no per second average we get a period. Report this as + # null + $value =~ s/\s\.\s/ null /g; + + # VBE happy probes can merge with the metric name. Add an extra space after + # the happy probe to get a space to split on. + if ($value =~ /^VBE.*/) { + $value =~ s/\.happy/\.happy /g; + ($metric_name, $metric_value, $metric_ps_avg, $metric_desc) = split(/ /, $value, 4); + + } else { + # Split out metrics + $value =~ /^([A-Z\.a-z_0-9]+)\s([0-9\.]+)\s([0-9\.]+|null)\s(.*)/; + $metric_name = $1; + $metric_value = $2; + $metric_ps_avg = $3; + $metric_desc = $4; + + } + + # Counted metrics can get large, but always seem to be + # whole numbers + $metrics{$metric_name} = [ $metric_value, 'L' ] ; + + + if ($metric_value ne "null") { + # The per second averages can be floats + $metrics{"$metric_name\_per_sec"} = [ $metric_ps_avg, 'n' ] ; + } + + } + + return \%metrics; + +}; + +1;