Skip to content

Commit

Permalink
add help/version info
Browse files Browse the repository at this point in the history
  • Loading branch information
VVelox committed May 12, 2024
1 parent 827e74f commit bf89867
Showing 1 changed file with 43 additions and 27 deletions.
70 changes: 43 additions & 27 deletions snmp/redis.pl
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

=head1 NAME
logsize - LinbreNMS JSON extend for redis.
redis.pl - LinbreNMS JSON extend for redis.
=head1 SYNOPSIS
Expand All @@ -14,6 +14,14 @@ =head2 -B
Do not the return output via GZip+Base64.
=head2 -h|--help
Print help info.
=head2 -v|--version
Print version info.
=head1 SETUP
Install the depends.
Expand All @@ -39,6 +47,7 @@ =head1 SETUP
use MIME::Base64;
use IO::Compress::Gzip qw(gzip $GzipError);
use File::Slurp;
use Pod::Usage;

$Getopt::Std::STANDARD_HELP_VERSION = 1;

Expand All @@ -47,52 +56,59 @@ sub main::VERSION_MESSAGE {
}

sub main::HELP_MESSAGE {
print '
-B Do not use Gzip+Base64 for the output.
';
pod2usage( -exitval => 255, -verbose => 2, -output => \*STDOUT, );
}

my $return_json = {
error => 0,
errorString => '',
version => 1,
data => {
},
data => {},
};

#gets the options
my %opts = ();
getopts( 'B', \%opts );
getopts( 'Bhv', \%opts );

if ( $opts{v} ) {
main::VERSION_MESSAGE;
exit 256;
}

if ( $opts{h} ) {
main::VERSION_MESSAGE;
pod2usage( -exitval => 255, -verbose => 2, -output => \*STDOUT, );
exit 256;
}

# ensure that $ENV{PATH} has has it
$ENV{PATH}=$ENV{PATH}.':/usr/bin:/usr/sbin:/usr/local/sbin:/usr/local/bin';
$ENV{PATH} = $ENV{PATH} . ':/usr/bin:/usr/sbin:/usr/local/sbin:/usr/local/bin';

my $output_raw=`redis-cli info 2> /dev/null`;
if ($? != 0) {
$return_json->{error}=1;
$return_json->{error}='redis-cli info exited non-zero';
print encode_json($return_json)."\n";
my $output_raw = `redis-cli info 2> /dev/null`;
if ( $? != 0 ) {
$return_json->{error} = 1;
$return_json->{error} = 'redis-cli info exited non-zero';
print encode_json($return_json) . "\n";
}

$output_raw=~s/\r//g;
$output_raw =~ s/\r//g;
my $section;
foreach my $line (split(/\n/, $output_raw)) {
if ($line ne '' && $line =~ /^# /) {
foreach my $line ( split( /\n/, $output_raw ) ) {
if ( $line ne '' && $line =~ /^# / ) {
$line =~ s/^# //;
$section= $line;
$return_json->{data}{$section}={};
}elsif ($line ne '' && defined($section)) {
my ($key, $value)=split(/\:/, $line);
if (defined($key) && defined($value)) {
$return_json->{data}{$section}{$key}=$value;
$section = $line;
$return_json->{data}{$section} = {};
} elsif ( $line ne '' && defined($section) ) {
my ( $key, $value ) = split( /\:/, $line );
if ( defined($key) && defined($value) ) {
$return_json->{data}{$section}{$key} = $value;
}
}
}
} ## end foreach my $line ( split( /\n/, $output_raw ) )

my $return_json_raw=encode_json($return_json);
if ($opts{B}) {
print $return_json_raw."\n";
my $return_json_raw = encode_json($return_json);
if ( $opts{B} ) {
print $return_json_raw. "\n";
exit 0;
}

Expand Down

0 comments on commit bf89867

Please sign in to comment.