Last active
August 29, 2015 14:08
-
-
Save mkubenka/e4ed500749ddf5a32bfe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use warnings; | |
use strict; | |
use utf8; | |
use File::Basename; | |
#--------------------------------------------------------------------- | |
# G R A P H D E F I N I T I O N S | |
#--------------------------------------------------------------------- | |
my %graphs = (); | |
# These are defaults to save typing in the graph definitions | |
my %defaults = ( | |
global_attrs => { | |
args => '--base 1000', | |
}, | |
data_source_attrs => { | |
min => '0', | |
type => 'DERIVE', | |
draw => 'AREASTACK', | |
}, | |
); | |
$graphs{userdeltacount} = { | |
config => { | |
global_attrs => { | |
title => 'User Delta Count', | |
vlabel => 'Count', | |
}, | |
data_source_attrs => { | |
draw => 'LINE1', | |
}, | |
}, | |
data_sources => [ | |
{name => 'user_delta_count', | |
label => 'User Delta Count', | |
type => 'GAUGE'}, | |
], | |
}; | |
$graphs{status} = { | |
config => { | |
global_attrs => { | |
title => 'Status', | |
vlabel => 'Operation per ${graph_period}', | |
}, | |
data_source_attrs => { | |
draw => 'LINE1', | |
}, | |
}, | |
data_sources => [ | |
{name => 'connections', | |
label => 'Connections',}, | |
{name => 'command_search', | |
label => 'Searches',}, | |
{name => 'queries', | |
label => 'Queries',}, | |
], | |
}; | |
#--------------------------------------------------------------------- | |
# M A I N | |
#--------------------------------------------------------------------- | |
# | |
# Global hash holding the data collected from phpfpm. | |
# | |
our $data; # Was 'my'. Changed to 'our' to facilitate testing. | |
sub main { | |
my ($id,$graph); | |
$0 =~ /sphinx(?:_([^_]+)|)_(.+)\s*$/; | |
$id = $1; | |
$graph = $2; | |
my $command = $ARGV[0] || 'show'; | |
my %command_map = ( | |
'autoconf' => \&autoconf, | |
'config' => \&config, | |
'show' => \&show, | |
'suggest' => \&suggest, | |
); | |
die "Unknown command: $command" | |
unless exists $command_map{$command}; | |
return $command_map{$command}->($graph,$id); | |
} | |
#--------------------------------------------------------------------- | |
# C O M M A N D H A N D L E R S | |
#--------------------------------------------------------------------- | |
# Each command handler should return an appropriate exit code | |
# http://munin.projects.linpro.no/wiki/ConcisePlugins#autoconf | |
sub autoconf { | |
my $graph_name = shift; | |
my $response; | |
if ($graph_name eq "userdeltacount") { | |
$response = `/usr/bin/indextool --check delta`; | |
} | |
if ($graph_name eq "status") { | |
$response = `/usr/bin/searchd --status`; | |
} | |
if ($? == 0) { | |
print "yes\n"; | |
return 0; | |
} else { | |
print "no\n"; | |
return 0; | |
} | |
} | |
# http://munin.projects.linpro.no/wiki/ConcisePlugins#suggest | |
sub suggest { | |
foreach my $graph (sort keys(%graphs)) { | |
print "$graph\n"; | |
} | |
return 0; | |
} | |
sub config { | |
my $graph_name = shift; | |
my $id = shift; | |
die 'Unknown graph ' . ($graph_name ? $graph_name : '') | |
unless $graphs{$graph_name}; | |
my $graph = $graphs{$graph_name}; | |
my %conf = (%{$defaults{global_attrs}}, %{$graph->{config}{global_attrs}}); | |
while (my ($k, $v) = each %conf) { | |
if ($id and $k eq 'title') { | |
print "graph_$k $id - $v\n"; | |
} | |
else { | |
print "graph_$k $v\n"; | |
} | |
} | |
print "graph_category sphinx\n"; | |
my $i = 0; | |
for my $ds (@{$graph->{data_sources}}) { | |
my %ds_spec = ( | |
%{$defaults{data_source_attrs}}, | |
%{$graph->{config}{data_source_attrs}}, | |
%$ds, | |
); | |
while (my ($k, $v) = each %ds_spec) { | |
# 'name' is only used internally in this script, not | |
# understood by munin. | |
next if ($k eq 'name'); | |
# AREASTACK is part of munin as of version 1.3.3 (not | |
# released yet). Until then ... | |
if ($k eq 'draw' && $v eq 'AREASTACK') { | |
printf("%s.%s %s\n", | |
clean_fieldname($ds->{name}), $k, ($i ? 'STACK' : 'AREA')); | |
} | |
else { | |
printf("%s.%s %s\n", clean_fieldname($ds->{name}), $k, $v); | |
} | |
$i++; | |
} | |
} | |
return 0; | |
} | |
sub show { | |
my $graph_name = shift; | |
die 'Unknown graph ' . ($graph_name ? $graph_name : '') | |
unless $graphs{$graph_name}; | |
my $graph = $graphs{$graph_name}; | |
get_data($graph_name); | |
for my $ds (@{$graph->{data_sources}}) { | |
printf "%s.value %s\n", | |
clean_fieldname($ds->{name}), $data->{$ds->{name}}; | |
} | |
return 0; | |
} | |
#--------------------------------------------------------------------- | |
# U T I L I T Y S U B S | |
#--------------------------------------------------------------------- | |
sub get_data { | |
my $graph_name = shift; | |
if ($graph_name eq "userdeltacount") { | |
my $response = `/usr/bin/indextool --dumpheader delta`; | |
if ($? != 0) { | |
$response = ""; | |
} | |
if ($response =~ /^total-documents:\s+([0-9\.]+)/im) { | |
$data->{"user_delta_count"} = "$1"; | |
} else { | |
$data->{"user_delta_count"} = "U"; | |
} | |
} | |
if ($graph_name eq "status") { | |
my $response = `/usr/bin/searchd --status`; | |
if ($? != 0) { | |
$response = ""; | |
} | |
if ($response =~ /^connections:\s+([0-9\.]+)/im) { | |
$data->{"connections"} = "$1"; | |
} else { | |
$data->{"connections"} = "U"; | |
} | |
if ($response =~ /^command_search:\s+([0-9\.]+)/im) { | |
$data->{"command_search"} = "$1"; | |
} else { | |
$data->{"command_search"} = "U"; | |
} | |
if ($response =~ /^queries:\s+([0-9\.]+)/im) { | |
$data->{"queries"} = "$1"; | |
} else { | |
$data->{"queries"} = "U"; | |
} | |
} | |
} | |
# | |
# Can't use variable names longer than 19 characters: | |
# http://munin.projects.linpro.no/wiki/notes_on_datasource_names | |
# | |
sub clean_fieldname { | |
my $name = shift; | |
# Replace a sequence of illegal leading chars with a single _ | |
$name =~ s/^[^A-Za-z_]+/_/; | |
# Replace remaining illegals with _ | |
$name =~ s/[^A-Za-z0-9_]/_/g; | |
# And use only the last 19 chars | |
$name = substr($name,-19); | |
return $name; | |
} | |
exit main() unless caller; | |
1; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment