Guest User

Untitled

a guest
Jun 18th, 2018
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2. # By atmos@atmos.org
  3. # this goes in your script/ directory
  4. # it parses your memcached.yml file and hooks you up w/ some info
  5. # it keeps you from having to mess w/ stale memcached daemons for whatever reason.
  6. require 'yaml'
  7. require 'timeout'
  8.  
  9. require File.join(File.dirname(File.dirname(__FILE__)), 'config', 'god', 'angel')
  10.  
  11. class MemcachedCtl
  12. attr_accessor :memcached, :memory, :pids, :servers, :ip_address, :ethernet_device
  13.  
  14. def initialize
  15. env = ENV['RAILS_ENV'] || 'development'
  16. self.memcached = `which memcached`.chomp
  17. self.servers = [ ]
  18. self.pids = { }
  19. self.ethernet_device = ENV['ETH'] || 'venet0:0'
  20. self.ip_address = '0.0.0.0'
  21. self.memory = '128'
  22.  
  23. config = YAML.load_file(File.expand_path(File.dirname(__FILE__) + "/../config/memcached.yml"))
  24. self.servers = [ config['defaults']['servers'] ].flatten rescue ['127.0.0.1:11211']
  25. self.servers = [ config[env]['servers'] ].flatten if config[env]['servers']
  26. # self.servers.reject! { |server| host,port = server.split(/:/); self.ip_address == host }
  27. self.memory = config[env]['memory'] unless config[env]['memory'].nil?
  28.  
  29. each_server do |host,port|
  30. `ps auwwx | grep memcached | grep '\\-l #{ip_address} \\-p #{port}' | grep -v grep`.split(/\n/).each do |line|
  31. self.pids[port] = line.split(/\s+/)[1]
  32. end
  33. self.pids[port] ||= 'Down'
  34. end
  35. end
  36.  
  37. def execute(cmd)
  38. send(cmd) rescue usage
  39. end
  40.  
  41. def restart; stop; sleep 1; start end
  42.  
  43. def status
  44. each_server { |host,port| puts "Port #{port} -> #{pids[port] =~ /\d+/ ? 'Up' : 'Down'}" }
  45. end
  46.  
  47. def kill
  48. each_server { |host,port| `kill -9 #{pids[port]} > /dev/null 2>&1` if pids[port] =~ /\d+/ }
  49. end
  50.  
  51. def stop; kill end
  52.  
  53. def start
  54. each_server do |host,port|
  55. `#{memcached} -d -m #{memory} -l #{ip_address} -p #{port} -P #{Angel.pids_dir}/memcached.pid`
  56. STDERR.puts "Try memcached_ctl status" unless $? == 0
  57. end
  58. end
  59.  
  60. def usage
  61. methods = %w[start stop restart kill status]
  62. puts "Usage: script/memcached_ctl [ " + (methods * ' | ') + " ]"
  63. end
  64.  
  65. protected
  66. def each_server
  67. servers.each do |server|
  68. host, port = server.split(/:/)
  69. yield host, port if [self.ip_address, get_ip_address, '127.0.0.1', 'localhost'].include?(host)
  70. end
  71. end
  72.  
  73. def get_ip_address # this works on linux you might have to tweak this on other oses
  74. line = `/sbin/ifconfig #{ethernet_device} | grep inet | grep -v inet6`.chomp
  75. if line =~ /\s*inet addr:((\d+\.){3}\d+)\s+.*/
  76. $1
  77. end
  78. end
  79. end
Add Comment
Please, Sign In to add comment