#!/usr/bin/perl # IBM(c) 2007 EPL license http://www.eclipse.org/legal/epl-v10.html # ##################################################### # # This script will read the CSM database and build # xCAT stanza files to be input into an xCAT database # using the chdef command # example node.stanza | chdef -z # ##################################################### use strict; use warnings; use Getopt::Long; use Data::Dumper; my $needhelp = 0; my $directory = "/tmp/csm2xcat"; if ( !GetOptions("help" => \$needhelp, "dir=s" => \$directory,) ) { &usage; exit 1; } if ($needhelp) { &usage; exit 0; } #Create the users choice directory mkdir $directory unless -d $directory; # create a log open(LOG, ">$directory/conversion.log") or die "Can't open logfile for writing: $!"; &log_this("Conversion started at " . scalar(localtime())); # build the stanza files for the site table &getSiteinfo; # build the stanza files for the node &getNodeinfo; # build the stanza files for the devices &getDevinfo; log_this("Conversion finished at " . scalar(localtime())); print "Conversion finished, please carefully review $directory/conversion.log and check results in the $directory stanza files before using chdef -z to update the database!\n"; close(LOG); exit 0; # end main # # logger # sub log_this { print LOG join('', @_), "\n"; } # # write to stanza files # sub write_stanza { print STANZA @_; } # # runs csmconfig or uses the csmconfig.output file # and builds site table stanza file # sub getSiteinfo { log_this("Reading Site information\n"); print "Running csmconfg to read site info!\n"; # open the site stanza file my $stanzafile = "$directory/site.stanza"; open(STANZA, ">$stanzafile") or die "Can't open $stanzafile for writing: $!"; write_stanza("# \n\n"); write_stanza("clustersite:\n"); write_stanza(" objtype=site\n"); my @results; my $cmd; # use of file is for debug my $csmconfiginfofile = "$directory/csmconfigdebug.output"; if (-e $csmconfiginfofile) { # use the imported file $cmd = "cat $csmconfiginfofile"; log_this("Reading $csmconfiginfofile information\n"); @results = runcmd($cmd); } else { # run the command $cmd = "/opt/csm/bin/csmconfig"; @results = runcmd($cmd); } if ($::RUNCMD_RC != 0) { my $msg = "Error processing csmconfig information\n"; log_this($msg); print "$msg"; close(STANZA); return; } foreach my $line (@results) { my $xcatline; $line =~ s/\s*//g; #remove extra blanks my ($attr, $value) = split(/=/, $line); if ($attr eq "RemoteShell") { if ($value) { $xcatline = " rsh="; $xcatline .= "$value\n"; } } if ($attr eq "RemoteCopyCmd") { if ($value) { $xcatline = " rcp="; $xcatline .= "$value\n"; } } if ($xcatline) { write_stanza($xcatline); } } close(STANZA); return 0; } # # runs lsnodes -l and build node stanza file # sub getNodeinfo { # open the node stanza file my $stanzafile = "$directory/node.stanza"; open(STANZA, ">$stanzafile") or die "Can't open $stanzafile for writing: $!"; write_stanza("# $stanzafile") or die "Can't open $stanzafile for writing: $!"; write_stanza("# { # attribute=>value # attribute=>value # . # # sub buildNodehash { my ($info) = @_; my @nodeinfo = @$info; my %nodehash; my $nodename; foreach my $line (@nodeinfo) { $line =~ s/\s*//g; #remove extra blanks my ($attr, $value) = split(/=/, $line); if (($attr eq "Hostname") || ($attr eq "Name")) { $nodename = $value; # set the hash key } else { # all other lines are part of hash $nodehash{$nodename}{$attr} = $value; } } return \%nodehash; } # # runs the input command and handles the errors. Returns the output # sub runcmd { my ($cmd) = @_; my $rc = 0; $::RUNCMD_RC = 0; my $outref = []; @$outref = `$cmd`; if ($?) { $rc = $? ; $::RUNCMD_RC = $rc; if ($rc > 0) { my $msg = "$cmd returned rc=$rc @$outref\n"; log_this($msg); print "$msg"; } } chomp(@$outref); return @$outref; } sub usage { print "CSM database to xCAT stanza files migration utility.\n"; print "Reads the CSM database and creates xCAT stanza files that \ncan be imported using the chdef command into the xCAT database.\n"; print "Usage:\n"; print "\t--help - usage\n"; print "\t--directory - output directory for stanza files.\n Default is /tmp/csm2xcat.\n"; print "\n"; return; }