| 1 |
#!/usr/bin/env ruby |
| 2 |
|
| 3 |
#%# family=auto |
| 4 |
#%# capabilities=autoconf suggest |
| 5 |
|
| 6 |
label = ENV["label"] |
| 7 |
|
| 8 |
command = ARGV.shift |
| 9 |
|
| 10 |
def autoconf |
| 11 |
pid_path_variable_names = [ |
| 12 |
"pid_path", |
| 13 |
"pid_file", # For backward compatibility. Remove me when 5.0.0. |
| 14 |
"http_pid_path", |
| 15 |
"httpd_pid_path", |
| 16 |
"gqtp_pid_path", |
| 17 |
] |
| 18 |
pid_paths = pid_path_variable_names.collect do |variable_name| |
| 19 |
ENV[variable_name] |
| 20 |
end |
| 21 |
pid_paths = pid_paths.compact |
| 22 |
if pid_paths.empty? |
| 23 |
variable_names = pid_path_variable_names.collect do |name| |
| 24 |
"env.#{name}" |
| 25 |
end |
| 26 |
variable_names_label = variable_names[0..-2].join(", ") |
| 27 |
variable_names_label << " and/or #{variable_names.last}" |
| 28 |
puts("no (No PID path is specified. Specify #{variable_names_label}.)") |
| 29 |
exit(false) |
| 30 |
end |
| 31 |
pid_paths.each do |pid_path| |
| 32 |
next unless File.exist?(pid_path) |
| 33 |
puts("yes") |
| 34 |
exit(true) |
| 35 |
end |
| 36 |
pid_paths_label = pid_paths.join(", ") |
| 37 |
puts("no (All PID paths don't exist: #{pid_paths_label})") |
| 38 |
exit(false) |
| 39 |
end |
| 40 |
|
| 41 |
def suggest |
| 42 |
exist_p = lambda do |variable_name| |
| 43 |
pid_path = ENV[variable_name] |
| 44 |
pid_path and File.exist?(pid_path) |
| 45 |
end |
| 46 |
if exist_p.call("http_pid_path") |
| 47 |
puts("http") |
| 48 |
end |
| 49 |
if exist_p.call("httpd_pid_path") |
| 50 |
puts("httpd") |
| 51 |
end |
| 52 |
if exist_p.call("gqtp_pid_path") |
| 53 |
puts("gqtp") |
| 54 |
end |
| 55 |
exit(true) |
| 56 |
end |
| 57 |
|
| 58 |
def target_pid_path |
| 59 |
if /_([^_]+)\z/ =~ $0 |
| 60 |
service_type = $1 |
| 61 |
pid_path_variable_name = "#{service_type}_pid_path" |
| 62 |
pid_path = ENV[pid_path_variable_name] |
| 63 |
else |
| 64 |
pid_path_variable_name = "pid_path" |
| 65 |
pid_path = ENV[pid_path_variable_name] |
| 66 |
# For backward compatibility. Remove me when 5.0.0. |
| 67 |
pid_path ||= ENV["pid_file"] |
| 68 |
end |
| 69 |
|
| 70 |
if pid_path.nil? |
| 71 |
$stderr.puts("PID path isn't specified by env.#{pid_path_variable_name}") |
| 72 |
exit(false) |
| 73 |
end |
| 74 |
|
| 75 |
unless File.exist?(pid_path) |
| 76 |
$stderr.puts("PID path doesn't exist: #{pid_path}") |
| 77 |
exit(false) |
| 78 |
end |
| 79 |
|
| 80 |
pid_path |
| 81 |
end |
| 82 |
|
| 83 |
case command |
| 84 |
when "autoconf", "detect" |
| 85 |
autoconf |
| 86 |
when "suggest" |
| 87 |
suggest |
| 88 |
when "config" |
| 89 |
if label |
| 90 |
title = "groonga: #{label}: CPU time" |
| 91 |
else |
| 92 |
title = "groonga: CPU time" |
| 93 |
end |
| 94 |
puts(<<EOF) |
| 95 |
graph_title #{title} |
| 96 |
graph_vlabel CPU time (days) |
| 97 |
graph_category groonga |
| 98 |
graph_info groonga CPU time |
| 99 |
|
| 100 |
cpu_time.label CPU time |
| 101 |
cpu_time.type GAUGE |
| 102 |
EOF |
| 103 |
exit(true) |
| 104 |
end |
| 105 |
|
| 106 |
groonga_pid = File.read(target_pid_path).strip |
| 107 |
time = `ps h -o time -p #{groonga_pid}`.chomp |
| 108 |
if /\A(?:(\d+)-)?(\d+):(\d+):(\d+)\z/ =~ time |
| 109 |
day, hours, minutes, seconds, = $1, $2, $3, $4 |
| 110 |
day = (day || 0).to_i |
| 111 |
hours = hours.to_i |
| 112 |
minutes = minutes.to_i |
| 113 |
seconds = seconds.to_i |
| 114 |
time_in_seconds = seconds + minutes * 60 + hours * 60 * 60 |
| 115 |
day_in_seconds = 60 * 60 * 24 |
| 116 |
fraction_in_day = time_in_seconds.to_f / day_in_seconds.to_f |
| 117 |
cpu_time_in_day = day + fraction_in_day |
| 118 |
puts("cpu_time.value #{cpu_time_in_day}") |
| 119 |
else |
| 120 |
$stderr.puts("invalid time format: <#{time}>") |
| 121 |
exit(false) |
| 122 |
end |