Projet

Général

Profil

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

root / plugins / other / thin_memory @ 631be558

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

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

    
32
module Munin
33
  class ThinProcessMemory
34
    
35
    # run main method
36
    def run
37
      pids = get_pids()
38
      port_list = Hash.new
39
      pids.sort.each do |pid, port|
40
        rss = (pid_rss(pid).to_i)/1024
41
        puts "thin_#{port}.value #{rss}"
42
      end
43
    end
44
    
45
    # only get the memory for each pid
46
    def pid_rss(pid)
47
      res = `grep "VmRSS" /proc/#{pid}/status`.split[1]
48
      if res.match("cannot access")
49
        return nil
50
      else
51
        return res
52
      end
53
    end
54
    
55
    # fetch all pids that match thin
56
    def get_pids
57
      pids_ports = []
58
      pids = `pgrep thin`.split
59
      pids.each do |t|
60
        # only works for linux i'm affraid
61
        # using lsof (list open files) 
62
        port = `lsof -p #{t} | grep LISTEN`.split[8]
63
        port = port.split(":")[1]
64
        pids_ports << [t,port]
65
      end
66
      pids_ports
67
    end
68

    
69
    def autoconf
70
      get_pids().length > 0
71
    end
72
    
73
  end
74
end
75

    
76
mpm = Munin::ThinProcessMemory.new
77

    
78
case ARGV[0]
79
when "config"
80
  puts "graph_title Thin Memory"
81
  puts "graph_vlabel RSS"
82
  puts "graph_category Thin"
83
  puts "graph_args --base 1024 -l 0"
84
  puts "graph_scale yes"
85
  puts "graph_info Tracks the size of individual thin processes"
86
  mpm.get_pids.sort.each do |pid,port|
87
    puts "thin_#{port}.label thin_#{port}"
88
    puts "thin_#{port}.info Process memory"
89
    puts "thin_#{port}.type GAUGE"
90
    puts "thin_#{port}.min 0"
91
  end
92
when "autoconf"
93
  if mpm.autoconf
94
    puts "yes"
95
    exit 0
96
  end
97
  puts "no"
98
  exit 1
99
else
100
  mpm.run
101
end