Skip to content

Instantly share code, notes, and snippets.

@hungryblank
Created May 10, 2012 17:05
Show Gist options
  • Save hungryblank/2654484 to your computer and use it in GitHub Desktop.
Save hungryblank/2654484 to your computer and use it in GitHub Desktop.
cleanup spikes from rrdtool xml dumps
require 'rubygems'
require 'nokogiri'
class RRow
attr_reader :exponent, :coefficient, :xml, :value
def initialize(xml)
@xml = xml
@value_xml = xml.children.first
@value = @value_xml.text
@coefficient, @exponent = @value.strip.split(/e\+|-/)
@sign = @value.match(/e([+\-])/)[1] if @exponent
if @coefficient == 'NaN'
@exponent = nil
else
@exponent = @exponent.to_i
@exponent = - @exponent if @sign == '-'
@coefficient = @coefficient.to_f
end
end
def number
return 0 unless exponent
coefficient * 10 ** exponent
end
def <=>(other)
number <=> other.number
end
def nullify
@value_xml.content = " NaN "
end
end
class Rra
attr_reader :xml
def initialize(xml)
@xml = xml
end
def values
@values ||= xml.search('value')
end
def rows
@rows ||= xml.search('v').map { |r| RRow.new(r) }.sort.reverse
end
def anomaly_index
max_exp = nil
max_exp = ARGV[1].to_i if ARGV[1]
position = nil
candidates = rows
candidates.each_with_index do |candidate, index|
if max_exp && candidate.exponent && candidate.exponent >= max_exp
position = index
next
end
next_exponent = candidates[index + 1] ? candidates[index + 1].exponent : nil
next if !(next_exponent.is_a?(Numeric)) || !candidate.exponent.is_a?(Numeric)
position = index if (candidate.exponent - next_exponent >= 2)
end
position
end
def normalize
return nil unless anomaly_index
anomalies = rows[0..anomaly_index]
p anomalies.map { |a| a.value }
#sane = rows[anomaly_index + 1].value
#to_replace = anomalies.first.value
anomalies.each { |a| a.nullify }
#[to_replace, sane]
end
end
to_clean = File.open(ARGV[0]) { |f| f.read }
document = Nokogiri::XML.parse(to_clean)
databases = document.search('rra')
replacements = databases.map { |rra| results = Rra.new(rra).normalize }.compact
xml = document.to_xml
#replacements.each do |replacement|
# xml.gsub!(replacement[0], replacement[1])
#end
File.open('/tmp/xml_foo', 'w') do |f|
f.puts xml
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment