我正在为此而努力。已经有2天我觉得我已经尝试了至少六打组合试图完成这个子程序。
问题: 子程序是有用的,但是在键“nas”下的哈希输出中可以看到应该列出2个磁盘,但只有一个出现,我已经验证了磁盘实际列在文件中,我大约98%肯定我的正则表达式能够匹配名为“nas_1.vmdk”的第二个磁盘。第一个被称为“nas.vmdk”以供参考。
这就是我的输出;
$VAR1 = {
'server02' => {
'nas' => {
'SCSI0' => {
'Disk1' => 'nas.vmdk'
}
}
},
'server01' => {
'NS02' => {
'SCSI0' => {
'Disk0' => 'NS02.vmdk'
}
},
'MX01' => {
'SCSI0' => {
'Disk0' => 'MX01.vmdk'
}
},
'NS01' => {
'SCSI0' => {
'Disk0' => 'NS01.vmdk'
}
},
'SQL01' => {
'SCSI0' => {
'Disk0' => 'SQL01.vmdk'
}
}
}
};
就像我说VM“nas”应该有一个disk0和一个disk1但只有disk1出现,这让我相信disk0的值会被disk1的值覆盖,然后才能写出来哈希,但我不确定它出错的地方。
我的代码:
sub discovery_find_snapshots {
foreach my $server_label (keys %virtual_machines) {
# Set the current server address to esx_host
my $esx_host = $config_file{$server_label}{address};
# Create a hash for storing the options needed by Net::OpenSSH
my %ssh_options = (
port => $config_file{$server_label}{port},
user => $config_file{$server_label}{user},
password => $config_file{$server_label}{password}
);
# Print the name of the server currently being discovered
print "Discovering snapshots for $esx_host\n";
# Create a new Net::OpenSSH object
my $ssh = Net::OpenSSH->new($esx_host, %ssh_options);
foreach my $vm (keys %{$virtual_machines{$server_label}}) {
# Create new scalars for ide port, ide disk, vmdk file and vmdk location
my $snapshot0_adapter_number;
my $snapshot0_disk_number;
my $snapshot0_file;
my $vmsd_source = $ssh->capture("cat $virtual_machines{$server_label}{$vm}{VMSD}");
if (! $vmsd_source eq '') {
while ($vmsd_source =~ m/^snapshot0\.disk([0-9]|1[0-5])\.node\s+=\s+"scsi(?<ADAPTER>[0-3])\:(?<DISK>([0-9]|1[0-5]))"/xmg) {
$snapshot0_adapter_number = "$+{ADAPTER}";
$snapshot0_disk_number = "$+{DISK}";
if ($vmsd_source =~ m/^snapshot0\.disk([0-9|1[0-5])\.fileName\s+=\s+"(?<VMDK_FILE>[^"]+?\.vmdk)"/xm) {
$snapshot0_file = "$+{VMDK_FILE}";
}
$vmsd_disks{$server_label}{$vm}{"SCSI$snapshot0_adapter_number"}{"Disk$snapshot0_disk_number"} = "$snapshot0_file";
}
} else {
print "$vm, no snapshot metadata found\n";
}
}
}
debug();
}
子程序“debug”只是一个额外的子程序,我允许我集中使用Data :: Dumper。
是的,我差点忘了。这是我想要匹配的信息。snapshot.lastUID = "1"
snapshot.numSnapshots = "1"
snapshot.current = "1"
snapshot0.uid = "1"
snapshot0.filename = "nas-Snapshot1.vmsn"
snapshot0.displayName = "Milestone - Test"
snapshot0.description = "Test for my script"
snapshot0.type = "1"
snapshot0.createTimeHigh = "301769"
snapshot0.createTimeLow = "-826832731"
snapshot0.numDisks = "2"
snapshot0.disk0.fileName = "nas.vmdk"
snapshot0.disk0.node = "scsi0:0"
snapshot0.disk1.fileName = "nas_1.vmdk"
snapshot0.disk1.node = "scsi0:1"
答案 0 :(得分:2)
在while循环中,将磁盘编号保存在变量中,并在以下正则表达式中使用它(在if中),如:
while ($vmsd_source =~ m/^snapshot0\.disk([0-9]|1[0-5])\.node\s+=\s+"scsi(?<ADAPTER>[0-3])\:(?<DISK>([0-9]|1[0-5]))"/xmg) {
my $disk = $1; # save the disk number
$snapshot0_adapter_number = "$+{ADAPTER}";
$snapshot0_disk_number = "$+{DISK}";
if ($vmsd_source =~ m/^snapshot0\.disk$disk\.fileName\s+=\s+"(?<VMDK_FILE>[^"]+?\.vmdk)"/xm) {
## ^ reuse the saved disk number
$snapshot0_file = "$+{VMDK_FILE}";
}
$vmsd_disks{$server_label}{$vm}{"SCSI$snapshot0_adapter_number"}{"Disk$snapshot0_disk_number"} = "$snapshot0_file";
}