| 1 | # | ||
| 2 | # $Id$ | ||
| 3 | # | ||
| 4 | # client::splunk Brik | ||
| 5 | # | ||
| 6 | package Metabrik::Client::Splunk; | ||
| 7 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 1 | ▶ | use base qw(Metabrik::Api::Splunk); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | sub brik_properties { | ||
| 13 | return { | ||
| 14 | revision => '$Revision$', | ||
| 15 | tags => [ qw(unstable rest) ], | ||
| 16 | author => 'GomoR <GomoR[at]metabrik.org>', | ||
| 17 | license => 'http://opensource.org/licenses/BSD-3-Clause', | ||
| 18 | attributes => { | ||
| 19 | uri => [ qw(uri) ], # Inherited | ||
| 20 | username => [ qw(username) ], # Inherited | ||
| 21 | password => [ qw(password) ], # Inherited | ||
| 22 | ssl_verify => [ qw(0|1) ], # Inherited | ||
| 23 | output_mode => [ qw(json|xml|csv) ], | ||
| 24 | count => [ qw(number) ], | ||
| 25 | offset => [ qw(number) ], | ||
| 26 | }, | ||
| 27 | attributes_default => { | ||
| 28 | uri => 'https://localhost:8089', | ||
| 29 | username => 'admin', | ||
| 30 | ssl_verify => 0, | ||
| 31 | output_mode => 'json', | ||
| 32 | count => 1000, | ||
| 33 | offset => 0, | ||
| 34 | }, | ||
| 35 | commands => { | ||
| 36 | search => [ qw(string) ], | ||
| 37 | is_job_done => [ qw(sid) ], | ||
| 38 | get_results => [ qw(sid) ], | ||
| 39 | }, | ||
| 40 | }; | ||
| 41 | } | ||
| 42 | |||
| 43 | # | ||
| 44 | # Example: | ||
| 45 | # run client::splunk search "index=main" | ||
| 46 | # | ||
| 47 | sub search { | ||
| 48 | my $self = shift; | ||
| 49 | my ($search) = @_; | ||
| 50 | |||
| 51 | $self->brik_help_run_undef_arg('search', $search) or return; | ||
| 52 | |||
| 53 | my $r = $self->search_jobs({ search => "search $search" }) or return; | ||
| 54 | |||
| 55 | if (! exists($r->{sid})) { | ||
| 56 | return $self->log->error("search: sid not found in response"); | ||
| 57 | } | ||
| 58 | |||
| 59 | return $r->{sid}; | ||
| 60 | } | ||
| 61 | |||
| 62 | sub is_job_done { | ||
| 63 | my $self = shift; | ||
| 64 | my ($sid) = @_; | ||
| 65 | |||
| 66 | $self->brik_help_run_undef_arg('is_job_done', $sid) or return; | ||
| 67 | |||
| 68 | my $r = $self->search_jobs_sid($sid); | ||
| 69 | if (! defined($r)) { | ||
| 70 | return; | ||
| 71 | } | ||
| 72 | elsif ($r == 0) { | ||
| 73 | return 0; | ||
| 74 | } | ||
| 75 | |||
| 76 | if (exists($r->{content}) | ||
| 77 | && exists($r->{content}{'s:dict'}) | ||
| 78 | && exists($r->{content}{'s:dict'}{'s:key'}) | ||
| 79 | && exists($r->{content}{'s:dict'}{'s:key'}{dispatchState}) | ||
| 80 | && exists($r->{content}{'s:dict'}{'s:key'}{dispatchState}{content})) { | ||
| 81 | my $status = $r->{content}{'s:dict'}{'s:key'}{dispatchState}{content}; | ||
| 82 | return $status eq 'DONE'; | ||
| 83 | } | ||
| 84 | |||
| 85 | return $self->log->error("is_job_done: invalid response"); | ||
| 86 | } | ||
| 87 | |||
| 88 | sub get_results { | ||
| 89 | my $self = shift; | ||
| 90 | my ($sid, $count, $offset) = @_; | ||
| 91 | |||
| 92 | $count ||= $self->count; | ||
| 93 | $offset ||= $self->offset; | ||
| 94 | $self->brik_help_run_undef_arg('get_results', $sid) or return; | ||
| 95 | |||
| 96 | my $output_mode = $self->output_mode; | ||
| 97 | if ($output_mode ne 'xml' | ||
| 98 | && $output_mode ne 'json' | ||
| 99 | && $output_mode ne 'csv') { | ||
| 100 | return $self->log->error("get_results: output_mode not supported [$output_mode]"); | ||
| 101 | } | ||
| 102 | |||
| 103 | my $r = $self->search_jobs_sid_results($sid, $count, $offset); | ||
| 104 | if (! defined($r)) { | ||
| 105 | return; | ||
| 106 | } | ||
| 107 | |||
| 108 | # No results from this search | ||
| 109 | if (! length($r)) { | ||
| 110 | return []; | ||
| 111 | } | ||
| 112 | |||
| 113 | return $r; | ||
| 114 | } | ||
| 115 | |||
| 116 | 1; | ||
| 117 | |||
| 118 | __END__ | ||