用于计算的Python循环

时间:2017-08-11 13:56:34

标签: python

我在python中为循环创建逻辑时遇到了一些麻烦,因为我是python的新手,我对模块没有任何想法。所以基本上我需要的是一个报告,它将计算组中所有类似主机名的总可用容量。例如Ka是一个组Kb是另一个组kax-nfs是nfs share { {1}}是nfs第二组。 kbx-nfs是群集,群集的第二个群组是kax-clus

每个组可能有一组服务器,需要计算如下所示。对于每个主机,总输出行为7行。为了更好地理解,我已经跳过了一些内容。在某些服务器中,它是kbx-clusTotal NFS volume size provisioned主机kbx-hostn,但是``总可用容量(GB):`保持不变。

应添加所有Total LUN size provisioned (GB):主机kbx-hostn 应添加所有Total available capacity (GB):个主机kbx-hostn

例如:

总可用数 - 总配置数=实际可用数/ 1024 =答案

我正在尝试在python中执行此操作。

必需输出

Total LUN size provisioned (GB): ----->是我需要的实际输出

Raw_Data

kbx-hostn 2.1 TB

Bash脚本

*Netapp Cluster: kbx-hostn*
------Some-content------------
------Some-content------------
------Some-content------------
Total available capacity (GB): 20506.90
------Some-content------------
Total LUN size provisioned (GB): 18301.64
------Some-content------------
*Netapp Cluster: kbx-hostn*
------Some-content------------
------Some-content------------
------Some-content------------
Total available capacity (GB): 20506.90
------Some-content------------
Total LUN size provisioned (GB): 20500.71
------Some-content------------
*Netapp Cluster: kbx-hostn*
------Some-content------------
------Some-content------------
------Some-content------------
Total available capacity (GB): 20506.90
------Some-content------------
Total LUN size provisioned (GB): 17251.87
------Some-content------------
*Netapp Cluster: kbx-hostn*
------Some-content------------
------Some-content------------
------Some-content------------
Total available capacity (GB): 20506.90
------Some-content------------
Total LUN size provisioned (GB): 0
------Some-content------------
*Netapp Cluster: kbx-hostn*
------Some-content------------
------Some-content------------
------Some-content------------
Total available capacity (GB): 20506.90
------Some-content------------
Total LUN size provisioned (GB): 0
------Some-content------------
*Netapp Cluster: kax-hostn*
------Some-content------------
------Some-content------------
------Some-content------------
Total available capacity (GB): 20506.90
------Some-content------------
Total LUN size provisioned (GB): 19250.54
------Some-content------------
*Netapp Cluster: kax-hostn*
------Some-content------------
------Some-content------------
------Some-content------------
Total available capacity (GB): 20506.90
------Some-content------------
Total LUN size provisioned (GB): 19751.66
------Some-content------------
*Netapp Cluster: kax-hostn*
------Some-content------------
------Some-content------------
------Some-content------------
Total available capacity (GB): 20506.90
------Some-content------------
Total LUN size provisioned (GB): 6000.29
------Some-content------------
*Netapp Cluster: kax-hostn*
------Some-content------------
------Some-content------------
------Some-content------------
Total available capacity (GB): 20506.90
------Some-content------------
Total LUN size provisioned (GB): 0
------Some-content------------
*Netapp Cluster: kax-hostn*
------Some-content------------
------Some-content------------
------Some-content------------
Total available capacity (GB): 20506.90
------Some-content------------
Total LUN size provisioned (GB): 0
------Some-content------------
*Netapp Cluster: kax-nfshostn*
------Some-content------------
------Some-content------------
------Some-content------------
Total available capacity (GB): 6530.23
Total NFS volume size provisioned (GB): 6144.00
------Some-content------------
------Some-content------------
*Netapp Cluster: kbx-nfshostn*
------Some-content------------
------Some-content------------
------Some-content------------
Total available capacity (GB): 6530.23
Total NFS volume size provisioned (GB): 5376.00
------Some-content------------
------Some-content------------
*Netapp Cluster: kax-nfshostn*
------Some-content------------
------Some-content------------
------Some-content------------
Total available capacity (GB): 6530.23
Total NFS volume size provisioned (GB): 512.00
------Some-content------------
------Some-content------------
*Netapp Cluster: kax-nfshostn*
------Some-content------------
------Some-content------------
------Some-content------------
Total available capacity (GB): 6530.23
Total NFS volume size provisioned (GB): 0
------Some-content------------
------Some-content------------
*Netapp Cluster: kax-nfshostn*
------Some-content------------
------Some-content------------
------Some-content------------
Total available capacity (GB): 6530.23
Total NFS volume size provisioned (GB): 0
------Some-content------------
------Some-content------------
*Netapp Cluster: kbx-nfshostn*
------Some-content------------
------Some-content------------
------Some-content------------
Total available capacity (GB): 6530.23
Total NFS volume size provisioned (GB): 0
------Some-content------------
------Some-content------------
*Netapp Cluster: kbx-nfshostn*
------Some-content------------
------Some-content------------
------Some-content------------
Total available capacity (GB): 6530.23
Total NFS volume size provisioned (GB): 0
------Some-content------------
------Some-content------------
*Netapp Cluster: kbx-nfshostn*
------Some-content------------
------Some-content------------
------Some-content------------
Total available capacity (GB): 6530.23
Total NFS volume size provisioned (GB): 0
------Some-content------------
------Some-content------------
*Netapp Cluster: kbx-clushostn*
------Some-content------------
------Some-content------------
------Some-content------------
Total available capacity (GB): 202046.45
Total NFS volume size provisioned (GB): 72383.24
------Some-content------------
------Some-content------------
lo8nv-stmg0001n APP [11:35 AM]
*Netapp Cluster: lax-hostn*
------Some-content------------
------Some-content------------
------Some-content------------
Total available capacity (GB): 13671.27
------Some-content------------
Total LUN size provisioned (GB): 13451.70
------Some-content------------
*Netapp Cluster: lax-hostn*
------Some-content------------
------Some-content------------
------Some-content------------
Total available capacity (GB): 13671.27
------Some-content------------
Total LUN size provisioned (GB): 12451.01
------Some-content------------
*Netapp Cluster: hbx-hostn*
------Some-content------------
------Some-content------------
------Some-content------------
Total available capacity (GB): 13671.27
------Some-content------------
Total LUN size provisioned (GB): 13300.87
------Some-content------------
*Netapp Cluster: hbx-hostn*
------Some-content------------
------Some-content------------
------Some-content------------
Total available capacity (GB): 13671.27
------Some-content------------
Total LUN size provisioned (GB): 11200.40
------Some-content------------

@Cas感谢你的努力和给出了这个时间,但我的解释有点棘手。

让我们在这里理解它:这些下面的主机是一堆的,它们在一起是一个集群,所以我们需要添加总可用容量和使用该特定输出配置的总LUN大小我们需要减去&在TB中打印实际答案/ 1024。

cat raw_data | sed '/^$/d' | sed '/^Total # of SVMs/d' | sed '/^Total IOPS/d'  | sed '/APP/d' |  sed '/^new/d' | sed '/^Total throughput/d'  |sed '/^\[/d'  | sed 's/\*Netapp Cluster://g' | sed '/^Total physical capacity /d'  | grep -A2  -e  "ax-cluster" -e "bx-cluster" | grep -v -e ^- -e ^$ | awk 'ORS=NR%3?FS:RS' | sort

1 个答案:

答案 0 :(得分:0)

如果*kbx-hostn1* *kbx-hostn2* *kbx-hostn3* 是可接受的语言,那么这非常简单:

perl

将此脚本保存为#!/usr/bin/perl use strict; my $cluster = ''; my $capacity = 0; my $provisioned = 0; my %C = (); while(<>) { chomp; if (m/^\*Netapp Cluster: /) { (undef,$cluster) = split(/: /,$_); next; }; if (m/^Total available capacity/) { (undef,$capacity) = split(/: /,$_); next; } if (m/^Total (LUN|NFS volume) size provisioned/) { (undef,$provisioned) = split(/: /,$_); $C{$cluster} += $capacity - $provisioned; ($cluster,$capacity,$provisioned) = ('',0,0); next; } }; for my $cluster (sort keys %C) { printf "%s %.1f TB\n", $cluster, $C{$cluster}/1024; } 并将示例输入保存为summarise-rawdata.pl后,输出为:

rawdata.txt

这是$ ./summarise-rawdata.pl rawdata.txt hbx-hostn* 2.8 TB kax-hostn* 56.2 TB kax-nfshostn* 19.0 TB kbx-clushostn* 126.6 TB kbx-hostn* 45.4 TB kbx-nfshostn* 20.3 TB lax-hostn* 1.4 TB 版本:

awk

#!/usr/bin/awk -f /^\*Netapp Cluster: / { cluster=$NF }; /^Total available capacity/ { capacity=$NF }; /^Total (LUN|NFS volume) size provisioned/ { C[cluster] += capacity - $NF; cluster=capacity=""; }; END { for (cluster in C) { printf "%s %.1f TB\n", cluster, C[cluster] / 1024; }; } awk版本的输出相同,只是perl输出未排序,而awk输出为。

可以在perl中按索引对关联数组进行排序,但如果需要排序,则通过awk管道输出会更容易。