Created
June 11, 2016 00:47
-
-
Save dr-kd/d5bf575052dae92d134cd8dbf28ba919 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package SplunkPipe::Client; | |
| use Moo; | |
| extends 'LWP::UserAgent::JSON'; | |
| use IO::Socket::SSL; | |
| use Path::Tiny; | |
| sub FOREIGNBUILDARGS { | |
| my ($class, %args) = @_; | |
| $args{ssl_opts} = { verify_hostname => 0, | |
| SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE, | |
| }; | |
| return %args; | |
| } | |
| sub BUILD { | |
| my ($self) = @_; | |
| # authenticate and on construction | |
| my $res = $self->post('/services/auth/login', | |
| { username => $self->splunk_credentials->{username}, | |
| password => $self->splunk_credentials->{password}, | |
| }); | |
| die "Unable to authentication with content " . $res->content . "\n" | |
| unless $res->status_line eq '200 OK'; | |
| my ($key) = $res->content =~ m{<sessionKey>(.+)</sessionKey>}ms; | |
| $self->default_header( Authorization => "Splunk $key" ); | |
| } | |
| has 'credential_file' => ( | |
| is => 'ro', | |
| default => sub { | |
| return path("$ENV{HOME}/.creds"); | |
| }, | |
| ); | |
| has 'splunk_credentials' => ( | |
| is => 'ro', | |
| default => sub { | |
| my ($self) = @_; | |
| my @data = $self->credential_file->lines; | |
| my $creds = { map { /(\S.*?)=(\S+)/} @data }; | |
| return $creds; | |
| }, | |
| ); | |
| has server => ( | |
| is => 'ro', | |
| default => 'https://your.splunk.host:8089' | |
| ); | |
| sub _mangle_request { | |
| my ($orig, $self, @args) = @_; | |
| $args[0] = '/' . $args[0] unless $args[0] =~ m{^/}; | |
| $args[0] = $self->server . $args[0]; # . '$mode'; | |
| if ( $args[0] !~ m{auth/login}) { | |
| $args[0] = "$args[0]?output_mode=json"; | |
| } | |
| return $self->$orig(@args); | |
| } | |
| around get => \&_mangle_request; | |
| around post => \&_mangle_request; | |
| around delete => \&_mangle_request; | |
| 1; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package SplunkPipe; | |
| use Moo; | |
| use SplunkPipe::Client; | |
| has 'client' => ( | |
| is => 'lazy', | |
| ); | |
| sub _build_client { | |
| my ($self) = @_; | |
| my $client = SplunkPipe::Client->new(); | |
| return $client; | |
| } | |
| sub do_search { | |
| my ($self, $search) = @_; | |
| $search = "search $search"; | |
| my $res = $self->client->post('/services/search/jobs', { search => $search}); | |
| return $res->json_content->{sid}; | |
| } | |
| sub get_search_status { | |
| my ($self, $sid, %options) = @_; | |
| my $res = $self->client->get("/services/search/jobs/$sid"); | |
| return $res->json_content if $options{full}; | |
| return $res->json_content->{entry}->[0]->{content}->{isDone}; | |
| } | |
| sub get_search_results { | |
| my ($self, $sid) = @_; | |
| my $res = $self->client->get("/services/search/jobs/$sid/results"); | |
| return $res->json_content; | |
| } | |
| 1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment