1
0
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:
VVelox
2019-06-25 02:23:50 -05:00
committed by GitHub

View File

@ -84,28 +84,32 @@ my $apcaccess_output=`$apcaccess`;
$toReturn{error}=$?;
# check for bad exit codes
if ( $? == -1){
if ( $? == -1) {
$toReturn{errorString}='failed to run apcaccess';
}
elsif ($? & 127) {
} elsif ($? & 127) {
$toReturn{errorString}= sprintf "apcaccess died with signal %d, %s coredump\n",
($? & 127), ($? & 128) ? 'with' : 'without';
($? & 127), ($? & 128) ? 'with' : 'without';
} else {
$toReturn{error}=$? >> 8;
$toReturn{errorString}="apcaccess exited with ".$toReturn{error};
$toReturn{error}=$? >> 8;
$toReturn{errorString}="apcaccess exited with ".$toReturn{error};
}
# 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
my %status;
# pulls apart the output
my @lines=split(/\n/, $apcaccess_output);
foreach my $line ( @lines ){
my ( $var, $val )=split(/\ *\:\ */, $line, 2);
$val=~s/\ .*//;
$status{$var}=$val;
foreach my $line ( @lines ) {
my ( $var, $val )=split(/\:\ */, $line, 2);
if (
defined( $var ) && defined( $val )
) {
$var=~s/\ .*//;
$val=~s/\ .*//;
$status{$var}=$val;
}
}
#pull the desired variables from the output
@ -123,11 +127,12 @@ $toReturn{data}=\%data;
# convert $toReturn to JSON and pretty print if asked to
my $j=JSON->new;
if ( $opts{p} ){
$j->pretty(1);
$j->canonical(1);
if ( $opts{p} ) {
$j->pretty(1);
}
print $j->encode( \%toReturn );
if (! $opts{p} ){
print "\n";
if (! $opts{p} ) {
print "\n";
}
exit 0;