Skip to content

Instantly share code, notes, and snippets.

Last active April 12, 2022 22:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save andrewpage/ddaa21ad929027c849e85aa5ba1acac7 to your computer and use it in GitHub Desktop.
Save andrewpage/ddaa21ad929027c849e85aa5ba1acac7 to your computer and use it in GitHub Desktop.
Report Phusion Passenger metrics to AWS CloudWatch.
#!/usr/bin/env ruby
# Report Instance Count and Request Queue Size metrics from locally running Phusion Passenger instance.
# Author: Andrew Page <>
# ==
# Phusion Passenger:
require 'net/http'
require 'phusion_passenger'
require 'aws-sdk'
PhusionPassenger.require_passenger_lib 'admin_tools/instance_registry'
include PhusionPassenger::AdminTools
# Extract a parameter from the Passenger pool status output.
def extract_metric(parameter, output)
pattern = %r{#{parameter}\s+:\s+(?<result>\d+)}
match = output.match(pattern)
match['result'] if match
# Get the Passenger pool status for our active instance,
def get_pool_status(instance)
request ='/pool.txt')
# Authenticate the HTTP request to the Passenger API.
request.basic_auth('ro_admin', instance.read_only_admin_password)
# Request pool status
response = instance.http_request('agents.s/core_api', request)
# Generate the metric to report to CloudWatch.
def build_cloudwatch_metric(metric_name, value, instance_id:, timestamp:
metric_name: metric_name,
timestamp: timestamp,
value: value,
storage_resolution: 1,
dimensions: [
name: 'InstanceId',
value: instance_id
instances =
if instances.count > 1
$stderr.puts 'Multiple Passenger instances... not sure which to use. Exiting.'
# Get pool status from Passenger instance.
instance = instances.first
output = get_pool_status(instance)
# Extract key metrics from Passenger output.
timestamp =
process_count = extract_metric('Processes', output)
request_queue_size = extract_metric('Requests in queue', output)
# Get current instance ID
instance_id = Net::HTTP.get(URI(''))
# Report metrics to AWS CloudWatch.
cloudwatch =
namespace: 'Passenger',
metric_data: [
build_cloudwatch_metric('ProcessCount', process_count, instance_id: instance_id, timestamp: timestamp),
build_cloudwatch_metric('RequestQueueSize', request_queue_size, instance_id: instance_id, timestamp: timestamp)
$stderr.puts "Reported Passenger process count of #{process_count} and request queue size of #{request_queue_size} to CloudWatch."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment