This file is indexed.

/usr/share/perl5/Octopussy/Alert.pm is in octopussy 1.0.6-0ubuntu2.

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

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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
# $HeadURL$
# $Revision: 579 $
# $Date: 2012-06-24 02:11:57 +0100 (Sun, 24 Jun 2012) $
# $Author: sebthebert $

=head1 NAME

Octopussy::Alert - Octopussy Alert module

=cut

package Octopussy::Alert;

use strict;
use warnings;
use Readonly;
use bytes;
use utf8;

use AAT::DB;
use AAT::Utils qw( ARRAY NOT_NULL );
use AAT::XML;
use Octopussy::Device;
use Octopussy::DeviceGroup;
use Octopussy::FS;
use Octopussy::Message;

Readonly my $DIR_ALERT   => 'alerts';
Readonly my $XML_ROOT    => 'octopussy_alert';
Readonly my @COMPARATORS => q(< > = <= >= LIKE);
Readonly my @LEVELS      => (
  {
    label => 'Warning',
    value => 'Warning',
    color => 'orange'
  },
  {
    label => 'Critical',
    value => 'Critical',
    color => 'red'
  }
);

my $dir_alerts = undef;
my %filename;

=head1 FUNCTIONS

=head2 New($conf)

Create a new Alert and then restart parser for Devices concerned

=cut

sub New
{
  my $conf = shift;
  
  return (undef)	if (! Valid_Name($conf->{name}));
  
  $dir_alerts ||= Octopussy::FS::Directory($DIR_ALERT);
  Octopussy::FS::Create_Directory($dir_alerts);
  my $file_xml = "$dir_alerts/$conf->{name}.xml";
  $conf->{msgbody}     =~ s/\r\n/ \@\@\@ /g;
  $conf->{action_body} =~ s/\r\n/ \@\@\@ /g;

  if (defined AAT::XML::Write($file_xml, $conf, $XML_ROOT))
  {
    my %devices = ();
    if (${$conf->{device}}[0] =~ /-ANY-/i)
    {
      foreach my $d (Octopussy::Device::List()) { $devices{$d} = 1; }
    }
    else
    {
      foreach my $d (ARRAY($conf->{device}))
      {
        if ($d =~ /group (.+)/)
        {
          foreach my $dev (Octopussy::DeviceGroup::Devices($1))
          {
            $devices{$dev} = 1;
          }
        }
        else { $devices{$d} = 1; }
      }
    }
    foreach my $d (sort keys %devices)
    {
      Octopussy::Device::Parse_Pause($d);
      Octopussy::Device::Parse_Start($d);
    }
    return ($file_xml);
  }

  return (undef);
}

=head2 Modify($old_alert, $conf_new)

Modify the configuration for the Alert '$old_alert'

=cut

sub Modify
{
  my ($old_alert, $conf_new) = @_;

  Remove($old_alert);
  New($conf_new);

  return (undef);
}

=head2 Remove($alert)

Removes the alert '$alert'

=cut

sub Remove
{
  my $alert = shift;

  my $nb = unlink Filename($alert);
  $filename{$alert} = undef;

  return ($nb);
}

=head2 List()

Get List of Alerts

=cut

sub List
{
  $dir_alerts ||= Octopussy::FS::Directory($DIR_ALERT);

  return (AAT::XML::Name_List($dir_alerts));
}

=head2 Comparators()

Get alerts comparators

=cut

sub Comparators
{
  return (@COMPARATORS);
}

=head2 Levels()

Get alerts levels

=cut

sub Levels
{
  return (@LEVELS);
}

=head2 Filename($alert_name)

Get the XML filename for the alert '$alert_name'

=cut

sub Filename
{
  my $alert_name = shift;

  return ($filename{$alert_name}) if (defined $filename{$alert_name});
  $dir_alerts ||= Octopussy::FS::Directory($DIR_ALERT);
  $filename{$alert_name} = "$dir_alerts/$alert_name.xml";

  return ($filename{$alert_name});
}

=head2 Configuration($alert_name)

Get the configuration for the alert '$alert_name'

=cut

sub Configuration
{
  my $alert_name = shift;

  my $conf = AAT::XML::Read(Filename($alert_name));
  $conf->{msgbody}     =~ s/ \@\@\@ /\n/g if (defined $conf->{msgbody});
  $conf->{action_body} =~ s/ \@\@\@ /\n/g if (defined $conf->{action_body});

  return ($conf);
}

=head2 Configurations($sort)

Get the configuration for all alerts

=cut

sub Configurations
{
  my $sort = shift || 'name';
  my (@configurations, @sorted_configurations) = ((), ());
  my @alerts = List();

  foreach my $a (@alerts)
  {
    my $conf = Configuration($a);
    push @configurations, $conf;
  }
  foreach my $c (sort { $a->{$sort} cmp $b->{$sort} } @configurations)
  {
    push @sorted_configurations, $c;
  }

  return (@sorted_configurations);
}

=head2 For_Device($device)

Get Alerts related to Device '$device'

=cut

sub For_Device
{
  my $device = shift;
  my @alerts = ();

  foreach my $ac (Octopussy::Alert::Configurations())
  {
    my $match   = 0;
    my %devices = ();
    foreach my $d (ARRAY($ac->{device}))
    {
      if ($d =~ /group (.+)/)
      {
        foreach my $dev (Octopussy::DeviceGroup::Devices($1))
        {
          $devices{$dev} = 1;
        }
      }
      else { $devices{$d} = 1; }
    }
    foreach my $d (sort keys %devices)
    {
      if (($d eq $device) || ($d eq '-ANY-'))
      {
        my @services = Octopussy::Device::Services($d);
        foreach my $s (@services)
        {
          foreach my $acs (ARRAY($ac->{service}))
          {
            $match = 1 if (($s eq $acs) || ($acs eq '-ANY-'));
          }
        }
      }
    }
    push @alerts, $ac
      if (($ac->{status} eq 'Enabled')
      && (($ac->{type} =~ 'Static') || ($match)));
  }

  return (@alerts);
}

=head2 Insert_In_DB($device, $alert, $line, $date)

=cut

sub Insert_In_DB
{
  my ($device, $alert, $line, $date) = @_;

	if ($date =~ /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/)
	{
  	my $datestr = "$1/$2/$3 $4:$5:$6";
  AAT::DB::Insert(
    'Octopussy',
    '_alerts_',
    {
      alert_id  => $alert->{name},
      date_time => $datestr,
      device    => $device,
      level     => $alert->{level},
      log       => $line
    }
  );
  	return (1);
	}

  return (0);
}

=head2 Check_All_Closed()

Checks if all Alerts are closed

=cut

sub Check_All_Closed
{
  my @result = AAT::DB::Query('Octopussy',
    q(SELECT * FROM _alerts_ WHERE status NOT LIKE 'Closed'));

  return (scalar(@result) == 0 ? 1 : 0);
}

=head2 Opened_List($device)

Returns List of Alerts with Status 'Opened'

=cut

sub Opened_List
{
  my $device = shift;

  my $query =
    'SELECT * FROM _alerts_ ' . "WHERE device='$device' AND status='Opened'";

  return (AAT::DB::Query('Octopussy', $query));
}

=head2 Update_Status($id, $status, $comment

Updates Alert with id '$id' to Status '$status' & with Comment '$comment'

=cut

sub Update_Status
{
  my ($id, $status, $comment) = @_;

  AAT::DB::Do('Octopussy',
        "UPDATE _alerts_ SET status='$status', "
      . "comment='$comment' WHERE log_id=$id");

  return (1);
}

=head2 Delete_From_Database($id)

Deletes Alert with id '$id' from Database

=cut

sub Delete_From_Database
{
	my $id = shift;

	AAT::DB::Do('Octopussy', 
		"DELETE FROM _alerts_ WHERE log_id='" . $id . "'");

  	return (1);
}

#
# Function: Add_Message($alert_name, $msg_id, $repeat, $interval)
#
# Add message '$msg_id' to alert '$alert_name'
#
# Parameters:
#
# - $alert_name: Alert name
# - $msg_id: Message Id to Add
#
sub Add_Message
{
  my ($alert_name, $msg_id, $repeat, $interval) = @_;

  my $conf = AAT::XML::Read(Filename($alert_name));
  push @{$conf->{message}},
    {msg_id => $msg_id, repeat => $repeat, interval => $interval};
  AAT::XML::Write(Filename($alert_name), $conf, $XML_ROOT);

  return (scalar @{$conf->{message}});
}

#
# Function: Remove_Message($alert_name, $msg_id)
#
# Removes message '$msg_id' from alert '$alert_name'
#
# Parameters:
#
# - $alert_name: Alert name
# - $msg_id: Message Id to Remove
#
sub Remove_Message
{
  my ($alert_name, $msg_id) = @_;

  my $conf = AAT::XML::Read(Filename($alert_name));
  my @messages =
    grep { $_->{msg_id} ne $msg_id } ARRAY($conf->{message});
  $conf->{message} = \@messages;
  AAT::XML::Write(Filename($alert_name), $conf, $XML_ROOT);

  return (scalar @messages);
}

#
# Function: Add_Message_Field($alert_name, $msg_id, $field, $comparator, $value)
#
# Add field '$field' to message '$msg_id' to alert '$alert_name'
#
# Parameters:
#
# - $alert_name: Alert name
# - $msg_id: Message Id
# - $field: Message Field to Add
# - $comparator: Message Field Comparator
# - $value: Message Field Value
#
sub Add_Message_Field
{
  my ($alert_name, $msg_id, $field, $comparator, $value) = @_;

  my $conf = AAT::XML::Read(Filename($alert_name));
  foreach my $m (ARRAY($conf->{message}))
  {
    if ($m->{msg_id} eq $msg_id)
    {
      push @{$m->{field}},
        {
        fname      => $field,
        comparator => $comparator,
        fvalue     => $value
        };
      last;
    }
  }
  AAT::XML::Write(Filename($alert_name), $conf, $XML_ROOT);

  return ($field);
}

#
# Function: Remove_Message_Field($alert_name, $msg_id, $field, $comparator, $value)
#
# Removes field '$field' to message '$msg_id' to alert '$alert_name'
#
# Parameters:
#
# - $alert_name: Alert name
# - $msg_id: Message Id
# - $field: Message Field to Remove
# - $comparator: Message Field Comparator
# - $value: Message Field Value
#
sub Remove_Message_Field
{
  my ($alert_name, $msg_id, $field, $comparator, $value) = @_;
  my @fields = ();

  my $conf = AAT::XML::Read(Filename($alert_name));
  foreach my $m (ARRAY($conf->{message}))
  {
    if ($m->{msg_id} eq $msg_id)
    {
      foreach my $f (ARRAY($m->{field}))
      {
        push @fields, $f
          if (($f->{fname} ne $field)
          || ($f->{comparator} ne $comparator)
          || ($f->{fvalue} ne $value));
      }
      $m->{field} = \@fields;
      last;
    }
  }
  AAT::XML::Write(Filename($alert_name), $conf, $XML_ROOT);

  return (scalar @fields);
}

#
# Function: Add_Action($alert_name, $type, $contact, $data)
#
# Add action to alert '$alert_name'
#
#	Parameters:
#
#	 - $alert_name: Alert name
#	 - $type: Action Type
#	 - $contact: Action Contact
#	 - $data: Action Data
#
sub Add_Action
{
  my ($alert_name, $type, $contact, $data) = @_;

  my $conf = AAT::XML::Read(Filename($alert_name));
  push @{$conf->{action}}, {type => $type, contact => $contact, data => $data};
  AAT::XML::Write(Filename($alert_name), $conf, $XML_ROOT);

  return (scalar @{$conf->{action}});
}

=head2 From_Device($device)

Returns Alerts generated by device '$device'

=cut

sub From_Device
{
  my ($device, $status) = @_;
  my @alerts = ();
  my $query  = "SELECT * FROM _alerts_ WHERE device='$device'";
  $query .= (defined $status ? " AND status='$status'" : '');
  @alerts = AAT::DB::Query('Octopussy', $query);

  return (@alerts);
}

sub Message_Replace
{
  my ($str, $alert, $device, $line, $field) = @_;

  $str =~ s/__device__/$device/gi;
  $str =~ s/__alert__/$alert->{name}/gi;
  $str =~ s/__level__/$alert->{level}/gi;
  $str =~ s/__log__/$line/gi;
  $str =~ s/__field_(\w+)__/$field->{$1}/gi;

  return ($str);
}

=head2 Message_Building($alert, $device, $line, $msg)

Builds Alert Message

=cut

sub Message_Building
{
  my ($alert, $device, $line, $msg) = @_;
  my %field = Octopussy::Message::Fields_Values($msg, $line);

  my $subject     = $alert->{msgsubject}     || '';
  my $body        = $alert->{msgbody}        || '';
  my $host        = $alert->{action_host}    || '';    # For Nagios/Zabbix
  my $service     = $alert->{action_service} || '';    # For Nagios/Zabbix
  my $action_body = $alert->{action_body}    || '';    # For Nagios/Zabbix

  $subject = Message_Replace($subject, $alert, $device, $line, \%field);
  $body    = Message_Replace($body,    $alert, $device, $line, \%field);
  $body =~ s/\s*\@\@\@\s*/\n/g;
  $action_body = Message_Replace($action_body, $alert, $device, $line, \%field);
  $action_body =~ s/\s*\@\@\@\s*/\n/g;
  $host    = Message_Replace($host,    $alert, $device, $line, \%field);
  $service = Message_Replace($service, $alert, $device, $line, \%field);

  return ($subject, $body, $host, $service, $action_body);
}

=head2 Tracker($al, $dev, $stat, $sort, $limit)

=cut

sub Tracker
{
  my ($al, $dev, $stat, $sort, $limit) = @_;
  $sort ||= 'date_time';
  my $query = 'SELECT * FROM _alerts_'
    . (
    (($al ne '') || ($dev ne '') || ($stat ne ''))
    ? ' WHERE '
      . (($al ne '') ? "alert_id='$al'" : '')
      . (($dev ne '') ? (($al ne '') ? ' AND ' : '') . "device='$dev'" : '')
      . (
        ($stat ne '')
      ? ((($al ne '') || ($dev ne '')) ? ' AND ' : '') . "status='$stat'"
      : ''
      )
    : ''
    )
    . " ORDER BY $sort "
    . ($sort ne 'date_time' ? 'ASC' : 'DESC')
    . (NOT_NULL($limit) ? " LIMIT $limit" : '');
  my @alerts = AAT::DB::Query('Octopussy', $query);

  return (@alerts);
}

=head2 Valid_Name($name)

Checks that '$name' is valid for an Alert name

=cut

sub Valid_Name
{
	my $name = shift;

	return (1)	if ((NOT_NULL($name)) && ($name =~ /^[a-z0-9][a-z0-9_-]*$/i));

	return (0);
}

=head2 Valid_Status_Name($name)

Checks that '$name' is valid for an Alert Status name

=cut

sub Valid_Status_Name
{
    my $name = shift;
    
	return (1)  
		if ((NOT_NULL($name)) && ($name =~ /^(-ANY-|Closed|Opened|Waiting for Info|Work in Progress)$/i));

    return (0);
}

1;

=head1 AUTHOR

Sebastien Thebert <octo.devel@gmail.com>

=cut