From c9bd24bda7fd0f9e85d5691bfdfa5d77a6cabd5f Mon Sep 17 00:00:00 2001 From: "pru.mike" Date: Sat, 11 Feb 2017 21:52:29 +0300 Subject: [PATCH] Fix for pull request munin-monitoring#798 --- plugins/{sensors => ups}/upsmonpro_ | 63 ++++++++++++++++++----------- 1 file changed, 39 insertions(+), 24 deletions(-) rename plugins/{sensors => ups}/upsmonpro_ (77%) diff --git a/plugins/sensors/upsmonpro_ b/plugins/ups/upsmonpro_ similarity index 77% rename from plugins/sensors/upsmonpro_ rename to plugins/ups/upsmonpro_ index cdb3083a..67e355e8 100644 --- a/plugins/sensors/upsmonpro_ +++ b/plugins/ups/upsmonpro_ @@ -18,10 +18,17 @@ Environment variables: host - UPSMON PRO server host, default localhost port - UPSMON PRO port, default 2601 +Example configuration (optional): + + [upsmonpro_*] + env.host localhost + env.port 2601 + =head1 MAGIC MARKERS #%# family=auto #%# capabilities=autoconf + #%# capabilities=suggest =head1 AUTHOR @@ -51,34 +58,36 @@ our %TYPES = ( temp => [q/temp/], status => [qw/power_failure low_battery voltage_status ups_status battery_test/] ); -our @TYPES = keys %TYPES; +our @TYPES = sort keys %TYPES; +my %DISPATCH_TABLE = (); +my $pkg = __PACKAGE__; -{ - no strict 'refs'; - for my $t (@TYPES) { - for my $sub (qw/autoconf/) { - *{ __PACKAGE__ . "::run_${sub}_$t" } = "run_$sub"; - } - *{ __PACKAGE__ . "::run_default_$t" } = sub { - run_default(@{ $TYPES{$t} }); - }; - } +$DISPATCH_TABLE{"${pkg}::run_suggest"} = \&run_suggest; +$DISPATCH_TABLE{"${pkg}::run_autoconf"} = \&run_autoconf; +for my $t (@TYPES) { + $DISPATCH_TABLE{"${pkg}::run_config_$t"} = \&{"run_config_$t"}; + $DISPATCH_TABLE{"${pkg}::run_autoconf_$t"} = \&run_autoconf; + $DISPATCH_TABLE{"${pkg}::run_default_$t"} = sub { + run_default(@{ $TYPES{$t} }); + }; } -{ - no strict 'refs'; - find_key($ARGV[0])->(); -} +find_key($ARGV[0])->(); sub find_key { - my $p = shift || ""; + my $argv = shift || ''; my $type_re = join '|', @TYPES; - unless ($Munin::Plugin::me =~ /upsmonpro_{1,}($type_re)$/) { - die "Could not determine script type [@TYPES] ? name=" . $Munin::Plugin::me . "\n"; + my $key; + if ($argv =~ /(suggest|autoconf)/i) { + $key = 'run_' . lc($1); + } elsif ($Munin::Plugin::me =~ /upsmonpro_{1,}($type_re)$/) { + my $graph = $1; + $key = 'run_' . ((grep { $argv eq $_ } qw/autoconf config/) ? $argv : 'default') . "_$graph"; + } else { + die "Could not determine script type [@TYPES] ? name=$Munin::Plugin::me\n"; } - my $graph = $1; - return 'run_' . ((grep { $p eq $_ } qw/autoconf config/) ? $ARGV[0] : 'default') . "_$graph"; + return $DISPATCH_TABLE{"${pkg}::$key"}; } sub run_config_voltage { @@ -86,7 +95,7 @@ sub run_config_voltage { graph_title UPS Input/Output Voltage graph_vlabel volt graph_scale no -graph_category sensors +graph_category ups input.label input input.info Input Voltage input.type GAUGE @@ -102,7 +111,7 @@ sub run_config_temp { graph_title UPS Temperature graph_vlabel celsius graph_scale no -graph_category sensors +graph_category ups temp.label temperature temp.type GAUGE END @@ -114,7 +123,7 @@ sub run_config_load { graph_title UPS Battery Load/Capacity graph_vlabel precent% graph_scale no -graph_category sensors +graph_category ups battery_load.label battery_load battery_load.type GAUGE battery_capacity.label battery_capacity @@ -128,7 +137,7 @@ sub run_config_status { graph_title UPS Statuses graph_vlabel status graph_scale no -graph_category sensors +graph_category ups power_failure.label power_failure power_failure.type GAUGE low_battery.label low_battery @@ -155,6 +164,12 @@ sub run_default { } } +sub run_suggest { + local $LIST_SEPARATOR = "\n"; + say "@TYPES"; + exit(0); +} + sub run_autoconf { if (gather_data($DEFAULT_HOST, $DEFAULT_PORT)->{response} eq 'ok') { say "yes";