Skip to content

Instantly share code, notes, and snippets.

@vitaliel
Created November 22, 2008 10:49
Show Gist options
  • Save vitaliel/27802 to your computer and use it in GitHub Desktop.
Save vitaliel/27802 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# $File: apache_status_parser.rb $
# Created by Vitalie Lazu on Mon, 05 Nov 2007 20:28:57 +0200
# Using hpricot to parse apache server status and kill processes that takes too much time to complete
#
# Debian/Ubuntu setup:
# apt-get install rubygems
# gem install hpricot --no-ri --no-rdoc
require 'rubygems'
require 'hpricot'
require 'open-uri'
class ApacheStatus
attr_accessor :doc, :data
def initialize()
self.data = []
end
# row keys
# :srv, :pid, :acc, :m, :cpu, :ss, :req, :conn, :child, :slot, :client, :vhost, :request
#
# Srv Child Server number - generation
# PID OS process ID
# Acc Number of accesses this connection / this child / this slot
# M Mode of operation
# CPU CPU usage, number of seconds
# SS Seconds since beginning of most recent request
# Req Milliseconds required to process most recent request
# Conn Kilobytes transferred this connection
# Child Megabytes transferred this child
# Slot Total megabytes transferred this slot
def parse(url)
self.doc = Hpricot(open(url))
tab = doc.search("//table").first
keys = []
tab.search("//th").each do |e|
keys << e.inner_text.chomp.downcase.to_sym
end
self.data = []
tab.search("//tr").each do |tr|
i = 0
rez = {}
tr.search("//td").each do |td|
rez[keys[i]] = td.inner_text.chomp
i += 1
end
data << rez if rez.size > 0
end
end
end
cl = ApacheStatus.new
cl.parse("http://localhost/server-status")
kill_pids = {}
cl.data.each do |row|
if row[:m] == "G" && row[:ss].to_i > 120 # 2 minutes
kill_pids[row[:pid]] = 1
puts "Long running pid: #{row[:pid]}, ss: #{row[:ss]}"
# elsif row[:m] == "W"
#puts row[:request]
end
end
if kill_pids.size > 0
cmd = "kill -9 #{kill_pids.keys * ' '}"
puts "I will #{cmd}"
#system(cmd)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment