summaryrefslogtreecommitdiff
path: root/storage/mroonga/vendor/groonga/data/munin/groonga_throughput_
blob: 241ae8e579494e0ee284d28f16a0d3faf3150f4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/env ruby

#%# family=auto
#%# capabilities=autoconf suggest

require 'net/http'
begin
  require 'json'
rescue LoadError
  require 'rubygems'
  require 'json'
end

label = ENV["label"]
@groonga = ENV["groonga"] || "groonga"
@default_host = ENV["host"] || "127.0.0.1"
@default_http_port = 10041
@default_gqtp_port = 10043

command = ARGV.shift

def parse(success, result)
  if success
    begin
      status, body = JSON.parse(result)
      return_code, start_time, elapsed, error_message = status
      if return_code.zero?
        [success, body]
      else
        [false, error_message]
      end
    rescue JSON::ParserError
      [false, $!.message]
    end
  else
    [success, result]
  end
end

def run_http(host, port, command)
  path = "/d/#{command}"
  response = Net::HTTP.start(host, port) do |http|
    http.get(path)
  end
  if response.is_a?(Net::HTTPSuccess)
    parse(true, response.body)
  else
    [false, "#{response.code}: #{response.body}"]
  end
end

def run_gqtp(host, port, command)
  groonga = "#{@groonga} -p #{port} -c #{host}"
  result = `#{groonga} #{command} 2>&1`
  parse($?.success?, result)
end

def run(command)
  case @service_type
  when "http"
    run_http(@http_host, @http_port, command)
  when "httpd"
    run_http(@httpd_host, @httpd_port, command)
  when "gqtp"
    run_gqtp(@gqtp_host, @gqtp_port, command)
  else
    [false, "unknown service type: #{@service_type}"]
  end
end

def setup_service_type
  if /_([^_]+)\z/ =~ $0
    @service_type = $1
  else
    @service_type = nil
  end
end

def setup_http
  @http_host = ENV["http_host"] || @default_host
  @http_port = (ENV["http_port"] || @default_http_port).to_i
end

def setup_httpd
  @httpd_host = ENV["httpd_host"] || @default_host
  @httpd_port = (ENV["httpd_port"] || @default_http_port).to_i
end

def setup_gqtp
  @gqtp_host = ENV["gqtp_host"] || @default_host
  @gqtp_port = (ENV["gqtp_port"] || @default_gqtp_port).to_i
end

def setup
  setup_service_type
  setup_http
  setup_httpd
  setup_gqtp
end

def autoconf
  results = []

  if @http_host and @http_port
    results << run_http(@http_host, @http_port, "status")
  end

  if @httpd_host and @httpd_port
    results << run_http(@httpd_host, @httpd_port, "status")
  end

  if @gqtp_host and @gqtp_port
    results << run_gqtp(@gqtp_host, @gqtp_port, "status")
  end

  if results.any? {|success, _| success}
    puts("yes")
    exit(true)
  else
    errors = results.collect do |_, result|
      result
    end
    error_detail = errors.join(", ")
    puts("no (#{error_detail})")
    exit(false)
  end
end

def suggest
  if @http_host and @http_port
    success, _ = run_http(@http_host, @http_port, "status")
    puts("http") if success
  end
  if @httpd_host and @httpd_port
    success, _ = run_http(@httpd_host, @httpd_port, "status")
    puts("httpd") if success
  end
  if @gqtp_host and @gqtp_port
    success, _ = run_gqtp(@gqtp_host, @gqtp_port, "status")
    puts("gqtp") if success
  end
  exit(true)
end

setup
case command
when "autoconf", "detect"
  autoconf
when "suggest"
  suggest
when "config"
  if label.nil?
    title = "groonga: throughput"
  else
    title = "groonga: #{label}: throughput"
  end
  puts(<<EOF)
graph_title #{title}
graph_vlabel queries per ${graph_period}
graph_category groonga
graph_info groonga throughput

n_queries.label N queries
n_queries.type COUNTER
EOF
  exit(true)
end

success, body = run("status")
unless success
  puts("error: #{body}")
  exit(false)
end
puts(<<EOF)
n_queries.value #{body["n_queries"]}
EOF