From f7cbf1707c90fb5ea4b4ea6c67634cf9c9ecbdeb Mon Sep 17 00:00:00 2001 From: Jamie Cameron Date: Thu, 3 May 2012 22:25:52 -0700 Subject: [PATCH] Make monitor details available to commands via environment --- status/CHANGELOG | 2 ++ status/monitor.pl | 32 ++++++++++++++++++-------------- status/status-lib.pl | 24 ++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 14 deletions(-) diff --git a/status/CHANGELOG b/status/CHANGELOG index 3814ebbc8..8f7553c67 100644 --- a/status/CHANGELOG +++ b/status/CHANGELOG @@ -77,3 +77,5 @@ Added a monitor-level option to run a command if the monitor times out. Enhanced the free memory monitor to be able to check virtual memory as well. ---- Changes since 1.530 ---- Update the SSL certificate monitor to check alternate names as well when looking for hostname mismatches. +---- Changes since 1.590 ---- +Commands run when a monitor goes up or down can now access details of the monitor via environment variables starting with STATUS_ diff --git a/status/monitor.pl b/status/monitor.pl index 6843f8980..0e2a93a98 100755 --- a/status/monitor.pl +++ b/status/monitor.pl @@ -385,39 +385,43 @@ print STDERR "No SNMP perl module found\n"; # run_on_command(&serv, command) sub run_on_command { -return undef if (!$_[1]); +local ($serv, $cmd) = @_; +return undef if (!$cmd); local $out; -if ($_[0]->{'runon'} && $_[0]->{'remote'}) { +if ($serv->{'runon'} && $serv->{'remote'}) { # Run on the remote host - local $cmd = quotemeta($_[1]); $remote_error_msg = undef; + &remote_foreign_call($serv->{'remote'}, "status", + "set_monitor_environment", $serv); &remote_error_setup(\&remote_error_callback); if ($config{'output'}) { - $out = &remote_eval($_[0]->{'remote'}, "status", - "`($cmd) 2>&1 {'remote'}, "status", + "backquote_command", "($cmd) 2>&1 {'remote'}, "status", - "system('($cmd) >/dev/null 2>&1 {'remote'}, "status", + "execute_command", $cmd); } &remote_error_setup(undef); + &remote_foreign_call($serv->{'remote'}, "status", + "reset_monitor_environment", $serv); if ($remote_error_msg) { - return &text('monitor_runerr', $_[1], $_[0]->{'remote'}, + return &text('monitor_runerr', $cmd, $serv->{'remote'}, $remote_error_msg); } - return &text('monitor_run1', $_[1], $_[0]->{'remote'})."\n"; + return &text('monitor_run1', $cmd, $serv->{'remote'})."\n".$out; } else { # Just run locally + &set_monitor_environment($serv); if ($config{'output'}) { - $out = `($_[1]) 2>&1 &1 /dev/null 2>&1 {'file'}); } +# set_monitor_environment(&serv) +# Sets environment variables based on some monitor +sub set_monitor_environment +{ +local ($serv) = @_; +foreach my $k (keys %$serv) { + if (!ref($serv->{$k})) { + $ENV{'STATUS_'.uc($k)} = $serv->{$k}; + } + } +} + +# reset_monitor_environment(&serv) +# Undoes the call to set_monitor_environment +sub reset_monitor_environment +{ +local ($serv) = @_; +foreach my $k (keys %$serv) { + if (!ref($serv->{$k})) { + delete($ENV{'STATUS_'.uc($k)}); + } + } +} + 1;