| 1 | =head1 NAME | ||
| 2 | |||
| 3 | WWW::Splunk - Client library for Splunk log search engine | ||
| 4 | |||
| 5 | =head1 SYNOPSIS | ||
| 6 | |||
| 7 | use WWW::Splunk; | ||
| 8 | |||
| 9 | my $splunk = new WWW::Splunk ({ | ||
| 10 | host => $host, | ||
| 11 | port => $port, | ||
| 12 | login => $login, | ||
| 13 | password => $password, | ||
| 14 | unsafe_ssl => 1, | ||
| 15 | }); | ||
| 16 | |||
| 17 | my $sid = $splunk->start_search ('selinux avc'); | ||
| 18 | $splunk->poll_search ($sid); | ||
| 19 | until ($splunk->results_read ($sid)) { | ||
| 20 | print scalar $splunk->search_results ($sid); | ||
| 21 | } | ||
| 22 | print " results found\n"; | ||
| 23 | |||
| 24 | =head1 DESCRIPTION | ||
| 25 | |||
| 26 | This module contains utility functions for Splunk API, implementing | ||
| 27 | version 4.1 API, verified to work with 4.2, 4.3 and 5.0.3 versions. | ||
| 28 | |||
| 29 | =cut | ||
| 30 | |||
| 31 | package WWW::Splunk; | ||
| 32 | |||
| 33 | use strict; | ||
| 34 | use warnings; | ||
| 35 | |||
| 36 | our $VERSION = '2.0'; | ||
| 37 | |||
| 38 | use WWW::Splunk::API; | ||
| 39 | use Carp; | ||
| 40 | use Date::Manip; | ||
| 41 | |||
| 42 | use base qw/WWW::Splunk::API/; | ||
| 43 | |||
| 44 | =head2 B<start_search> (F<string>) [(F<since>)] [(F<until>] | ||
| 45 | |||
| 46 | Initiate a search, return a SID (Search ID) string. | ||
| 47 | |||
| 48 | =cut | ||
| 49 | sub start_search | ||
| 50 | { | ||
| 51 | my $self = shift; | ||
| 52 | my $string = shift; | ||
| 53 | my $since = shift; | ||
| 54 | my $until = shift; | ||
| 55 | |||
| 56 | # Format dates | ||
| 57 | ($since, $until) = map { defined $_ ? scalar UnixDate (ParseDate ($_) || $_, '%O') || $_ : undef } | ||
| 58 | ($since, $until); | ||
| 59 | |||
| 60 | $self->{results_consumed} = 0; | ||
| 61 | my $response = $self->post ('/search/jobs', { | ||
| 62 | search => "search $string", | ||
| 63 | (defined $since ? (earliest_time => $since) : ()), | ||
| 64 | (defined $until ? (latest_time => $until) : ()), | ||
| 65 | }); | ||
| 66 | die 'Unexpected response format ' | ||
| 67 | unless $response and ref $response eq 'XML::LibXML::Document'; | ||
| 68 | my $sid = $response->findvalue ('/response/sid'); | ||
| 69 | croak "Bad response" unless defined $sid; | ||
| 70 | return $sid; | ||
| 71 | } | ||
| 72 | |||
| 73 | =head2 B<rt_search> (F<string>) (F<callback>) [(F<since>)] [(F<until>] | ||
| 74 | |||
| 75 | Initiate a real-time search, calling a callback for each line matched. | ||
| 76 | |||
| 77 | Finishes only if connection terminates (potentially never), returning number of | ||
| 78 | results consumed. | ||
| 79 | |||
| 80 | =cut | ||
| 81 | sub rt_search | ||
| 82 | { | ||
| 83 | my $self = shift; | ||
| 84 | my $string = shift; | ||
| 85 | my $callback = shift; | ||
| 86 | my $since = shift || 'rt'; | ||
| 87 | my $until = shift || 'rt'; | ||
| 88 | my $counter = 0; | ||
| 89 | |||
| 90 | $self->post ('/search/jobs/export', { | ||
| 91 | search => "search $string", | ||
| 92 | earliest_time => $since, | ||
| 93 | latest_time => $until, | ||
| 94 | search_mode => 'realtime', | ||
| 95 | }, sub { | ||
| 96 | $callback->(@_); | ||
| 97 | $counter++; | ||
| 98 | }); | ||
| 99 | |||
| 100 | return $counter; | ||
| 101 | } | ||
| 102 | |||
| 103 | =head2 B<search_done> (F<sid>) | ||
| 104 | |||
| 105 | Return true if the search is finished. | ||
| 106 | |||
| 107 | =cut | ||
| 108 | sub search_done | ||
| 109 | { | ||
| 110 | my $self = shift; | ||
| 111 | my $sid = shift; | ||
| 112 | |||
| 113 | my $search = $self->get ('/search/jobs/'.$sid); | ||
| 114 | #use Data::Dumper; | ||
| 115 | #warn Dumper $search->toStringC14N; | ||
| 116 | return $search->{isDone}; | ||
| 117 | } | ||
| 118 | |||
| 119 | =head2 B<poll_search> (F<sid>) | ||
| 120 | |||
| 121 | Wait for a search to finish. | ||
| 122 | |||
| 123 | =cut | ||
| 124 | sub poll_search | ||
| 125 | { | ||
| 126 | my $self = shift; | ||
| 127 | my $sid = shift; | ||
| 128 | |||
| 129 | until ($self->search_done ($sid)) { sleep 1; } | ||
| 130 | } | ||
| 131 | |||
| 132 | =head2 B<search_results> (F<sid>) | ||
| 133 | |||
| 134 | Return an array of the matched events. | ||
| 135 | If called multiple times, it only returns events which | ||
| 136 | were added from the time of the last call. | ||
| 137 | Oh, and you can't run multiple search concurrently | ||
| 138 | with single L<WWW::Splunk> instance. Otherwise, | ||
| 139 | L<WWW::Splunk> is perfectly thread-safe. | ||
| 140 | |||
| 141 | =cut | ||
| 142 | sub search_results | ||
| 143 | { | ||
| 144 | my $self = shift; | ||
| 145 | my $sid = shift; | ||
| 146 | |||
| 147 | my $done = $self->search_done ($sid); | ||
| 148 | my @results = $self->get ('/search/jobs/'.$sid.'/results?count=1024&offset='. | ||
| 149 | $self->{results_consumed}); | ||
| 150 | $self->{results_consumed} += scalar @results; | ||
| 151 | $self->{last_read} = scalar @results if $done; | ||
| 152 | |||
| 153 | return @results; | ||
| 154 | } | ||
| 155 | |||
| 156 | =head2 B<results_read> (F<sid>) | ||
| 157 | |||
| 158 | Return true if search is finished and all there are no | ||
| 159 | more results to read (everything was fetched with L<search_results>). | ||
| 160 | |||
| 161 | =cut | ||
| 162 | sub results_read | ||
| 163 | { | ||
| 164 | my $self = shift; | ||
| 165 | my $sid = shift; | ||
| 166 | |||
| 167 | return undef if not defined $self->{last_read}; | ||
| 168 | return $self->{last_read} eq 0; | ||
| 169 | } | ||
| 170 | |||
| 171 | =head1 AUTHORS | ||
| 172 | |||
| 173 | Lubomir Rintel, L<< <lkundrak@v3.sk> >> | ||
| 174 | |||
| 175 | The code is hosted on GitHub L<http://github.com/lkundrak/perl-WWW-Splunk>. | ||
| 176 | Bug fixes and feature enhancements are always welcome. | ||
| 177 | |||
| 178 | =cut | ||
| 179 | |||
| 180 | 1; |