Skip to content

Instantly share code, notes, and snippets.

@csabahenk
Last active August 30, 2017 22:42
Show Gist options
  • Save csabahenk/b483b0caa7b4848c1a3175d337dd359b to your computer and use it in GitHub Desktop.
Save csabahenk/b483b0caa7b4848c1a3175d337dd359b to your computer and use it in GitHub Desktop.
GlusterFS state dump parsing snippet
#!/usr/bin/env ruby
require 'yaml'
require 'time'
ARRSEP = /^(-----=-----)?$/
HEAD = /^\[(.*)\]$/
d0 = [[]]
d1 = {}
$<.each { |l|
l = l.strip
if l =~ /^(DUMP-(?:START|END)-TIME):\s+(.*)/
d1["_misc"]||={}
(d1["_misc"]["dates"]||={})[$1] = Time.parse $2
next
end
if l =~ HEAD
d0 << [l]
next
end
d0.last << l
}
d0.each { |s|
arraylike = false
s.grep(/\S/).empty? and next
head = nil
while s.last =~ /^\s*$/
s.pop
end
body = catch { |misc2|
s[0] =~ HEAD ? (head = $1) : (throw misc2)
body = [[]]
s[1..-1].each { |l|
if l =~ ARRSEP
arraylike = true
body << []
next
end
body.last << l
}
body.reject(&:empty?).map { |e|
ea = e.map { |l|
k,v = l.split("=",2)
[k, begin
Integer v
rescue ArgumentError
begin
Float v
rescue ArgumentError
v
end
end]
}
begin
ea.to_h
rescue
throw misc2
end
}
}
if body
(d1[head]||=[]) << (arraylike ? body : body[0])
else
d1["_misc"]||={}
(d1["_misc"]["misc"]||=[]) << s
end
}
puts d1.to_yaml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment