#!/usr/bin/perl
#
# check_thecus_fans.pl
# - A nagios plugin to monitor a THECUS NAS applicance fan RPM
#
# 
# Usage:
#  check_thecus_temp.pl [--user <username>] [--password <password>] <host>
#  example: check_thecus_temp.pl --password 123password 192.168.0.100 
# 
# where
#  <host>			is the network address of the appliance
#  --user <username>		specifies the username for authentication (default: admin)
#  --password <pass>		specifies the password for authentication (default: admin)
# 
# Returns : (values are adjustable)
#  WARNING if a fan RPM is below 2000 or above 4000
#  CRITICAL if a fan RPM is < 1
#  Ignores WARNING if CRITICAL is flagged
#  OK otherwise and prints values for all fans
# 
# EXAMPLE OUTPUT
# OK: Thecus NAS FANS running normally 
# CPU FAN = 3543 RPM 
# System FAN 1 = 2721 RPM
# System FAN 2 = 2806 RPM
# System FAN 3 = 3688 RPM
# System FAN 4 = 2657 RPM
#
# WARNING: As the applicance provides no real monitoring interface this plugin has to rely on
#  parsing the data out of the web-interface. That might cause errors when using different
#  firmware versions or even languages.
# 
# ======================
# Original Author credits
# =======================
# Author: Moritz Bechler <mbechler@eenterphace.org> for Schmieder IT Solutions (http://www.schmieder.de)
# License: MIT License
#
# Copyright (c) 2010 Moritz Bechler
# 
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#  
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# =====================
# Adjusted for only temperature monitoring and working for
# Manufacturer:	Thecus
# Product No.:	N16000PRO
# Firmware Version:	2.04.06a 
#
use strict;
use FindBin;
use lib "$FindBin::Bin";
use Getopt::Long;
use LWP;
use LWP::UserAgent;
use Data::Dumper;
use HTTP::Cookies;


my $ua = new LWP::UserAgent;
$ua->agent('check_thecus Nagios plugin');
#$ua->cookie_jar(new HTTP::Cookies(file => '/tmp/cookies', ignore_discard => 1));
$ua->cookie_jar( { } );


my $user = 'admin';
my $pass = 'admin';
my $running = '';
my $stopped = '';

GetOptions("user=s" => \$user, "password=s" => \$pass, "running=s" => \$running, "stopped=s" => \$stopped);

my $host = shift;

if(!$host) {
	die "Usage: $0 [--user <username>] [--password <password>] [--running <Service1,Service2,...>] [--stopped <Service1,Service2,...>] <host>\n";
}

# Logging into the appliance, does not return if it fails
# my $res = $ua->post("http://$host/usr/login.html", [ 'username' => $user, 'pwd' => $pass ]);
my $res = $ua->post("http://$host/adm/login.php", [ 'username' => $user, 'pwd' => $pass ]);

if($res->is_error()) {
	print "CRITICAL: Thecus login failed (" . $res->status_line . ")\n";
	exit 2;
}

my $aboutRes = $ua->get("http://$host/adm/getform.html?name=about");

if(!$aboutRes->is_success()) {
	print "CRITICAL: Could not fetch status information (" . $aboutRes->status_line . ")\n";
	exit 2;
}

if(!$aboutRes =~ m/^\<html\>/s) {
	print "UNKNOWN: Could not log in to Thecus NAS";
	exit 3;
}

# Getting the system results from the appliance in raw text
my $sysStatRes = $ua->get("http://$host/adm/getmain.php?fun=monitor&action=update");



if(!$sysStatRes->is_success()) {
	print "CRITICAL: Could not fetch status information (" . $sysStatRes->status_line . ")\n";
	exit 2;
}
my $sysStatStr = $sysStatRes->content();

# ==== test section - print results in terminal ====
#  Uncomment this to see the entire Output on the Screen
# print "OUTPUT: $sysStatStr\n";
#
my @tmp = ();
my @lines = split "]", $sysStatStr;
my $CPU_FAN = 0;
my $HDD_FAN1 = 0;
my $HDD_FAN2 = 0;
my $HDD_FAN3 = 0;
my $HDD_FAN4 = 0;
for my $line (@lines) {
	my @linetmp = split ",", $line;
	if(@linetmp == 2) {
		push @tmp, { key => $linetmp[0], value => $linetmp[1] };
# CPU_FAN : current CPU Fan speed
	};
	if($linetmp[2] eq "\"CPU_FAN\""){
	my @cput = split " ",$linetmp[3];
	$CPU_FAN = int(substr($cput[0],1)); 
# HDD_FAN1 : current System Fan 1 speed
	}
	if($linetmp[2] eq "\"HDD_FAN1\""){
        my @cput = split " ",$linetmp[3];
        $HDD_FAN1 = int(substr($cput[0],1));
# HDD_FAN2 : current System Fan 2 speed
        }   
        if($linetmp[2] eq "\"HDD_FAN2\""){
        my @cput = split " ",$linetmp[3];
        $HDD_FAN2 = int(substr($cput[0],1));
# HDD_FAN3 : current System Fan 4 speed
        }   
        if($linetmp[2] eq "\"HDD_FAN3\""){
        my @cput = split " ",$linetmp[3];
        $HDD_FAN3 = int(substr($cput[0],1));
	}
# HDD_FAN4 : current System Fan 4 speed
        if($linetmp[2] eq "\"HDD_FAN4\""){
        my @cput = split " ",$linetmp[3];
        $HDD_FAN4 = int(substr($cput[0],1));
	}
}

# Define the values for temperatures here


#
# set error value to zero, so if it turns one, it will print the error
#
{
	my $error= 0;

#
# CPU Fan values here
	if($CPU_FAN < 1)
                                {
                print "CRITICAL: CPU FAN is OFF, speed at $CPU_FAN RPM\n";
                $error = 1;
        } else {
	
	if($CPU_FAN > 4000 || $CPU_FAN < 2000)
                                {
                print "WARNING: CPU FAN is running at $CPU_FAN, should be between 2000 and 4000 RPM\n";
                $error = 1;
                        }
	}
#
#  SYSTEM FAN 1 values here
        if($HDD_FAN1 < 1)

                                {
                print "CRITICAL: SYSTEM FAN 1 is OFF, speed at $HDD_FAN1 RPM\n";
		$error = 1;
        } else {

	if($HDD_FAN1 > 4000 || $HDD_FAN1 < 2000)

                                {
                print "WARNING: SYSTEM FAN 1 is running at $HDD_FAN1, should be between 2000 and 4000 RPM\n";
                $error = 1;
                        }
			
#
#  SYSTEM FAN 2 values here
        if($HDD_FAN2 < 1)

                                {
                print "CRITICAL: SYSTEM FAN 2 is OFF, speed at $HDD_FAN2 RPM\n";
		$error = 1;
        } else {

	if($HDD_FAN2 > 4000 || $HDD_FAN2 < 2000)

                                {
                print "WARNING: SYSTEM FAN 2 is running at $HDD_FAN2, should be between 2000 and 4000 RPM\n";
                $error = 1;
                        }
		
#
#  SYSTEM FAN 3 values here
        if($HDD_FAN3 < 1)

                                {
                print "CRITICAL: SYSTEM FAN 3 is OFF, speed at $HDD_FAN3 RPM\n";
		$error = 1;
        } else {

	if($HDD_FAN3 > 4000 || $HDD_FAN3 < 2000)

                                {
                print "WARNING: SYSTEM FAN 3 is running at $HDD_FAN3, should be between 2000 and 4000 RPM\n";
                $error = 1;
                        }
		
#
#  SYSTEM FAN 4 values here
        if($HDD_FAN4 < 1)

                                {
                print "CRITICAL: SYSTEM FAN 4 is OFF, speed at $HDD_FAN4 RPM\n";
		$error = 1;
        } else {

	if($HDD_FAN3 > 4000 || $HDD_FAN3 < 2000)

                                {
                print "WARNING: SYSTEM FAN 4 is running at $HDD_FAN4, should be between 2000 and 4000 RPM\n";
                $error = 1;
                        }
		

# final error statement exit out and skip the status OK
	}

	if ($error eq 1) {
		exit 1;
	
	}	
#
# If all of the above errors result to 0, print the OK status
#
	print "OK: Thecus NAS FANS running normally \nCPU FAN = $CPU_FAN RPM \nSystem FAN 1 = $HDD_FAN1 RPM\nSystem FAN 2 = $HDD_FAN2 RPM\nSystem FAN 3 = $HDD_FAN3 RPM\nSystem FAN 4 = $HDD_FAN4 RPM\n";
	exit 0;
}}}}
