Skip to content

Instantly share code, notes, and snippets.

@jmelis
Last active April 18, 2016 09:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmelis/d41526b1fdf87f4942387b83e6754b08 to your computer and use it in GitHub Desktop.
Save jmelis/d41526b1fdf87f4942387b83e6754b08 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
ONE_LOCATION=ENV["ONE_LOCATION"]
if !ONE_LOCATION
RUBY_LIB_LOCATION="/usr/lib/one/ruby"
else
RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby"
end
$: << RUBY_LIB_LOCATION
require 'opennebula'
require 'ipaddr'
require 'set'
require 'pp'
require 'pry'
include OpenNebula
def get_ar(net_id, ar_id)
vnet = VirtualNetwork.new_with_id(net_id, @client)
rc = vnet.info
if OpenNebula.is_error?(rc)
STDERR.puts rc.message
exit 1
end
ar = nil
vnet.each("AR_POOL/AR[AR_ID='#{ar_id}']"){|e| ar = e}
ar
end
@client = Client.new
pool = VirtualNetworkPool.new(@client, -1)
rc = pool.info
if OpenNebula.is_error?(rc)
STDERR.puts rc.message
exit 1
end
errors = []
pool.each do |vnet_from_list|
vnet_id = vnet_from_list['ID']
vnet = VirtualNetwork.new_with_id(vnet_id.to_i, @client)
rc = vnet.info
if OpenNebula.is_error?(rc)
STDERR.puts rc.message
exit 1
end
parent_network_id = vnet['PARENT_NETWORK_ID']
vnet.each('AR_POOL/AR') do |ar|
ar_id = ar['AR_ID']
ip = IPAddr.new(ar['IP'])
size = ar['SIZE'].to_i
ip_end = ip.to_i + size
if ar['SIZE'].to_i < ar['USED_LEASES'].to_i
errors << "SIZE != USED_LEASES: VNET #{vnet_id}, AR #{ar_id}"
end
ar.each('LEASES/LEASE') do |l|
if (IPAddr.new(l['IP']).to_i > ip_end)
errors << "LEASE out of AR Range: Parent VNET #{vnet_id}, AR #{ar_id}, VNET #{l['VNET']}"
end
end
parent_ar_id = ar['PARENT_NETWORK_AR_ID']
if !parent_network_id.nil? && !parent_network_id.empty?
parent_ar = get_ar(parent_network_id.to_i, parent_ar_id.to_i)
parent_ip = IPAddr.new(parent_ar['IP'])
parent_size = parent_ar['SIZE'].to_i
parent_ip_end = parent_ip.to_i + parent_size
if ip < parent_ip || ip > parent_ip_end || ip_end > parent_ip_end
errors << "IP/SIZE out of Parent's Range: Parent VNET #{parent_network_id}, AR #{parent_ar_id}, Child NET #{vnet_id}, AR #{ar_id}"
end
end
end
end
errors.uniq.each {|e| puts e}
#!/usr/bin/env ruby
ONE_LOCATION=ENV["ONE_LOCATION"]
if !ONE_LOCATION
RUBY_LIB_LOCATION="/usr/lib/one/ruby"
else
RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby"
end
$: << RUBY_LIB_LOCATION
require 'opennebula'
require 'ipaddr'
require 'set'
require 'pp'
include OpenNebula
@client = Client.new
pool = VirtualNetworkPool.new(@client, -1)
rc = pool.info
if OpenNebula.is_error?(rc)
STDERR.puts rc.message
exit 1
end
def get_ar(net_id, ar_id)
vnet = VirtualNetwork.new_with_id(net_id, @client)
rc = vnet.info
if OpenNebula.is_error?(rc)
STDERR.puts rc.message
exit 1
end
ar = nil
vnet.each("AR_POOL/AR[AR_ID='#{ar_id}']"){|e| ar = e}
ar
end
orange = Set.new
pool.each do |vnet|
vnet_id = vnet['ID']
parent_network_id = vnet['PARENT_NETWORK_ID']
next if parent_network_id.nil? || parent_network_id.empty?
vnet.each('AR_POOL/AR') do |ar|
ar_id = ar['AR_ID']
parent_ar_id = ar['PARENT_NETWORK_AR_ID']
parent_ar = get_ar(parent_network_id.to_i, parent_ar_id.to_i)
ip = IPAddr.new(ar['IP'])
size = ar['SIZE'].to_i
ip_end = ip.to_i + size
parent_ip = IPAddr.new(parent_ar['IP'])
parent_size = parent_ar['SIZE'].to_i
parent_ip_end = parent_ip.to_i + parent_size
if ip < parent_ip || ip > parent_ip_end || ip_end > parent_ip_end
puts "NET #{vnet_id} AR #{ar_id} / IP: #{ip} / SIZE: #{size} **PARENT** NET #{parent_network_id} AR #{parent_ar_id} / IP: #{parent_ip} / SIZE: #{parent_size}"
end
end
end
# puts
# if !orange.empty?
# puts "Wrong VNETS: "
# orange.each{|n| puts "- #{n}"}
# else
# puts "No wrong VNETS found."
# end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment