# -*- perl -*-
#
# $Revision: 1.10 $
package ipmi;

require Exporter;
@ISA = qw(Exporter);
@EXPORT_OK = qw(&print_usage &print_version &service_test);
use lib $main::runtimedir;
use csl_utils qw($TIMEOUT %ERRORS);
use strict;

sub print_usage ();
sub print_version ();
sub service_test (%);

sub print_usage () {

    print "
Usage:  Check ipmi service.
Options:
    -H <hostname> [--verbose]
    --help
    --version
    --usage
";
    return $ERRORS{'OK'};
}

sub print_version () {
    my $version = '$Revision: 1.10 $';
    $version =~ s/^\$Revision: //;
    $version =~ s/ \$\s*$//;

    print "ipmi.pm - $version\n";
    return $ERRORS{'OK'};
}

sub service_test (%) {
    my %options = @_;

    if ($options{"verbose"}) {
        foreach my $opt (sort keys(%options)) {
            if ($options{$opt}) {
                print "$opt - $options{$opt}\n";
            }
        }
    }

    my $host = $options{"opt_H"};

    if ((!$host) || ($host eq "")) {
        print "\nHost name not specified.\n";
        &print_usage();
        return $ERRORS{'OK'};
    }

    my %codes = (
        "ns" => "OK",
        "ok" => "OK",
        "cr" => "CRITICAL",
        "nc" => "CRITICAL",
    );

    my %ignoretype;

    if ( $options{opt_i} ) {
        foreach ( split(",", $options{opt_i}) ) {
            $ignoretype{$_} = 1;
        }
    }

    my $imp = "/s/ipmi-csl/bin/imp $host sdr 2>\&1";
    my $status = "OK";
    my $output;

    my @IPMI = `$imp` or die "Couldn't run imp: $!\n";
    print @IPMI if $options{"verbose"};
    LINE: foreach my $line (@IPMI) {
        chomp $line;
        if ($line =~ /No service processor found/) {
            $output = "Host is not configured! $line";
            $status = "CRITICAL";
        } else {
            $line =~ s/  +//g;
            $line =~ s/\| /\|/g;
            my ($type, $value, $code) = split('\|', $line);
            if (! $code) {
                $line =~ s/\r//g;
                $output .= "Invalid format: $line ";
                $output = "TRY TO REMOVE /etc/csl/sp/configured: $output" if ($line =~ /Activate Session command failed/);
                $output = "UNPLUG AND REBOOT TO CLEAR: $output" if ($line =~ /Error: Unable to establish IPMI .* session/);
                $status = "WARNING";
                last LINE;
            } elsif ($ignoretype{$type}) {
                print "Ignore $type: $value ($code)\n" if $options{"verbose"};
            } else {
                if ( ! $codes{$code} ) {
                    $output .= "<br>Unknown status type: $code! $type: $value ($code) ";
                    $status = "CRITICAL";
                } elsif ( $codes{$code} ne "OK" ) {
                    $output .= "<br>Error on $type: $value ($code) ";
                    $status = "CRITICAL";
                }
            }
        }
    }

    $output = "All elements are fine." unless $output;

    print "$status: $output\n";
    print "Exit status: $status\n" if $options{"verbose"};
    exit $ERRORS{$status};
#    exit $ERRORS{OK};
}

