null+****@clear*****
null+****@clear*****
2011年 11月 17日 (木) 16:00:42 JST
Kouhei Sutou 2011-11-17 07:00:42 +0000 (Thu, 17 Nov 2011) New Revision: 158db02f9ec5e0130d9e49fffee7fa21636a0352 Log: [munin] add a Munin plugin to measure throughput. refs #1171 Added files: data/munin/groonga_throughput Modified files: data/munin/Makefile.am Modified: data/munin/Makefile.am (+2 -1) =================================================================== --- data/munin/Makefile.am 2011-11-17 04:57:02 +0000 (34d72ac) +++ data/munin/Makefile.am 2011-11-17 07:00:42 +0000 (8fb5382) @@ -7,5 +7,6 @@ dist_munin_plugins_SCRIPTS = \ groonga_memory \ groonga_n_records \ groonga_query_performance \ - groonga_disk + groonga_disk \ + groonga_throughput endif Added: data/munin/groonga_throughput (+102 -0) 100755 =================================================================== --- /dev/null +++ data/munin/groonga_throughput 2011-11-17 07:00:42 +0000 (30d8bd8) @@ -0,0 +1,102 @@ +#!/usr/bin/env ruby + +#%# family=auto +#%# capabilities=autoconf + +begin + require 'json' +rescue LoadError + require 'rubygems' + require 'json' +end + +label = ENV["label"] + @ groonga = ENV["groonga"] || "groonga" + @ host = ENV["host"] || "localhost" + @ port = ENV["port"] || 10041 + @ protocol = ENV["protocol"] || "gqtp" + +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(command, *args) + if @protocol == "http" + require "net/http" + response = Net::HTTP.start(@host, @port) do |http| + http.get("/d/status.json") + end + if response.is_a?(Net::HTTPSuccess) + parse(true, response.body) + else + parse(false, "#{response.code}: #{response.body}") + end + else + groonga = "#{@groonga} -p #{@port} -c #{@host}" + result = `#{groonga} #{command} #{args.join(' ')} 2>&1` + parse($?.success?, result) + end +end + +def parse_list(header, list) + list.collect do |item| + parsed_item = {} + header.each_with_index do |(name, type), i| + parsed_item[name] = item[i] + end + parsed_item + end +end + +case command +when "autoconf", "detect" + success, body = run("status") + if success + puts "yes" + exit(true) + else + puts "no (#{body})" + exit(false) + end +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