我有一个名为的变量DISK_INFO
,其内容如下:
diskid HGST HUSMH8010BSS204 serial no no [0] Slot00
diskid HGST HUH728080AL4204 serial no no [0] Slot02
diskid HGST HUH728080AL4204 serial no no [0] Slot03
diskid HGST HUH728080AL4204 serial no no [0] Slot04
diskid HGST HUH728080AL4204 serial no no [0] Slot05
diskid HGST HUH728080AL4204 serial no no [0] Slot06
diskid HGST HUH728080AL4204 serial no no [0] Slot07
diskid HGST HUH728080AL4204 serial no no [0] Slot08
diskid HGST HUH728080AL4204 serial no no [0] Slot09
diskid HGST HUH728080AL4204 serial no no [0] Slot10
diskid HGST HUH728080AL4204 serial no no [0] Slot11
diskid HGST HUH728080AL4204 serial no no [0] Slot12
diskid HGST HUH728080AL4204 serial no no [0] Slot13
diskid HGST HUH728080AL4204 serial no no [0] Slot14
diskid HGST HUH728080AL4204 serial no no [0] Slot15
diskid HGST HUH728080AL4204 serial no no [0] Slot16
diskid HGST HUH728080AL4204 serial no no [0] Slot17
diskid HGST HUH728080AL4204 serial no no [0] Slot18
diskid HGST HUH728080AL4204 serial no no [0] Slot19
diskid HGST HUH728080AL4204 serial no no [0] Slot20
diskid HGST HUH728080AL4204 serial no no [0] Slot21
diskid HGST HUH728080AL4204 serial no no [0] Slot22
diskid HGST HUH728080AL4204 serial no no [0] Slot23
diskid HGST HUH728080AL4204 serial no no [1] Slot00
diskid HGST HUH728080AL4204 serial no no [1] Slot01
diskid HGST HUH728080AL4204 serial no no [1] Slot02
diskid HGST HUH728080AL4204 serial no no [1] Slot03
diskid HGST HUH728080AL4204 serial no no [1] Slot04
diskid HGST HUH728080AL4204 serial no no [1] Slot05
diskid HGST HUH728080AL4204 serial no no [1] Slot06
diskid HGST HUH728080AL4204 serial no no [1] Slot07
diskid HGST HUH728080AL4204 serial no no [1] Slot08
diskid HGST HUH728080AL4204 serial no no [1] Slot09
diskid HGST HUH728080AL4204 serial no no [1] Slot10
diskid HGST HUH728080AL4204 serial no no [1] Slot11
c2t0d0 Kingston DataTraveler 2.0 - - - -
当磁盘发生故障时,它将从此列表中删除,在此示例中,机箱 0 插槽 01 中的磁盘发生故障。
假设盘柜 0 将始终有 24 个磁盘 00-23,而盘柜 1 将始终有 12 个磁盘 00-11,我如何高效、准确地确定丢失的磁盘?
我目前有以下内容,但我确信这可以通过单个 awk 命令完成:
enclosure0=($(awk '$7 ~ "[0]"{print $8}' <<<"$DISK_INFO" | sort -n))
enclosure1=($(awk '$7 ~ "[1]"{print $8}' <<<"$DISK_INFO" | sort -n))
for n in {00..23}; do
grep -q "$n" <<<"${enclosure0[@]}" || missing+=("Enclosure 0 - Slot$n")
done
for n in {00..11}; do
grep -q "$n" <<< "${enclosure1[@]}" || missing+=("Enclosure 1 - Slot$n")
done
答案1
没有awk
,对于每个外壳:
{ printf '[0] Slot%s\n' {00..23} ; grep -Eo '\[0\] Slot..' disks ; } | sort | uniq -u
慢动作:
printf '[0] Slot%s\n' {00..23}
生成所有可能磁盘的列表grep -Eo '\[0\] Slot..' disks
提取现有磁盘- {..} 连接两个命令的输出
sort | uniq -u
提取只出现一次的行
您可以用适当的函数替换 printf 和 grep 步骤,或者用另一个文件(即预期的磁盘列表)上的类似 grep 替换 printf 部分。
答案2
由于您事先知道哪些项目需要存在,因此创建一个列表并在看到它们时将其勾掉。
awk '
BEGIN {
for (i = 0; i < 24; i++) missing[0][sprintf("%02d", i)] = 1;
for (i = 0; i < 12; i++) missing[1][sprintf("%02d", i)] = 1;
}
$7 ~ /^\[[0-9]+\]$/ && $8 ~ /^Slot[0-9]+$/ {
gsub(/[^0-9]/, "", $7);
sub(/^[^0-9]+/, "", $8);
delete missing[$7][$8];
}
END {
for (enclosure in missing) {
for (slot in missing[enclosure]) {
printf "Missing enclosure %d Slot%s\n", enclosure, slot;
}
}
}
'
答案3
perl -sle '
my(@e, @AoA) = qw/ 24 12 /;
$AoA[$1][$2]++ while /\[([01])]\h+(?:(?!\d)\S)+0*(\d+)$/mg;
for my $enc ( 0 .. $#e ) {
for my $m_slot ( grep { ! defined $AoA[$enc][$_] } 0 .. $e[$enc]-1 ) {
print "in enclosure $enc - Slot$m_slot is missing.";
}
}
' -- -_="$DISK_INFO";
解释:
° Initialize the array @e which holds the number of slots in the various enclosures.
° The Disk info variable is passed into the command line as $_ initialized to $DISK_INFO.
° progressively scan and match the $_ variable using the while loop and look for the numbers in the '[..]' and the 'Slot...' locations. Using these we update the array of array @AoA, it can be viewed as a matrix.
° Now once we have ingested all the data, its time to process it now in two for loops.
° The outer for loops on the enclosures, in our case, they are two.
° The inner for loop computes the indices of the current enclosure elements that are undefined, IOW, those slots that were never encountered during the data collection drive in the while loop.