#!/usr/bin/env ruby # # Copyright (C) 2010-2013 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] @label = ENV["label"] @pid_file = ENV["pid_file"] @ruby = ENV["ruby"] || Gem.ruby passenger_spec = Gem::Specification.find_by_name("passenger") @passenger_status = passenger_spec.bin_file("passenger-status") 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} #{pid}` [$?.success?, result] end def label_to_key(label) label.downcase.gsub(/[ -]+/, "_") end def extract_application_name(path) components = path.split(/\//) application_name, tag = components.last.split(/#/) application_name end def parse_result(result) sections = {} section = nil result.each_line do |line| case line when /-+\s+(.+)\s+-+/ section = $1 sections[section] = [] else case section when "General information" case line.chomp when /\A(.+):\s*(\d+)\z/ label = $1.strip value = $2.to_i key = label_to_key(label) sections[section] << { :key => key, :label => label, :value => value, } end when "Application groups" case line.chomp when /\A(\/.+):\s*\z/ path = $1 application_name = extract_application_name(path) sections[section] << [application_name, []] when /\A\s+(.+):\s*(\d+)\z/ label = $1.strip value = $2.to_i _application_name, attributes = sections[section].last key = "#{_application_name}_#{label_to_key(label)}" label = "#{label}: #{_application_name}" attributes << { :key => key, :label => label, :value => value, } end 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 attributes = sections["General information"] sections["Application groups"].each do |_, application_attributes| attributes += application_attributes end attributes.each do |attribute| key = attribute[:key] next if key == "count" puts("#{key}.label #{attribute[:label]}") case key when /max/, /queue/ draw = "LINE2" else if have_stack_base draw = "STACK" else draw = "AREA" have_stack_base = true end end puts("#{attribute[:key]}.draw #{draw}") end end def report success, result = passenger_status exit(false) unless success sections = parse_result(result) attributes = sections["General information"] sections["Application groups"].each do |_, application_attributes| attributes += application_attributes end attributes.each do |attribute| puts("#{attribute[:key]}.value #{attribute[: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