This file is indexed.

/usr/sbin/octo_extractor_fields is in octopussy 1.0.6-0ubuntu1.

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
#!/usr/bin/perl -w

=head1 NAME

octo_extractor_fields - Octopussy Logs Extractor (by table fields) program

=head1 SYNOPSIS

octo_extractor --device <device> --service <service> 
  --table <table> --loglevel <loglevel> --taxonomy <taxonomy> 
	--begin YYYYMMDDHHMM --end YYYYMMDDHHMM 
  --fields <field1,field2,fieldn>
	[ --pid_param <string> ] [ --json <json_outputfile> ]

=head1 DESCRIPTION

octo_extractor_fields is the program used by the Octopussy Project to extract Logs (by table fields)

=cut

use strict;
use warnings;
use Readonly;

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

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

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

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

=head1 FUNCTIONS

=head2 String_List($type, $any, $fct, @args)

Returns List of elements separated by ", " from one function and args

=cut

sub String_List
{
    my ($type, $any, $fct, @args) = @_;
    my @list = (defined $any ? ('-ANY-') : ());

    my @data = $fct->(@args);
    foreach my $d (@data)
    {
        if (ref $d eq 'HASH')
        {
            if    (defined $d->{label}) { push @list, $d->{label}; }
            elsif (defined $d->{value}) { push @list, $d->{value}; }
        }
        else { push @list, $d; }
    }

    return (" $type list: " . join(', ', sort @list) . "\n");
}

=head2 Help()

Prints Help

=cut

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

$PROG_NAME (version $PROG_VERSION)

 Usage: $PROG_NAME --device <device> --service <service> 
        --table <table> [ --loglevel <loglevel> ] [ --taxonomy <taxonomy> ]
        --begin YYYYMMDDHHMM --end YYYYMMDDHHMM"
        --fields <field1,field2,fieldn>
        [ --pid_param <string> ] [ --user <user> ] [ --output <outputfile> ]

EOF

    print $help_str;
    if (!@opt_devices)
    {
        print String_List('Device', 'any', \&Octopussy::Device::List);
    }
    elsif (!@opt_services)
    {
        print String_List('Service', 'any', \&Octopussy::Device::Services,
            @opt_devices);
    }
    elsif (!defined $opt_table)
    {
        print String_List('Table', undef, \&Octopussy::Table::List,
            \@opt_devices, \@opt_services);
    }
    elsif (!defined $opt_loglevel)
    {
        print String_List('Loglevel', undef,
            \&Octopussy::Loglevel::List_And_Any,
            \@opt_devices, \@opt_services);
    }
    elsif (!defined $opt_taxonomy)
    {
        print String_List('Taxonomy', undef,
            \&Octopussy::Taxonomy::List_And_Any,
            \@opt_devices, \@opt_services);
    }
    print "\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_$$", $status);

    return ($status);
}

=head2 Get_Messages_To_Parse($services, $loglevel, $taxonomy, $table, $fields)

Returns list of Messages to parse

=cut

sub Get_Messages_To_Parse
{
    my ($services, $loglevel, $taxonomy, $table, $fields) = @_;

    my @msg_to_parse =
        Octopussy::Message::Parse_List($services, $loglevel, $taxonomy, $table,
        $fields, undef, $fields);

    return (@msg_to_parse);
}

=head2 Get_TimePeriod_Files($devices, $services, $begin, $end)

Returns list of Files for Devices $devices, Services $services 
and Period $begin-$end

=cut

sub Get_TimePeriod_Files
{
    my ($devices, $services, $begin, $end) = @_;

    my ($y1, $m1, $d1, $hour1, $min1);
    my ($y2, $m2, $d2, $hour2, $min2);

    ($y1, $m1, $d1, $hour1, $min1) = ($1, $2, $3, $4, $5)
        if ($begin =~ /^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})$/);
    ($y2, $m2, $d2, $hour2, $min2) = ($1, $2, $3, $4, $5)
        if ($end =~ /^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})$/);
    my %start = (
        year  => $y1,
        month => $m1,
        day   => $d1,
        hour  => $hour1,
        min   => $min1,
    );
    my %finish = (
        year  => $y2,
        month => $m2,
        day   => $d2,
        hour  => $hour2,
        min   => $min2,
    );
    my ($files, $nb_files) =
        Octopussy::Logs::Minutes_Hash($devices, $services, \%start, \%finish);

    return ($files, $nb_files);
}

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

Prints Logs

=cut

sub Print_Logs
{
    my ($devices, $services, $table, $loglevel, $taxo, $begin, $end, $fields) =
        @_;
    my $time  = time;
    my @lines = ();

    Progress('_MSG_EXTRACT_PROGRESS_LISTING_FILES', '1/1', 0);
    my ($files, $total) =
        Get_TimePeriod_Files($devices, $services, $begin, $end);
    my $nb_match = 0;
    my $i        = 1;
    my @msg_to_parse =
        Get_Messages_To_Parse($services, $loglevel, $taxo, $table, $fields);
    my @logs = ();
    foreach my $min (sort keys %{$files})
    {

        foreach my $f (@{$files->{$min}})
        {
            Progress('_MSG_EXTRACT_PROGRESS_DATA', $i . "/$total", $nb_match);
			my $cat = ($f =~ /.+\.gz$/ ? 'zcat' : 'cat');
			if (defined open my $FILE, '-|', "$cat \"$f\"")
            {
                while (<$FILE>)
                {
                    my $line = $_;
                    foreach my $msg (@msg_to_parse)
                    {
                        if (my @args = $line =~ $msg->{re})
                        {
                            my %data = ();
                            my $i    = 0;
                            foreach my $field (@{$fields})
                            {
                                $data{$field} = $args[$i];
                                $i++;
                            }
                            push @logs, \%data;
                            $nb_match++;
                        }
                    }
                }
                close $FILE;
                $i++;
            }
            else
            {
                print "Unable to open file '$f'\n";
                AAT::Syslog::Message('octo_extractor_fields',
                    'UNABLE_OPEN_FILE', $f);
            }
        }
    }

=head2
  my $correlation_key    = 'id';
  my @correlation_values = ('idpes', 'idcnx', 'cpcnx');
  my %correl             = ();
  # Init correlation table
  foreach my $d (@logs)
  {
    foreach my $cv (@correlation_values)
    {
      if ( NOT_NULL($d->{$cv})
        && ($d->{$cv} ne "N/A")
        && ($d->{$cv} ne "0"))
      {
        $correl{$d->{$correlation_key}}{$cv} = $d->{$cv};
      }
    }
  }
  # Use correlation table to fill NULL values
  foreach my $d (@logs)
  {
    foreach my $cv (@correlation_values)
    {
      if (NULL($d->{$cv}) || ($d->{$cv} eq 'N/A') || ($d->{$cv} eq '0'))
      {
        $d->{$cv} = $correl{$d->{$correlation_key}}{$cv};
      }
    }
  }
=cut

    if (NOT_NULL($output))
    {
        if (defined open my $OUT, '>', $output)
        {
            my $json_text = to_json(\@logs);
            print {$OUT} $json_text;
            close $OUT if (NOT_NULL($output));
        }
    }
    else
    {
        foreach my $l (@logs)
        {
            foreach my $f (@{$fields})
            {
                print '| ', ((defined $l->{$f}) ? $l->{$f} : 'N/A') . ' ';
            }
            print "|\n";
        }
    }

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

    return (scalar @logs);
}

=head2 End()

Ends Extraction

=cut

sub End
{
    AAT::Syslog::Message($PROG_NAME, 'Logs Extraction Aborted !');
    $cache->remove("status_$$");
    exit;
}

#
# MAIN
#
$SIG{USR2} = \&End;

my $status = GetOptions(
    'h'           => \$help,
    'help'        => \$help,
    'device=s'    => \@opt_devices,
    'service=s'   => \@opt_services,
    'table=s'     => \$opt_table,
    'fields=s'    => \$opt_fields,
    'loglevel=s'  => \$opt_loglevel,
    'taxonomy=s'  => \$opt_taxonomy,
    'begin=s'     => \$opt_begin,
    'end=s'       => \$opt_end,
    'pid_param=s' => \$pid_param,
    'user=s'      => \$user,
    'json=s'      => \$output,
);

Help()
    if ((!$status)
    || ($help)
    || (!@opt_devices)
    || (!@opt_services)
    || (!defined $opt_table)
    || (!defined $opt_fields)
    || (!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);

my @fields = split /,/, $opt_fields;

Print_Logs(\@opt_devices, \@opt_services, $opt_table, $opt_loglevel,
    $opt_taxonomy, $opt_begin, $opt_end, \@fields);

$cache->remove("status_$$");

=head1 AUTHOR

Sebastien Thebert <octo.devel@gmail.com>

=head1 SEE ALSO

octo_dispatcher, octo_extractor, octo_parser, octo_uparser, octo_reporter, octo_scheduler

=cut