#! /usr/bin/perl
# Created by:	Chris
# Date:		20.04.2011
# Script:	check_waas.pl
# Version:	4.0

use strict;

use Getopt::Long;
use LWP::UserAgent;


our $opt_m;
my $opt_p;
my $opt_d;
my $opt_c;
my $opt_u;
my $opt_p;
my $opt_port;
my $verbose;
my $msg;
my $xml_body;
my $xml_header;
my $xml_footer;
my $xml_file;
my $funct;
my $uri;
my $RPCPath;
my $statuscode;
my $returns;
my $output;
my $tempoutput;

my %ERRORS=('OK'=>0,'WARNING'=>1,'CRITICAL'=>2,'UNKNOWN'=>3,'DEPENDENT'=>4);
my $PROGNAME = "check_waas.pl";

#Getopt::Long::Configure('bundling');
GetOptions
	("m=s"   => \$opt_m,
	"port=s"	=> \$opt_port,
	 "d=s"   => \$opt_d,
	 "c=s"   => \$opt_c,
	 "u=s"   => \$opt_u,
	 "p=s"   => \$opt_p,
	 "v" 	=> \$verbose);

if (!$opt_m) {
	print_usage();
	exit $ERRORS{'CRITICAL'};
}

if (!$opt_d) {
	print_usage();
	exit $ERRORS{'CRITICAL'};
}

if (!$opt_c) {
	print_usage();
	exit $ERRORS{'CRITICAL'};
}

if (!$opt_u) {
	print_usage();
	exit $ERRORS{'CRITICAL'};
}

if (!$opt_p) {
	print_usage();
	exit $ERRORS{'CRITICAL'};
}

if ($opt_port == "") {
	$opt_port        =  '8443';
}


my $xml_header = '<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
  SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
  xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
  xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
  xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:xsd="http://www.w3.org/1999/XMLSchema"
>
<SOAP-ENV:Header xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<wsse:Security SOAP-ENC:root="1">
<wsse:UsernameToken>
<wsse:Username xsi:type="xsd:string">'.$opt_u.'</wsse:Username>
<wsse:Password xsi:type="xsd:string">'.$opt_p.'</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
';
my $xml_footer='
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>';


if($opt_c eq 'alarms') {
	#Calls check for DeviceAlarms
	
	$returns = check_deviceAlarms ();
	$output = $tempoutput;
} elsif ($opt_c eq 'status') {
	#Calls check for DeviceStatus
	
	$returns = check_deviceStatus ();
	$output = $tempoutput;
} elsif ($opt_c eq 'both') {
	#Calls both checks
	
	my $return1 = check_deviceStatus ();
	my $output1 = $tempoutput;
	my $return2 = check_deviceAlarms ();
	my $output2 = $tempoutput;
	
	if($return1 ne "OK") {
		$returns = $return1;
		$output = $output1."<br>".$output2;
	} elsif($return2 ne "OK") {
		$returns = $return2;
		$output = $output2."<br>".$output1;
	} else {
		$output = $output1."<br>".$output2;
	}
} else {
	print print_usage();
	exit $ERRORS{'CRITICAL'};
}

print $output;
exit $ERRORS{$returns};





















#Checks the alarms in Central Manager
sub check_deviceAlarms {
	print_debug ("Check: DeviceAlarms");
	print_debug ("Manager: ".$opt_m);
	print_debug ("Device:" .$opt_d."\n");
	
	$xml_body = '';
	$xml_file = $xml_header.$xml_body.$xml_footer;	
	$RPCPath  =  '/ws/AlarmStatus';
	$uri = 'https://' . $opt_m . ':' . $opt_port . $RPCPath;
	
	my $ua = LWP::UserAgent->new;
	# we need to authenticate the https connection:
	$ua->credentials( "", "AlarmStatus", $opt_u, $opt_p );
	
	my $header = HTTP::Headers->new;
	$header->header('SOAPAction' => '"retrieveAllAlarmsResponse"');
	# this is to define what SOAP RPC call we are making
	my $request = HTTP::Request->new("POST",$uri,$header );
	$request->content($xml_file);
	
	my $response = $ua->request($request);
	
	print_debug ("######################################################################################################################");
	print_debug ("");
	print_debug ($xml_file);
	print_debug ("");
	print_debug ("######################################################################################################################");
	print_debug ("");
	print_debug ($response->as_string);
	print_debug ("");
	print_debug ("######################################################################################################################");
	
	if($response->as_string =~ /<ax238:deviceName>$opt_d<\/ax238:deviceName>/i) {
		$tempoutput = "Alarms on ".$opt_d;
		return "CRITICAL";
	} else {
		$tempoutput = "No Arlams on ".$opt_d;
		return "OK";
	}
}

#Checks the device status in Central Manager
sub check_deviceStatus {
	print_debug ("Check: DeviceStatus");
	print_debug ("Manager: ".$opt_m);
	print_debug ("Device:" .$opt_d."\n");
	
	$xml_body = '<ns1:getDeviceStatus xmlns:ns1="http://service.stats.ws.waas.cisco.com" SOAP-ENC:root="1">
<name>'.$opt_d.'</name>
</ns1:getDeviceStatus>';
	$xml_file = $xml_header.$xml_body.$xml_footer;	
	$RPCPath  =  '/ws/DeviceStatus';
	$uri = 'https://' . $opt_m . ':' . $opt_port . $RPCPath;
		
	my $ua = LWP::UserAgent->new;
	# we need to authenticate the https connection:
	$ua->credentials( "", "DeviceStatus", $opt_u, $opt_p );
	
	my $header = HTTP::Headers->new;
	$header->header('SOAPAction' => '"getDeviceStatus"');
	# this is to define what SOAP RPC call we are making
	my $request = HTTP::Request->new("POST",$uri,$header );
	$request->content($xml_file);
	
	my $response = $ua->request($request);
	
	print_debug ("######################################################################################################################");
	print_debug ("");
	print_debug ($xml_file);
	print_debug ("");
	print_debug ("######################################################################################################################");
	print_debug ("");
	print_debug ($response->as_string);
	print_debug ("");
	print_debug ("######################################################################################################################");
	
	if($response->as_string =~ /<ax235:status>Online<\/ax235:status>/i) {
		$tempoutput = "Status of ".$opt_d." is Online";
		return "OK";
	} elsif($response->as_string =~ /<ax235:status>Offline<\/ax235:status>/) {
		$tempoutput = "Status of ".$opt_d." is Offline";
		return "CRITICAL";
	} else {
		$tempoutput = "Status of ".$opt_d." is Unknown";
		return "UNKNOWN";
	}
	
}

#Retruns help informations
sub print_usage {
	print "Usage: $PROGNAME -m <central-manager> -d <waas-devicename> -c <alarms|status|both> -u <user> -p <password> [-port <port>] [-v]\n\n";
	print "-m\tThe IP-Adress of the WAAS Central Manager\n";	
	print "-d\tThe Name of the WAAS-Device\n";
	print "-c\tThe type of check: [status | alarms | both]\n";	
	print "-u\tThe user for WAAS Central Manager login\n";
	print "-p\tThe password for WAAS Central Manager login\n";
	print "[-port\tThe Port of the WAAS Central Manager. Default 8443]\n";
	print "[-v\tDebug output]\n\n";
	print "EXAMPLE: $PROGNAME -m 192.168.2.1 -d BEAWAE001 -c alarms -u admin -p default\n";
}

#Retruns debug infos
sub print_debug {
	if($verbose) {
		print "DEBUG: ".$_[0]."\n";
	}
}
