Created
September 16, 2012 17:03
-
-
Save jjb/3733242 to your computer and use it in GitHub Desktop.
Restart httpd when it is eating memory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env ruby | |
require 'rubygems' | |
require 'pony' | |
# total memory used | |
total_memory_used = `free | awk '$1=="Mem:" {print $3 / $2 * 100}'`.to_i | |
# active memory used | |
used = `awk '$1=="Active:"{active=$2}$1=="MemTotal:"{total=$2}END{print active/total * 100}' /proc/meminfo`.to_i | |
acceptable = 75 | |
if used > acceptable | |
timestamp = Time.now.strftime("%d/%b/%Y:%H:%M:%S") | |
top_pids = `ps -eo pmem,pid,user,args | sort -r | grep apache | head -3 | awk '{printf $2 " "}' | xargs echo` | |
# top_ten_pids = `ps -eo pmem,pid,user,args,etime,ppid,rss | sort -r | grep apache | head` | |
top_ten_pids = `ps -eo pmem,pid,user,args,etime,ppid,rss | sort -r ` | |
log_marker = "==== Apache Restarted by restart_httpd_when_eating_memory #{top_pids} @ [#{timestamp}] ====" | |
[ '/var/log/httpd/main-access.log', | |
'/var/log/httpd/main-error.log', | |
'/var/log/php-error.log' | |
].each{ |log| | |
`echo "#{log_marker}" >> #{log}` | |
} | |
`/sbin/service httpd restart` | |
message =" | |
#{timestamp} | |
httpd on the web node has been restarted | |
#{total_memory_used}% of total memory was in use | |
#{used}% of memory was active | |
#{acceptable}% is the current active memory threshold | |
__all processes, sorted by memory usage__ | |
#{top_ten_pids} | |
" | |
Pony.instance_eval{ | |
@sendmail_binary = '/usr/sbin/sendmail' | |
} | |
Pony.mail(:to => '[email protected],[email protected]', :from => '[email protected]', :subject => 'httpd restarted on web server', :body=>message) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment