[perl] TCP Connection Status

Templates, scripts for templates, scripts and requests for templates.

Moderators: Developers, Moderators

Rael
Cacti User
Posts: 129
Joined: Thu Apr 05, 2007 9:28 am

Post by Rael »

Now all results for graph are 0

snmpnetstat works just fine.

What gives?
Rael
Cacti User
Posts: 129
Joined: Thu Apr 05, 2007 9:28 am

Post by Rael »

Fixed script to use the old my_cmd instead of the new one with -Cn -Cp,

instead uses -n -P
eaglecz
Posts: 6
Joined: Fri Oct 19, 2007 9:21 pm

Post by eaglecz »

greeting guys

i just spent some time fixing this perl script. It become somehow broken, due to changes in net-snmp

here is my version, which is running fine with

****************START OF THE FILE *********************
#!/usr/bin/perl -w
# --------------------------------------------------
# ARGV[0] = <hostname> required
# ARGV[1] = <snmp version> required
# ARGV[2] = <snmp community> required
# ARGV[3] = <snmp port> required
# ARGV[4] = <snmp timeout> required
# --------------------------------------------------
$in_hostname = $ARGV[0] if defined $ARGV[0];
$in_version = $ARGV[1] if defined $ARGV[1];
$in_community = $ARGV[2] if defined $ARGV[2];
$in_port = $ARGV[3] if defined $ARGV[3];
$in_timeout = $ARGV[4] if defined $ARGV[4];

# if ($in_version == 2)
#{
# $in_version = "2c";
#}

if ($in_version eq "2")
{
$in_version = "2c";
}

my $_cmd = "snmpnetstat -v $in_version -c '$in_community' $in_hostname:$in_port -Cn -Cp tcp -Cw $in_timeout";


# usage notes
if (
( ! defined $in_hostname ) ||
( ! defined $in_version ) ||
( ! defined $in_community ) ||
( ! defined $in_port ) ||
( ! defined $in_timeout )
) {
print "usage:\n\n
$0 <host> <snmp version> < snmp community> <snmp port> <snmp timeout>\n\n";
exit;
}


my @_output = `$_cmd`;

my $_estab = 0;
my $_listen = 0;
my $_timewait = 0;
my $_timeclose = 0;
my $_finwait1 = 0;
my $_finwait2 = 0;
my $_synsent = 0;
my $_synrecv = 0;
my $_closewait = 0;

#print "$_output\n";

foreach ( @_output ) {
#print $_;
$_estab++ if /ESTABLISHED/;
$_listen++ if /LISTEN/;
$_timewait++ if /TIMEWAIT/;
$_timeclose++ if /TIMECLOSE/;
$_finwait1++ if /FINWAIT1/;
$_finwait2++ if /FINWAIT2/;
$_synsent++ if /SYNSENT/;
$_synrecv++ if /SYNRECV/;
$_closewait++ if /CLOSEWAIT/;
}

#
print "established:$_estab listen:$_listen timewait:$_timewait timeclose:$_timeclose finwait1:$_finwait1 finwait2:$_finwait2 synsent:$_synsent synrecv:$_synrecv closewait:$_closewait";
****************END OF THE FILE *********************

And here are changes, that i made:
'$in_community' has to be within '' , otherwise its not working with community containing | , even if you put comm into '' while you are passing this to perl script.

operands -Cn -Cp tcp -Cw $in_timeout" has been adjusted to fit curent syntax. ( -t has been changed to -Cw, -p has been changed to -Cp and so on)

snmp version 2(2c) are both working.

..... and im not programmer nor perl guru, so tweak it as you wish.

hope it will be useful
eaglecz
Posts: 6
Joined: Fri Oct 19, 2007 9:21 pm

Post by eaglecz »

here is modified XML file

I modified path to the perl from /usr/local/perl .... to just perl
It runs fine under apache user on my Gentoo. So this XML and above posted script are working for me.

I kindly ask some to check it out and if you confirm, that its ok, i would suggest to remove original files with obsolete syntax from the first page.
Attachments
cacti_graph_template_snmp_get_tcp_connection_status(2).xml
(36.62 KiB) Downloaded 1436 times
looc
Cacti User
Posts: 110
Joined: Mon Aug 13, 2007 3:40 am

Post by looc »

super-hornet wrote:Hi all

I found a few bugs in the scripts. I know this script is for Windows but if it is going to use in Linux, use this script instead.
Take note that for some net-snmp (or snmpnetstat), the parameters is different

Also take note that the "perl" path in the XML file is hard coded to /usr/local/bin.
For some Linux (openSUSE), it is at /usr/bin instead. Alternate is that u can just use perl.
e.g.
perl <path_cacti>/scripts/lvm_netstat_tcp.pl <host> <snmp_version> <snmp_community> <snmp_port> <snmp_timeout>
----------------------------------- Start of script -----------------------
#!/usr/bin/perl -w

# --------------------------------------------------
# ARGV[0] = <hostname> required
# ARGV[1] = <snmp version> required
# ARGV[2] = <snmp community> required
# ARGV[3] = <snmp port> required
# ARGV[4] = <snmp timeout> required
# --------------------------------------------------
$in_hostname = $ARGV[0] if defined $ARGV[0];
$in_version = $ARGV[1] if defined $ARGV[1];
$in_community = $ARGV[2] if defined $ARGV[2];
$in_port = $ARGV[3] if defined $ARGV[3];
$in_timeout = $ARGV[4] if defined $ARGV[4];

# if ($in_version == 2)
#{
# $in_version = "2c";
#}

if ($in_version eq "2")
{
$in_version = "2c";
}

### my $_cmd = "snmpnetstat -v $in_version -c $in_community -t $in_timeout -n -P tcp $in_hostname:$in_port";
my $_cmd = "snmpnetstat -v $in_version -c $in_community -t $in_timeout -Cn -Cp tcp $in_hostname:$in_port";

# usage notes
if (
( ! defined $in_hostname ) ||
( ! defined $in_version ) ||
( ! defined $in_community ) ||
( ! defined $in_port ) ||
( ! defined $in_timeout )
) {
print "usage:\n\n
$0 <host> <snmp version> < snmp community> <snmp port> <snmp timeout>\n\n";
exit;
}


my @_output = `$_cmd`;

my $_estab = 0;
my $_listen = 0;
my $_timewait = 0;
my $_timeclose = 0;
my $_finwait1 = 0;
my $_finwait2 = 0;
my $_synsent = 0;
my $_synrecv = 0;
my $_closewait = 0;

#print "$_output\n";

foreach ( @_output ) {
#print $_;
$_estab++ if /ESTABLISHED/;
$_listen++ if /LISTEN/;
$_timewait++ if /TIMEWAIT/;
$_timeclose++ if /TIMECLOSE/;
$_finwait1++ if /FINWAIT1/;
$_finwait2++ if /FINWAIT2/;
$_synsent++ if /SYNSENT/;
$_synrecv++ if /SYNRECV/;
$_closewait++ if /CLOSEWAIT/;
}

#
print "established:$_estab listen:$_listen timewait:$_timewait timeclose:$_timeclose finwait1:$_finwait1 finwait2:$_finwait2 synsent:$_synsent synrecv:$_synrecv closewait:$_closewait";

--------------------- End of Script ---------------------------
Hi,that a great script.i use your changed script can read data.but my cacti have a error.
"10/25/2007 05:16:19 PM - CACTID: Poller[0] Host[91] ERROR: Empty result [xxx.xxx.xxx.xxx]: 'perl e:\www\scripts\lvm_netstat_tcp.pl xxx.xxx.xxx.xxx 2 yangpxsonghl 161 800'
in DOS,can read data~
Cacti Version - 0.8.7 & spine
Plugin Architecture - 1.3
Server Info - Windows 2003 EP
Web Server -IIS6.0
PHP - 5.2.3
MySQL - 4.1.10
RRDTool - 1.2.15
SNMP - 5.4.1
--------------------------
plugins:
reportit 0.5.1|monitor 0.8.1|thold 0.3.8
looc
Cacti User
Posts: 110
Joined: Mon Aug 13, 2007 3:40 am

Post by looc »

i have a new matter~
in dos:
C:\>perl e:\www\scripts\lvm_netstat_tcp.pl xxx.xxx.xxx.xxx 1 xxxxxx 161 500

Cannot find module (NOTIFICATION-LOG-MIB): At line 1 in (none)
Cannot find module (NET-SNMP-AGENT-MIB): At line 1 in (none)
Cannot find module (DISMAN-EVENT-MIB): At line 1 in (none)
established:10 listen:0 timewait:0 timeclose:0 finwait1:0 finwait2:0 synsent:1 s
ynrecv:0 closewait:0
but in cacti,it has a error:
10/25/2007 06:16:20 PM - CACTID: Poller[0] Host[91] ERROR: Empty result [xxx.xxx.xxx.xxx]: 'perl E:/www/scripts/lvm_netstat_tcp.pl xxx.xxx.xxx.xxx 1 xxxxxxxx 161 500'
graph no data.

this is my pl.
#!perl -w

# --------------------------------------------------
# ARGV[0] = <hostname> required
# ARGV[1] = <snmp version> required
# ARGV[2] = <snmp community> required
# ARGV[3] = <snmp port> required
# ARGV[4] = <snmp timeout> required
# --------------------------------------------------
$in_hostname = $ARGV[0] if defined $ARGV[0];
$in_version = $ARGV[1] if defined $ARGV[1];
$in_community = $ARGV[2] if defined $ARGV[2];
$in_port = $ARGV[3] if defined $ARGV[3];
$in_timeout = $ARGV[4] if defined $ARGV[4];

# if ($in_version == 2)
#{
# $in_version = "2c";
#}

#if ($in_version eq "2")
#{
#$in_version = "2c";
#}


my $_cmd = "snmpnetstat -v $in_version -c $in_community -t $in_timeout -Cn -Cp tcp $in_hostname:$in_port";

# usage notes
if (
( ! defined $in_hostname ) ||
( ! defined $in_version ) ||
( ! defined $in_community ) ||
( ! defined $in_port ) ||
( ! defined $in_timeout )
) {
print "usage:\n\n
$0 <host> <snmp version> < snmp community> <snmp port> <snmp timeout>\n\n";
exit;
}


my @_output = `$_cmd`;

my $_estab = 0;
my $_listen = 0;
my $_timewait = 0;
my $_timeclose = 0;
my $_finwait1 = 0;
my $_finwait2 = 0;
my $_synsent = 0;
my $_synrecv = 0;
my $_closewait = 0;

#print "$_output\n";

foreach ( @_output ) {
#print $_;
$_estab++ if /ESTABLISHED/;
$_listen++ if /LISTEN/;
$_timewait++ if /TIMEWAIT/;
$_timeclose++ if /TIMECLOSE/;
$_finwait1++ if /FINWAIT1/;
$_finwait2++ if /FINWAIT2/;
$_synsent++ if /SYNSENT/;
$_synrecv++ if /SYNRECV/;
$_closewait++ if /CLOSEWAIT/;
}

#
print "established:$_estab listen:$_listen timewait:$_timewait timeclose:$_timeclose finwait1:$_finwait1 finwait2:$_finwait2 synsent:$_synsent synrecv:$_synrecv closewait:$_closewait";
why? :oops:
Last edited by looc on Fri Oct 26, 2007 5:06 am, edited 1 time in total.
Cacti Version - 0.8.7 & spine
Plugin Architecture - 1.3
Server Info - Windows 2003 EP
Web Server -IIS6.0
PHP - 5.2.3
MySQL - 4.1.10
RRDTool - 1.2.15
SNMP - 5.4.1
--------------------------
plugins:
reportit 0.5.1|monitor 0.8.1|thold 0.3.8
looc
Cacti User
Posts: 110
Joined: Mon Aug 13, 2007 3:40 am

Post by looc »

have a php script for this templates?
Cacti Version - 0.8.7 & spine
Plugin Architecture - 1.3
Server Info - Windows 2003 EP
Web Server -IIS6.0
PHP - 5.2.3
MySQL - 4.1.10
RRDTool - 1.2.15
SNMP - 5.4.1
--------------------------
plugins:
reportit 0.5.1|monitor 0.8.1|thold 0.3.8
sjhome
Posts: 1
Joined: Tue Nov 27, 2007 2:09 am

Post by sjhome »

hellp.

server:10.1.4.11 win2000 server

[root@localhost scripts]# perl lvm_netstat_tcp.pl 10.1.4.11 2c public 161 500
Argument "2c" isn't numeric in numeric eq (==) at lvm_netstat_tcp.pl line 31.
snmpnetstat: Unknown host (tcp) (Resource temporarily unavailable)
established:0 listen:0 timewait:0 timeclose:0 finwait1:0 finwait2:0 synsent:0 synrecv:0 closewait:0

why??thinks~
MatsB
Cacti User
Posts: 57
Joined: Tue Jan 10, 2006 9:25 am

Post by MatsB »

I'm not getting any graphs. When I run this command I get a result back

Code: Select all

snmpnetstat -v 1 -c public 127.0.0.1 -Cn -Cp tcp -Cw 2000
Active Internet (tcp) Connections
Proto Local Address          Remote Address         (state)
tcp   192.168.0.2.80         192.168.0.10.51529    SYNRECEIVED
tcp   192.168.0.2.80         192.168.0.10.51530    SYNRECEIVED
tcp   192.168.0.2.80         192.168.0.10.51531    TIMEWAIT
tcp   192.168.0.2.80         192.168.0.10.51532    TIMEWAIT
I keep getting these in my Cacti log:

Code: Select all

CMDPHP: Poller[0] Host[7] DS[54] WARNING: Result from CMD not valid. Partial Result:
Debug output:

Code: Select all

/usr/bin/rrdtool create \
/usr/share/cacti/site/rra/cacti_established_54.rrd \
--step 300  \
DS:established:GAUGE:600:0:U \
DS:timewait:GAUGE:600:0:U \
DS:timeclose:GAUGE:600:0:U \
DS:synsent:GAUGE:600:0:U \
DS:synrecv:GAUGE:600:0:U \
DS:listen:GAUGE:600:0:U \
DS:finwait2:GAUGE:600:0:U \
DS:finwait1:GAUGE:600:0:U \
DS:closewait:GAUGE:600:0:U \
RRA:AVERAGE:0.5:1:600 \
RRA:AVERAGE:0.5:6:700 \
RRA:AVERAGE:0.5:24:775 \
RRA:AVERAGE:0.5:288:797 \
RRA:MIN:0.5:1:600 \
RRA:MIN:0.5:6:700 \
RRA:MIN:0.5:24:775 \
RRA:MIN:0.5:288:797 \
RRA:MAX:0.5:1:600 \
RRA:MAX:0.5:6:700 \
RRA:MAX:0.5:24:775 \
RRA:MAX:0.5:288:797 \
RRA:LAST:0.5:1:600 \
RRA:LAST:0.5:6:700 \
RRA:LAST:0.5:24:775 \
RRA:LAST:0.5:288:797 \
wilperi
Posts: 12
Joined: Mon Sep 03, 2007 6:55 am

Post by wilperi »

Hi

In the script here excactly i had to put my own options (community, port etc)?

-wilperi-
netzine21
Posts: 3
Joined: Mon Apr 28, 2008 8:35 pm

i got same result all graphs "0"

Post by netzine21 »

Posted: Mon Apr 28, 2008 9:06 pm Post subject: Why graphs all "0" ?

--------------------------------------------------------------------------------

i do run script (lvm_netstat_tcp.pl) at cacti server and result is correct

/www/cacti/scripts/lvm_netstat_tcp.pl host 1 community_name 161 500

and result is like this

established:259 listen:0 timewait:64 timeclose:0 finwait1:0 finwait2:0 synsent:0 synrecv:0 closewait:0

but the cacti graphs all data is "0"

i use cacti Version 0.6.8.b
rrdtool Version 1.2

what can i do ?
User avatar
gandalf
Developer
Posts: 22383
Joined: Thu Dec 02, 2004 2:46 am
Location: Muenster, Germany
Contact:

Post by gandalf »

Please do not crosspost. I answered to the other thread
Reinhard
sllywhtboy
Posts: 42
Joined: Sun Jul 09, 2006 1:51 am
Location: detroit'ish
Contact:

Post by sllywhtboy »

i had to update line #21 for ubuntu 8.04/hardy. (all on 1 line though)

Code: Select all

my $_cmd        = "snmpnetstat -v $in_version  -c $in_community -t $in_timeout -Ca -Cn -Cp TCP $in_hostname:$in_port";
the example updates listed above didn't give me all the listening/partial-open, etc.
laamidd
Posts: 48
Joined: Fri Feb 17, 2006 10:31 am

Post by laamidd »

Date Fri, 29 Aug 2008 07:50:28 -0400
Cacti Version 0.8.7b
Cacti OS win32

SNMP Version net-snmp
RRDTool Version RRDTool 1.2.x
PHP Version 5.2.6
PHP OS WINNT
PHP uname Windows NT WEBSENSEAGENT 5.2 build 3790

PHP SNMP Installed

UCD-SNMP Support enabled
UCD-SNMP Version ucd-snmp-4.2.3

Env variable MIBDIRS = E:\php\extras\mibs

When run from cacti as the default data input (I added the "c" for, <snmp-version>c )

Code: Select all

C:/perl/bin/perl <path_cacti>/scripts/lvm_netstat_tcp.pl <host> <snmp_version>c <snmp_community> <snmp_port> <snmp_timeout> 
my graphs of course end up with all 0's.

So, I try from cmd. line two different ways:
C:\Perl\bin>snmpnetstat 10.6.244.5 2c commname 161 400
Cannot find module (NOTIFICATION-LOG-MIB): At line 1 in (none)
Cannot find module (NET-SNMP-AGENT-MIB): At line 1 in (none)
Cannot find module (DISMAN-EVENT-MIB): At line 1 in (none)


And WITHOUT the "c"

Code: Select all

C:\Perl\bin>perl E:\cacti\scripts\lvm_netstat_tcp.pl 10.6.244.5 2 commname 161 400
Invalid version specified after -v flag: 2
usage: snmpnetstat [snmp_opts] [-Can] [-Cf address_family]
       snmpnetstat [snmp_opts] [-CbdgimnrSs] [-Cf address_family]
       snmpnetstat [snmp_opts] [-Cbdn] [-CI interface] [-Cw wait]
       snmpnetstat [snmp_opts] [-Cs] [-Cp protocol]
       snmpnetstat [snmp_opts] [-Ca] [-Cf address_family] [-Ci | -CI interface]
established:0 listen:0 timewait:0 timeclose:0 finwait1:0 finwait2:0 synsent:0 synrecv:0 closewait:0
=======================================

C:\Perl\bin>perl E:\cacti\scripts\lvm_netstat_tcp.pl 10.6.244.5 2c commname 161 400
Argument "2c" isn't numeric in numeric eq (==) at E:\cacti\scripts\lvm_netstat_tcp.pl line 31.
(my line 31:

Code: Select all

if ($in_version == 2)
{
        $in_version = "2c";
}
Cannot find module (NOTIFICATION-LOG-MIB): At line 1 in (none)
Cannot find module (NET-SNMP-AGENT-MIB): At line 1 in (none)
Cannot find module (DISMAN-EVENT-MIB): At line 1 in (none)
snmpnetstat: Unknown host (tcp) (No such file or directory)
established:0 listen:0 timewait:0 timeclose:0 finwait1:0 finwait2:0 synsent:0 synrecv:0 closewait:0
[/code]

I'm not getting anything in the log files either...

Any ideas?

Thanks,
Bob
Rael
Cacti User
Posts: 129
Joined: Thu Apr 05, 2007 9:28 am

Post by Rael »

Why is it when this perl script is run from the poller it produces all 0's and when run by hand it produces the correct results?

Both being run as the cacti user.

Code: Select all

09/22/2008 10:20:10 AM - SPINE: Poller[0] Host[13] DS[1107] SCRIPT: /usr/local/bin/perl /proj/cacti/cacti-0.8.7b/scripts/lvm_netstat_tcp.pl miux77.cac.com 2 EDIT 161 500, output: established:0 listen:0 timewait:0 timeclose:0 finwait1:0 finwait2:0 synsent:0 synrecv:0 closewait:0
By Hand

Code: Select all

root@mivl02:/proj/cacti/cacti-0.8.7b/log > /usr/local/bin/perl /proj/cacti/cacti-0.8.7b/scripts/lvm_netstat_tcp.pl miux77.cac.com 2 EDIT 161 500
established:109 listen:0 timewait:35 timeclose:0 finwait1:0 finwait2:0 synsent:0
Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests