This file is indexed.

/usr/sbin/octo_extractor is in octopussy 1.0.6-0ubuntu2.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
#!/usr/bin/perl -w

=head1 NAME

octo_extractor - Octopussy Logs Extractor program

=head1 SYNOPSIS

octo_extractor --device <device> --service <service> 
	--loglevel <loglevel> --taxonomy <taxonomy> --msgid <msgid>
	--begin YYYYMMDDHHMM --end YYYYMMDDHHMM 
	[ --include '<regexp to include>' ] [ --exclude '<regexp to exclude>' ] 
	[ --pid_param <string> ] [ --output <outputfile> ]

=head1 DESCRIPTION

octo_extractor is the program used by the Octopussy Project to extract Logs

=cut

use strict;
use warnings;
use Readonly;

use Getopt::Long;
Getopt::Long::Configure('bundling');

use AAT::Syslog;
use AAT::Translation;
use AAT::Utils qw( ARRAY NOT_NULL NULL );
use Octopussy;
use Octopussy::Cache;
use Octopussy::Device;
use Octopussy::Loglevel;
use Octopussy::Logs;
use Octopussy::Message;
use Octopussy::Service;
use Octopussy::Taxonomy;

Readonly my $PROG_NAME    => 'octo_extractor';
Readonly my $PROG_VERSION => Octopussy::Version();

my $help;
my (@opt_devices, @opt_services) = ((), ());
my ($opt_loglevel, $opt_taxonomy, $opt_msgid);
my ($opt_begin, $opt_end, $pid_param, $user, $output);
my (@opt_include, @opt_exclude);
my $file_pid = undef;
my $cache    = undef;

=head1 FUNCTIONS

=head2 Help()

Prints Help

=cut

sub Help
{
    my $help_str = <<"EOF";

$PROG_NAME (version $PROG_VERSION)

 Usage: $PROG_NAME --device <device> --service <service> 
        [ --loglevel <loglevel> ] [ --taxonomy <taxonomy> ] [ --msgid <msgid> ]
        --begin YYYYMMDDHHMM --end YYYYMMDDHHMM
        [ --include '<regexp to include>' ] [ --exclude '<regexp to exclude>' ] 
        [ --pid_param <string> ] [ --user <user> ] [ --output <outputfile> ]

EOF

    print $help_str;
    my $list = ' ';
    if (!@opt_devices) { $list .= Octopussy::Device::String_List('any'); }
    elsif (!@opt_services)
    {
        $list .= Octopussy::Device::String_Services(@opt_devices);
    }
    elsif (!defined $opt_loglevel)
    {
        $list .=
            Octopussy::Loglevel::String_List(\@opt_devices, \@opt_services);
    }
    elsif (!defined $opt_taxonomy)
    {
        $list .=
            Octopussy::Taxonomy::String_List(\@opt_devices, \@opt_services);
    }

    my @d_unknowns = Octopussy::Device::Unknowns(@opt_devices);
    my @s_unknowns = Octopussy::Service::Unknowns(@opt_services);
    my @l_unknowns = Octopussy::Loglevel::Unknowns($opt_loglevel);
    my @t_unknowns = Octopussy::Taxonomy::Unknowns($opt_taxonomy);
    printf "[ERROR] Unknown Device(s): %s\n", join ', ', @d_unknowns
        if (scalar @d_unknowns);
    printf "[ERROR] Unknown Service(s): %s\n", join ', ', @s_unknowns
        if (scalar @s_unknowns);
    printf "[ERROR] Unknown Loglevel(s): %s\n", join ', ', @l_unknowns
        if (scalar @l_unknowns);
    printf "[ERROR] Unknown Taxonom(y/ies): %s\n", join ', ', @t_unknowns
        if (scalar @t_unknowns);

    print "$list\n\n";

    exit;
}

=head2 Progress($msg, $num, $nb_match)

Sets progress status

=cut

sub Progress
{
    my ($msg, $num, $nb_match) = @_;

    my $status = AAT::Translation::Get('EN', $msg) . " [$num] [$nb_match]";
    $cache->set("status_${pid_param}", $status) if (defined $pid_param);

    return ($status);
}

=head2 Get_Messages_To_Parse($services, $loglevel, $taxonomy, $msgid)

Returns list of Messages to parse

=cut

sub Get_Messages_To_Parse
{
    my ($services, $loglevel, $taxonomy, $msgid) = @_;
    my @msg_to_parse = ();

    if ((NOT_NULL($msgid)) && ($msgid ne '-ANY-'))
    {
        my $msg = Octopussy::Message::Configuration($services, $msgid);
        my $re = Octopussy::Message::Pattern_To_Regexp($msg);
        push @msg_to_parse, {re => $re};
    }
    else
    {
        @msg_to_parse =
            Octopussy::Message::Parse_List($services, $loglevel, $taxonomy,
            undef, undef, undef, undef);
    }

    return (@msg_to_parse);
}

=head2 Line_Match_Regexps

=cut

sub Line_Match_Regexps
{
    my ($line, $includes, $excludes) = @_;
    my $match = 1;

    foreach my $inc (ARRAY($includes))
    {
        $match = 0 if ($line !~ $inc);
    }
    foreach my $excl (ARRAY($excludes))
    {
        $match = 0
            if ($line =~ $excl);
    }

    return ($match);
}

=head2 Print_Lines($OUT, $ouput, $logs)

=cut

sub Print_Lines
{
    my ($OUT, $ouput, $logs) = @_;

    if (NOT_NULL($output))
    {
        foreach my $l (sort @{$logs}) { print {$OUT} $l; }
    }
    else
    {
        foreach my $l (sort @{$logs}) { print $l; }
    }

    return (scalar @{$logs});
}

=head2 File_Handler_With_Filter($f, $logs, $includes, $excludes, $msg_to_parse)

=cut

sub File_Handler_With_Filter
{
    my ($f, $logs, $includes, $excludes, $msg_to_parse) = @_;
    my $nb_match = 0;

	my $cat = ($f =~ /.+\.gz$/ ? 'zcat' : 'cat');
	if (defined open my $FILE, '-|', "$cat \"$f\"")
    {
        if ($f !~ /Unknown\/\d{4}\/\d{2}\/\d{2}/)
        {    # 'Known' Service -> filter with msg_to_parse
            while (my $line = <$FILE>)
            {
                foreach my $msg (@{$msg_to_parse})
                {
                    if ($line =~ $msg->{re})
                    {
                        if (Line_Match_Regexps($line, $includes, $excludes))
                        {
                            push @{$logs}, $line;
                            $nb_match++;
                        }
                        last;
                    }
                }
            }
        }
        else
        {    # 'Unknown' Service -> no filter
            while (my $line = <$FILE>)
            {
            	if (Line_Match_Regexps($line, $includes, $excludes))
                {
                    push @{$logs}, $line;
                    $nb_match++;
                }
            }
        }
        close $FILE;
    }
    else
    {
        print "Unable to open file '$f'\n";
        AAT::Syslog::Message('octo_extractor', 'UNABLE_OPEN_FILE', $f);
    }

    return ($nb_match);
}

=head2 File_Handler_Without_Filter($f, $logs, $includes, $excludes)

=cut

sub File_Handler_Without_Filter
{
    my ($f, $logs, $includes, $excludes) = @_;
    my $nb_match = 0;

	my $cat = ($f =~ /.+\.gz$/ ? 'zcat' : 'cat');
	if (defined open my $FILE, '-|', "$cat \"$f\"")
    {
        while (my $line = <$FILE>)
        {
            if (Line_Match_Regexps($line, $includes, $excludes))
            {
                push @{$logs}, $line;
                $nb_match++;
            }
        }
        close $FILE;
    }
    else
    {
        print "Unable to open file '$f'\n";
        AAT::Syslog::Message('octo_extractor', 'UNABLE_OPEN_FILE', $f);
    }

    return ($nb_match);
}

=head2 Print_Logs($devices, $services, $loglevel, $taxo, $msgid,
	$begin, $end, $re_incl, $re_excl)

Prints Logs

=cut

sub Print_Logs
{
    my (
        $devices, $services, $loglevel, $taxo, $msgid,
        $begin,   $end,      $re_incl,  $re_excl
       ) = @_;
    my $time     = time;
    my @lines    = ();
    my @includes = ();
    my @excludes = ();
    foreach my $inc (ARRAY($re_incl))
    {
        push @includes, qr/$inc/ if (NOT_NULL($inc));
    }
    foreach my $excl (ARRAY($re_excl))
    {
        push @excludes, qr/$excl/ if (NOT_NULL($excl));
    }

    Progress('_MSG_EXTRACT_PROGRESS_LISTING_FILES', '0/1', 0);
    my ($files, $total) =
        Octopussy::Logs::Get_TimePeriod_Files($devices, $services, $begin,
        $end);
    my $nb_match = 0;

    if ((NOT_NULL($output)) && (!defined open my $OUT, '>', $output))
    {    # output file defined but not able to open
        print "Unable to open file '$output'\n";
        AAT::Syslog::Message('octo_extractor', 'UNABLE_OPEN_FILE', $output);
    }
    else
    {    # output file opened or STDIN
        if (   (@{$services}[0] =~ /-ANY-/i)
            && (NULL($taxo) || ($taxo =~ /-ANY-/i))
            && (NULL($loglevel) || ($loglevel =~ /-ANY-/i)))
        {
            my $i = 1;
            foreach my $min (sort keys %{$files})
            {
                my @logs = ();
                foreach my $f (@{$files->{$min}})
                {
                    Progress('_MSG_EXTRACT_PROGRESS_DATA', $i . "/$total",
                        $nb_match);
                    $nb_match +=
                        File_Handler_Without_Filter($f, \@logs, \@includes,
                        \@excludes);
                    $i++;
                }
                Print_Lines($OUT, $output, \@logs);
            }
        }
        else
        {
            my $i = 1;
            my @msg_to_parse =
                Get_Messages_To_Parse($services, $loglevel, $taxo, $msgid);

            foreach my $min (sort keys %{$files})
            {
                my @logs = ();
                foreach my $f (@{$files->{$min}})
                {
                    Progress('_MSG_EXTRACT_PROGRESS_DATA', $i . "/$total",
                        $nb_match);
                    $nb_match +=
                        File_Handler_With_Filter($f, \@logs, \@includes,
                        \@excludes, \@msg_to_parse);
                    $i++;
                }
                Print_Lines($OUT, $output, \@logs);
            }
        }
        close $OUT if (NOT_NULL($output));
    }

    AAT::Syslog::Message(
        'octo_extractor', 'LOG_SEARCH',
        join(',', @opt_devices),
        join(',', @opt_services),
        "${opt_begin}-${opt_end}", time() - $time, $user || 'UNKNOWN'
    );

    return ($nb_match);
}

=head2 End()

Ends Extraction

=cut

sub End
{
    AAT::Syslog::Message($PROG_NAME, 'Logs Extraction Aborted !');

    unlink $output;
    $cache->remove("status_${pid_param}") if (defined $pid_param);
    exit;
}

#
# MAIN
#
exit if (!Octopussy::Valid_User($PROG_NAME));

$SIG{USR2} = \&End;

my $status = GetOptions(
    'h'           => \$help,
    'help'        => \$help,
    'device=s'    => \@opt_devices,
    'service=s'   => \@opt_services,
    'loglevel=s'  => \$opt_loglevel,
    'taxonomy=s'  => \$opt_taxonomy,
    'msgid=s'     => \$opt_msgid,
    'begin=s'     => \$opt_begin,
    'end=s'       => \$opt_end,
    'include=s'   => \@opt_include,
    'exclude=s'   => \@opt_exclude,
    'pid_param=s' => \$pid_param,
    'user=s'      => \$user,
    'output=s'    => \$output
);

Help()
    if ((!$status)
    || ($help)
    || (!@opt_devices)
    || (!@opt_services)
    || (!defined $opt_begin)
    || (!defined $opt_end));

$cache = Octopussy::Cache::Init('octo_extractor');

my $pid_name = $PROG_NAME . (defined $pid_param ? "_$pid_param" : '');
$file_pid = Octopussy::PID_File($pid_name);

Print_Logs(
    \@opt_devices, \@opt_services, $opt_loglevel,
    $opt_taxonomy, $opt_msgid,     $opt_begin,
    $opt_end,      \@opt_include,  \@opt_exclude
);

$cache->remove("status_${pid_param}") if (defined $pid_param);

=head1 AUTHOR

Sebastien Thebert <octo.devel@gmail.com>

=head1 SEE ALSO

octo_dispatcher, octo_parser, octo_uparser, octo_reporter, octo_scheduler

=cut