Skip to content

Instantly share code, notes, and snippets.

@sergiopena
Created December 12, 2012 10:37
Show Gist options
  • Save sergiopena/4266778 to your computer and use it in GitHub Desktop.
Save sergiopena/4266778 to your computer and use it in GitHub Desktop.
Populates initiator_mapping table with all possibles initiatorIqn values.
#!/usr/bin/perl -w
#
# Author: Sergio Pena
# Abiquo 12.11.2012
#
# This script is intended to avoid problems generated by lack of
# manage initiator mapping when performing vmotions on vms.
#
# http://jira.abiquo.com/browse/ABICLOUDPREMIUM-4381
#
# For every vm with attached volumes, it will add to
# initiator_mapping table entries for every hypervisor on
# that datacenter.
#
# Only allocated VMs will be taken into account
#
# First exec
# real 6m14.493s
# user 0m17.825s
# sys 0m0.820s
#
# Second execution without modify
# real 1m2.436s
# user 0m59.744s
# sys 0m1.136s
#
#
use strict;
use warnings;
use DBI;
use Getopt::Long;
use Data::Dumper;
# setup the default options
my $dbhost = 'localhost';
my $dbport = 3306;
my $dbuser = 'root';
my $dbpass = '';
my $help;
# database handlers
my $dsn;
my $dbh;
my $sth;
my $ref;
GetOptions(
'dbhost=s' => \$dbhost,
'dbport=i' => \$dbport,
'dbuser=s' => \$dbuser,
'dbpass=s' => \$dbpass,
'help!' => \$help,
) or die "Incorrect ussage\n";
if ($help) {
print 'Usage:';
}
$dsn = "DBI:mysql:database=kinton:host=$dbhost:port=$dbport";
$dbh = DBI->connect($dsn, $dbuser, $dbpass);
#
# Check premises to allow this script to be executed safely
# We suppose that for every idManagement there's only one targetIqn and targetLun
# as we use any value of targetIqn and targetLun matching idMgmt row to populate the table
# We MUST check that tuple (idManagement,targetIqn,targetLun) is unique
#
$sth = $dbh->prepare("
SELECT idManagement,targetLun,targetIqn
FROM initiator_mapping
GROUP BY idManagement
HAVING
COUNT(DISTINCT(targetlun)) > 1 OR
COUNT(DISTINCT(targetiqn)) > 1 ;");
$sth->execute();
if ($sth->rows != 0 ){
print Dumper($sth->fetchall_hashref('idManagement'));
die "ERROR Cannot populate table, inconsistences found!\n";
}
#
# Check for duplicated entries
#
$sth = $dbh->prepare("
SELECT idManagement, initiatorIqn
FROM initiator_mapping
GROUP BY idManagement , initiatorIqn
HAVING
COUNT(*) > 1;");
$sth->execute();
if ($sth->rows != 0 ){
print Dumper($sth->fetchall_hashref('idManagement'));
die "ERROR Cannot populate table, duplicated found!\n";
}
$sth = $dbh->prepare("SELECT rm.idManagement,rm.idVM,pm.idPhysicalMachine, pm.idDatacenter,pm.initiatoriqn from rasd_management rm, virtualmachine vm, hypervisor h, physicalmachine pm where rm.idVM = vm.idVM and vm.idHypervisor = h.id and h.idPhysicalMachine = pm.idPhysicalMachine and vm.state not like 'NOT_ALLOCATED' and vm.state not like 'LOCKED' and rm.idVM is not null and rm.idResourceType = 8;");
$sth->execute();
my $resources = $sth->fetchall_hashref('idManagement');
$sth = $dbh->prepare("select idPhysicalMachine, initiatoriqn, idDatacenter from physicalmachine;");
$sth->execute();
my $physicalmachines = $sth->fetchall_hashref('idPhysicalMachine');
$sth = $dbh->prepare("select idInitiatorMapping, idManagement, initiatorIqn, targetIqn, targetLun, version_c from initiator_mapping;");
$sth->execute();
my $initiators = $sth->fetchall_hashref('idInitiatorMapping');
# MEGALOOP!!!
my $targetLun; # Save existent targetLun for a given idManagement from initiator_mappping table
my $targetIqn; # Save existent targetIqn for a given idManagement from initiator_mappping table
my $initiatorExists; # FLAG
foreach my $idManagement (keys $resources){
print "Checking volume with resource $idManagement\n";
print " Used by VM $resources->{$idManagement}->{idVM}";
print " Hosted by $resources->{$idManagement}->{idPhysicalMachine}\n";
print " Allocated on datacenter $resources->{$idManagement}->{idDatacenter}\n";
foreach my $idPhysicalMachine ( keys $physicalmachines){
$initiatorExists = 0;
print " Checking PM $idPhysicalMachine\n";
# for each PM test if the initiator for the other PMs on the same datacenter exist on database
if ( $physicalmachines->{$idPhysicalMachine}->{idDatacenter} == $resources->{$idManagement}->{idDatacenter}) {
print " is located on the same datacenter\n";
foreach my $initiator ( keys $initiators ){
if ( $initiators->{$initiator}->{idManagement} == $idManagement ){
print " $initiator is an initiator for the same volume $idManagement!\n";
if ( $initiators->{$initiator}->{initiatorIqn} eq $physicalmachines->{$idPhysicalMachine}->{initiatoriqn} ) {
print " actually is the same initiator :) EXISTS!!!!!\n";
$initiatorExists = 1;
}
$targetIqn = $initiators->{$initiator}->{targetIqn};
$targetLun = $initiators->{$initiator}->{targetLun};
print " gathering target $targetIqn $targetLun\n";
}
}
if ( $initiatorExists == 0 ){
$sth = $dbh->prepare("INSERT INTO initiator_mapping (idManagement, initiatorIqn, targetIqn, targetLun) values (?,?,?,?);");
$sth->execute($idManagement, $physicalmachines->{$idPhysicalMachine}->{initiatoriqn},$targetIqn,$targetLun);
print " Inserting $idManagement, $physicalmachines->{$idPhysicalMachine}->{initiatoriqn}, $targetIqn, $targetLun\n";
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment