Class: Deadpool::CommandLineServer

Inherits:
Object
  • Object
show all
Defined in:
lib/deadpool/command_line_server.rb

Instance Method Summary collapse

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(options)
  case options[:command]
  when :help
    help
  when :full_report
    full_report options
  when :nagios_report
    nagios_report options
  when :promote_server
    promote_server options
  when :stop
    stop options
  else
    help
  end
end

#full_report(options) ⇒ Object



105
106
107
# File 'lib/deadpool/command_line_server.rb', line 105

def full_report(options)
  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(message=nil)
  unless message.nil?
    puts message
  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(options)
  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_lineObject



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
  options               = Hash.new
  options[:command_count] = 0
  options[:config_path]     = '/etc/deadpool'

  @option_parser = OptionParser.new do |opts|
    opts.banner = "Usage: deadpool_hosts {help|full_report|nagios_report} [options]"

    opts.separator "Commands:"
    opts.on("-h", "--help", "Print this help message.") do |help|
      options[:command_count] += 1
      options[:command]        = :help
    end
    opts.on("--full_report", "Give the full system report.") do |full_report|
      options[:command_count] += 1
      options[:command]        = :full_report
    end
    opts.on("--nagios_report", "Report system state in Nagios plugin format.") do |nagios_report|
      options[:command_count] += 1
      options[:command]        = :nagios_report
    end
    opts.on("--promote_server", "Promote specified server to the master.") do |nagios_report|
      options[:command_count] += 1
      options[:command]        = :promote_server
    end
    opts.on("--stop", "Stop the server.") do |stop|
      options[:command_count] += 1
      options[:command]        = :stop
    end

    opts.separator "Options:"
    opts.on("--server=SERVER_LABEL", String, "primary_host or secondary_host.") do |server|
      options[:server] = server
    end
    opts.on("--pool=POOL_NAME", String, "Deadpool name to operate on.") do |pool|
      options[:pool] = pool
    end
    opts.on("--config_path=PATH", String, 
        "Path to configs and custom plugins. #{options[:config_path]} by default.") do |config_path|
      options[: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 options[:command_count] == 0
    help "You must specify a command."
  end

  return options
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(options)
  error_messages = []

  if options[:pool].nil?
    error_messages << "Promoting server requires --pool argument."
  end

  if options[:server].nil?
    error_messages << "Promoting server requires --server argument."
  end
  
  unless error_messages.empty?
    help error_messages.join "\n"
  end

  puts send_command_to_deadpool_server :command => 'promote_server', :pool => options[:pool], :server => options[:server]
end

#runObject



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(options)
  output = ''
  socket = TCPSocket.open(@config[:admin_hostname], @config[:admin_port])

  if socket
    socket.puts JSON.dump(options)
    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(options)
  puts send_command_to_deadpool_server :command => 'stop'
end