在脚本中将 GB 转换为 TB

在脚本中将 GB 转换为 TB

应该是这样0.972 / 3=0.324这里的实际值。所以如果 的值HDD UsedG所以应该以TB为单位计算,然后进行除法。

# isi storagepool list -v |
awk '
    /Requested Protection:/ { parity=substr($NF,length($NF)-1,1) }
    /Nodes:/ { nodes=$NF }
    /HDD Total/ { hdd_total=$NF }
    /HDD Used/ { hdd_used=$NF }
    END {
        multiplier=nodes-parity
        total=hdd_total/nodes*multiplier
        used=hdd_used/nodes
        print "parity =" parity
        print "NodeNumber =" nodes
        print "Total =" total "TB"
        print "Effective Total volume = " total*0.8 " TB"
        print "USED =" used "%"
        print "Effective used=" used*multiplier*0.8 " TB"
        print "Available volume=" (hdd_total-hdd_used)/nodes*multiplier*0.8 " TB" }'
parity =1
NodeNumber =3
Total =37.3013TB
Effective Total volume = 29.8411 TB
USED =324.307%
Effective used=518.891 TB
Available volume=-489.05 TB

HDD Used命令中的实际输出isi storagepool list --v是G,如下所述,我们需要以TB为单位进行计算

# isi storagepool list -v
                Name: s210_21tb_800gb-ssd_128gb
               Nodes: 1, 2, 3
Requested Protection: +2d:1n
                Type: nodepool
            Children: -
               Usage
                HDD Used: 972.905G
               HDD Total: 55.9520T
              HDD % Used: 1.70%
                SSD Used: 0b
               SSD Total: 0b
              SSD % Used: 0.00%

# cat isi.py
isi storagepool list -v |
awk '
    /Requested Protection:/ { parity=substr($NF,length($NF)-1,1) }
    /Nodes:/ { nodes=$NF }
    /HDD Total/ { hdd_total=$NF }
    /HDD Used/ { hdd_used=num2gb($NF) }
    END {
        multiplier=nodes-parity
        total=hdd_total/nodes*multiplier
        used=hdd_used/nodes
        print "parity =" parity
        print "NodeNumber =" nodes
        print "Total = " total " TB"
        print "Effective Total volume = " total*0.8 " TB"
        print "USED =" used "%"
        print "Effective used=" used*multiplier*0.8 " TB"
        print "Available volume=" (hdd_total-hdd_used)/nodes*multiplier*0.8 " TB" }'

编辑 1 @ilkkachu 答案

# cat isi.py
#!/usr/bin/awk -f
isi storagepool list -v | awk 'function num2gb(n) { if (n ~ /T$/) return n * 1; return n*1024; }
    /Requested Protection:/ { parity=substr($NF,length($NF)-1,1) }
    /Nodes:/ { nodes=$NF }
    /HDD Total/ { hdd_total=$NF }
    /HDD Used/ { hdd_used=num2gb($NF) }
    END {
        multiplier=nodes-parity
        total=hdd_total/nodes*multiplier
        used=hdd_used/nodes
        print "parity =" parity
        print "NodeNumber =" nodes
        print "Total = " total " TB"
        print "Effective Total volume = " total*0.8 " TB"
        print "USED =" used "%"
        print "Effective used=" used*multiplier*0.8 " TB"
        print "Available volume=" (hdd_total-hdd_used)/nodes*multiplier*0.8 " TB" }'

输出

cat storageinfo_example_info
parity =1
NodeNumber =3
Total = 37.3013 TB
Effective Total volume = 29.8411 TB
USED =333925%
Effective used=534281 TB
Available volume=-534251 TB

答案1

如果输入有时以 GB 为单位,有时以 TB 为单位,我会编写一个函数来处理这两种情况(GNU awk 手册中的函数):

#!/usr/bin/awk -f
function num2gb(n) { 
    if (n ~ /T$/) return n * 1024;    # if TB, scale
    return n * 1;                     # else assume GB. * 1 converts to number
}  
{ printf "%.2f G\n", num2gb($1) }     # print, as an example

然后,您可以在从输入读取数字时使用该函数来获取 GB 中的数字:

/HDD Total/ { hdd_total = num2gb($NF) }
/HDD Used/ { hdd_used = num2gb($NF) }

如果需要,添加MB和 的情况PB,并检查生成输入的程序是否考虑 的 次方1024或 的 次方1000

输出时,你当然可以选择你喜欢的倍数。

上面将是一个独立的awk脚本,在命令行上你会做类似的事情

$ somecmd | awk 'function num2gb(n) { if (n ~ /T$/) return n * 1024; return n*1; }  
   /some pattern/ { some action }
   /other pattern/ { something with num2gb($n) ... } '

相关内容