Debian Codegraph @

data/munin/groonga_n_records_

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 #!/usr/bin/ruby
2
3 #%# family=auto
4 #%# capabilities=autoconf suggest
5
6 require 'shellwords'
7 require 'net/http'
8 require 'erb'
9 begin
10 require 'json'
11 rescue LoadError
12 require 'rubygems'
13 require 'json'
14 end
15
16 label = ENV["label"]
17 @groonga = ENV["groonga"] || "groonga"
18 @default_host = ENV["host"] || "127.0.0.1"
19 @default_http_port = 10041
20 @default_gqtp_port = 10043
21 @default_http_pid_path = "/run/groonga/groonga-http.pid"
22 @default_httpd_pid_path = "/run/groonga/groonga-httpd.pid"
23 @default_gqtp_pid_path = "/run/groonga/groonga-gptp.pid"
24 @database_path = nil
25
26 command = ARGV.shift
27
28 def parse(success, result)
29 if success
30 begin
31 status, body = JSON.parse(result)
32 return_code, start_time, elapsed, error_message = status
33 if return_code.zero?
34 [success, body]
35 else
36 [false, error_message]
37 end
38 rescue JSON::ParserError
39 [false, "#{$!.message}: #{result}"]
40 end
41 else
42 [success, result]
43 end
44 end
45
46 def run_http(host, port, command, *args)
47 path = "/d/#{command}"
48 unless args.empty?
49 parameters = args.each_slice(2).collect do |key, value|
50 key = key.gsub(/\A--/, "")
51 "#{ERB::Util.u(key)}=#{ERB::Util.u(value)}"
52 end
53 path << "?" << parameters.join("&")
54 end
55 begin
56 response = Net::HTTP.start(host, port) do |http|
57 http.get(path)
58 end
59 rescue SystemCallError, TimeoutError
60 return [false, "#{$!.class}: #{$!}"]
61 end
62 if response.is_a?(Net::HTTPSuccess)
63 parse(true, response.body)
64 else
65 [false, "#{response.code}: #{response.body}"]
66 end
67 end
68
69 def run_gqtp(host, port, command, *args)
70 groonga = "#{@groonga} -p #{port} -c #{host}"
71 result = `#{groonga} #{command} #{args.join(' ')} 2>&1`
72 parse($?.success?, result)
73 end
74
75 def run_command_line(database_path, command, *args)
76 database_path = Shellwords.shellescape(database_path)
77 result = `#{@groonga} #{database_path} #{command} #{args.join(' ')} 2>&1`
78 parse($?.success?, result)
79 end
80
81 def run(command, *args)
82 case @service_type
83 when "http"
84 run_http(@http_host, @http_port, command, *args)
85 when "httpd"
86 run_http(@httpd_host, @httpd_port, command, *args)
87 when "gqtp"
88 run_gqtp(@gqtp_host, @gqtp_port, command, *args)
89 else
90 run_command_line(@database_path, command, *args)
91 end
92 end
93
94 def exclude_tables
95 case @service_type
96 when "http"
97 @http_exclude_tables
98 when "httpd"
99 @httpd_exclude_tables
100 when "gqtp"
101 @gqtp_exclude_tables
102 else
103 @exclude_tables
104 end
105 end
106
107 def parse_list(header, list)
108 list.collect do |item|
109 parsed_item = {}
110 header.each_with_index do |(name, type), i|
111 parsed_item[name] = item[i]
112 end
113 parsed_item
114 end
115 end
116
117 def table_list
118 success, body = run("table_list")
119 unless success
120 puts("error: #{body}")
121 exit(false)
122 end
123 tables = parse_list(body[0], body[1..-1])
124 tables.reject do |table|
125 name = table["name"]
126 exclude_tables.include?(name)
127 end
128 end
129
130 def setup_service_type
131 if /_([^_]+)\z/ =~ $0
132 @service_type = $1
133 else
134 @service_type = nil
135 end
136 end
137
138 def parse_exclude_tables(raw_exclude_tables_value)
139 (raw_exclude_tables_value || "").split(/\s*,\s*/)
140 end
141
142 def setup_command_line
143 @database_path = ENV["database_path"]
144 # For backward compatibility. Remove me when 5.0.0.
145 @database_path ||= ENV["path"]
146 @exclude_tables = parse_exclude_tables(ENV["exclude_tables"])
147 end
148
149 def setup_http
150 @http_host = ENV["http_host"] || @default_host
151 @http_port = (ENV["http_port"] || @default_http_port).to_i
152 @http_running = File.exist?(ENV["http_pid_path"] || @default_http_pid_path)
153 @http_exclude_tables = parse_exclude_tables(ENV["http_exclude_tables"])
154 end
155
156 def setup_httpd
157 @httpd_host = ENV["httpd_host"] || @default_host
158 @httpd_port = (ENV["httpd_port"] || @default_http_port).to_i
159 @httpd_running = File.exist?(ENV["httpd_pid_path"] || @default_httpd_pid_path)
160 @httpd_exclude_tables = parse_exclude_tables(ENV["httpd_exclude_tables"])
161 end
162
163 def setup_gqtp
164 @gqtp_host = ENV["gqtp_host"] || @default_host
165 @gqtp_port = (ENV["gqtp_port"] || @default_gqtp_port).to_i
166 @gqtp_running = File.exist?(ENV["gqtp_pid_path"]) || @default_gqtp_pid_path
167 @gqtp_exclude_tables = parse_exclude_tables(ENV["gqtp_exclude_tables"])
168 end
169
170 def setup
171 setup_service_type
172 setup_command_line
173 setup_http
174 setup_httpd
175 setup_gqtp
176 end
177
178 def autoconf
179 results = []
180
181 if @database_path and File.exist?(@database_path)
182 results << run_command_line(@database_path, "status")
183 end
184
185 if @http_host and @http_port and @http_running
186 results << run_http(@http_host, @http_port, "status")
187 end
188
189 if @httpd_host and @httpd_port and @httpd_running
190 results << run_http(@httpd_host, @httpd_port, "status")
191 end
192
193 if @gqtp_host and @gqtp_port and @gqtp_running
194 results << run_gqtp(@gqtp_host, @gqtp_port, "status")
195 end
196
197 if results.any? {|success, _| success}
198 puts("yes")
199 exit(true)
200 else
201 errors = results.collect do |_, result|
202 result
203 end
204 error_detail = errors.join(", ")
205 puts("no (#{error_detail})")
206 exit(false)
207 end
208 end
209
210 def suggest
211 if @http_host and @http_port and @http_running
212 success, _ = run_http(@http_host, @http_port, "status")
213 puts("http") if success
214 end
215 if @httpd_host and @httpd_port and @httpd_running
216 success, _ = run_http(@httpd_host, @httpd_port, "status")
217 puts("httpd") if success
218 end
219 if @gqtp_host and @gqtp_port and @gqtp_running
220 success, _ = run_gqtp(@gqtp_host, @gqtp_port, "status")
221 puts("gqtp") if success
222 end
223 exit(true)
224 end
225
226 setup
227 case command
228 when "autoconf", "detect"
229 autoconf
230 when "suggest"
231 suggest
232 when "config"
233 if label.nil?
234 title = "groonga: number of records"
235 else
236 title = "groonga: #{label}: number of records"
237 end
238 puts(<<EOF)
239 graph_title #{title}
240 graph_vlabel records
241 graph_category groonga
242 graph_info number of records in groonga table
243 EOF
244 table_list.each do |table|
245 name = table["name"]
246 puts(<<EOF)
247
248 #{name}.label #{name}
249 #{name}.type GAUGE
250 EOF
251 end
252 exit(true)
253 end
254
255 table_list.each do |table|
256 name = table["name"]
257 success, body = run("select", "--table", name, "--limit", "0")
258 unless success
259 puts("error: #{body}")
260 exit(false)
261 end
262 n_records = body[0][0][0]
263 puts(<<EOF)
264 #{name}.value #{n_records}
265 EOF
266 end