From bf89867d6d44dfe7861b5e83058c3829aad8032f Mon Sep 17 00:00:00 2001 From: "Zane C. Bowers-Hadley" Date: Sun, 12 May 2024 10:41:14 -0500 Subject: [PATCH] add help/version info --- snmp/redis.pl | 70 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 43 insertions(+), 27 deletions(-) mode change 100644 => 100755 snmp/redis.pl diff --git a/snmp/redis.pl b/snmp/redis.pl old mode 100644 new mode 100755 index 490ef6d81..3d4a80830 --- a/snmp/redis.pl +++ b/snmp/redis.pl @@ -2,7 +2,7 @@ =head1 NAME -logsize - LinbreNMS JSON extend for redis. +redis.pl - LinbreNMS JSON extend for redis. =head1 SYNOPSIS @@ -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. @@ -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; @@ -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; }