How to Graph TCP Connection Status with Cacti

I found this way to get the TCP connections by an accident because I actually was tinkering on a way to make a script using netstat and gather the requested TCP connection status in a pretty straight forward fashion:

# netstat -an | grep 8009 | awk '{print $7}' |sort | uniq -c
105 CLOSE_WAIT
54 ESTABLISHED
105 FIN_WAIT_2
1 LISTEN
18 TIME_WAIT

But the only way to store the data with this alternative is probably into a text file. So I went to Google, type “TCP Connection status Cacti” and Bingo, a really useful thread in Cacti Forum lighted me up.

The thread explains how to gather the TCP connection status using snmpnetstat -honestly, quite new to me, implemented into a perl script which parse the output from the command.
First of all, you have to verify the availability of snmpnetstat in your Linux box -I’m using the latest ubuntu release, by typing the following line:

# snmpnetstat --version
NET-SNMP version: 5.4.2.1

If you have a successful output, lets try display Active TCP Connections of localhost:

$ snmpnetstat -v2c -c public -t 100 -Cn -Cp tcp localhost
Active Internet (tcp) Connections
Proto Local Address Remote Address (state)
tcp *.* *.* CLOSED
tcp *.32806 *.* CLOSED
tcp *.33103 *.* CLOSED
tcp *.33332 *.* CLOSED
tcp 127.0.0.1.8009 127.0.0.1.51617 ESTABLISHED
tcp 127.0.0.1.8009 127.0.0.1.51618 ESTABLISHED
tcp 127.0.0.1.31000 127.0.0.1.32000 ESTABLISHED
tcp 127.0.0.1.31002 127.0.0.1.32001 ESTABLISHED
tcp 127.0.0.1.32000 127.0.0.1.31000 ESTABLISHED
tcp 127.0.0.1.32001 127.0.0.1.31002 ESTABLISHED
tcp 127.0.0.1.51617 127.0.0.1.8009 ESTABLISHED
tcp 127.0.0.1.51618 127.0.0.1.8009 ESTABLISHED

If we’re doing ok so far, great, you can Download lvm_netstat_tcp modified to run on Ubuntu on snmpnetstat version 5.4.2.1. Before continuing, I’d like to recommend you to test the perl script as follows:

# perl lvm_netstat_tcp.pl server.domain.com 2c public 161 100
established:195 listen:0 timewait:46 timeclose:0 finwait1:1 finwait2:0 synsent:0 synrecv:0 closewait:0

Successful output? Copy lvm_netstat_tcp.pl to $CACTI_HOME/scripts.
As of now, we’re ready to download and import the XML templates and import them via Console -> Import Templates through the GUI.
Afterwards a successful import go and create a new graph for i.e. localhost device and if you wait some hours, you must get something like the following graph.

tcp_connection_status

Quite simple, isn’t it? If you have any concern/issues/inquiries, please let me know and g’luck.

One Reply to “How to Graph TCP Connection Status with Cacti”