| 1 | # | ||
| 2 | # $Id$ | ||
| 3 | # | ||
| 4 | # api::splunk Brik | ||
| 5 | # | ||
| 6 | package Metabrik::Api::Splunk; | ||
| 7 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 2 | ▶ | use base qw(Metabrik::Client::Rest); |
|
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, # 0 means return everything | ||
| 33 | offset => 0, # 0 means return everything | ||
| 34 | }, | ||
| 35 | commands => { | ||
| 36 | get => [ qw(path) ], | ||
| 37 | reset_user_agent => [ ], # Inherited | ||
| 38 | apps_local => [ ], | ||
| 39 | search_jobs => [ qw(search) ], | ||
| 40 | search_jobs_sid => [ qw(sid) ], | ||
| 41 | search_jobs_sid_results => [ qw(sid count|OPTIONAL offset|OPTIONAL) ], | ||
| 42 | licenser_groups => [ ], | ||
| 43 | data_lookup_table_files_acl => [ qw(app csv_file perm|OPTIONAL) ], | ||
| 44 | cluster_config => [ ], | ||
| 45 | cluster_config_config => [ ], | ||
| 46 | deployment_client => [ ], | ||
| 47 | deployment_client_config => [ ], | ||
| 48 | search_data_lookuptablefiles => [ qw(username|OPTIONAL) ], | ||
| 49 | }, | ||
| 50 | }; | ||
| 51 | } | ||
| 52 | |||
| 53 | # | ||
| 54 | # API reference: | ||
| 55 | # http://docs.splunk.com/Documentation/Splunk/latest/RESTREF/RESTlist | ||
| 56 | # | ||
| 57 | |||
| 58 | sub get { | ||
| 59 | my $self = shift; | ||
| 60 | my ($path, $count) = @_; | ||
| 61 | |||
| 62 | $count ||= $self->count; | ||
| 63 | $self->brik_help_run_undef_arg('get', $path) or return; | ||
| 64 | |||
| 65 | my $uri = $self->uri; | ||
| 66 | |||
| 67 | my $resp = $self->SUPER::get($uri.$path.'?count='.$count) or return; | ||
| 68 | |||
| 69 | my $content = $self->content('xml') or return; | ||
| 70 | my $code = $self->code or return; | ||
| 71 | |||
| 72 | $self->log->verbose("get: returned code [$code]"); | ||
| 73 | |||
| 74 | return $content; | ||
| 75 | } | ||
| 76 | |||
| 77 | sub apps_local { | ||
| 78 | my $self = shift; | ||
| 79 | |||
| 80 | return $self->get('/services/apps/local'); | ||
| 81 | } | ||
| 82 | |||
| 83 | # | ||
| 84 | # run api::splunk search_jobs "{ search => 'search index=main' }" https://localhost:8089 | ||
| 85 | # | ||
| 86 | sub search_jobs { | ||
| 87 | my $self = shift; | ||
| 88 | my ($post) = @_; | ||
| 89 | |||
| 90 | $self->brik_help_run_undef_arg('search_jobs', $post) or return; | ||
| 91 | $self->brik_help_run_invalid_arg('search_jobs', $post, 'HASH') or return; | ||
| 92 | |||
| 93 | my $uri = $self->uri; | ||
| 94 | |||
| 95 | my $resp = $self->post($post, $uri.'/services/search/jobs') or return; | ||
| 96 | |||
| 97 | my $code = $self->code; | ||
| 98 | |||
| 99 | $self->log->verbose("search_jobs: returned code [$code]"); | ||
| 100 | $self->log->debug("search_jobs: content [".$resp->{content}."]"); | ||
| 101 | |||
| 102 | if ($code == 201) { # Job created | ||
| 103 | return $self->content('xml'); | ||
| 104 | } | ||
| 105 | |||
| 106 | return $self->log->error("search_jobs: failed with code [$code]"); | ||
| 107 | } | ||
| 108 | |||
| 109 | sub search_jobs_sid { | ||
| 110 | my $self = shift; | ||
| 111 | my ($sid) = @_; | ||
| 112 | |||
| 113 | $self->brik_help_run_undef_arg('search_jobs_sid', $sid) or return; | ||
| 114 | |||
| 115 | my $uri = $self->uri; | ||
| 116 | |||
| 117 | my $resp = $self->SUPER::get($uri.'/services/search/jobs/'.$sid) or return; | ||
| 118 | |||
| 119 | my $code = $self->code; | ||
| 120 | |||
| 121 | $self->log->verbose("search_jobs_sid: returned code [$code]"); | ||
| 122 | $self->log->debug("search_jobs_sid: content [".$resp->{content}."]"); | ||
| 123 | |||
| 124 | if ($code == 404) { | ||
| 125 | return 0; | ||
| 126 | } | ||
| 127 | elsif ($code == 200) { | ||
| 128 | return $self->content('xml'); | ||
| 129 | } | ||
| 130 | |||
| 131 | return $self->log->error("search_jobs_sid: failed with code [$code]"); | ||
| 132 | } | ||
| 133 | |||
| 134 | # | ||
| 135 | # http://docs.splunk.com/Documentation/Splunk/latest/RESTREF/RESTsearch#search.2Fjobs.2F.7Bsearch_id.7D.2Fresults | ||
| 136 | # | ||
| 137 | sub search_jobs_sid_results { | ||
| 138 | my $self = shift; | ||
| 139 | my ($sid, $count, $offset) = @_; | ||
| 140 | |||
| 141 | $count ||= $self->count; | ||
| 142 | $offset ||= $self->offset; | ||
| 143 | $self->brik_help_run_undef_arg('search_jobs_sid_results', $sid) or return; | ||
| 144 | |||
| 145 | my $uri = $self->uri; | ||
| 146 | my $output_mode = $self->output_mode; | ||
| 147 | |||
| 148 | my $resp = $self->SUPER::get( | ||
| 149 | $uri.'/services/search/jobs/'.$sid. | ||
| 150 | '/results/?output_mode='.$output_mode."&offset=$offset&count=$count" | ||
| 151 | ) or return; | ||
| 152 | |||
| 153 | my $code = $self->code; | ||
| 154 | |||
| 155 | $self->log->verbose("search_jobs_sid_results: returned code [$code]"); | ||
| 156 | $self->log->debug("search_jobs_sid_results: content [".$resp->{content}."]"); | ||
| 157 | |||
| 158 | if ($code == 200) { # Job finished | ||
| 159 | return $self->content($output_mode); | ||
| 160 | } | ||
| 161 | elsif ($code == 204) { # Job not finished | ||
| 162 | return $self->log->error("search_jobs_sid_results: job not done"); | ||
| 163 | } | ||
| 164 | |||
| 165 | return $self->log->error("search_jobs_sid_results: failed with code [$code]"); | ||
| 166 | } | ||
| 167 | |||
| 168 | # | ||
| 169 | # http://docs.splunk.com/Documentation/Splunk/6.1.3/RESTAPI/RESTlicense | ||
| 170 | # | ||
| 171 | sub licenser_groups { | ||
| 172 | my $self = shift; | ||
| 173 | |||
| 174 | return $self->get('/services/licenser/groups'); | ||
| 175 | } | ||
| 176 | |||
| 177 | # | ||
| 178 | # curl -k -u admin https://localhost:8089/servicesNS/admin/$APP/data/lookup-table-files/$FILE.csv/acl -d owner=nobody -d sharing=global | ||
| 179 | # | ||
| 180 | # http://docs.splunk.com/Documentation/Splunk/6.1.3/RESTAPI/RESTknowledge | ||
| 181 | # | ||
| 182 | sub data_lookup_table_files_acl { | ||
| 183 | my $self = shift; | ||
| 184 | my ($app, $csv_file, $perm) = @_; | ||
| 185 | |||
| 186 | $perm ||= { owner => 'nobody', sharing => 'global' }; | ||
| 187 | $self->brik_help_run_undef_arg('data_lookup_table_files_acl', $app) or return; | ||
| 188 | $self->brik_help_run_undef_arg('data_lookup_table_files_acl', $csv_file) or return; | ||
| 189 | |||
| 190 | my $uri = $self->uri; | ||
| 191 | my $username = $self->username; | ||
| 192 | |||
| 193 | if ($csv_file !~ m{\.csv$}) { | ||
| 194 | return $self->log->error("data_lookup_table_files_acl: csv file [$csv_file] must ends with .csv extension"); | ||
| 195 | } | ||
| 196 | |||
| 197 | my $resp = $self->post( | ||
| 198 | $perm, $uri.'/servicesNS/'.$username.'/'.$app.'/data/lookup-table-files/'.$csv_file.'/acl' | ||
| 199 | ) or return; | ||
| 200 | |||
| 201 | my $code = $self->code; | ||
| 202 | |||
| 203 | $self->log->verbose("data_lookup_table_files_acl: returned code [$code]"); | ||
| 204 | $self->log->debug("data_lookup_table_files_acl: content [".$resp->{content}."]"); | ||
| 205 | |||
| 206 | return $self->content('xml'); | ||
| 207 | } | ||
| 208 | |||
| 209 | sub cluser_config { | ||
| 210 | my $self = shift; | ||
| 211 | |||
| 212 | return $self->get('/services/cluster/config'); | ||
| 213 | } | ||
| 214 | |||
| 215 | sub cluser_config_config { | ||
| 216 | my $self = shift; | ||
| 217 | |||
| 218 | return $self->get('/services/cluster/config/config'); | ||
| 219 | } | ||
| 220 | |||
| 221 | sub deployment_client { | ||
| 222 | my $self = shift; | ||
| 223 | |||
| 224 | return $self->get('/services/deployment/client'); | ||
| 225 | } | ||
| 226 | |||
| 227 | sub deployment_client_config { | ||
| 228 | my $self = shift; | ||
| 229 | |||
| 230 | return $self->get('/services/deployment/client/config'); | ||
| 231 | } | ||
| 232 | |||
| 233 | sub search_data_lookuptablefiles { | ||
| 234 | my $self = shift; | ||
| 235 | my ($username) = @_; | ||
| 236 | |||
| 237 | $username ||= $self->username; | ||
| 238 | |||
| 239 | return $self->get('/servicesNS/'.$username.'/search/data/lookup-table-files'); | ||
| 240 | } | ||
| 241 | |||
| 242 | 1; | ||
| 243 | |||
| 244 | __END__ | ||