Skip to content

Commit

Permalink
HTTPS and CA file validation implemented.
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco-Valle committed Apr 28, 2024
1 parent 80d6ac6 commit 68da0b4
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions snmp/opensearch
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,14 @@ Add this to snmpd.conf as below and restart snmpd.
Supported command line options are as below.
-c <path> CA file path.
Default: empty
-h <host> The host to connect to.
Default: 127.0.0.1
-p <port> The port to use.
Default: 9200
-P Pretty print.
-S Use HTTPS.
The last is only really relevant to the usage with SNMP.
Expand All @@ -55,25 +58,31 @@ sub main::VERSION_MESSAGE {

sub main::HELP_MESSAGE {
print "\n"
. "-c <path> CA file path.\n"
. "-h <host> The host to connect to.\n"
. " Default: 127.0.0.1\n"
. "-p <port> The port to use.\n"
. " Default: 9200\n"
. "-P Pretty print.\n";
. "-P Pretty print.\n"
. "-S Use HTTPS.\n";
}

my $host = '127.0.0.1';
my $port = 9200;
my $schema = 'http';

#gets the options
my %opts;
getopts( 'h:p:P', \%opts );
getopts( 'c:h:p:P:S', \%opts );
if ( defined( $opts{h} ) ) {
$host = $opts{h};
}
if ( defined( $opts{p} ) ) {
$port = $opts{p};
}
if ( $opts{S} ) {
$schema = 'https';
}

#
my $to_return = {
Expand All @@ -83,8 +92,8 @@ my $to_return = {
date => {},
};

my $stats_url = 'http://' . $host . ':' . $port . '/_stats';
my $health_url = 'http://' . $host . ':' . $port . '/_cluster/health';
my $stats_url = $schema . '://' . $host . ':' . $port . '/_stats';
my $health_url = $schema . '://' . $host . ':' . $port . '/_cluster/health';

my $json = JSON->new->allow_nonref->canonical(1);
if ( $opts{P} ) {
Expand All @@ -93,6 +102,11 @@ if ( $opts{P} ) {

my $ua = LWP::UserAgent->new( timeout => 10 );

if ( defined( $opts{c} ) ) {
# set ca file
$ua->ssl_opts( SSL_ca_file => $opts{c});
}

my $stats_response = $ua->get($stats_url);
my $stats_json;
if ( $stats_response->is_success ) {
Expand Down

0 comments on commit 68da0b4

Please sign in to comment.