Skip to content

Instantly share code, notes, and snippets.

@asim
Created April 28, 2011 12:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asim/946255 to your computer and use it in GitHub Desktop.
Save asim/946255 to your computer and use it in GitHub Desktop.
tcp conns
#!/usr/bin/perl
my @statuses = ("ESTABLISHED","SYN_SENT","SYN_RECV","FIN_WAIT1","FIN_WAIT2","TIME_WAIT","CLOSED","CLOSE_WAIT","LAST_ACK","CLOSING","UNKNOWN");
my @ports = ( "80", "443" );
my %data = ();
my $netstat = 'netstat -ant | awk \'$4 ~ /:(80|443)$/ && $6 != "LISTEN" { split($4,a,":"); print a[2],$6;}\' | sort | uniq -c';
# initialize to 0
foreach $status (@statuses) {
foreach $port (@ports) {
$data{$status . "_" . $port} = 0;
}
}
sub send_data {
my $metric=shift;
my $value=shift;
select(undef, undef, undef, 0.05);
system("/usr/bin/gmetric -t uint16 -n $metric -v $value");
}
open (NETSTAT,"$netstat|") or die "unable to read netstat\n";
while (<NETSTAT>) {
if ($_ =~ m/^\s+([0-9]+) ([0-9]{2,3}) (\w+)/) {
$data{$3 . "_" . $2} = $1;
}
}
close(NETSTAT);
foreach $key (keys %data) {
&send_data($key, $data{$key});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment