root / plugins / other / port_ @ b0b39b01
Historique | Voir | Annoter | Télécharger (2,54 ko)
| 1 |
#!/usr/bin/env 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 |
graph_attributes "#{SERVICE} port usage, known as #{PORT}",
|
| 24 |
:category => 'network', |
| 25 |
:info => 'This graph shows connection split by the state of the socket.', |
| 26 |
:vlabel => 'Current connections' |
| 27 |
|
| 28 |
declare_field :ESTABLISHED, |
| 29 |
:label => 'Established', :draw => :AREA, |
| 30 |
:type => :GAUGE, :min => 0 |
| 31 |
|
| 32 |
declare_field :CLOSE_WAIT, |
| 33 |
:label => 'Waiting close', :draw => :STACK, |
| 34 |
:type => :GAUGE, :min => 0 |
| 35 |
|
| 36 |
declare_field :TIME_WAIT, |
| 37 |
:label => 'Waiting after close', :draw => :STACK, |
| 38 |
:type => :GAUGE, :min => 0 |
| 39 |
|
| 40 |
declare_field :CLOSING, |
| 41 |
:label => 'Closing', :draw => :STACK, |
| 42 |
:type => :GAUGE, :min => 0 |
| 43 |
|
| 44 |
declare_field :LAST_ACK, |
| 45 |
:label => 'Waiting for acknowledgement', :draw => :STACK, |
| 46 |
:type => :GAUGE, :min => 0 |
| 47 |
|
| 48 |
declare_field :FIN_WAIT_1, |
| 49 |
:label => 'Socket closed, connection shutting down', :draw => :STACK, |
| 50 |
:type => :GAUGE, :min => 0 |
| 51 |
|
| 52 |
declare_field :FIN_WAIT_2, |
| 53 |
:label => 'Connection closed, Socket still waiting', :draw => :STACK, |
| 54 |
:type => :GAUGE, :min => 0 |
| 55 |
|
| 56 |
def retrieve_values |
| 57 |
@_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 |
end |
| 67 |
|
| 68 |
private |
| 69 |
|
| 70 |
def count(source, regex) |
| 71 |
@_result = 0 |
| 72 |
|
| 73 |
source.each { |obj| @_result += 1 if obj.match(regex) }
|
| 74 |
|
| 75 |
return @_result |
| 76 |
end |
| 77 |
end |
| 78 |
|
| 79 |
PortMonit.new.run |
