????

Your IP : 216.73.216.174


Current Path : /lib/raider/Raider/Jobs/
Upload File :
Current File : //lib/raider/Raider/Jobs/3ware.pm

use strict;
use warnings;

package Raider::Jobs::3ware;
use base qw( Raider::Jobs );

use Raider::Notification::API;
use Raider::Notification::Email;

=head1 NAME

Raider::Jobs::3ware - 3ware specific tasks for checking raid health

=head1 DESCRIPTION

Checks raid health for 3ware cards.

=head1 USAGE

use Raider::Jobs::3ware;
my jobs3ware = Raider::Jobs::3ware->new();

=head1 METHODS

=head2 run_job

Takes all needed actions for checking raid health.

=head2 get_controller_list()

Returns a list of controllers.

=head2 get_array_list(\%args)

Returns a list of arrays.

=head2 array_is_ok(\%args)

Returns true if array is healthy.

=cut


my $icmd = '/usr/local/bin/tw_cli';

sub run_job {
  my $self = shift;
  $self->icmd_in_path({ icmd => "$icmd" });
  my $controller_list_ref = $self->get_controller_list();
  my @controller_list = @$controller_list_ref;
  my $output;
  my $raid_bad = 0;
  for my $controller ( @controller_list ) {
    my $array_list_ref = $self->get_array_list({ c => "$controller" });
    my @array_list = @$array_list_ref;
     for my $u ( @array_list ) {
      my $health_check = `$icmd info $controller $u`;
      unless ( $self->array_is_ok({ health_check => $health_check }) ) {
        $output .= "root ~ # $icmd info $controller $u\n";
        $output .= "$health_check\n";
        $output .= "root ~ #\n";
        $raid_bad = 1;
      }
    };
  };

  my $notifEmail = Raider::Notification::Email->new();
  my $notifAPI = Raider::Notification::API->new();

  if ( $raid_bad ) {
    if ( $notifAPI->can_alert({ notify_type => '3ware' }) ) {
      $self->logger({ cat => 'w', msg => '3Ware Hardware RAID Alarm Detected' });
      my $notify_msg = "3Ware Hardware RAID Alarm Detected on [$Raider::Base::base_conf{'hostname'}] at [$self->{uniq_id}]\nDetails:\n $output";
      if ( $notifAPI->can_api() ) {
        $notifAPI->send_notification({ 
          message => $notify_msg
        });
      }
      else {
        $notifEmail->send_notification({ 
            subject => "RAID Controller Alarm on $Raider::Base::base_conf{'hostname'}", 
            message => $notify_msg
          });
      }
      $notifAPI->place_alert({notify_type => '3ware'});
    }
    else {
      $self->logger({ cat => 'w', msg => 'Notification for 3ware suppressed, alert file for today already exists.' });
    }
  }
  else {
    # health is ok, clear alerts for device
    $notifAPI->clear_alerts_for_device('3ware');
  }

}  

sub get_controller_list {
  my $self = shift;

  my @controller_list;
  for my $line ( split /^/, `$icmd info` ) {
    if ( $line =~ /(^c\d+)/i ) {
      push(@controller_list, $1);
    }
  }

  return \@controller_list;
}

sub get_array_list {
  my $self = shift;
  my $opts = shift;
  
  my @array_list;
  for my $line ( split /^/, `$icmd info $opts->{c}` ) {
    if ( $line =~ /(^u\d+)/i ) {
      push(@array_list, $1);
    }
  }
  
  return \@array_list;
}

sub array_is_ok {
  my $self = shift;
  my $opts = shift;
  
  $self->logger({ cat => 'c', msg => "array_is_ok() missing an argument!" }) unless ( $opts->{health_check} );
  
  # Check raid health. Ignore rebuilding array if set in config.  
  my $conf_file = $self->read_conf_file();
  if ( $conf_file->{'ignore_rebuilding_array'} ) {
    if ( $opts->{health_check} =~ /u\d+\s+RAID-\d+\s+OK/i || $opts->{health_check} =~ /u\d+\s+RAID-\d+\s+REBUILDING/i ) {
      return 1;
    }
  }
  elsif ( $opts->{health_check} =~ /u\d+\s+RAID-\d+\s+OK/i ) {
    return 1;    
  }

  return 0;
}


1;