summaryrefslogtreecommitdiff
path: root/storage/mroonga/vendor/groonga/data/munin/groonga_disk_
blob: c65aad52b2b7b5571e25b0f3d283373127b86525 (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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#!/usr/bin/env ruby

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

require 'shellwords'
begin
  require 'json'
rescue LoadError
  require 'rubygems'
  require 'json'
end
require 'English'

label = ENV["label"]
@groonga = ENV["groonga"] || "groonga"
@du = ENV["du"] || "du"

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)
  database_path = Shellwords.shellescape(@database_path)
  result = `#{@groonga} #{database_path} #{command} #{args.join(' ')} 2>&1`
  parse($?.success?, result)
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

def schema
  tables = []
  success, table_list_body = run("table_list")
  unless success
    puts("error: #{table_list_body}")
    exit(false)
  end
  parse_list(table_list_body[0], table_list_body[1..-1]).each do |table|
    table_name = table["name"]
    table["key"] = "table_#{table_name}"
    success, column_list_body = run("column_list", table_name)
    unless success
      puts("error: #{column_list_body}")
      exit(false)
    end
    table["columns"] = parse_list(column_list_body[0], column_list_body[1..-1])
    table["columns"].each do |column|
      column["key"] = "column_#{table_name}_#{column['name']}"
      column["full_name"] = "#{table_name}.#{column['name']}"
    end
    tables << table
  end
  tables
end

def parse_du_result(result)
  usages = {}
  result.each_line do |line|
    if /\A(\d+)\s+/ =~ line
      usage = $1
      path = $POSTMATCH.strip
      usages[path] = usage.to_i
    end
  end
  usages
end

def compute_size(usages, base_path)
  usage = 0
  return usage if base_path.empty?

  usages.each do |path, size|
    usage += size if path.start_with?(base_path)
  end
  usage
end

def setup_database_path
  if /_([^_]+)\z/ =~ $0
    service_type = $1
    database_path_variable_name = "#{service_type}_database_path"
    database_path = ENV[database_path_variable_name]
  else
    database_path_variable_name = "database_path"
    database_path = ENV[database_path_variable_name]
    # For backward compatibility. Remove me when 5.0.0.
    database_path ||= ENV["path"]
  end

  if database_path.nil?
    key = "env.#{database_path_variable_name}"
    $stderr.puts("Database path isn't specified by #{key}")
    exit(false)
  end

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

  @database_path = database_path
end

def autoconf
  database_path_variable_names = [
    "database_path",
    "path", # For backward compatibility. Remove me when 5.0.0.
    "http_database_path",
    "httpd_database_path",
    "gqtp_database_path",
  ]
  database_paths = database_path_variable_names.collect do |variable_name|
    ENV[variable_name]
  end
  database_paths = database_paths.compact
  if database_paths.empty?
    variable_names = database_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 database path is specified. Specify #{variable_names_label}.)")
    exit(false)
  end
  database_paths.each do |database_path|
    next unless File.exist?(database_path)
    puts("yes")
    exit(true)
  end
  database_paths_label = database_paths.join(", ")
  puts("no (All database paths don't exist: #{database_paths_label})")
  exit(false)
end

def suggest
  exist_p = lambda do |variable_name|
    database_path = ENV[variable_name]
    database_path and File.exist?(database_path)
  end
  if exist_p.call("http_database_path")
    puts("http")
  end
  if exist_p.call("httpd_database_path")
    puts("httpd")
  end
  if exist_p.call("gqtp_database_path")
    puts("gqtp")
  end
  exit(true)
end

case command
when "autoconf", "detect"
  autoconf
when "suggest"
  suggest
when "config"
  setup_database_path
  if label.nil?
    title = "groonga: disk usage"
  else
    title = "groonga: #{label}: disk usage"
  end
  puts(<<EOF)
graph_title #{title}
graph_vlabel Bytes
graph_category groonga
graph_info disk usage in groonga tables and columns
graph_args --base 1024
graph_total Total

database.label Database
database.draw AREA
EOF
  schema.each do |table|
    table_key = table["key"]
    table_name = table["name"]
    puts(<<EOF)

#{table_key}.label #{table_name}
#{table_key}.draw STACK
EOF
    table["columns"].each do |column|
      column_key = column["key"]
      column_name = column["full_name"]
      puts(<<EOF)

#{column_key}.label #{column_name}
#{column_key}.draw STACK
EOF
    end
  end
  exit(true)
end

setup_database_path
database_path = Shellwords.shellescape(@database_path)
du_result = `#{@du} -B1 #{database_path}*`
unless $?.success?
  $stderr.puts("error: #{du_result}")
  exit(false)
end
usages = parse_du_result(du_result)
usage = compute_size(usages, @database_path)
puts(<<EOF)
database.value #{usage}
EOF
schema.each do |table|
  table_key = table["key"]
  table_name = table["name"]
  usage = compute_size(usages, table["path"])
  puts(<<EOF)
#{table_key}.value #{usage}
EOF
  table["columns"].each do |column|
    column_key = column["key"]
    usage = compute_size(usages, column["path"])
    puts(<<EOF)
#{column_key}.value #{usage}
EOF
  end
end