Skip to content

Commit

Permalink
Merge pull request #39 from mlf4aiur/patch-1
Browse files Browse the repository at this point in the history
Update metrics-netstat-tcp.rb
  • Loading branch information
eheydrick authored Jun 16, 2016
2 parents 272f209 + 8a8d605 commit e4120cd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
This CHANGELOG follows the format listed at [Keep A Changelog](http://keepachangelog.com/)

## [Unreleased]
### Added
- metrics-netstat-tcp.rb: Add IPv6 state counts

## [1.0.0] - 2016-06-14
### Fixed
Expand Down
18 changes: 12 additions & 6 deletions bin/metrics-netstat-tcp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,10 @@ class NetstatTCPMetrics < Sensu::Plugin::Metric::CLI::Graphite
long: '--port PORT',
proc: proc(&:to_i)

def netstat(protocol = 'tcp')
state_counts = Hash.new(0)
TCP_STATES.each_pair { |_hex, name| state_counts[name] = 0 }

def netstat(protocol, pattern, state_counts)
File.open('/proc/net/' + protocol).each do |line|
line.strip!
if m = line.match(/^\s*\d+:\s+(.{8}):(.{4})\s+(.{8}):(.{4})\s+(.{2})/) # rubocop:disable AssignmentInCondition
if m = line.match(pattern) # rubocop:disable AssignmentInCondition
connection_state = m[5]
connection_port = m[2].to_i(16)
connection_state = TCP_STATES[connection_state]
Expand All @@ -98,7 +95,16 @@ def netstat(protocol = 'tcp')

def run
timestamp = Time.now.to_i
netstat('tcp').each do |state, count|
state_counts = Hash.new(0)
TCP_STATES.each_pair { |_hex, name| state_counts[name] = 0 }

tcp4_pattern = /^\s*\d+:\s+(.{8}):(.{4})\s+(.{8}):(.{4})\s+(.{2})/
state_counts = netstat('tcp', tcp4_pattern, state_counts)

tcp6_pattern = /^\s*\d+:\s+(.{32}):(.{4})\s+(.{32}):(.{4})\s+(.{2})/
state_counts = netstat('tcp6', tcp6_pattern, state_counts)

state_counts.each do |state, count|
graphite_name = config[:port] ? "#{config[:scheme]}.#{config[:port]}.#{state}" :
"#{config[:scheme]}.#{state}"
output graphite_name.to_s, count, timestamp
Expand Down

0 comments on commit e4120cd

Please sign in to comment.