Skip to content

Instantly share code, notes, and snippets.

@chirauki
Created February 21, 2013 16:59
Show Gist options
  • Save chirauki/5006214 to your computer and use it in GitHub Desktop.
Save chirauki/5006214 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
#
# Author: Sergio Pena
#
# yum -y install mysql-devel
# gem install mysql
# gem install rest-client
# gem install logger
# gem install xml-simple
require 'rubygems'
require 'mysql'
require 'restclient'
require 'logger'
require 'xmlsimple'
$log = Logger.new(STDOUT)
$log.level = Logger::INFO
#$log.level = Logger::DEBUG
#
# Generate list of VMs from Nodecollector
query_nodecollectors = "select idDatacenter,uri from remote_service where remoteServiceType = 'NODE_COLLECTOR';"
query_hypervisors = "select h.ip,h.user,h.password,h.type from hypervisor h, physicalmachine pm where h.idPhysicalMachine = pm.idPhysicalmachine and pm.idDatacenter = "
con = Mysql.new('localhost', 'root', '', 'kinton')
nc = con.query(query_nodecollectors)
$log.debug "Launch query #{query_nodecollectors}"
nc_list = Hash.new
vm_list_from_nc = Array.new()
vm_list_from_hyp = Array.new()
nc.each_hash do |nc|
$log.info "Testing datacenter #{nc['idDatacenter']}"
nc_list[nc['idDatacenter']] = Hash.new
#
# Hypervisor
nc_hyp = con.query(query_hypervisors+nc['idDatacenter'])
nc_hyp.each_hash do |nc_hyp|
nc_list[nc['idDatacenter']][nc_hyp['ip']] = Hash.new
url_nc_hyp = "#{nc['uri']}/#{nc_hyp['ip']}/virtualsystem?hyp=#{nc_hyp['type']}&user=#{nc_hyp['user']}&passwd=#{nc_hyp['password']}"
$log.debug "Trying to retrieve: #{url_nc_hyp}"
data = nil
begin
response = RestClient.get(url_nc_hyp)
$log.debug "Response from restclient: #{response}"
data = XmlSimple.xml_in(response)
$log.debug "Parsed xml data: #{data}"
rescue => e
e.response
end
if data.respond_to?("each")
if data['virtualSystems'].respond_to?("each")
nc_list_tmp = Array.new()
data['virtualSystems'].each do |x|
tmpsys = "#{x['name']}|#{x['uuid']}|#{x['status']}|#{x['cpu']}|#{x['ram']}"
$log.info "TMPSYS: #{tmpsys}"
nc_list_tmp.push(tmpsys)
end
nc_list[nc['idDatacenter']][nc_hyp['ip']] = nc_list_tmp
$log.info "According NC => VMs in hypervisor #{nc_hyp['ip']} = #{nc_list_tmp.length}"
end
end
end
end
#$log.info "NODECOLLECTOR: #{nc_list}"
#
# Generate list from mysql
my_list = Hash.new
my_dc = con.query('select idDatacenter from datacenter;')
my_dc.each_hash do |x|
my_list[x['idDatacenter']] = Hash.new
my_hypervisors = con.query("select h.ip,h.id from hypervisor h, physicalmachine pm where h.idPhysicalmachine = pm.idPhysicalmachine and pm.idDatacenter = #{x['idDatacenter']}")
my_hypervisors.each_hash do |h|
my_list[x['idDatacenter']][h['ip']] = Hash.new
my_vms = con.query("select name,uuid as uuid, vdrpPort as vport, ram<<20 as ram, cpu, state from virtualmachine where idHypervisor = #{h['id']}")
my_list_tmp = Array.new()
my_vms.each_hash do |vm|
# my_list[x['idDatacenter']][h['ip']]['name'].push(vm['name'])
# my_list[x['idDatacenter']][h['ip']]['uuid'].push(vm['uuid'])
# my_list[x['idDatacenter']][h['ip']]['vport'].push(vm['vport'])
# my_list[x['idDatacenter']][h['ip']]['ram'].push(vm['ram'])
# my_list[x['idDatacenter']][h['ip']]['cpu'].push(vm['cpu'])
# my_list[x['idDatacenter']][h['ip']]['state'].push(vm['state'])
tmpsys = "#{vm['name']}|#{vm['uuid']}|#{vm['state']}|#{vm['cpu']}|#{vm['ram']}"
$log.info "FROM DB: #{tmpsys}"
my_list_tmp.push(tmpsys)
end
#my_list[x['idDatacenter']][h['ip']] = { "vmarray" => vm_list_from_hyp }
my_list[x['idDatacenter']][h['ip']] = my_list_tmp
$log.info "According DB => VMs in hypervisor #{h['ip']} = #{my_list_tmp.length}"
end
end
vm_list_from_hyp.clear
vm_list_from_nc.clear
#
# FIND vms in NC output on DB
# FOR each VM in NC output info, will check if DB matches
#
nc_list_keys = nc_list.keys
nc_list_keys.each do |datacenterid|
$log.info "NC -- Entering DC #{datacenterid}"
dc_hypervisors = nc_list[datacenterid].keys
dc_hypervisors.each do |hypervisor_ip|
$log.info "Analysing hypervisor #{hypervisor_ip} from NC"
ncc_hyp_list = my_list[datacenterid][hypervisor_ip]
ncc_nc_list = nc_list[datacenterid][hypervisor_ip]
ncc_hyp_list.sort
ncc_nc_list.sort
ncc_nc_list.each do |vm_nc_record|
if ncc_hyp_list.include? vm_nc_record
name, uuid, state, cpu, ram = vm_nc_record.split("|")
$log.info "VM #{name} matches NC and DB (state #{state}, cpu #{cpu}, ram #{ram})"
else
name, uuid, state = vm_nc_record.split("|")
$log.info "VM #{name} from NC running in #{hypervisor_ip} and missing in DB hypervisor #{hypervisor_ip}"
end
end
end
end
vm_list_from_hyp.clear
vm_list_from_nc.clear
#
# FIND vms in DB output on NC
# FOR each VM in DB query info, will check if NC matches
#
my_list_keys = my_list.keys
my_list_keys.each do |datacenterid|
$log.info "DB -- Entering DC #{datacenterid}"
dc_hypervisors = my_list[datacenterid].keys
dc_hypervisors.each do |hypervisor_ip|
$log.info "Analysing hypervisor #{hypervisor_ip} from DB"
dbc_db_list = my_list[datacenterid][hypervisor_ip]
dbc_nc_list = nc_list[datacenterid][hypervisor_ip]
dbc_db_list.sort
dbc_nc_list.sort
dbc_db_list.each do |vm_db_record|
if dbc_nc_list.include? vm_db_record
name, uuid, state, cpu, ram = vm_db_record.split("|")
$log.info "VM #{name} matches NC and DB (state #{state}, cpu #{cpu}, ram #{ram})"
else
name, uuid, state = vm_db_record.split("|")
$log.info "VM #{name} from DB running in #{hypervisor_ip} and missing in NC hypervisor #{hypervisor_ip}"
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment