root / plugins / other / port_ @ b0b39b01
Historique | Voir | Annoter | Télécharger (2,54 ko)
| 1 | 7a37bfb1 | Lars Kruse | #!/usr/bin/env ruby |
|---|---|---|---|
| 2 | 89a83b2a | Luis Garc?a Acosta | # |
| 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 | b0b39b01 | Lars Kruse | SERVICE = $0.split('_').last
|
| 19 | 89a83b2a | Luis Garc?a Acosta | SERVICE_F = '/etc/services' |
| 20 | b0b39b01 | Lars Kruse | PORT = /^[\d]+(\.[\d]+){0,1}$/ === SERVICE ? SERVICE : %x[grep #{SERVICE} #{SERVICE_F}].split("\t\t")[1].split('/')[0]
|
| 21 | 89a83b2a | Luis Garc?a Acosta | |
| 22 | class PortMonit < Munin::Plugin |
||
| 23 | graph_attributes "#{SERVICE} port usage, known as #{PORT}",
|
||
| 24 | b0b39b01 | Lars Kruse | :category => 'network', |
| 25 | :info => 'This graph shows connection split by the state of the socket.', |
||
| 26 | :vlabel => 'Current connections' |
||
| 27 | 89a83b2a | Luis Garc?a Acosta | |
| 28 | b0b39b01 | Lars Kruse | declare_field :ESTABLISHED, |
| 29 | :label => 'Established', :draw => :AREA, |
||
| 30 | :type => :GAUGE, :min => 0 |
||
| 31 | 89a83b2a | Luis Garc?a Acosta | |
| 32 | b0b39b01 | Lars Kruse | declare_field :CLOSE_WAIT, |
| 33 | :label => 'Waiting close', :draw => :STACK, |
||
| 34 | :type => :GAUGE, :min => 0 |
||
| 35 | 89a83b2a | Luis Garc?a Acosta | |
| 36 | b0b39b01 | Lars Kruse | declare_field :TIME_WAIT, |
| 37 | :label => 'Waiting after close', :draw => :STACK, |
||
| 38 | :type => :GAUGE, :min => 0 |
||
| 39 | 89a83b2a | Luis Garc?a Acosta | |
| 40 | b0b39b01 | Lars Kruse | declare_field :CLOSING, |
| 41 | :label => 'Closing', :draw => :STACK, |
||
| 42 | :type => :GAUGE, :min => 0 |
||
| 43 | 89a83b2a | Luis Garc?a Acosta | |
| 44 | b0b39b01 | Lars Kruse | declare_field :LAST_ACK, |
| 45 | :label => 'Waiting for acknowledgement', :draw => :STACK, |
||
| 46 | :type => :GAUGE, :min => 0 |
||
| 47 | 89a83b2a | Luis Garc?a Acosta | |
| 48 | b0b39b01 | Lars Kruse | declare_field :FIN_WAIT_1, |
| 49 | :label => 'Socket closed, connection shutting down', :draw => :STACK, |
||
| 50 | :type => :GAUGE, :min => 0 |
||
| 51 | 89a83b2a | Luis Garc?a Acosta | |
| 52 | b0b39b01 | Lars Kruse | declare_field :FIN_WAIT_2, |
| 53 | :label => 'Connection closed, Socket still waiting', :draw => :STACK, |
||
| 54 | :type => :GAUGE, :min => 0 |
||
| 55 | 89a83b2a | Luis Garc?a Acosta | |
| 56 | def retrieve_values |
||
| 57 | b0b39b01 | Lars Kruse | @_netstat = %x[netstat -n -P tcp | egrep "\.#{PORT} "].split("\n")
|
| 58 | |||
| 59 | { :ESTABLISHED => count(@_netstat, 'ESTABLISHED'),
|
||
| 60 | :CLOSE_WAIT => count(@_netstat, 'CLOSE_WAIT'), |
||
| 61 | :CLOSING => count(@_netstat, 'CLOSING'), |
||
| 62 | :LAST_ACK => count(@_netstat, 'LAST_ACK'), |
||
| 63 | :FIN_WAIT_1 => count(@_netstat, 'FIN_WAIT_1'), |
||
| 64 | :FIN_WAIT_2 => count(@_netstat, 'FIN_WAIT_2'), |
||
| 65 | :TIME_WAIT => count(@_netstat, 'TIME_WAIT') } |
||
| 66 | 89a83b2a | Luis Garc?a Acosta | end |
| 67 | |||
| 68 | private |
||
| 69 | b0b39b01 | Lars Kruse | |
| 70 | def count(source, regex) |
||
| 71 | 89a83b2a | Luis Garc?a Acosta | @_result = 0 |
| 72 | |||
| 73 | b0b39b01 | Lars Kruse | source.each { |obj| @_result += 1 if obj.match(regex) }
|
| 74 | 17f78427 | Lars Kruse | |
| 75 | 89a83b2a | Luis Garc?a Acosta | return @_result |
| 76 | end |
||
| 77 | end |
||
| 78 | |||
| 79 | PortMonit.new.run |
