Home Section Blog Cacti SNMP - Zing TCP Stats on Solaris 8
07 | 09 | 2010
SNMP - Zing TCP Stats on Solaris 8 PDF Print
Written by Martijn van Leeuwen   
Friday, 03 April 2009 06:47

I've been using the Zing TCP Stats for Cacti for awhile now.

It gives a nice display on the tcp connections status on you machine.

But by default it doesn't work for Solaris 8, and we have a lot of Solaris 8 at work.

I've rebuild the scripts and here's a small howto to get it working..

First a nice graphical view of what the Zing TCP Stats look like:

 

By default the scripts were written in Python. Since we don't have Python installed I've rewritten the scripts to Perl.

To collect the information:

First we need to create the file that is read by the perl script for SNMP.

We do this by creating a file in /etc/cron.d: netstat (make it executable)

The contents of the file:

#!/usr/bin/ksh

cd /tmp && netstat -an -f inet -P tcp | grep -v "-" | grep -v "TCP" | awk '{print $7}' | sort | uniq -c > net.tmp && mv -f net.tmp net.cache

Now to enable it by using CRON:

#
# Collect Network statistics
#
0,3,6,9,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57 * * * * /etc/cron.d/netstat > /dev/null 2>&1

You can also schedule to run at a 2min interval.

Due to the fact that the cron on Solaris 8 is a bit old, your not able to use the */3 * * * * notation.

 

To get the information into SNMP:

 

 Here is the script that collects the information, I've stored it in /usr/local/bin:

 #!/usr/bin/perl -w

# Debug level used for differing verbosity.
# Put val into %ENV so all modules can access.
# 0 = print only errs
# 1 = print basic progress
# 2 = print actions
# 3 = print details inside subs
#
my $DEBUG_LEVEL = 3;    # default to 1

# Perl settings.
if ($DEBUG_LEVEL == 3){
        # Include Library files
        use Data::Dumper;
        use diagnostics;
}

#use strict;
use Time::Local;

#########################################################
#
#       Scriptname:             netstat.prl
#
#       Author:                 Martijn van Leeuwen
#
#       Purpose of script:      Collect Network Statistics
#
#########################################################
#
#               Version & Change history
#
#               0.1.0   Initial start
#               0.2.0   First run.
#
#########################################################
#
#   Script checked by:
#
#   -
#
#
#
#########################################################
# Set version
#########################################################

my $VERSION="0.2.0";

#########################################################
#  Function Prototypes
#########################################################

sub open_log;
sub close_log;
sub write_log ($);
sub displayversion;
sub read_data_file;
sub getnextoid($);
sub getoid($);

#########################################################
#  Set variables
#########################################################

my $systemname = qx(uname -n);chomp $systemname;
my $scriptname="netstat";
my $TMP_DIR="/tmp";
my $LOG_DIR="/var/log";
my $LOGHANDLER;
my $LOG_FILE="$LOG_DIR/$scriptname.log";
my $SYNTAX="true";
my $Errormsg="";
my $CS="false";
my $LOGGING="false";
my $TMP_DATADUMPFILE="$TMP_DIR/$scriptname-datadumpfile.txt";
my $WRITE_LOG_AND_SCREEN="false";
my $base_oid = ".1.3.6.1.3.2";
my $netstat_cache="/tmp/net.cache";
my %stats= (
        'CLOSE_WAIT' => '0',
        'CLOSING' => '0',
        'ESTABLISHED' => '0',
        'FIN_WAIT1' => '0',
        'LISTEN' => '0',
        'SYN_RECV' => '0',
        'TIME_WAIT' => '0');
my $GETOID="FALSE";
my $NEXTOID="FALSE";
my $INPUTOID;

#################################################
#                                               #
#                   MAIN                        #
#                                               #
#################################################

MAIN: {

# Check input parameters
if (@ARGV>0)
{
 while (@ARGV>0)
 {
  my $x;
  my $param=shift;
  if (substr($param,0,1) eq "-")
  {
   for($x=1;$x    {
    my $char=substr($param,$x,1);
    if ($char eq "l") {$LOGGING="TRUE"}
    elsif ($char eq "c") {$CS="TRUE"}
    elsif ($char eq "h"){
                $Errormsg = "Overview of the parameters.";
        $SYNTAX="false";
        }
        elsif ($char eq "D" && @ARGV>0) {$DEBUG_LEVEL=shift}
        elsif ($char eq "v"){
                print "\n";
                displayversion;
                exit 0;}
        elsif ($char eq "g" && @ARGV>0){ $GETOID="TRUE";$INPUTOID=shift;}
        elsif ($char eq "n" && @ARGV>0){ $NEXTOID="TRUE";$INPUTOID=shift;}
        else {
                print "Supplied parameter '-$char' is INVALLID!\n";
                $SYNTAX="false"
    }
   }
  } else { $INPUTOID=$param}
 }
}
 else {
 $SYNTAX="false";
}

if ($SYNTAX eq "false") {
        displayversion;
        printf("\t%s\n",$Errormsg);
        print "
        Syntax: $scriptname [-lhvc -D -g -n OID ]
        \n\t-c\t sets output to Comma Sperated.
        \n\t-h\t Displays help.
        \n\t-v\t Displays version information.
        \n\t-l\t Writes output to logfile: ($LOG_FILE).
        \n\t-D\t Sets debug level : Currently ($DEBUG_LEVEL).
        \n\t-g\t Get OID
        \n\t-n\t Get next OID
        \n";
        exit 1
}

# Enable logging ?
if ($LOGGING eq "TRUE") {
 open_log($LOG_FILE);
 write_log "Starting $scriptname..";
}

read_data_file();

if (defined $NEXTOID && $NEXTOID eq "TRUE"){
 my $next=getnextoid($INPUTOID);
 getoid($next);
} elsif ($GETOID eq "TRUE") {
 getoid($INPUTOID);
} else {
 getoid($INPUTOID);
}

#DumpDataStructure;
#foreach my $KEYS (%stats){
#print "\t $KEYS\n";
#}
#========================================================
#
#   Finished
#
#       Close open files/Clean Up and Exit
#
#========================================================
#
 if($LOGGING eq "TRUE") {
  close_log();
 }
 exit 0;
}


#########################################################
#
#
#               Utility functions
#
#
#########################################################

sub read_data_file {
 my $FH;
 my @line;
 open($FH, "<", $netstat_cache) or die "$0: Unable to open $netstat_cache for reading: $!";

 while(my $lines = <$FH>){
   chomp($lines);
   $lines =~ s/^\s+//;
   @line=split(/ /,$lines);
   if (defined $line[1]){
    $stats{$line[1]}=$line[0];
   }
 }
 close($FH) or die "$0: Unable to close $netstat_cache: $!";
}

sub getoid($) {
 my $req=shift;
 my $stat=0;
 if ($req eq ".1.3.6.1.3.2.1") { $stat = $stats{'CLOSE_WAIT'};}
 if ($req eq ".1.3.6.1.3.2.2") { $stat = $stats{'CLOSING'};}
 if ($req eq ".1.3.6.1.3.2.3") { $stat = $stats{'ESTABLISHED'};}
 if ($req eq ".1.3.6.1.3.2.4") { $stat = $stats{'FIN_WAIT1'};}
 if ($req eq ".1.3.6.1.3.2.5") { $stat = $stats{'LISTEN'};}
 if ($req eq ".1.3.6.1.3.2.6") { $stat = $stats{'SYN_RECV'};}
 if ($req eq ".1.3.6.1.3.2.7") { $stat = $stats{'TIME_WAIT'};}
 print "$req\n";
 print "Counter32\n";
 print "$stat\n";
 return $stat;
}

sub getnextoid($) {
 my $req=shift;
 my $num=0;

 if ($req eq $base_oid) { $req = "$base_oid".".1";}
 else {
  $num=int(substr($req,-1));
  if ( $num < 7) {
        $num++;
        $req = "$base_oid"."."."$num";
  }
  else {return;}
 }
 return $req;
}

#########################################################
#
#  Systemlogging
#
#########################################################

sub open_log {

        open($LOGHANDLER, ">>", $LOG_FILE) or die "$0: Can't write $LOG_FILE: $!";
}

sub close_log {
        close($LOGHANDLER) or die "$0: $LOG_FILE didn't close: $!";
}

sub write_log ($) {
    my $LogOutput=shift;
    my $TimeStamp="[ ". ctime(). " ]";
    print $LOGHANDLER "$TimeStamp - $LogOutput\n";
}

sub displayversion {
        if ($CS eq "TRUE"){
                print "File:$scriptname;Version:$VERSION;\n";
        }
        else {
                printf("\t%s\n","$scriptname - v$VERSION\n");
        }
}

  __END__

To enable the parsing into snmp modify your snmp.conf with the following line:

pass .1.3.6.1.3.2 /usr/local/bin/netstat.pl

This will call the script and parse all information to SNMP.

Do not forget to restart your SNMP (A kill -HUP on the process ID should do the trick)

 

 

 

Last Updated on Friday, 17 April 2009 13:56