#!/usr/bin/perl -w

# ------------------------------------------------------------------------------
# File Name:    check_peplink.pl
# Author:    	Marco Gottardello
# Date:      	31/10/2012
# Version:    	1.1
# Description:  This script will do checks on Peplink Balance devices 
#			    running firmware 5.4 and with SSH access available
# Email:    	marco.gottardello(at)reti.it
# ------------------------------------------------------------------------------
# Copyright 2012 (c) Marco Gottardello
# It's been a long time since my last perl code, if you make an improvement 
# on this script, please email me a copy :)
# Some code from Perl examples found online.
# ------------------------------------------------------------------------------
# License GPL
# ------------------------------------------------------------------------------

my $username='admin';
my $password='password';

#-------------------------------------------------------------------------------



sub usage
{
        print "\n";
        print "Peplink Check for Nagios\n";
        print "Copyright (c) 2012 Marco Gottardello\n";
        print "\n";
        print "Usage: $0 <host> <check_type>\n";
        print "\n";
        print "Please edit the script code to define username and password";
        print "\n";
        print "<check type> parameter could be on of the follow:\n";
		print "\n";
		print "WANn - WAN status, where n is the WAN number (eg. WAN1, WAN2 and so on)\n";
		print "HA   - HA status of a VRRP cluster\n";
		print "CPU  - Show the CPU Load on the Balance device\n";
		print "VPN  - VPN status, return OK if at least 1 is UP\n";
        exit -1;

}



use strict;
use Net::SSH::Expect;



my $hostname = shift || &usage;
my $check = shift || &usage;

$check = uc($check);

my $ssh = Net::SSH::Expect->new (
            host => $hostname, 
            password=> $password, 
            user => $username, 
            raw_pty => 1
	);
$ssh->login();


if($check eq "HA"){

	 my $result=$ssh->exec("get ha");

	 if(($result=~ "Enabled") && ($result=~ "Master")){
	  print "OK - HA Enabled and Device is Master\n";
	  exit 0;
	} elsif(($result=~ "Enabled")&&($result=~"Slave")){
	  print "WARNING - HA Enabled and Device is Slave\n";
	  exit 1;
	} else {
	  print "CRITICAL - HA not working\n";
	  exit 2;
	}



} elsif ($check=~ /WAN/) {

 	my $wan_number= substr $check,-1,1; 
	my $result=$ssh->exec("get wan $wan_number");

	 if($result=~ "Connected") {
	  print "OK - WAN interface $wan_number is Connected\n";
	  exit 0;
	} elsif($result=~ "Standby"){
	  print "OK - WAN$wan_number is configured as Standby\n";
	  exit 1;
	} elsif($result=~ "Failed"){
	  print "CRITICAL - WAN$wan_number is Failed!\n";
	  exit 2;
	} elsif($result=~ "Disabled"){
	  print "UNKNOWN - WAN$wan_number is DISABLED!\n";
	  exit 3;
	} else {
	  print "Check Failed - please verify your input\n";
	#  print $result;
	  exit -1;
	}

} elsif ($check=~ /VPN/) {

        my $wan_number= substr $check,-1,1;
        my $result=$ssh->exec("get s2svpn");

         if($result=~ "Established") {
          print "OK - VPN is Established\n";
          exit 0;
     #   } elsif($result=~ "Standby"){
     #     print "OK - WAN$wan_number is configured as Standby\n";
     #     exit 2;
     #   } elsif($result=~ "Failed"){
     #     print "CRITICAL - WAN$wan_number is Failed!\n";
     #     exit 2;
     #   } elsif($result=~ "Disabled"){
     #     print "UNKNOWN - WAN$wan_number is DISABLED!\n";
     #     exit 3;
        } else {
          print "CRITICAL - no VPN Established\n";
        #  print $result;
                    exit 2;
          }
        


} elsif ($check=~ "CPU") {
	 $ssh->send ("get cpuload");
	my $chunk;
        while ($chunk = $ssh->peek(5)) { # grabs chunks of output each 1 second
            my $result=$ssh->eat($chunk);
	if ($result=~ "%") {
		my @lines = split /\n/, $result;
		my $where=9;
		 my $load = substr($lines[1], $where, 8);
		chomp($load);
		print "CPU Load: $load\n|CPU=$load\n";
		exit 0;
		}
        }

} else {
	  print "$check: unknown function\n";
	  exit -1;
}


$ssh->close();


