Created
August 30, 2012 20:43
-
-
Save bwhaley/3540423 to your computer and use it in GitHub Desktop.
collectd tcp connections exec script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'getoptlong' | |
PLUGIN_NAME = 'tcpconns' | |
def usage | |
puts("#{$0} -h <fqdn> [-i <sampling_interval>]") | |
exit | |
end | |
# Main | |
begin | |
# Sync stdout so that it will flush to collectd properly. | |
$stdout.sync = true | |
# Parse command line options | |
hostname = nil | |
sampling_interval = 10 # sec, Default value | |
opts = GetoptLong.new( | |
[ '--hostid', '-h', GetoptLong::REQUIRED_ARGUMENT ], | |
[ '--sampling-interval', '-i', GetoptLong::OPTIONAL_ARGUMENT ] | |
) | |
opts.each do |opt, arg| | |
case opt | |
when '--hostid' | |
hostname = arg | |
when '--sampling-interval' | |
sampling_interval = arg.to_i | |
end | |
end | |
usage if !hostname | |
# Collection loop | |
while true do | |
start_run = Time.now.to_i | |
next_run = start_run + sampling_interval | |
states = [ | |
'ESTABLISHED', | |
'SYN_SENT', | |
'SYN_RECV', | |
'FIN_WAIT1', | |
'FIN_WAIT2', | |
'TIME_WAIT', | |
'CLOSE', | |
'CLOSE_WAIT', | |
'LAST_ACK', | |
'LISTEN', | |
'CLOSING', | |
] | |
netdata = `/bin/netstat -an` | |
states.each do |state| | |
count = netdata.scan(/\s#{state}\s/).length | |
puts("PUTVAL #{hostname}/#{PLUGIN_NAME}/gauge-#{state} #{start_run}:#{count}") | |
end | |
# sleep to make the interval | |
while((time_left = (next_run - Time.now.to_i)) > 0) do | |
sleep(time_left) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment