Last active
March 11, 2016 23:25
-
-
Save kogent/3504753 to your computer and use it in GitHub Desktop.
test_command
This file contains 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
sub test_command(@) { | |
my $name = $_[1]; | |
my $command = $_[2]; | |
my $host = $_[3]; | |
my $arg_string = $_[4]; | |
my $monarch_home = $_[5]; | |
my $service_desc = $_[6]; | |
my $nagios_ver = $_[7]; | |
$arg_string =~ s/$name!//; | |
unless ($service_desc) { $service_desc = 'service_desc' } | |
my %resources = get_resources(); | |
my %host = StorProc->fetch_one( 'hosts', 'name', $host ); | |
unless ( $host{'alias'} ) { $host{'alias'} = $host } | |
unless ( $host{'address'} ) { $host{'address'} = $host } | |
my @args; | |
if ( $nagios_ver =~ /^[12]\.x$/ ) { | |
@args = split( /!/, $arg_string ); | |
} | |
else { | |
@args = unescaped_command_args( $arg_string ); | |
} | |
my $cnt = 1; | |
foreach my $a (@args) { | |
$command =~ s/\$ARG$cnt\$/$a/g; | |
$cnt++; | |
} | |
foreach my $res ( keys %resources ) { | |
if ( $command =~ /$res/i ) { | |
$command =~ s/\$$res\$/$resources{$res}/ig; | |
} | |
} | |
$command =~ s/\$HOSTNAME\$/$host/g; | |
$command =~ s/\$HOSTALIAS\$/$host{'alias'}/g; | |
$command =~ s/\$HOSTADDRESS\$/$host{'address'}/g; | |
$command =~ s/\$HOSTSTATE\$/UP/g; | |
$command =~ s/\$HOSTSTATEID\$/0/g; | |
$command =~ s/\$SERVICEDESC\$/$service_desc/g; | |
$command =~ s/\$SERVICESTATE\$/UP/g; | |
$command =~ s/\$SERVICESTATEID\$/0/g; | |
$command =~ s/\$SERVICECHECKCOMMAND\$/$name/g; | |
my $dt = datetime(); | |
$command =~ s/\$LONGDATETIME\$/$dt/g; | |
$command =~ s/\$SHORTDATETIME\$/$dt/g; | |
$command =~ s/\$DATE\$/$dt/g; | |
$dt =~ s/\d+-\d+-\d+\s+//; | |
$command =~ s/\$TIME\$/$dt/g; | |
my $now = time; | |
$command =~ s/\$TIMET\$/$now/g; | |
$command =~ s/\$\S+\$/-/g; | |
my $results = '<b><kbd>' . HTML::Entities::encode($command) . '</kbd></b><br>'; | |
if ( -x "$monarch_home/bin/monarch_as_nagios" ) { | |
$results .= monarch_as_nagios( '', $command, $monarch_home ); | |
} | |
else { | |
$! = 0; | |
my $output = qx($command 2>&1); | |
if ( $output eq "\n" ) { | |
## Work around <pre>'s suppression of output (and collapsing together | |
## of top and bottom margins around the enclosed text) in this case. | |
$results .= '<pre><br></pre>'; | |
} | |
elsif ($output) { | |
$results .= '<pre style="overflow: auto;">' . HTML::Entities::encode($output) . '</pre>'; | |
} | |
if ( $? == -1 ) { | |
$results .= "Error executing command ($!)" if $!; | |
} | |
else { | |
$results .= 'Command returned ' . wait_status_message($?); | |
} | |
} | |
return $results; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment