#!/usr/bin/perl 

#   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 St, Fifth Floor, Boston, MA  02110-1301  USA
#
# (c) 2009, spirit.nn@gmail.com
# Version 0.1, 2009-06-19

use strict;
use IO::Socket;
use Getopt::Long;
my ( %temp, %rot, %vcore, %volt );
my ( $help, $jfb, $w, $c, $type, $num, $host );

unless (
        GetOptions( "f1" => \$help,
                   "jfb" => \$jfb,
                   "w=f" => \$w,
                   "c=f" => \$c,
                   "Type=s" => \$type,
                   "Num=i" => \$num,
                   "Host=s" => \$host)
        and not defined $help ) {
    print "Usage $0:\n" .
    "--f1 - for this help\n" .
    "--jfb - just feth bitch\n" .
    "--w - warning value\n" .
    "--c - critical value\n" .
    "--Type - Temp, Rot, Vcore or Volt\n" .
    "--Num - element #num\n" .
    "--Host - host for check\n";
    exit(3);
}

if( ( not defined $type or $type !~ m/(?:Temp|Rot|Vcore|Volt)/ ) and not defined $jfb ) {
    print "Invalid type, try $0 --f1\n";
    exit(3);
}

if( not defined $host ) {
    print "Host not defined.\n";
    exit(3);
}
$num = defined $num ? $num : 0;

get_data();

# solve temperature
if( $type =~ m/Temp/ ) {
    unless( defined $temp{$num} ) {
        print "Can't fetch type $type num $num. Num is invalid.\n";
        exit(3);
    }
    print $temp{$num};
    exit(0) if( not defined $w and not defined $c );
    if( defined $w and defined $c ) {
        if( $w <= $c ) {
            exit(1) if( $temp{$num} >= $w  and $temp{$num} < $c);
            exit(2) if( $temp{$num} >= $c );
        } else {
            exit(1) if( $temp{$num} <= $w and $temp{$num} > $c );
            exit(2) if( $temp{$num} <= $c );
        }
    }
    exit(1) if( defined $w and not defined $c and $temp{$num} >= $w );
    exit(2) if( defined $c and not defined $w and $temp{$num} >= $c );
    exit(0);
}

# solve rotate
if( $type =~ m/Rot/ ) {
    unless( defined $rot{$num} ) {
        print "Can't fetch type $type num $num. Num is invalid.\n";
        exit(3);
    }
    print $rot{$num};
    exit(0) if( not defined $w and not defined $c );
    if( defined $w and defined $c ) {
        if( $w <= $c ) {
            exit(1) if( $rot{$num} >= $w  and $rot{$num} < $c);
            exit(2) if( $temp{$num} >= $c );
        } else {
            exit(1) if( $rot{$num} <= $w  and $rot{$num} > $c );
            exit(2) if( $rot{$num} <= $c );            
        }
    }
    exit(1) if( defined $w and not defined $c and $rot{$num} >= $w );
    exit(2) if( defined $c and not defined $w and $rot{$num} >= $c );
    exit(0);
}

# solve vcore
if( $type =~ m/Vcore/ ) {
    unless( defined $vcore{$num} ) {
        print "Can't fetch type $type num $num. Num is invalid.\n";
        exit(3);
    }
    print $vcore{$num};
    exit(0) if( not defined $w and not defined $c );
    if( defined $w and defined $c ) {
        if( $w <= $c ) {
            exit(1) if( $vcore{$num} >= $w  and $vcore{$num} < $c);
            exit(2) if( $vcore{$num} >= $c );
        } else {
            exit(1) if( $vcore{$num} <= $w and $vcore{$num} > $c );
            exit(2) if( $vcore{$num} <= $c );
        }
    }
    exit(1) if( defined $w and not defined $c and $vcore{$num} >= $w );
    exit(2) if( defined $c and not defined $w and $vcore{$num} >= $c );
    exit(0);
}

# solve volts
if( $type =~ m/Volt/ ) {
    unless( defined $volt{$num} ) {
        print "Can't fetch type $type num $num. Num is invalid.\n";
        exit(3);
    }
    print $volt{$num};
    exit(0) if( not defined $w and not defined $c );
    if( defined $w and defined $c ) {
        if( $w <= $c ) {
            exit(1) if( $volt{$num} >= $w  and $volt{$num} < $c);
            exit(2) if( $volt{$num} >= $c );
        } else {
            exit(1) if( $volt{$num} <= $w and $volt{$num} > $c );
            exit(2) if( $volt{$num} <= $c );
        }
    }
    exit(1) if( defined $w and not defined $c and $volt{$num} >= $w );
    exit(2) if( defined $c and not defined $w and $volt{$num} >= $c );
    exit(0);
}

die( "What the fuck?!" );

sub get_data() {
    my $sck = IO::Socket::INET->new( PeerAddr => "$host",
                                    PeerPort => 12999,
                                    Proto => 'tcp');
    unless( defined $sck and $sck->connected() ) {
        print "Can't connect to host $host\n";
        exit(3);
    }
    my @reply = <$sck>;
    close( $sck );
    if( defined $jfb ) {
        print @reply;
        exit(3);
    }
    my $reply = join( "" , @reply );
    $reply =~ s/\n/;/g;
    $reply =~ s/\s+//g;
    foreach my $i ( split(/;/, $reply ) ) {
        # Fetch temperature
        if ( $i =~ m/^Temp/ ) {
            $i =~ s/^.+=(.+)$/$1/;
            my @t = split(/,/, $i );
            for( my $q=0; $q <= $#t; $q++ ) {
                $temp{$q} = $t[$q];
            }
        }
        # Fetch rotate
        if ( $i =~ m/^Rot/ ) {
            $i =~ s/^.+=(.+)$/$1/;
            my @t = split(/,/, $i );
            for( my $q=0; $q <= $#t; $q++ ) {
                $rot{$q} = $t[$q];
            }
        }
        # Fetch vcore
        if ( $i =~ m/^Vcore/ ) {
            $i =~ s/^.+=(.+)$/$1/;
            my @t = split(/,/, $i );
            for( my $q=0; $q <= $#t; $q++ ) {
                $vcore{$q} = $t[$q];
            }
        }
        # Fetch voltage
        if ( $i =~ m/^Volt/ ) {
            $i =~ s/^.+=(.+)$/$1/;
            my @t = split(/,/, $i );
            for( my $q=0; $q <= $#t; $q++ ) {
                $volt{$q} = $t[$q];
            }
        }
    }
}
