Skip to content

Commit

Permalink
v0.4.11 - interfaces-portability
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric Wilhelm committed Nov 27, 2013
2 parents 7bffa8f + 3137e4e commit 9159a0a
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 26 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 0.4.11

* collectors/interfaces - portability: parsing netstat for data

# 0.4.10

* collectors/process - portability, rename ni -> nice, skip thcount
Expand Down
8 changes: 8 additions & 0 deletions collectors/interfaces/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Description

Reports 'rx packets', 'tx packets', and similar metrics per-interface.

# Portability

On linux hosts, reads `/proc/net/dev` and reports those metrics. For
others, parses output of `netstat -i` and reports fewer metrics.
89 changes: 64 additions & 25 deletions collectors/interfaces/interfaces
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,71 @@

require 'json'
require 'panoptimon/util'
Panoptimon::Util.os(linux: true)

head = [
'rx bytes',
'rx packets',
'rx errs',
'rx drop',
'rx fifo',
'rx frame',
'rx compressed',
'rx multicast',
'tx bytes',
'tx packets',
'tx drops',
'tx fifo',
'tx colls',
'tx carrier',
'tx compressed'
]

opt = ARGV[0].nil? ? {interval: 1}
: JSON::parse(ARGV[0], {symbolize_names: true})

$stdout.sync = true # persistent process

class Array; def to_h ; Hash[*self.flatten]; end; end

state = File.read('/proc/net/dev').
split("\n").drop(2).map {|l|
(iface, row) = *l.sub(/^\s+/, '').split(/:\s+/, 2)
[iface, head.zip(row.split(/\s+/)).to_h]
}.to_h
Panoptimon::Util.os(
linux: ->(){
head = [
'rx bytes',
'rx packets',
'rx errs',
'rx drop',
'rx fifo',
'rx frame',
'rx compressed',
'rx multicast',
'tx bytes',
'tx packets',
'tx errs',
'tx drop',
'tx fifo',
'tx colls',
'tx carrier',
'tx compressed'
]
# TODO delete some of these that aren't available on other OS?

while(1) do
state = File.read('/proc/net/dev').
split("\n").drop(2).map {|l|
(iface, row) = *l.sub(/^\s+/, '').split(/:\s+/, 2)
[iface, head.zip(row.split(/\s+/)).to_h]
}.to_h

puts JSON::generate(state)
sleep(opt[:interval] || 5)
end
},
default: ->() {
hmap = {
'Ipkts' => 'rx packets',
'Ierrs' => 'rx errs',
'Opkts' => 'tx packets',
'Oerrs' => 'tx errs',
'Collis' => 'tx colls',
}
IO.popen(['netstat', '-in']) {|fh|
header = fh.gets.split(/\s+/)
raise "unexpected - #{header}" unless header[0] == 'Name'
header = (0..(header.length - 1)).map {|i| [header[i], i]}.to_h

data = {}
while(line = fh.gets) do
break if line == "\n" # TODO parse/handle ipv6 outputs
row = line.split(/\s+/)
iface = row[header['Name']]
data[iface] = hmap.keys.map {|k| [hmap[k], row[header[k]]]}.to_h
end

puts JSON::generate(data)
}
exit $?.exitstatus unless $?.success?

puts JSON::generate(state)
},
);
2 changes: 1 addition & 1 deletion lib/panoptimon/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Copyright (C) 2012 Sourcefire, Inc.

module Panoptimon
VERSION = "0.4.10"
VERSION = "0.4.11"
end

0 comments on commit 9159a0a

Please sign in to comment.