Projet

Général

Profil

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

root / plugins / thin / thins_peak_memory @ f9000cdc

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

1
#!/usr/bin/env ruby
2
# thin_peak_memory - 
3
#     A munin plugin for Linux to monitor the maximum memory size
4
#     that an each individual thin process has reached
5
# 
6
# For Linux ONLY !
7
# DOES NOT WORK on OSX, Solaris or BSD.
8
# only linux, because this script relies on proc filesystem
9
#
10
# Author: 
11
#   Frederico de Souza Araujo - fred.the.master@gmail.com
12
#   http://www.frederico-araujo.com
13
#
14
# Based on:
15
#   thin_process_memory - 
16
#       A munin plugin to monitor memory size of 
17
#       each individual thin process
18
#   by Ben VandenBos and Avvo, Inc.
19
#
20
# This program is free software; you can redistribute it and/or modify
21
# it under the terms of the GNU General Public License version 2 
22
# as published by the Free Software Foundation.
23
# 
24
# This program is distributed in the hope that it will be useful,
25
# but WITHOUT ANY WARRANTY; without even the implied warranty of
26
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27
# GNU General Public License for more details.
28
# 
29
# You should have received a copy of the GNU General Public License along
30
# with this program; if not, write to the Free Software Foundation, Inc.,
31
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
32
#
33
#
34
#%# family=auto
35
#%# capabilities=autoconf
36

    
37
module Munin
38
  class ThinPeakMemory
39
    
40
    def run
41
      pids = get_pids()
42
      port_list = Hash.new
43
      pids.sort.each do |pid, port|
44
        hwm = (pid_hwm(pid).to_i)/1024
45
        puts "thin_#{port}.value #{hwm}"
46
      end
47
    end
48
    
49
    # only get VmHWM count for each pid
50
    #  (Virtual Memory High Water Mark)
51
    # Using Proc filesystem
52
    # ONLY LINUX! because relies on proc filesystem
53
    # TODO: make this work on OSX and Solaris,
54
    #      so the whole unix gang is happy ;)
55
    def pid_hwm(pid)
56
      res = `grep "VmHWM" /proc/#{pid}/status`.split[1]
57
      if res.match("cannot access")
58
        return nil
59
      else
60
        return res
61
      end
62
    end
63
    
64
    # Get pis using lsof (list open files) 
65
    #   look for listening tcp applications 
66
    #   that match the name thin
67
    def get_pids
68
      pids_ports = []
69
      pids = `pgrep thin`.split
70
      pids.each do |t|
71
        port = `lsof -p #{t} | grep LISTEN`.split[8]
72
        port = port.split(":")[1]
73
        pids_ports << [t,port]
74
      end
75
      pids_ports
76
    end
77

    
78
    def autoconf
79
      get_pids().length > 0
80
    end
81
    
82
  end
83
end
84

    
85
mpm = Munin::ThinPeakMemory.new
86

    
87
case ARGV[0]
88
when "config"
89
  puts "graph_title Thin Peak Memory (High Water Mark)"
90
  puts "graph_vlabel HWM"
91
  puts "graph_category Thin"
92
  puts "graph_args -l 0"
93
  puts "graph_scale yes"
94
  puts "graph_info Tracks the peak memory of thin processes, aka High Water Mark."
95
  mpm.get_pids.sort.each do |pid,port|
96
    puts "thin_#{port}.label thin_#{port}"
97
    puts "thin_#{port}.info Peak Memory"
98
    puts "thin_#{port}.type GAUGE"
99
    puts "thin_#{port}.min 0"
100
  end
101
when "autoconf"
102
  if mpm.autoconf
103
    puts "yes"
104
    exit 0
105
  end
106
  puts "no"
107
  exit 1
108
else
109
  mpm.run
110
end