配置文件config:
vd0
adapter 0
fd0 0:0
fd1 0:1
fd2 0:2
raid 5
vd1
adapter 1
fd0 1:0
fd1 1:1
fd2 1:2
raid 6
脚本1 a.pl
#!/usr/bin/perl
use strict;
use warnings;
my $conf = "config";
my $vdref;
my $index;
my $count;
open(FH,"<",$conf) or die "$!\n";
while()
{
chomp;
if(/vd(\d+)/)
{
$index = $1;
$count = 0;
}
if(/(adapter)\s+(\d+)/)
{
$vdref->{$index}->{$1} = $2;
}
if(/(fd\d+)\s+(.*)/)
{
$vdref->{$index}->{$count}=$2;
$count++;
}
$vdref->{$index}->{'cnt'} = $count;
if(/(raid)\s+(\d)+/)
{
$vdref->{$index}->{$1} = $2;
}
}
close(FH);
foreach my $external_key (sort keys %$vdref)
{
my $raid_arg;
my $raid_arg = "-r".$vdref->{$external_key}->{'raid'}." [";
my $temp;
my @array;
for(my $i = 0; $i < $vdref->{$external_key}->{'cnt'};$i++)
{
push @array,$vdref->{$external_key}->{$i};
$temp.=$vdref->{$external_key}->{$i};
}
#print "|---$temp---|\n";
my $ret = join ",",@array;
#print "|---$ret---|\n";
my $ret_ex = $ret."] -a".$vdref->{$external_key}->{'adapter'};
print "MegaCli -cfgldadd $raid_arg$ret_ex \n";
}
脚本2 b.pl
#!/usr/bin/perl
use strict;
use warnings;
my $conf = "config";
my($index,$adapter, $raid);
my $fd = "";
my @aRaid;
open(FH,"<",$conf) or die "$!\n";
while(){
chomp;
if(/vd(\d+)/) {
$index = $1;
}
if(/adapter\s+(\d+)/) {
$adapter = $1;
}
if(/fd\d+\s+(.*)/) {
$fd .= "$1,";
print "|---$fd---|\n"
}
if(/raid\s+(\d)+/) {
$raid = $1;
$fd =~ s/,$//;
print "<$fd>\n";
push @aRaid, "megacli -cfgladd -r$raid [$fd] -a$adapter";
$fd = "";
}
}
close(FH);
foreach (@aRaid){
print "$_\n";
}
脚本3 c.pl
#!/usr/bin/perl
use strict;
use warnings;
my $rh_cfg = {};
my $index;
while()
{
chomp;
next if /^\s*$/;
$index = $1 if /vd(\d+)$/;
my ($key,$value) = split /\s+/,$_;
$rh_cfg->{$index}->{$key} = $value if defined $key and defined $value;
}
for my $index (sort keys %$rh_cfg)
{
print "|---$index---|\n";
while (my($key,$value)=each(%{$rh_cfg->{$index}}))
{
print "$key\t$value\n";
}
my @list;
@list = grep {/:/} sort values %{$rh_cfg->{$index}};
foreach (@list)
{
print "|+++$_+++|\n";
}
my $square_bracket = join ",",grep{/:/} sort values %{$rh_cfg->{$index}};
my $last_arg = '-a'.$rh_cfg->{$index}->{adapter};
my $cmd = 'megacli -cfgldadd -r'.$rh_cfg->{$index}->{raid}.' ['.$square_bracket.']'." $last_arg";
print "$cmd\n";
}
__DATA__
vd0
adapter 0
fd0 0:0
fd1 0:1
fd2 0:2
raid 5
vd1
adapter 1
fd0 1:0
fd1 1:1
fd2 1:2
raid 6
方法1和3用的思路基本差不多,用了哈希的数据结构,方法2比较简单,直接正则匹配,进行字符串处理。
阅读(647) | 评论(0) | 转发(0) |