Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / plugins / bsd / netstat_bsd_m_ @ 809639ab

Historique | Voir | Annoter | Télécharger (2,64 ko)

1 580c41ea Artem Sheremet
#!/usr/bin/env ruby
2
3 b0b39b01 Lars Kruse
=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 809639ab Lars Kruse
  Supposed: BSD
13
  Tested:   FreeBSD 8.2
14 b0b39b01 Lars Kruse
15
Author: Artem Sheremet <dot.doom@gmail.com>
16
17 580c41ea Artem Sheremet
#%# family=auto
18
#%# capabilities=autoconf suggest
19
20 b0b39b01 Lars Kruse
=end
21
22 580c41ea Artem Sheremet
# original filename
23 809639ab Lars Kruse
PLUGIN_NAME = 'netstat_bsd_m_'.freeze
24 580c41ea Artem Sheremet
25
class String
26 b0b39b01 Lars Kruse
  def escape
27 809639ab Lars Kruse
    gsub(/[^\w]/, '_')
28 b0b39b01 Lars Kruse
  end
29
30
  unless method_defined? :start_with?
31
    def start_with?(str)
32
      self[0...str.size] == str
33
    end
34
  end
35 580c41ea Artem Sheremet
end
36
37 85419c53 Artem Sheremet
def netstat_m(filter = nil)
38 809639ab Lars Kruse
  Hash[`netstat -m`.split($/).map do |line|
39
    if line =~ %r{^([\d/K]+) (.*) \(([\w/+]+)\)$}
40 b0b39b01 Lars Kruse
      # 7891K/22385K/30276K bytes allocated to network (current/cache/total)
41 809639ab Lars Kruse
      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 b0b39b01 Lars Kruse
    elsif line =~ /^(\d+) (.*)$/
46
      # 12327 requests for I/O initiated by sendfile
47 809639ab Lars Kruse
      value = Regexp.last_match(1)
48
      desc = Regexp.last_match(2)
49
      [desc, [[:value, value]]] if filter.nil? || (desc.escape == filter)
50 b0b39b01 Lars Kruse
    end
51 809639ab Lars Kruse
  end.compact]
52 580c41ea Artem Sheremet
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 b0b39b01 Lars Kruse
  puts `uname -s`.include?('FreeBSD') ? 'yes' : 'no'
60 580c41ea Artem Sheremet
when 'suggest'
61 b0b39b01 Lars Kruse
  puts netstat_m.keys.map(&:escape).join $/
62 580c41ea Artem Sheremet
when 'config'
63 b0b39b01 Lars Kruse
  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 809639ab Lars Kruse
      "#{esc_name}.draw " + if %w[total max].include? name
79 b0b39b01 Lars Kruse
                              '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 580c41ea Artem Sheremet
when nil # fetch
93 b0b39b01 Lars Kruse
  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 580c41ea Artem Sheremet
else
101 b0b39b01 Lars Kruse
  warn "unrecognized argument <#{ARGV.first}>"
102 580c41ea Artem Sheremet
end