# ----------------------------------------------------------------------------
# File: zyxel_stats.pl
# Author: BSOD2600
# Version: 1.0
#
# Purpose: Script used to fetch Firewall, NAT table statistics and CPU
# usage from a ZyXEL router via Telnet and feed it to Cacti.
#
# Usage: zyxel_stats.pl IP port password
#
# Info: This has been tested on WinXP/Win2003 with Perl 5.8.8.
#
# History:
# 06-10-2006 1.0 Initial release
#
#my $cli_prompt = "/P443W>/i";
#-------- Don't edit below --------------------------------------------------
use strict;
if ($#ARGV != 2) {
print("\nUsage: zyxel_nat_stats.pl IP port password\n\n");
exit(-1);
}
my ($i, $ip, $pw, $port, $t, @nat, @cpu, @firewall,
$cpu_avg, $tablesize, $allocated, $freed, $full, $expired, $cachehit,
$icmpidle, $udpidle, $tcpidle, $tcpsynidle, $tcpfinidle, $land, $ipspoof,
$icmpecho, $icmp, $netbios, $trace, $teardrop, $synflood, $smtp);
$ip = $ARGV[0];
$port = $ARGV[1];
$pw = $ARGV[2];
use Net::Telnet;
$t = new Net::Telnet(
Dump_log => "dump.txt",
Output_log => "output.txt",
Input_log => "input.txt");
$t->errmode(sub
{
if ($t->timed_out) {
warn "$ip busy: ", $t->errmsg, "\n";
}
elsif ($t->eof) {
warn "$ip offline: ", $t->errmsg, "\n";
}
else {
warn $t->errmsg, "\n";
$t->print('\r\n');
$t->errmode('return');
}
}
);
#----- Connecting to router ---------------------------------------------------#
$t->open(Host => $ip,
Port => $port);
$t->waitfor('/password: $/i');
$t->print($pw);
$t->waitfor('/Number:/i');
$t->print('24');
$t->waitfor('/Number:/i');
$t->print('8');
$t->waitfor('/P334W>/i');
$t->prompt('/P334W>/i');
$t->buffer_empty;
#----- Collect CPU Stats ------------------------------------------------------#
@cpu = $t->cmd(String => "sys cpu disp");
#print @cpu;
for ($i = 0; $i < @cpu; $i++) {
if ($cpu[$i] =~ /\s+\d+\s\d+\s+(\d+.\d+)\s+\d+\s+\d+\s+(\d+.\d+)\s+\d+\s+\d+\s+(\d+.\d+)\s+\d+\s+\d+\s+(\d+.\d+)/i){
$cpu_avg += $1 + $2 + $3 + $4;
}
}
$cpu_avg = sprintf("%.3f",$cpu_avg/60);
#----- Collect Firewall Stats -------------------------------------------------#
$t->buffer_empty;
@firewall = $t->cmd(String => "sys firewall cnt disp");
#print @firewall;
for ($i = 0; $i < @firewall; $i++) {
if ($firewall[$i] =~ /ICMP\sIdle\sTimeout:\s(\d+)\s+UDP\sIdle\sTimeout:\s(\d+)/i){
$icmpidle = $1;
$udpidle = $2;
}
if ($firewall[$i] =~ /TCP\sIdle\sTimeout:\s(\d+)\s+TCP\sSYN\sIdle\sTimeout:\s(\d+)/i) {
$tcpidle = $1;
$tcpsynidle = $2;
}
if ($firewall[$i] =~ /TCP\sFIN\sIdle\sTimeout:\s(\d+)/i) {
$tcpfinidle = $1;
}
if ($firewall[$i] =~ /Land\sAttack:\s(\d+)\s+IP\sSpoof\sAttack:\s(\d+)/i) {
$land = $1;
$ipspoof = $2;
}
if ($firewall[$i] =~ /ICMP\sEcho\sAttack:\s(\d+)\s+ICMP\sAttack:\s(\d+)/i) {
$icmpecho = $1;
$icmp = $2;
}
if ($firewall[$i] =~ /Netbios\sAttack:\s(\d+)\s+Trace\sRoute\sAttack:\s(\d+)/i) {
$netbios = $1;
$trace = $2;
}
if ($firewall[$i] =~ /Tear\sDrop\sAttack:\s(\d+)\s+Syn\sFlood\sAttack:\s(\d+)/i) {
$teardrop= $1;
$synflood = $2;
}
if ($firewall[$i] =~ /SMTP\sAttack:\s(\d+)/i) {
$smtp = $1;
}
}
#----- Collect NAT Stats ------------------------------------------------------#
$t->buffer_empty;
@nat = $t->cmd(String => "ip nat iface enif1 st",
Prompt => '/\n*\s*/');
print @nat;
#$t->print('ip nat iface enif1 st'); 'trying stuff
#$t->prompt('/\n*\s*/i'); 'trying stuff
#@nat = $t->getlines(); 'trying stuff
for ($i = 0; $i < @nat; $i++) {
if ($nat[$i] =~ /\s*\w*\s\w*:\s(\d+)\s*\w*:\s*\w*\s\w*\s*\w*\s\w*:\s\w*\s\w*\s*/i){
$tablesize = $1;
}
if ($nat[$i] =~ /\s*\w*:\s\w*\s(\d+),\s\w*\s(\d+),\s\w*\s(\d+),\s\w*\s(\d+),\s\w*\s\w*\s(\d+)%/i) {
$allocated = $1;
$freed = $2;
$full = $3;
$expired = $4;
$cachehit = $5;
}
}
sleep(1.5);
$t->print('\r\n');
$t->prompt('/P334W>/i');
print @nat;
$t->print('exit');
$t->waitfor('/Number:/i');
$t->print('99');
print "Tablesize:$tablesize Allocated:$allocated Freed:$freed Full:$full Expired:$expired Cachehit:$cachehit cpu:$cpu_avg ICMPIdle:$icmpidle UDPIdle:$udpidle TCPIdle:$tcpidle TCPSYNIdle:$tcpsynidle TCPFINIdle:$tcpfinidle LandAttack:$land IPSpoof:$ipspoof ICMPEchoAttack:$icmpecho ICMPAttack:$icmp NetBIOSAttack:$netbios TraceRouteAttack:$trace TearDropAttack:$teardrop SynFloodAttack:$synflood SMTPAttack:$smtp";
exit;