From 716519db9558249da85bd700fc54c65dcc1df033 Mon Sep 17 00:00:00 2001 From: Edwin Hoksberg Date: Tue, 23 Apr 2024 14:45:41 +0200 Subject: [PATCH] [add-opensearch-options] Add secure(-S) and disable hostname validation(-i) options for opensearch script --- snmp/opensearch | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/snmp/opensearch b/snmp/opensearch index 5b731b2eb..91f8752d4 100755 --- a/snmp/opensearch +++ b/snmp/opensearch @@ -35,6 +35,8 @@ Supported command line options are as below. Default: 127.0.0.1 -p The port to use. Default: 9200 + -S Use https instead of http. + -I Do not verify hostname (when used with -S). -P Pretty print. The last is only really relevant to the usage with SNMP. @@ -59,21 +61,27 @@ sub main::HELP_MESSAGE { . " Default: 127.0.0.1\n" . "-p The port to use.\n" . " Default: 9200\n" + . "-S Use https instead of http.\n" + . "-I Do not verify hostname (when used with -S).\n" . "-P Pretty print.\n"; } +my $protocol = 'http'; my $host = '127.0.0.1'; my $port = 9200; #gets the options my %opts; -getopts( 'h:p:P', \%opts ); +getopts( 'h:p:SIP', \%opts ); if ( defined( $opts{h} ) ) { $host = $opts{h}; } if ( defined( $opts{p} ) ) { $port = $opts{p}; } +if ( $opts{S} ) { + $protocol = 'https'; +} # my $to_return = { @@ -83,8 +91,8 @@ my $to_return = { date => {}, }; -my $stats_url = 'http://' . $host . ':' . $port . '/_stats'; -my $health_url = 'http://' . $host . ':' . $port . '/_cluster/health'; +my $stats_url = $protocol . '://' . $host . ':' . $port . '/_stats'; +my $health_url = $protocol . '://' . $host . ':' . $port . '/_cluster/health'; my $json = JSON->new->allow_nonref->canonical(1); if ( $opts{P} ) { @@ -93,6 +101,10 @@ if ( $opts{P} ) { my $ua = LWP::UserAgent->new( timeout => 10 ); +if ( $opts{I} ) { + $ua->ssl_opts( verify_hostname => 0, SSL_verify_mode => 0x00 ); +} + my $stats_response = $ua->get($stats_url); my $stats_json; if ( $stats_response->is_success ) {