Skip to content

Instantly share code, notes, and snippets.

@jnewland
Created January 20, 2010 19:46
Show Gist options
  • Save jnewland/282164 to your computer and use it in GitHub Desktop.
Save jnewland/282164 to your computer and use it in GitHub Desktop.
# Apache2 Status by Hampton Catlin
#
# Free Use Under the MIT License
#
# Please note, your server must respond to "apache2ctl status"
#
class Apache2Status < Scout::Plugin
OPTIONS=<<-EOS
sample_size:
name: Apache Request Samples
notes: Since Requests come in unevenly, how many samples should we average
default: 5
sample_sleep:
name: Apache Request Sample Pause
notes: Time Between Samples (in seconds)
default: 0.2
EOS
def build_report
report(:requests_being_processed => fetch('requests currently being processed'))
report(:idle_workers => fetch('idle workers'))
report(:apache_reserved_memory_size => apache_reserved_memory_size)
end
# Calculate the total reserved memory size from a ps aux with a couple greps
def apache_reserved_memory_size
# Fetch the process list and split it into an array
process_list = (`ps aux | grep apache2 | grep -v grep | grep -v ruby`).split("\n")
# Aggregate with this variable
memory_size = 0
# Iterate over the process list and sum up the memory values
process_list.each do |line|
# Split the data into columns
columns = line.split(" ")
memory_size += columns[4].to_i
end
# Calculate how many MB that is
mb = ((memory_size / 1024.0) / 1024)
# Display with MB on the end
mb.to_s + " MB"
end
# Run calcuations
def calculate(postfix)
total = 0
# How many samples should we do?
sample_size = option('sample_size').to_i
sample_size.times do
# Sum up the total number of idle workers by calling the fetch method
total += fetch(postfix)
# Pause for a moment to make sure the sample is good
sleep(option('sample_sleep').to_f)
end
# Return the average number of requests
average = (total / sample_size)
end
def fetch(postfix)
# Must have mod_status installed
results = `/usr/sbin/apache2ctl status`
requests_being_processed = results.match(/([0-9]*) #{postfix}/)[1].to_i
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment