Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env ruby
- require 'yaml'
- require 'nokogiri'
- require 'bigdecimal'
- require 'optparse'
- options = {}
- optparse = OptionParser.new do |opts|
- opts.banner = "Usage: rrd.rb -f rrd_file [options]"
- opts.on('-f', '--file [FILE]', 'RRD File') do |f|
- options[:file] = f
- end
- opts.on('-g', '--removegaps' ) { |g| options[:remove_gaps] = true}
- end
- optparse.parse!
- # Raise an exception when an rrd file is not specified
- if options[:file].nil?
- puts 'No rrd file specified'
- exit 1
- elsif ooptions[:remove_gaps].nil?
- puts 'Please specify an action'
- exit 1
- end
- def extract_rra_rows(cf:, pdp_per_row:)
- @doc.css('rra').select do |x|
- if (x.css('cf')[0].inner_text == "#{cf}" && x.css('pdp_per_row')[0].inner_text == "#{pdp_per_row}")
- rra_avg_rows = x.css('database>row')
- # TODO: require timeframe which will then be used to return
- # the rows within the given timeframe
- # Currently, return rows for the last 7 days
- # 464 = 800 - 336 (336 * 30m = 10080 / 60 = 168h / 24 = 7d)
- yield rra_avg_rows[464, 799]
- end
- end
- end
- def remove_gaps(rows)
- for i in 0..(rows.size-1)
- break if i == rows.size - 1
- current_bits_in = BigDecimal(rows[i].css('v')[0].inner_text)
- if current_bits_in == 'NaN'
- puts "#{rows[i].css('v')[0].content}"
- if BigDecimal(rows[i-1].css('v')[0].inner_text) != 'NaN'
- rows[i].css('v')[0].content = BigDecimal(rows[i-1].css('v')[0].inner_text)
- elsif
- rows[i].css('v')[0].content = BigDecimal(rows[i-2].css('v')[0].inner_text)
- end
- end
- current_bits_out = BigDecimal(rows[i].css('v')[1].inner_text)
- if current_bits_out == 'NaN'
- if BigDecimal(rows[i-1].css('v')[1].inner_text) != 'NaN'
- rows[i].css('v')[1].content = BigDecimal(rows[i-1].css('v')[1].inner_text)
- elsif
- rows[i].css('v')[1].content = BigDecimal(rows[i-2].css('v')[1].inner_text)
- end
- end
- end
- end
- dump_xml = File.join(__dir__, 'ixp.xml')
- `rrdtool dump #{options[:file]} #{dump_xml}`
- @doc = Nokogiri::XML(open(dump_xml))
- extract_rra_rows(cf: 'AVERAGE', pdp_per_row: '6') do |rows|
- remove_gaps(rows)
- end
- extract_rra_rows(cf: 'MAX', pdp_per_row: '6') do |rows|
- remove_gaps(rows)
- end
- File.write(dump_xml, @doc)
- `rrdtool restore #{dump_xml} ixp_normalized.rrd`
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement