summaryrefslogtreecommitdiff
path: root/storage/mroonga/vendor/groonga/data/munin/groonga_cpu_time_
blob: 5c60c17db5587bc307492945ed20850d1453166b (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
#!/usr/bin/env ruby

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

label = ENV["label"]

command = ARGV.shift

def autoconf
  pid_path_variable_names = [
    "pid_path",
    "pid_file", # For backward compatibility. Remove me when 5.0.0.
    "http_pid_path",
    "httpd_pid_path",
    "gqtp_pid_path",
  ]
  pid_paths = pid_path_variable_names.collect do |variable_name|
    ENV[variable_name]
  end
  pid_paths = pid_paths.compact
  if pid_paths.empty?
    variable_names = pid_path_variable_names.collect do |name|
      "env.#{name}"
    end
    variable_names_label = variable_names[0..-2].join(", ")
    variable_names_label << " and/or #{variable_names.last}"
    puts("no (No PID path is specified. Specify #{variable_names_label}.)")
    exit(false)
  end
  pid_paths.each do |pid_path|
    next unless File.exist?(pid_path)
    puts("yes")
    exit(true)
  end
  pid_paths_label = pid_paths.join(", ")
  puts("no (All PID paths don't exist: #{pid_paths_label})")
  exit(false)
end

def suggest
  exist_p = lambda do |variable_name|
    pid_path = ENV[variable_name]
    pid_path and File.exist?(pid_path)
  end
  if exist_p.call("http_pid_path")
    puts("http")
  end
  if exist_p.call("httpd_pid_path")
    puts("httpd")
  end
  if exist_p.call("gqtp_pid_path")
    puts("gqtp")
  end
  exit(true)
end

def target_pid_path
  if /_([^_]+)\z/ =~ $0
    service_type = $1
    pid_path_variable_name = "#{service_type}_pid_path"
    pid_path = ENV[pid_path_variable_name]
  else
    pid_path_variable_name = "pid_path"
    pid_path = ENV[pid_path_variable_name]
    # For backward compatibility. Remove me when 5.0.0.
    pid_path ||= ENV["pid_file"]
  end

  if pid_path.nil?
    $stderr.puts("PID path isn't specified by env.#{pid_path_variable_name}")
    exit(false)
  end

  unless File.exist?(pid_path)
    $stderr.puts("PID path doesn't exist: #{pid_path}")
    exit(false)
  end

  pid_path
end

case command
when "autoconf", "detect"
  autoconf
when "suggest"
  suggest
when "config"
  if label
    title = "groonga: #{label}: CPU time"
  else
    title = "groonga: CPU time"
  end
  puts(<<EOF)
graph_title #{title}
graph_vlabel CPU time (days)
graph_category groonga
graph_info groonga CPU time

cpu_time.label CPU time
cpu_time.type GAUGE
EOF
  exit(true)
end

groonga_pid = File.read(target_pid_path).strip
time = `ps h -o time -p #{groonga_pid}`.chomp
if /\A(?:(\d+)-)?(\d+):(\d+):(\d+)\z/ =~ time
  day, hours, minutes, seconds, = $1, $2, $3, $4
  day = (day || 0).to_i
  hours = hours.to_i
  minutes = minutes.to_i
  seconds = seconds.to_i
  time_in_seconds = seconds + minutes * 60 + hours * 60 * 60
  day_in_seconds = 60 * 60 * 24
  fraction_in_day = time_in_seconds.to_f / day_in_seconds.to_f
  cpu_time_in_day = day + fraction_in_day
  puts("cpu_time.value #{cpu_time_in_day}")
else
  $stderr.puts("invalid time format: <#{time}>")
  exit(false)
end