| •1 |
#!/usr/bin/ruby |
| 2 |
|
| 3 |
#%# family=auto |
| 4 |
#%# capabilities=autoconf suggest |
| 5 |
|
| 6 |
require 'net/http' |
| 7 |
begin |
| 8 |
require 'json' |
| 9 |
rescue LoadError |
| 10 |
require 'rubygems' |
| 11 |
require 'json' |
| 12 |
end |
| 13 |
|
| 14 |
label = ENV["label"] |
| 15 |
@groonga = ENV["groonga"] || "groonga" |
| 16 |
@default_host = ENV["host"] || "127.0.0.1" |
| 17 |
@default_http_port = 10041 |
| 18 |
@default_gqtp_port = 10043 |
| 19 |
@default_http_pid_path = "/run/groonga/groonga-http.pid" |
| 20 |
@default_httpd_pid_path = "/run/groonga/groonga-httpd.pid" |
| 21 |
@default_gqtp_pid_path = "/run/groonga/groonga-gptp.pid" |
| 22 |
|
| 23 |
command = ARGV.shift |
| 24 |
|
| 25 |
def parse(success, result) |
| 26 |
if success |
| 27 |
begin |
| 28 |
status, body = JSON.parse(result) |
| 29 |
return_code, start_time, elapsed, error_message = status |
| 30 |
if return_code.zero? |
| 31 |
[success, body] |
| 32 |
else |
| 33 |
[false, error_message] |
| 34 |
end |
| 35 |
rescue JSON::ParserError |
| 36 |
[false, $!.message] |
| 37 |
end |
| 38 |
else |
| 39 |
[success, result] |
| 40 |
end |
| 41 |
end |
| 42 |
|
| 43 |
def run_http(host, port, command) |
| 44 |
path = "/d/#{command}" |
| 45 |
begin |
| 46 |
response = Net::HTTP.start(host, port) do |http| |
| 47 |
http.get(path) |
| 48 |
end |
| 49 |
rescue SystemCallError, TimeoutError |
| 50 |
return [false, "#{$!.class}: #{$!}"] |
| 51 |
end |
| 52 |
if response.is_a?(Net::HTTPSuccess) |
| 53 |
parse(true, response.body) |
| 54 |
else |
| 55 |
[false, "#{response.code}: #{response.body}"] |
| 56 |
end |
| 57 |
end |
| 58 |
|
| 59 |
def run_gqtp(host, port, command) |
| 60 |
groonga = "#{@groonga} -p #{port} -c #{host}" |
| 61 |
result = `#{groonga} #{command} 2>&1` |
| 62 |
parse($?.success?, result) |
| 63 |
end |
| 64 |
|
| 65 |
def run(command) |
| 66 |
case @service_type |
| 67 |
when "http" |
| 68 |
run_http(@http_host, @http_port, command) |
| 69 |
when "httpd" |
| 70 |
run_http(@httpd_host, @httpd_port, command) |
| 71 |
when "gqtp" |
| 72 |
run_gqtp(@gqtp_host, @gqtp_port, command) |
| 73 |
else |
| 74 |
[false, "unknown service type: #{@service_type}"] |
| 75 |
end |
| 76 |
end |
| 77 |
|
| 78 |
def setup_service_type |
| 79 |
if /_([^_]+)\z/ =~ $0 |
| 80 |
@service_type = $1 |
| 81 |
else |
| 82 |
@service_type = nil |
| 83 |
end |
| 84 |
end |
| 85 |
|
| 86 |
def setup_http |
| 87 |
@http_host = ENV["http_host"] || @default_host |
| 88 |
@http_port = (ENV["http_port"] || @default_http_port).to_i |
| 89 |
@http_running = File.exist?(ENV["http_pid_path"] || @default_http_pid_path) |
| 90 |
end |
| 91 |
|
| 92 |
def setup_httpd |
| 93 |
@httpd_host = ENV["httpd_host"] || @default_host |
| 94 |
@httpd_port = (ENV["httpd_port"] || @default_http_port).to_i |
| 95 |
@httpd_running = File.exist?(ENV["httpd_pid_path"] || @default_httpd_pid_path) |
| 96 |
end |
| 97 |
|
| 98 |
def setup_gqtp |
| 99 |
@gqtp_host = ENV["gqtp_host"] || @default_host |
| 100 |
@gqtp_port = (ENV["gqtp_port"] || @default_gqtp_port).to_i |
| 101 |
@gqtp_running = File.exist?(ENV["gqtp_pid_path"] || @default_gqtp_pid_path) |
| 102 |
end |
| 103 |
|
| 104 |
def setup |
| 105 |
setup_service_type |
| 106 |
setup_http |
| 107 |
setup_httpd |
| 108 |
setup_gqtp |
| 109 |
end |
| 110 |
|
| 111 |
def autoconf |
| 112 |
results = [] |
| 113 |
|
| 114 |
if @http_host and @http_port and @http_running |
| 115 |
results << run_http(@http_host, @http_port, "status") |
| 116 |
end |
| 117 |
|
| 118 |
if @httpd_host and @httpd_port and @httpd_running |
| 119 |
results << run_http(@httpd_host, @httpd_port, "status") |
| 120 |
end |
| 121 |
|
| 122 |
if @gqtp_host and @gqtp_port and @gqtp_running |
| 123 |
results << run_gqtp(@gqtp_host, @gqtp_port, "status") |
| 124 |
end |
| 125 |
|
| 126 |
if results.any? {|success, _| success} |
| 127 |
puts("yes") |
| 128 |
exit(true) |
| 129 |
else |
| 130 |
errors = results.collect do |_, result| |
| 131 |
result |
| 132 |
end |
| 133 |
error_detail = errors.join(", ") |
| 134 |
puts("no (#{error_detail})") |
| 135 |
exit(false) |
| 136 |
end |
| 137 |
end |
| 138 |
|
| 139 |
def suggest |
| 140 |
if @http_host and @http_port and @http_running |
| 141 |
success, _ = run_http(@http_host, @http_port, "status") |
| 142 |
puts("http") if success |
| 143 |
end |
| 144 |
if @httpd_host and @httpd_port and @httpd_running |
| 145 |
success, _ = run_http(@httpd_host, @httpd_port, "status") |
| 146 |
puts("httpd") if success |
| 147 |
end |
| 148 |
if @gqtp_host and @gqtp_port and @gqtp_running |
| 149 |
success, _ = run_gqtp(@gqtp_host, @gqtp_port, "status") |
| 150 |
puts("gqtp") if success |
| 151 |
end |
| 152 |
exit(true) |
| 153 |
end |
| 154 |
|
| 155 |
setup |
| 156 |
case command |
| 157 |
when "autoconf", "detect" |
| 158 |
autoconf |
| 159 |
when "suggest" |
| 160 |
suggest |
| 161 |
when "config" |
| 162 |
if label.nil? |
| 163 |
title = "groonga: status" |
| 164 |
else |
| 165 |
title = "groonga: #{label}: status" |
| 166 |
end |
| 167 |
puts(<<EOF) |
| 168 |
graph_title #{title} |
| 169 |
graph_vlabel status |
| 170 |
graph_category groonga |
| 171 |
graph_info groonga status |
| 172 |
|
| 173 |
alloc_count.label alloc count |
| 174 |
alloc_count.type GAUGE |
| 175 |
EOF |
| 176 |
exit(true) |
| 177 |
end |
| 178 |
|
| 179 |
success, body = run("status") |
| 180 |
unless success |
| 181 |
puts("error: #{body}") |
| 182 |
exit(false) |
| 183 |
end |
| 184 |
puts(<<EOF) |
| 185 |
alloc_count.value #{body["alloc_count"]} |
| 186 |
EOF |