root / plugins / bsd / netstat_bsd_m_ @ 809639ab
Historique | Voir | Annoter | Télécharger (2,64 ko)
| 1 |
#!/usr/bin/env ruby |
|---|---|
| 2 |
|
| 3 |
=begin |
| 4 |
|
| 5 |
netstat_bsd_m revision 1 (Feb 2012) |
| 6 |
|
| 7 |
This plugin shows various statistics from 'netstat -m' |
| 8 |
|
| 9 |
Required privileges: none |
| 10 |
|
| 11 |
OS: |
| 12 |
Supposed: BSD |
| 13 |
Tested: FreeBSD 8.2 |
| 14 |
|
| 15 |
Author: Artem Sheremet <dot.doom@gmail.com> |
| 16 |
|
| 17 |
#%# family=auto |
| 18 |
#%# capabilities=autoconf suggest |
| 19 |
|
| 20 |
=end |
| 21 |
|
| 22 |
# original filename |
| 23 |
PLUGIN_NAME = 'netstat_bsd_m_'.freeze |
| 24 |
|
| 25 |
class String |
| 26 |
def escape |
| 27 |
gsub(/[^\w]/, '_') |
| 28 |
end |
| 29 |
|
| 30 |
unless method_defined? :start_with? |
| 31 |
def start_with?(str) |
| 32 |
self[0...str.size] == str |
| 33 |
end |
| 34 |
end |
| 35 |
end |
| 36 |
|
| 37 |
def netstat_m(filter = nil) |
| 38 |
Hash[`netstat -m`.split($/).map do |line| |
| 39 |
if line =~ %r{^([\d/K]+) (.*) \(([\w/+]+)\)$}
|
| 40 |
# 7891K/22385K/30276K bytes allocated to network (current/cache/total) |
| 41 |
values = Regexp.last_match(1) |
| 42 |
desc = Regexp.last_match(2) |
| 43 |
names = Regexp.last_match(3) |
| 44 |
[desc, names.split('/').zip(values.split('/'))] if filter.nil? || (desc.escape == filter)
|
| 45 |
elsif line =~ /^(\d+) (.*)$/ |
| 46 |
# 12327 requests for I/O initiated by sendfile |
| 47 |
value = Regexp.last_match(1) |
| 48 |
desc = Regexp.last_match(2) |
| 49 |
[desc, [[:value, value]]] if filter.nil? || (desc.escape == filter) |
| 50 |
end |
| 51 |
end.compact] |
| 52 |
end |
| 53 |
|
| 54 |
stat_name = File.basename($0, '.*').escape |
| 55 |
stat_name.slice! 0, PLUGIN_NAME.size if stat_name.start_with? PLUGIN_NAME |
| 56 |
|
| 57 |
case ARGV.first |
| 58 |
when 'autoconf' |
| 59 |
puts `uname -s`.include?('FreeBSD') ? 'yes' : 'no'
|
| 60 |
when 'suggest' |
| 61 |
puts netstat_m.keys.map(&:escape).join $/ |
| 62 |
when 'config' |
| 63 |
data = netstat_m(stat_name) |
| 64 |
if data.empty? |
| 65 |
warn "no data for <#{stat_name}>. Try running with 'suggest'"
|
| 66 |
else |
| 67 |
desc, values = data.first |
| 68 |
stack = values.size > 1 |
| 69 |
first = true |
| 70 |
puts <<~CONFIG |
| 71 |
graph_title Netstat: #{desc}
|
| 72 |
graph_category network |
| 73 |
graph_vlabel current |
| 74 |
graph_order #{values.map { |name, _| name.to_s.escape }.join ' '}
|
| 75 |
CONFIG |
| 76 |
puts values.map { |name, _|
|
| 77 |
esc_name = name.to_s.escape |
| 78 |
"#{esc_name}.draw " + if %w[total max].include? name
|
| 79 |
'LINE' |
| 80 |
elsif stack |
| 81 |
if first |
| 82 |
first = false |
| 83 |
'AREA' |
| 84 |
else |
| 85 |
'STACK' |
| 86 |
end |
| 87 |
else |
| 88 |
'LINE2' |
| 89 |
end + "\n#{esc_name}.label #{name}"
|
| 90 |
}.join $/ |
| 91 |
end |
| 92 |
when nil # fetch |
| 93 |
data = netstat_m(stat_name) |
| 94 |
unless data.empty? |
| 95 |
puts data.first.last.map { |name, value|
|
| 96 |
value = value.to_i * 1024 if value.end_with? 'K' |
| 97 |
"#{name.to_s.escape}.value #{value}"
|
| 98 |
}.join $/ |
| 99 |
end |
| 100 |
else |
| 101 |
warn "unrecognized argument <#{ARGV.first}>"
|
| 102 |
end |
