Make monitor details available to commands via environment

This commit is contained in:
Jamie Cameron
2012-05-03 22:25:52 -07:00
parent bd2373a93c
commit f7cbf1707c
3 changed files with 44 additions and 14 deletions

View File

@@ -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_

View File

@@ -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 </dev/null`");
$out = &remote_foreign_call($serv->{'remote'}, "status",
"backquote_command", "($cmd) 2>&1 </dev/null");
}
else {
&remote_eval($_[0]->{'remote'}, "status",
"system('($cmd) >/dev/null 2>&1 </dev/null')");
&remote_foreign_call($serv->{'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 </dev/null`;
return &text('monitor_run2', $_[1])."\n".
$out;
$out = &backquote_command("($cmd) 2>&1 </dev/null");
}
else {
system("($_[1]) >/dev/null 2>&1 </dev/null");
return &text('monitor_run2', $_[1])."\n";
&execute_command($cmd);
}
&reset_monitor_environment($serv);
return &text('monitor_run2', $cmd)."\n".$out;
}
}

View File

@@ -509,5 +509,29 @@ local ($tmpl) = @_;
&unlink_logged($tmpl->{'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;