Advertisement
Guest User

Untitled

a guest
Mar 30th, 2020
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2.  
  3. require 'yaml'
  4. require 'nokogiri'
  5. require 'bigdecimal'
  6. require 'optparse'
  7.  
  8. options = {}
  9. optparse = OptionParser.new do |opts|
  10. opts.banner = "Usage: rrd.rb -f rrd_file [options]"
  11.  
  12. opts.on('-f', '--file [FILE]', 'RRD File') do |f|
  13. options[:file] = f
  14. end
  15. opts.on('-g', '--removegaps' ) { |g| options[:remove_gaps] = true}
  16. end
  17.  
  18. optparse.parse!
  19.  
  20. # Raise an exception when an rrd file is not specified
  21. if options[:file].nil?
  22. puts 'No rrd file specified'
  23. exit 1
  24. elsif ooptions[:remove_gaps].nil?
  25. puts 'Please specify an action'
  26. exit 1
  27. end
  28.  
  29. def extract_rra_rows(cf:, pdp_per_row:)
  30. @doc.css('rra').select do |x|
  31. if (x.css('cf')[0].inner_text == "#{cf}" && x.css('pdp_per_row')[0].inner_text == "#{pdp_per_row}")
  32. rra_avg_rows = x.css('database>row')
  33. # TODO: require timeframe which will then be used to return
  34. # the rows within the given timeframe
  35. # Currently, return rows for the last 7 days
  36. # 464 = 800 - 336 (336 * 30m = 10080 / 60 = 168h / 24 = 7d)
  37. yield rra_avg_rows[464, 799]
  38. end
  39. end
  40. end
  41.  
  42. def remove_gaps(rows)
  43. for i in 0..(rows.size-1)
  44. break if i == rows.size - 1
  45.  
  46. current_bits_in = BigDecimal(rows[i].css('v')[0].inner_text)
  47.  
  48. if current_bits_in == 'NaN'
  49. puts "#{rows[i].css('v')[0].content}"
  50. if BigDecimal(rows[i-1].css('v')[0].inner_text) != 'NaN'
  51. rows[i].css('v')[0].content = BigDecimal(rows[i-1].css('v')[0].inner_text)
  52. elsif
  53. rows[i].css('v')[0].content = BigDecimal(rows[i-2].css('v')[0].inner_text)
  54. end
  55. end
  56.  
  57. current_bits_out = BigDecimal(rows[i].css('v')[1].inner_text)
  58.  
  59. if current_bits_out == 'NaN'
  60. if BigDecimal(rows[i-1].css('v')[1].inner_text) != 'NaN'
  61. rows[i].css('v')[1].content = BigDecimal(rows[i-1].css('v')[1].inner_text)
  62. elsif
  63. rows[i].css('v')[1].content = BigDecimal(rows[i-2].css('v')[1].inner_text)
  64. end
  65. end
  66. end
  67. end
  68.  
  69. dump_xml = File.join(__dir__, 'ixp.xml')
  70.  
  71. `rrdtool dump #{options[:file]} #{dump_xml}`
  72.  
  73. @doc = Nokogiri::XML(open(dump_xml))
  74.  
  75. extract_rra_rows(cf: 'AVERAGE', pdp_per_row: '6') do |rows|
  76. remove_gaps(rows)
  77. end
  78.  
  79. extract_rra_rows(cf: 'MAX', pdp_per_row: '6') do |rows|
  80. remove_gaps(rows)
  81. end
  82.  
  83. File.write(dump_xml, @doc)
  84.  
  85. `rrdtool restore #{dump_xml} ixp_normalized.rrd`
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement