#! /usr/bin/perl

BEGIN {
	our $mandir = "/usr/share/man";
	our $PACKAGE = "noc-cacti-plugins";
	our $pdfdir = "/usr/share/doc/noc-cacti-plugins";
	our $DEFS = "-DPACKAGE_NAME=\"noc-cacti-plugins\" -DPACKAGE_TARNAME=\"noc-cacti-plugins\" -DPACKAGE_VERSION=\"0.2\" -DPACKAGE_STRING=\"noc-cacti-plugins 0.2\" -DPACKAGE_BUGREPORT=\"\" -DPACKAGE_URL=\"\" -DPACKAGE=\"noc-cacti-plugins\" -DVERSION=\"0.2\"";
	our $ECHO_N = "-n";
	our $sharedstatedir = "/usr/com";
	our $PERL = "/usr/bin/perl";
	our $infodir = "/usr/share/info";
	our $AWK = "gawk";
	our $sysconfdir = "/etc";
	our $bindir = "/usr/bin";
	our $htmldir = "/usr/share/doc/noc-cacti-plugins";
	our $mkdir_p = "/bin/mkdir -p";
	our $PACKAGE_NAME = "noc-cacti-plugins";
	our $libexecdir = "/usr/lib/noc-cacti-plugins";
	our $localedir = "/usr/share/locale";
	our $includedir = "/usr/include";
	our $PACKAGE_STRING = "noc-cacti-plugins 0.2";
	our $build_alias = "x86_64-linux-gnu";
	our $PACKAGE_VERSION = "0.2";
	our $dvidir = "/usr/share/doc/noc-cacti-plugins";
	our $libdir = "/usr/lib";
	our $sbindir = "/usr/sbin";
	our $am__untar = "/bin/bash /home/joostvb/build/noc-cacti-plugins/missing --run tar xf -";
	our $AUTOHEADER = "/bin/bash /home/joostvb/build/noc-cacti-plugins/missing --run autoheader";
	our $localstatedir = "/var";
	our $AMTAR = "/bin/bash /home/joostvb/build/noc-cacti-plugins/missing --run tar";
	our $docdir = "/usr/share/doc/noc-cacti-plugins";
	our $MKDIR_P = "/bin/mkdir -p";
	our $MAKEINFO = "/bin/bash /home/joostvb/build/noc-cacti-plugins/missing --run makeinfo";
	our $INSTALL_SCRIPT = "";
	our $psdir = "/usr/share/doc/noc-cacti-plugins";
	our $PACKAGE_TARNAME = "noc-cacti-plugins";
	our $INSTALL_DATA = " -m 644";
	our $datarootdir = "/usr/share";
	our $CYGPATH_W = "echo";
	our $AUTOCONF = "/bin/bash /home/joostvb/build/noc-cacti-plugins/missing --run autoconf";
	our $oldincludedir = "/usr/include";
	our $ACLOCAL = "/bin/bash /home/joostvb/build/noc-cacti-plugins/missing --run aclocal-1.11";
	our $INSTALL_PROGRAM = "";
	our $install_sh = "/bin/bash /home/joostvb/build/noc-cacti-plugins/install-sh";
	our $PATH_SEPARATOR = ":";
	our $exec_prefix = "/usr";
	our $datadir = "/usr/share";
	our $AUTOMAKE = "/bin/bash /home/joostvb/build/noc-cacti-plugins/missing --run automake-1.11";
	our $SHELL = "/bin/bash";
	our $am__leading_dot = ".";
	our $VERSION = "0.2";
	our $program_transform_name = "s,x,x,";
	our $prefix = "/usr";
}

#!/usr/bin/perl

use strict;
use warnings 'all';
use Net::SSH2; 

#if ($#ARGV != 0)
#{
#        print "Usage $0 <ip>\n";
#        print "\t<ip> = ip of switch\n";
#        exit 0;
#}

#my $ip = $ARGV[0];
my $ip = "172.31.12.4";

my $ssh_output = getInfoBySSHCliCommand($ip, "cw2k", "DwvhnwUvTbca6Mfl", "enable\nDwvhnwUvTbca6Mfl\nchangeto context system\nshow cpu usage context all\nquit\n");

my %fwsm_context_cpu_usage = ();

my @lines = split /\n/, $ssh_output;
foreach my $line (@lines)
{
	if ($line =~ m/\s+(\d+\.\d)%\s+(\d+\.\d)%\s+(\d+\.\d)%.\s+(.*)\s+$/)
	{
		#print "match : $line\n";
		my $cpu5min = trim($3);
		my $context = $4;
		#print "cpu usage $context : $cpu5min\n";
		#print ">$1< >$2< >$3< >$4<\n";
		$fwsm_context_cpu_usage{$context} = trim($cpu5min);
	}
}

my $output = "";
for my $context_name ( keys %fwsm_context_cpu_usage )
{
        my $cpu_usage = $fwsm_context_cpu_usage{$context_name};
        $output .= "$context_name:$cpu_usage ";
}

print trim($output);

sub getInfoBySSHCliCommand
{
	my ($ip, $username, $password, $command) = @_;

        local $SIG{ALRM} = sub { die "" };
        alarm 3;

	my $ssh2 = Net::SSH2->new();
	$ssh2->connect($ip) or die "Can't connect to $ip : $!\n";
	$ssh2->auth_password($username, $password) or die "Authentication failed: $!\n";

	my $output = ''; my $buffer;
	my $chan = $ssh2->channel();
	$chan->exec("$command") or die "Couldn't exec '$command': $!\n";
	while ( ! $chan->eof() )
	{
		$chan->read($buffer, 1);
		$output .= $buffer;
	}
	$chan->close();
	$ssh2->disconnect();

	$output =~ s/^.*?Password://s;
	$output =~ s/quit.*?$//s; 

        alarm 0; #cancel the alarm if does not hang

	return $output; 
}

# Perl trim function to remove whitespace and carriage return from the start and end of the string
sub trim
{
  my $string = shift;
  $string =~ s/^\s+//;
  $string =~ s/\s+$//;
  return $string;
}

