Class: Deadpool::Admin
- Inherits:
-
Object
- Object
- Deadpool::Admin
- Defined in:
- lib/deadpool/admin.rb
Instance Method Summary collapse
- #execute_command(options) ⇒ Object
- #foreground(options) ⇒ Object
- #full_report(options) ⇒ Object
- #help(message = nil) ⇒ Object
-
#initialize(argv) ⇒ Admin
constructor
A new instance of Admin.
- #nagios_report(options) ⇒ Object
- #parse_command_line ⇒ Object
- #promote_server(options) ⇒ Object
- #run ⇒ Object
- #send_command_to_deadpool_server(options) ⇒ Object
- #start(options) ⇒ Object
- #stop(options) ⇒ Object
Constructor Details
#initialize(argv) ⇒ Admin
Returns a new instance of Admin.
13 14 15 |
# File 'lib/deadpool/admin.rb', line 13 def initialize(argv) @argv = argv end |
Instance Method Details
#execute_command(options) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/deadpool/admin.rb', line 88 def execute_command() case [:command] when :help help when :full_report full_report when :nagios_report nagios_report when :promote_server promote_server when :stop stop when :start start when :foreground foreground else help end end |
#foreground(options) ⇒ Object
168 169 170 |
# File 'lib/deadpool/admin.rb', line 168 def foreground() puts Deadpool::Server.new().run(false) end |
#full_report(options) ⇒ Object
117 118 119 |
# File 'lib/deadpool/admin.rb', line 117 def full_report() puts send_command_to_deadpool_server :command => 'full_report' end |
#help(message = nil) ⇒ Object
109 110 111 112 113 114 115 |
# File 'lib/deadpool/admin.rb', line 109 def help(=nil) unless .nil? puts end puts @option_parser.help exit 4 end |
#nagios_report(options) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/deadpool/admin.rb', line 121 def nagios_report() response = send_command_to_deadpool_server :command => 'nagios_report' if (response.to_s =~ /^OK/) != nil puts response.to_s exit OK elsif (response.to_s =~ /^WARNING/) != nil puts response.to_s exit WARNING elsif (response.to_s =~ /^CRITICAL/) != nil puts response.to_s exit CRITICAL elsif (response.to_s =~ /^UNKNOWN/) != nil puts response.to_s exit UNKNOWN else puts "UNKNOWN - #{response.to_s}" exit UNKNOWN end end |
#parse_command_line ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/deadpool/admin.rb', line 24 def parse_command_line = Hash.new [:command_count] = 0 [:config_path] = '/etc/deadpool' @option_parser = OptionParser.new do |opts| opts. = "Usage: deadpool_hosts --command [options]" opts.separator "Commands:" opts.on("-h", "--help", "Print this help message.") do |help| [:command_count] += 1 [:command] = :help end opts.on("--full_report", "Give the full system report.") do |full_report| [:command_count] += 1 [:command] = :full_report end opts.on("--nagios_report", "Report system state in Nagios plugin format.") do |nagios_report| [:command_count] += 1 [:command] = :nagios_report end opts.on("--promote_server", "Promote specified server to the master.") do |nagios_report| [:command_count] += 1 [:command] = :promote_server end opts.on("--stop", "Stop the server.") do |stop| [:command_count] += 1 [:command] = :stop end opts.on("--start", "Start the server in the background.") do |stop| [:command_count] += 1 [:command] = :start end opts.on("--foreground", "Start the server in the foreground.") do |stop| [:command_count] += 1 [:command] = :foreground end opts.separator "Options:" opts.on("--server=SERVER_LABEL", String, "primary_host or secondary_host.") do |server| [:server] = server end opts.on("--pool=POOL_NAME", String, "Deadpool name to operate on.") do |pool| [:pool] = pool end opts.on("--config_path=PATH", String, "Path to configs and custom plugins. #{[:config_path]} by default.") do |config_path| [:config_path] = config_path end end remaining_arguments = @option_parser.parse! @argv unless remaining_arguments.empty? help "[#{remaining_arguments.join(' ')}] is not understood." end if [:command_count] == 0 help "You must specify a command." end return end |
#promote_server(options) ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/deadpool/admin.rb', line 142 def promote_server() = [] if [:pool].nil? << "Promoting server requires --pool argument." end if [:server].nil? << "Promoting server requires --server argument." end unless .empty? help .join "\n" end puts send_command_to_deadpool_server :command => 'promote_server', :pool => [:pool], :server => [:server] end |
#run ⇒ Object
17 18 19 20 21 22 |
# File 'lib/deadpool/admin.rb', line 17 def run @options = self.parse_command_line @config = Deadpool::Helper.configure @options self.execute_command(@options) end |
#send_command_to_deadpool_server(options) ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/deadpool/admin.rb', line 172 def send_command_to_deadpool_server() output = '' begin socket = TCPSocket.open(@config[:admin_hostname], @config[:admin_port]) rescue return "Couldn't connect to deadpool server. Is it running?" end if socket socket.puts JSON.dump() while line = socket.gets output += line end socket.close else return "Couldn't connect to deadpool server." end return output end |
#start(options) ⇒ Object
164 165 166 |
# File 'lib/deadpool/admin.rb', line 164 def start() puts Deadpool::Server.new().run(true) end |
#stop(options) ⇒ Object
160 161 162 |
# File 'lib/deadpool/admin.rb', line 160 def stop() puts send_command_to_deadpool_server :command => 'stop' end |