Guest User

Untitled

a guest
Jun 19th, 2018
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.54 KB | None | 0 0
  1. #!/usr/bin/ruby
  2.  
  3.  
  4. require 'tmpdir'
  5. require 'open3'
  6. require 'socket'
  7.  
  8.  
  9. ##----------------------------------------------------------------------
  10. ## CONSTANTS
  11. ##----------------------------------------------------------------------
  12. ICOTS_ROOT = '/var/www/rails/icots'
  13. PID_DIR = "#{ICOTS_ROOT}/shared/pids"
  14.  
  15. SURVEILLANCE_THRESHOLD = 500
  16. PREJUDICE_THRESHOLD = 625
  17. TERMINATE_THRESHOLD = 750
  18.  
  19. UNDER_SURVEILLANCE = Hash.new
  20.  
  21.  
  22. ##----------------------------------------------------------------------
  23. ## methods
  24. ##----------------------------------------------------------------------
  25.  
  26. def check_and_possibly_create_tmp_animal_control
  27. tacd = Dir.tmpdir + File::SEPARATOR + 'animal_control'
  28. Dir.mkdir( tacd ) unless File.exist?( tacd )
  29. end
  30.  
  31. def read_mongrel_pid( mongrel_pid_filename )
  32.  
  33. mongrel_pid = String.new
  34. File.open( mongrel_pid_filename ) { |f| f.read( nil, mongrel_pid ) }
  35. puts "mongrel_pid = '#{mongrel_pid}'" if $DEBUG
  36.  
  37. mongrel_pid.to_i
  38.  
  39. end
  40.  
  41.  
  42. def under_surveillance?( mongrel_pid )
  43. us = UNDER_SURVEILLANCE.has_key?( mongrel_pid )
  44. puts "under_surveillance? = '#{us}'" if $DEBUG
  45. us
  46. end
  47.  
  48.  
  49. def over_surveillance_threshold?( mongrel_pid )
  50.  
  51. vsz_in_mb = `ps -o vsz= #{mongrel_pid}`.chomp.to_i / 1024
  52. puts "vsz_in_mb = '#{vsz_in_mb}'" if $DEBUG
  53. puts "SURVEILLANCE_THRESHOLD = '#{SURVEILLANCE_THRESHOLD}'" if $DEBUG
  54.  
  55. ost = vsz_in_mb > SURVEILLANCE_THRESHOLD
  56. puts "over_surveillance_threshold? = '#{ost}'" if $DEBUG
  57.  
  58. ost
  59.  
  60. end
  61.  
  62.  
  63. def over_terminate_threshold?( mongrel_pid )
  64.  
  65. vsz_in_mb = `ps -o vsz= #{mongrel_pid}`.chomp.to_i / 1024
  66. puts "vsz_in_mb = '#{vsz_in_mb}'" if $DEBUG
  67. puts "TERMINATE_THRESHOLD = '#{TERMINATE_THRESHOLD}'" if $DEBUG
  68.  
  69. ott = vsz_in_mb > TERMINATE_THRESHOLD
  70. puts "over_terminate_threshold = '#{ott}'" if $DEBUG
  71.  
  72. ott
  73.  
  74. end
  75.  
  76.  
  77. def over_prejudice_threshold?( mongrel_pid )
  78.  
  79. vsz_in_mb = `ps -o vsz= #{mongrel_pid}`.chomp.to_i / 1024
  80. puts "vsz_in_mb = '#{vsz_in_mb}'" if $DEBUG
  81. puts "PREJUDICE_THRESHOLD = '#{PREJUDICE_THRESHOLD}'" if $DEBUG
  82.  
  83. opt = vsz_in_mb > PREJUDICE_THRESHOLD
  84. puts "over_prejudice_threshold = '#{opt}'" if $DEBUG
  85.  
  86. opt
  87.  
  88. end
  89.  
  90.  
  91. def should_surveil?( mongrel_pid )
  92. v = (! under_surveillance?(mongrel_pid)) && over_surveillance_threshold?( mongrel_pid )
  93. puts "should_surveil? = '#{v}'" if $DEBUG
  94. v
  95. end
  96.  
  97.  
  98. def should_terminate?( mongrel_pid )
  99. v = under_surveillance?(mongrel_pid) && over_terminate_threshold?( mongrel_pid )
  100. puts "should_terminate? = '#{v}'" if $DEBUG
  101. v
  102. end
  103.  
  104.  
  105. def should_terminate_with_prejudice?( mongrel_pid )
  106. v = (! under_surveillance?(mongrel_pid)) && over_prejudice_threshold?( mongrel_pid )
  107. puts "should_terminate_with_prejudice? = '#{v}'" if $DEBUG
  108. v
  109. end
  110.  
  111. def tmp_filename( mongrel_pid_filename )
  112. tf = String.new
  113. tf << Dir.tmpdir
  114. tf << File::SEPARATOR
  115. tf << 'animal_control'
  116. tf << File::SEPARATOR
  117. tf << Socket.gethostname
  118. tf << '_'
  119. tf << File.basename(mongrel_pid_filename)[0,12].gsub('.','_')
  120. tf << '_'
  121. tf << Time.now.strftime('%Y%m%d_%H%M%S')
  122. tf << '.'
  123. tf << 'strace'
  124. puts "tmp_filename = '#{tf}'" if $DEBUG
  125. tf
  126. end
  127.  
  128. def start_surveillance( mongrel_pid_filename, mongrel_port, mongrel_pid )
  129.  
  130. strace_filename = tmp_filename( mongrel_pid_filename )
  131. cpid = fork { exec("sudo strace -p #{mongrel_pid} -o #{strace_filename}; gzip #{strace_filename}; echo 'see attachment' | mail -s '#{File.basename(strace_filename)}.gz' -a #{strace_filename}.gz jheath@appriss.com wsheldahl@appriss.com") }
  132. Process.detach( cpid )
  133.  
  134. UNDER_SURVEILLANCE[ mongrel_pid ] = true
  135. `echo "surveilling mongrel #{Socket.gethostname}:#{mongrel_port}" | mail -s "surveilling mongrel #{Socket.gethostname}:#{mongrel_port}" jheath@appriss.com wsheldahl@appriss.com aocsystems2@appriss.com`
  136. end
  137.  
  138.  
  139. def call_start
  140. sleep 5
  141. `sudo -u appserv mongrel_rails cluster::start -C /var/www/rails/icots/current/config/mongrel/prod/icots.yml`
  142. end
  143.  
  144.  
  145. def terminate_mongrel( mongrel_pid_filename, mongrel_port, mongrel_pid )
  146.  
  147. Process.kill('SIGKILL', mongrel_pid )
  148.  
  149. UNDER_SURVEILLANCE.delete( mongrel_pid )
  150.  
  151. File.delete( mongrel_pid_filename )
  152.  
  153. `echo "terminated mongrel #{Socket.gethostname}:#{mongrel_port}" | mail -s "terminated mongrel #{Socket.gethostname}:#{mongrel_port}" jheath@appriss.com wsheldahl@appriss.com aocsystems2@appriss.com`
  154. call_start
  155.  
  156. end
  157.  
  158.  
  159. def terminate_runaway_mongrel( mongrel_pid_filename, mongrel_port, mongrel_pid )
  160. Process.kill( 'SIGKILL', mongrel_pid )
  161. `echo "terminated runaway mongrel not under surveillance #{Socket.gethostname}:#{mongrel_port}" | mail -s "terminated runaway mongrel not under surveillance #{Socket.gethostname}:#{mongrel_port}" jheath@app
  162. File.delete( mongrel_pid_filename )
  163. call_start
  164. end
  165.  
  166.  
  167. ##----------------------------------------------------------------------
  168. ## MAIN
  169. ##----------------------------------------------------------------------
  170.  
  171. puts "animal_control.rb started at #{Time.now}"
  172.  
  173. check_and_possibly_create_tmp_animal_control
  174.  
  175. loop do
  176.  
  177. Dir.glob("#{PID_DIR}/*.pid").each do |mongrel_pid_filename|
  178.  
  179. mongrel_pid = read_mongrel_pid( mongrel_pid_filename )
  180. mongrel_port = File.basename(mongrel_pid_filename)[8,4].to_i
  181.  
  182. start_surveillance( mongrel_pid_filename, mongrel_port, mongrel_pid ) if should_surveil?( mongrel_pid )
  183. terminate_mongrel( mongrel_pid_filename, mongrel_port, mongrel_pid ) if should_terminate?( mongrel_pid )
  184. terminate_runaway_mongrel( mongrel_pid_filename, mongrel_port, mongrel_pid ) if should_terminate_with_prejudice?( mongrel_pid )
  185.  
  186. end
  187.  
  188. sleep 1
  189.  
  190. end
Add Comment
Please, Sign In to add comment