Projet

Général

Profil

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

root / plugins / other / port_ @ 430d68ff

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

1 89a83b2a Luis Garc?a Acosta
#! /opt/csw/bin/ruby
2
#
3
# Wildcard-script to monitor network port usage using netstat. To monitor a
4
# port, link port_<service> to this file. E.g. This plugin shall run by root user
5
#
6
#    ln -s /usr/share/munin/node/plugins-auto/port_ /etc/munin/node.d/port_www
7
#
8
# ...will monitor www connections. Services are those listed in
9
# /etc/services. Case service is not listed the numeric value shall be passed
10
#
11
# Author: Luis García Acosta
12
# V 1.0
13
# Date Tue April 12 09:20:21 CET 2011
14
15
require 'rubygems'
16
require 'munin'
17
18
SERVICE   = $0.split( '_' ).last
19
SERVICE_F = '/etc/services'
20
PORT      = /^[\d]+(\.[\d]+){0,1}$/ === SERVICE ? SERVICE : %x[grep #{SERVICE} #{SERVICE_F}].split( "\t\t" )[1].split( '/' )[0]
21
22
class PortMonit < Munin::Plugin
23
24
  graph_attributes "#{SERVICE} port usage, known as #{PORT}",
25 7f8ae73b Luis Garc?a Acosta
    :category   => 'network',
26 89a83b2a Luis Garc?a Acosta
    :info       => 'This graph shows connection split by the state of the socket.',
27
    :vlabel     => 'Current connections'
28
29
   declare_field :ESTABLISHED,
30
    :label  => 'Established',         :draw => :AREA,
31
    :type   => :GAUGE, :min => 0
32
33
   declare_field :CLOSE_WAIT,
34
    :label  => 'Waiting close',       :draw => :STACK,
35
    :type   => :GAUGE, :min => 0
36
37
   declare_field :TIME_WAIT,
38
    :label  => 'Waiting after close', :draw => :STACK,
39
    :type   => :GAUGE, :min => 0
40
41
   declare_field :CLOSING,
42
    :label  => 'Closing',             :draw => :STACK,
43
    :type   => :GAUGE, :min => 0
44
45
   declare_field :LAST_ACK,
46
    :label  => 'Waiting for acknowledgement',   :draw => :STACK,
47
    :type   => :GAUGE, :min => 0
48
49
   declare_field :FIN_WAIT_1,
50
    :label  => 'Socket closed, connection shutting down', :draw => :STACK,
51
    :type   => :GAUGE, :min => 0
52
53
   declare_field :FIN_WAIT_2,
54
    :label  => 'Connection closed, Socket still waiting', :draw => :STACK,
55
    :type   => :GAUGE, :min => 0
56
57
  def retrieve_values
58
   
59
    @_netstat   = %x[netstat -n -P tcp | egrep "\.#{PORT} "].split( "\n" )
60
61
62
    { :ESTABLISHED  => count( @_netstat, 'ESTABLISHED' ),
63
      :CLOSE_WAIT   => count( @_netstat, 'CLOSE_WAIT' ),
64
      :CLOSING      => count( @_netstat, 'CLOSING' ),
65
      :LAST_ACK     => count( @_netstat, 'LAST_ACK' ),
66
      :FIN_WAIT_1   => count( @_netstat, 'FIN_WAIT_1' ),
67
      :FIN_WAIT_2   => count( @_netstat, 'FIN_WAIT_2' ),
68
      :TIME_WAIT    => count( @_netstat, 'TIME_WAIT' ) }
69
  end
70
71
  private
72
  def count( source, regex )
73
    @_result = 0
74
75
    source.each { |obj| @_result += 1 if obj.match( regex ) }
76
    
77
    return @_result
78
  end
79
end
80
81
PortMonit.new.run