Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HTTPS with CA file and Authorization header from file for opensearch script #517

Merged
merged 2 commits into from
May 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 42 additions & 6 deletions snmp/opensearch
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,15 @@ Add this to snmpd.conf as below and restart snmpd.

Supported command line options are as below.

-a <path> Auth token path.
-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 +59,40 @@ sub main::VERSION_MESSAGE {

sub main::HELP_MESSAGE {
print "\n"
. "-a <path> Auth token path.\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( 'a: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 $auth_token;
if ( defined( $opts{a} ) ) {
open my $auth_file, '<', $opts{a};
$auth_token = <$auth_file>;
close $auth_file;
chop $auth_token;
}

#
my $to_return = {
Expand All @@ -83,8 +102,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,7 +112,18 @@ if ( $opts{P} ) {

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

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

my $stats_response;
if ( defined( $opts{a} ) ) {
$stats_response = $ua->get($stats_url, "Authorization" => $auth_token,);
} else {
$stats_response = $ua->get($stats_url);
}

my $stats_json;
if ( $stats_response->is_success ) {
eval { $stats_json = decode_json( $stats_response->decoded_content ); };
Expand All @@ -117,7 +147,13 @@ else {
exit;
}

my $health_response = $ua->get($health_url);
my $health_response;
if ( defined( $opts{a} ) ) {
$health_response = $ua->get($health_url, "Authorization" => $auth_token,);
} else {
$health_response = $ua->get($health_url);
}

my $health_json;
if ( $health_response->is_success ) {
eval { $health_json = decode_json( $health_response->decoded_content ); };
Expand Down
Loading