mirror of
https://github.com/librenms/librenms-agent.git
synced 2024-05-09 09:54:52 +00:00
Merge pull request #240 from VVelox/upsfix
fix occasionally random ordering for ups-apc
This commit is contained in:
@ -84,28 +84,32 @@ my $apcaccess_output=`$apcaccess`;
|
|||||||
$toReturn{error}=$?;
|
$toReturn{error}=$?;
|
||||||
|
|
||||||
# check for bad exit codes
|
# check for bad exit codes
|
||||||
if ( $? == -1){
|
if ( $? == -1) {
|
||||||
$toReturn{errorString}='failed to run apcaccess';
|
$toReturn{errorString}='failed to run apcaccess';
|
||||||
}
|
} elsif ($? & 127) {
|
||||||
elsif ($? & 127) {
|
|
||||||
$toReturn{errorString}= sprintf "apcaccess died with signal %d, %s coredump\n",
|
$toReturn{errorString}= sprintf "apcaccess died with signal %d, %s coredump\n",
|
||||||
($? & 127), ($? & 128) ? 'with' : 'without';
|
($? & 127), ($? & 128) ? 'with' : 'without';
|
||||||
} else {
|
} else {
|
||||||
$toReturn{error}=$? >> 8;
|
$toReturn{error}=$? >> 8;
|
||||||
$toReturn{errorString}="apcaccess exited with ".$toReturn{error};
|
$toReturn{errorString}="apcaccess exited with ".$toReturn{error};
|
||||||
}
|
}
|
||||||
|
|
||||||
# if no bad exit codes, we can process $apcaccess_output
|
# if no bad exit codes, we can process $apcaccess_output
|
||||||
if ( $toReturn{error} == 0 ){
|
if ( $toReturn{error} == 0 ) {
|
||||||
# holds the found data for the apcupsd status
|
# holds the found data for the apcupsd status
|
||||||
my %status;
|
my %status;
|
||||||
|
|
||||||
# pulls apart the output
|
# pulls apart the output
|
||||||
my @lines=split(/\n/, $apcaccess_output);
|
my @lines=split(/\n/, $apcaccess_output);
|
||||||
foreach my $line ( @lines ){
|
foreach my $line ( @lines ) {
|
||||||
my ( $var, $val )=split(/\ *\:\ */, $line, 2);
|
my ( $var, $val )=split(/\:\ */, $line, 2);
|
||||||
$val=~s/\ .*//;
|
if (
|
||||||
$status{$var}=$val;
|
defined( $var ) && defined( $val )
|
||||||
|
) {
|
||||||
|
$var=~s/\ .*//;
|
||||||
|
$val=~s/\ .*//;
|
||||||
|
$status{$var}=$val;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#pull the desired variables from the output
|
#pull the desired variables from the output
|
||||||
@ -123,11 +127,12 @@ $toReturn{data}=\%data;
|
|||||||
|
|
||||||
# convert $toReturn to JSON and pretty print if asked to
|
# convert $toReturn to JSON and pretty print if asked to
|
||||||
my $j=JSON->new;
|
my $j=JSON->new;
|
||||||
if ( $opts{p} ){
|
$j->canonical(1);
|
||||||
$j->pretty(1);
|
if ( $opts{p} ) {
|
||||||
|
$j->pretty(1);
|
||||||
}
|
}
|
||||||
print $j->encode( \%toReturn );
|
print $j->encode( \%toReturn );
|
||||||
if (! $opts{p} ){
|
if (! $opts{p} ) {
|
||||||
print "\n";
|
print "\n";
|
||||||
}
|
}
|
||||||
exit 0;
|
exit 0;
|
||||||
|
Reference in New Issue
Block a user