Class: Deadpool::CommandLineServer
- Inherits:
-
Object
- Object
- Deadpool::CommandLineServer
- Defined in:
- lib/deadpool/command_line_server.rb
Instance Method Summary collapse
- #execute_command(options) ⇒ Object
- #full_report(options) ⇒ Object
- #help(message = nil) ⇒ Object
-
#initialize(argv) ⇒ CommandLineServer
constructor
A new instance of CommandLineServer.
- #nagios_report(options) ⇒ Object
- #parse_command_line ⇒ Object
- #promote_server(options) ⇒ Object
- #run ⇒ Object
- #send_command_to_deadpool_server(options) ⇒ Object
- #stop(options) ⇒ Object
Constructor Details
#initialize(argv) ⇒ CommandLineServer
Returns a new instance of CommandLineServer.
13 14 15 |
# File 'lib/deadpool/command_line_server.rb', line 13 def initialize(argv) @argv = argv end |
Instance Method Details
#execute_command(options) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/deadpool/command_line_server.rb', line 80 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 else help end end |
#full_report(options) ⇒ Object
105 106 107 |
# File 'lib/deadpool/command_line_server.rb', line 105 def full_report() puts send_command_to_deadpool_server :command => 'full_report' end |
#help(message = nil) ⇒ Object
97 98 99 100 101 102 103 |
# File 'lib/deadpool/command_line_server.rb', line 97 def help(=nil) unless .nil? puts end puts @option_parser.help exit 4 end |
#nagios_report(options) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/deadpool/command_line_server.rb', line 109 def nagios_report() response = send_command_to_deadpool_server :command => 'nagios_report' puts response if (response.to_s =~ /^OK/) != nil exit 0 elsif (response.to_s =~ /^WARNING/) != nil exit 1 elsif (response.to_s =~ /^CRITICAL/) != nil exit 2 elsif (response.to_s =~ /^UNKNOWN/) != nil exit 3 else exit 4 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 |
# File 'lib/deadpool/command_line_server.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 {help|full_report|nagios_report} [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.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
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/deadpool/command_line_server.rb', line 126 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/command_line_server.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
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/deadpool/command_line_server.rb', line 148 def send_command_to_deadpool_server() output = '' socket = TCPSocket.open(@config[:admin_hostname], @config[:admin_port]) if socket socket.puts JSON.dump() while line = socket.gets output += line end socket.close else puts "Couldn't connect to deadpool server." exit 4 end return output end |
#stop(options) ⇒ Object
144 145 146 |
# File 'lib/deadpool/command_line_server.rb', line 144 def stop() puts send_command_to_deadpool_server :command => 'stop' end |