Repository
Munin (contrib)
Last change
2020-08-25
Graph Categories
Family
auto
Capabilities
Keywords
Language
Ruby

thin_memory

Sadly there is no documentation for this plugin.

#!/usr/bin/env ruby

=begin

thin_memory - A munin plugin for Linux to monitor memory size of each individual thin process

For Linux ONLY !
DOES NOT WORK on OSX, Solaris or BSD.
only linux, because this script relies on proc filesystem

Original author:
  Frederico de Souza Araujo - fred.the.master@gmail.com
  http://www.frederico-araujo.com

Usurper:
  Adam Michel - elfurbe@furbism.com
  http://www.furbism.com

Originally based on:
  thin_process_memory -
      A munin plugin to monitor memory size of
      each individual thin process
  by Ben VandenBos and Avvo, Inc.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2
as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

#%# family=auto
#%# capabilities=autoconf

=end

module Munin
  class ThinProcessMemory
    # run main method
    def run
      instances = get_pids
      instances.each do |instance|
        pid, port = instance.split('|')
        rss = pid_rss(pid).to_i / 1024
        puts "thin_#{port}.value #{rss}"
      end
    end

    # only get the memory for each pid
    def pid_rss(pid)
      res = `grep "VmRSS" /proc/#{pid}/status`.split[1]
      if res.match('cannot access')
        nil
      else
        res
      end
    end

    # fetch all pids that match thin
    def get_pids
      pids = `pgrep -f 'thin' -l | awk -F " " '{ if (substr( $4, 10, 4)>=1) print $1"|"substr( $4, 10, 4)}' | sort -t'|' -nk 2`.split(/\r?\n/)
    end

    def autoconf
      get_pids.length > 0
    end
  end
end

mpm = Munin::ThinProcessMemory.new

case ARGV[0]
when 'config'
  puts 'graph_title Thin Memory'
  puts 'graph_vlabel RSS'
  puts 'graph_category webserver'
  puts 'graph_args --base 1024 -l 0'
  puts 'graph_scale yes'
  puts 'graph_info Tracks the size of individual thin processes'
  mpm.get_pids.each do |instance|
    pid, port = instance.split('|')
    puts "thin_#{port}.label thin_#{port}"
    puts "thin_#{port}.info Process memory"
    puts "thin_#{port}.type GAUGE"
    puts "thin_#{port}.min 0"
  end
when 'autoconf'
  if mpm.autoconf
    puts 'yes'
    exit 0
  end
  puts 'no'
  exit 0
else
  mpm.run
end