#!/usr/bin/env ruby # # Copyright (C) 2010 Kouhei Sutou # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # 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, see . # #%# family=auto #%# capabilities=autoconf require 'rubygems' mode = ARGV[0] def passenger_status_path(gem_path) File.join(gem_path, "bin", "passenger-status") end @label = ENV["label"] @pid_file = ENV["pid_file"] @ruby = ENV["ruby"] || Gem.ruby @gem_path = ((ENV["GEM_HOME"] || '').split(/:/) + Gem.path).find do |path| File.exist?(passenger_status_path(path)) end def passenger_status if @pid_file unless File.readable?(@pid_file) return [false, "PID file isn't readable: #{@pid_file}"] end pid = File.read(@pid_file).strip else pid = nil end result = `#{@ruby} #{passenger_status_path(@gem_path)} #{pid}` [$?.success?, result] end def parse_result(result) sections = {} section = nil result.each_line do |line| case section when "General information" case line.chomp when /\A(\S+)\s*=\s*(\d+)\z/ key = $1 value = $2.to_i sections[section] << {:key => key, :label => key, :value => value} when /\A(Waiting on global queue):\s*(\d+)\z/ label = $1 value = $2.to_i sections[section] << {:key => "global_queue", :label => label, :value => value} end else if /-+\s+(.+)\s+-+/ =~ line section = $1 sections[section] = [] end end end sections end def config success, result = passenger_status unless success puts result exit(false) end if @label title = "Passenger: #{@label}: status" else title = "Passenger: status" end sections = parse_result(result) puts(<<-EOC) graph_title #{title} graph_category passenger graph_info Passenger status graph_vlabel number of processes EOC have_stack_base = false sections["General information"].each do |attributes| key = attributes[:key] next if key == "count" puts("#{key}.label #{attributes[:label]}") case key when "max", "global_queue" draw = "LINE2" else if have_stack_base draw = "STACK" else draw = "AREA" have_stack_base = true end end puts("#{attributes[:key]}.draw #{draw}") end end def report success, result = passenger_status exit(false) unless success sections = parse_result(result) sections["General information"].each do |attributes| puts("#{attributes[:key]}.value #{attributes[:value]}") end end case mode when "auto", "autoconf", "detect" success, result = passenger_status if success puts "yes" exit(true) else puts "no (#{result})" exit(false) end when "config" config else report end