Debian Codegraph @

data/munin/groonga_delta_

raw
1 #!/usr/bin/env ruby
2
3 #%# family=auto
4 #%# capabilities=autoconf suggest
5
6 require "find"
7 require "yaml"
8
9 label = ENV["label"]
10
11 command = ARGV.shift
12 type = File.basename($0).gsub(/\Agroonga_delta_/, "")
13
14 def apply_status_path
15 ENV["apply_status_path"]
16 end
17
18 def import_status_path
19 ENV["import_status_path"]
20 end
21
22 def delta_dir
23 ENV["delta_dir"]
24 end
25
26 def autoconf
27 invalid_messages = []
28 path = apply_status_path
29 if path and not File.exist?(path)
30 invalid_messages << "apply_status_path doesn't exist: #{path}"
31 end
32 path = import_status_path
33 if path and not File.exist?(path)
34 invalid_messages << "import_status_path doesn't exist: #{path}"
35 end
36 path = delta_dir
37 if path and not File.directory?(path)
38 invalid_messages << "delta_dir isn't directory: #{path}"
39 end
40 unless invalid_messages.empty?
41 message = invalid_messages.join(", ")
42 puts("no (#{message})")
43 exit(false)
44 end
45 puts("yes")
46 exit(true)
47 end
48
49 def suggest
50 path = apply_status_path
51 if path and File.exist?(path)
52 puts("apply_local")
53 end
54 path = import_status_path
55 if path and File.exist?(path)
56 status = YAML.load(File.read(path))
57 if status.key?("local")
58 puts("import_local")
59 end
60 if status.key?("mysql")
61 puts("import_mysql")
62 end
63 end
64 path = delta_dir
65 if path and File.directory?(path)
66 puts("storage")
67 end
68 exit(true)
69 end
70
71 case command
72 when "autoconf", "detect"
73 autoconf
74 when "suggest"
75 suggest
76 when "config"
77 title = "groonga-delta"
78 title << ": #{label}" if label
79 case type
80 when "apply_local"
81 puts(<<-CONFIG)
82 graph_title #{title}: apply local delta
83 graph_vlabel The last applied ID
84 graph_category groonga-delta
85 graph_info groonga-delta apply local delta
86
87 id.label ID
88 id.type GAUGE
89 CONFIG
90 when "import_local"
91 puts(<<-CONFIG)
92 graph_title #{title}: import local source
93 graph_vlabel The last imported ID
94 graph_category groonga-delta
95 graph_info groonga-delta import local source
96
97 id.label ID
98 id.type GAUGE
99 CONFIG
100 when "import_mysql"
101 puts(<<-CONFIG)
102 graph_title #{title}: import MySQL source
103 graph_vlabel Location
104 graph_category groonga-delta
105 graph_info groonga-delta import MySQL source
106
107 last_event.label Last event location
108 last_event.type GAUGE
109 last_table_map_event.label Last table map event location
110 last_table_map_event.type GAUGE
111 CONFIG
112 when "storage"
113 puts(<<-CONFIG)
114 graph_title #{title}: storage usage
115 graph_vlabel Bytes
116 graph_category groonga-delta
117 graph_info groonga-delta storage usage
118
119 schema.label Schema
120 schema.type GAUGE
121 CONFIG
122 Dir.glob(File.join(delta_dir, "data", "*")) do |table_dir|
123 next unless File.directory?(table_dir)
124 name = File.basename(table_dir)
125 key = "table_#{name}"
126 puts("#{key}.label #{name}")
127 puts("#{key}.type GAUGE")
128 end
129 end
130 exit(true)
131 end
132
133 case type
134 when "apply_local"
135 status = YAML.load(File.read(apply_status_path))
136 sec, nsec = start_time = status.dig("start_time") || [0, 0]
137 puts("id.value #{sec}.#{nsec}")
138 when "import_local"
139 status = YAML.load(File.read(import_status_path))
140 id = status.dig("local", "number") || 0
141 puts("id.value #{id}")
142 when "import_mysql"
143 status = YAML.load(File.read(import_status_path))
144 def pack_location(file, position)
145 index = file.split(".").last.to_i(10)
146 index + "0.#{position}".to_f
147 end
148 last_event = pack_location(status.dig("mysql", "last_file") || "",
149 status.dig("mysql", "last_position") || 0)
150 last_table_map_event =
151 pack_location(status.dig("mysql", "last_table_map_file") || "",
152 status.dig("mysql", "last_table_map_position") || 0)
153 puts("last_event.value #{last_event}")
154 puts("last_table_map_event.value #{last_table_map_event}")
155 when "storage"
156 def compute_storage_usage(dir)
157 return 0 unless File.exist?(dir)
158 usage = 0
159 Dir.glob(File.join(dir, "**", "*")) do |path|
160 next unless File.file?(path)
161 usage += File.size(path)
162 end
163 usage
164 end
165 schema_storage_usage = compute_storage_usage(File.join(delta_dir, "schema"))
166 puts("schema.value #{schema_storage_usage}")
167 Dir.glob(File.join(delta_dir, "data", "*")) do |table_dir|
168 next unless File.directory?(table_dir)
169 usage = compute_storage_usage(table_dir)
170 key = "table_#{File.basename(table_dir)}"
171 puts("#{key}.value #{usage}")
172 end
173 end