#!/usr/bin/perl # $Id: netconncnt,v 1.3 2007/06/07 21:29:16 cos Exp $ # # Count web TCP connections in each state, and feed to ganglia # Run this from cron as often as you want to sample; once/min: # * * * * * /usr/local/bin/netconncnt # # Requires that ganglia-gmond is installed, gmond is running, # and gmetric is in the path of whatever user runs this script. # # Contributions/suggestions/bugs to: Ofer Inbar # # Written at, and donated to the community by, Going.com # All my scripts distributed under the terms of Larry Wall's excellent # Artistic License: http://language.perl.com/misc/Artistic.html $netstat = "netstat -n -t"; $filter = ":80"; # tcp 0 0 192.168.1.6:56351 192.168.1.2:11211 ESTABLISHED %statecount = ( 'CLOSING' => 0, 'CLOSE_WAIT' => 0, 'TIME_WAIT' => 0, 'ESTABLISHED' => 0, 'FIN_WAIT1' => 0, 'FIN_WAIT2' => 0, 'SYN_SENT' => 0, 'SYN_RECV' => 0, 'LAST_ACK' => 0 ); # sleep a random few seconds so we don't count other monitoring scripts sleep(rand(30)); open NETSTAT, "$netstat |" or die "$0: can't run $netstat: $!\n"; ; ; while () { my ($src,$dest,$state) = /^tcp\s+\d+\s+\d+\s+(\d\S+)\s+(\d\S+)\s+(\w+)/ or warn "$0: can't parse $_" and next; next unless ($src =~ /$filter/); defined($statecount{$state}) or warn "$0: unknown state: '$state' in $_" and next; $statecount{$state}++; } foreach $state (keys %statecount) { my $metric = "net_port80_" . lc($state); my $gmetric = "gmetric --name $metric --value $statecount{$state} --type uint8 --units 'connections'"; # print "$gmetric\n"; system($gmetric) and warn "$0: can't run gmetric: $?\n"; }