#!/usr/bin/perl -w
#
# Nagios plugin to check hardware of Sun's M4000/M5000/M8000/M9000 series via
# eXtended System Control Facility (XSCF).
#
# Copy to Nagios libexec directory (requires utils.pm from Nagios plugins).
# Requires a config file.
#
# $Id: check_xscf_hw.pl,v 1.0 2009/06/18 kaunt Exp $
#
# Copyright (C) 2006-2009  Thomas Kaun / Telefonica o2 Germany GmbH & Co OHG
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#

##################################################################################
### loading modules
##################################################################################

use strict;
use lib "/usr/local/nagios/libexec";
use utils qw(%ERRORS);

##################################################################################
### declarations
##################################################################################

my $a = 0; my $critical = 0; my $warning = 0; my $temp = 0;
my $obj = ""; my $rline = ""; my $file = ""; my $unit = "";
my $arg = ""; my $xscf = ""; my $keyfile = ""; my $user = "";
my $cmd = ""; my $opt = ""; my $stat = ""; my $perf = "";
my @result; my @line; my @status; my %para; my @meas;

##################################################################################
### parsing arguments
##################################################################################

foreach $arg (@ARGV) {
	if ($arg eq "-n") {
		$xscf = $ARGV[$a+1];
	}
	if ($arg eq "-i") {
		$keyfile = $ARGV[$a+1];
	}
	if ($arg eq "-u") {
		$user = $ARGV[$a+1];
	}
	if ($arg eq "-c") {
		$cmd = $ARGV[$a+1];
		if ($cmd eq "showenvironment") {
			$file = $ARGV[$a+2];
			&read_config ("$file");
    	}
	}
	if ($arg eq "-h") {
		printhelp();
	}
	$a++;
}

##################################################################################
### main task - analyze data
##################################################################################

if ($cmd eq "showhardconf") {
	query();
	$obj = "Hardware";
	foreach $_ (@result) {
		if (($_ =~ "Status") and !(($_ =~ "Normal") or ($_ =~ "Running") or ($_ =~ "On"))) {
			@line = split (/;/, $_);
			@status = split (/ +/, $line[0]);
			$stat = ($stat.$status[1]." ".$status[2].", ");
			$critical++;
		}
	}
}
elsif ($cmd eq "showenvironment") {
	$obj = "Environment";
	$perf = ($para{temp_c}."C, ".$para{temp_w}."C, ".$para{factor_high}."*U, ".$para{factor_low}."*U, ");
	foreach $_ ('temp','volt','Fan') {
		$opt = $_;
		query();
		if ($opt =~ "temp") {
			foreach $rline (@result) {
				if ($rline =~ ":") {
					chomp $rline; $rline =~ s/ //g;
					@line = split (/:/, $rline);
					$temp = substr($line[1],0,5);
					if ($temp >= $para{temp_c}) {
						$critical++;
						$stat = ($stat.$rline.", ")
					}
					elsif ($temp >= $para{temp_w}) {
						$warning++;
						$stat = ($stat.$rline.", ")
					}
				}
			}
		}
		elsif ($opt =~ "volt") {
			foreach $rline (@result) {
			chomp $rline;
			$rline =~ s/ //g;
				if (!($rline =~ ":")) {
					$unit = $rline;
				}
				else {
					@line = split (/V/,$rline);
					$line[0] =~ s/-//g;
					@meas = split (/:/,$line[1]);
					$meas[1] =~ s/-//g;
					if (($meas[1] > ($line[0]*$para{factor_high})) or ($meas[1] < ($line[0]*$para{factor_low}))) {
						$warning++;
						$stat = ($stat.$unit.":".$rline.", ")
					}
				}
			}
		}
		else {
			foreach $rline (@result) {
				chomp $rline;
				if ($rline =~ /speed/) {
					if (!($rline =~ /Low/)) {
						$warning++;
						$stat = ($stat.$rline.", ");
					}
				}
			}
		}
	}
}
else {
	printhelp();
}

##################################################################################
### generating return code and message
##################################################################################

if ($critical > 0) {
	print "$critical CRITICAL's, $warning WARNINGS ! $stat|$perf\n";
	exit $ERRORS{'CRITICAL'};
}
elsif ($warning > 0) {
	print "$warning WARNINGS's ! $stat|$perf\n";
	exit $ERRORS{'WARNING'};
}
print "$obj OK ! $stat|$perf\n";
exit $ERRORS{'OK'};

##################################################################################
### sub's
##################################################################################

sub printhelp {
	print "\nusage: ./check_xscf.pl -n [hostname|ip] -u [username] -i [keyfile] -c [command] [cfg_file]\n";
	print "Options:\n";
	print "	-n	:hostname or IP of the XSCF\n";
	print "	-u	:username to login the XSCF\n";
	print "	-i	:path and file of your privat key\n";
	print "	-c	:command [showhardconf|showenvironment]\n";
	print "	cfg_file: e.g.: '/usr/local/nagios/libexec/check_xscf.cfg (only for environment)'\n";
	print "\n";
	exit $ERRORS{'UNKNOWN'};
}

sub query {
	open (RESULT, "ssh -l $user -i $keyfile $xscf $cmd $opt |") || die "can't connect to $xscf";
	@result = <RESULT>;
	close RESULT;
}

sub read_config {
  my (@line);
  if (-e $_[0]) {
    open(CONFIG,$_[0]);
	foreach (<CONFIG>) {
		chomp;
		if (!($_ =~ /^#/)) {
			(@line) = split(/=/, $_, 2);
			$para{$line[0]} = $line[1]+0;
		}
	}
    close(CONFIG);
  }
}
