????

Your IP : 216.73.216.174


Current Path : /lib/raider/Raider/
Upload File :
Current File : //lib/raider/Raider/Notification.pm

use strict;
use warnings;

package Raider::Notification;
use base qw( Raider::Base );

use Time::gmtime; # Included in perl
use Time::localtime; # Included in perl

=head1 NAME

Raider::Notification - Base class for Notification tasks

=head1 DESCRIPTION

Base class for notification tasks.

=head1 USAGE

use Raider::Notification;
my notObj = Raider::Notification->new();

=head1 METHODS

=head2 can_alert(\%args)

Returns true if alterting is allowed.

=head2 get_todays_alert_file(\%args)

Returns the location of todays alert file.

=head2 get_alert_day_year()

Returns the day year to be used in alert filename.

=head2 place_alert(\%args)

Place an alert file for today.

=head2 can_api()

Returns true if api notifications are enabled and
the api host is accessible.

=head2 can_raidalarm_api()

Returns true if the raidalarm endpoint is accessible.

=cut

sub can_alert {
  my $self = shift;
  my $opts = shift;

  my $can_notify = 0;
  my $proposed_alert_file_path = $self->get_todays_alert_file({ notify_type => "$opts->{notify_type}" });
  if ( ! -e $proposed_alert_file_path ) {
    $can_notify = 1;
  }
  return $can_notify;
}

sub get_todays_alert_file {
  my $self = shift;
  my $opts = shift;
  my $day_year = $self->get_alert_day_year();
  my $alert_file = "$Raider::Base::base_conf{'alert_path'}/$opts->{notify_type}-$day_year";
  chomp($alert_file);
  return $alert_file;
}

sub get_alert_day_year {
  my $self = shift;
  my $gm = gmtime();
  my $day_year = $gm->mday();
  my $year = localtime->year() + 1900;
  my $day_year_ret = "$day_year-$year";  
  return $day_year_ret;  
}

sub place_alert {
  my $self = shift;
  my $opts = shift;
  my $day_year = $self->get_alert_day_year();
  my $alert_file_path = "$Raider::Base::base_conf{'alert_path'}/$opts->{notify_type}-$day_year";
  chomp($alert_file_path);
  if ( ! -e $alert_file_path ) {
    open(FH,">$alert_file_path") or $self->logger({ cat => 'c', msg => "Unable to place alert file, got STDERR; $!" });
    close(FH);
  }
}

sub can_api {
  my ($self) = @_;
  my $conf_file = $self->read_conf_file();
  unless ($conf_file->{api_notifications}) {
    $self->logger({cat => 'i', msg => 'API notification suppressed by config file.'});
    return 0;
  }

  if ($self->can_raidalarm_api()) {
    $self->logger({cat => 'i', msg => 'System is compatibile with API notifications and API host is online, using API notifications.'});
    return 1;
  }
  return 0;
}

sub can_raidalarm_api {
  my ($self) = @_;
  eval {
    $self->{raidalarm}->call(method => '/');
  };
  if (my $e = $@) {
    $self->logger({cat => 'w', msg => "Unable to reach RaidAlarm endpoint:\n$e\n"});
    return 0;
  }
  return 1;
}

sub clear_alerts_for_device {
  my ($self, $device) = @_;
  opendir (DIR, $Raider::Base::base_conf{'alert_path'}) or $self->logger({cat => 'c',
      msg => "unable to open [$Raider::Base::base_conf{'alert_path'}]: $!"
    });
  while (my $alert_file = readdir(DIR)) {
    my $alert_path_file = $Raider::Base::base_conf{'alert_path'} . '/' . $alert_file;
    if (-f $alert_path_file) {
      if ($alert_file =~ /^$device-/) {
        unlink($alert_path_file) or $self->logger({cat => 'c', msg => "failed removing [$alert_path_file]"});
      }
    }
  }
  closedir(DIR);
}


1;