I have tried to come with below bash script which is at least working for me, i know its not perfect and that why i want some review and suggestion to make it more aligned with bash practices. I'm trying to read bash manuals and seeking help from google and stackoverflow questions and answers.
I'm not able to explain well thus not getting fruitful help.. but that's the way it works :-) .
Below is my working code which basically have a array_list
of volumes like array_fxn1 ...
which i'm taking from a file file and then building them and further making sure if ldap_volList
is not empty at the same time and then doing some for loop over the array_list
further doing check and then ssh to the Server to run the command and do some sum of the certain columns.
As you may see, its always relative to array_list or say volume_list, for an example if its array_fxn1=
then its dbcl101
, if its array_fxn2=
then its dbcl201
and so on ..
array_fxn1 then fxn101 array_fxn2 then fxn201 array_fxn3 then fxn301 array_fxn4 then fxn401
but i am not getting that logic build so come to the below one which can work for me being not an expert.
Any suggestion, will be much much appreciated.
my code:
#!/bin/bash # read -rp "Please Enter LDAP Project Name: " ldap_proj ldap_volList=$(cat ldap-project-nismap.csv |grep "$ldap_proj" | tr "," "\t"| awk '{print $NF}'| tr -d '"') # Build an array list of desired Volumes beforehand to make it more precise array_fxn1=$(echo "$ldap_volList" | grep ^fxn1) array_fxn2=$(echo "$ldap_volList" | grep ^fxn2) array_fxn3=$(echo "$ldap_volList" | grep ^fxn3) array_fxn4=$(echo "$ldap_volList" | grep ^fxn4) array_fxn6=$(echo "$ldap_volList" | grep ^fxn6) array_fxn7=$(echo "$ldap_volList" | grep ^fxn7) array_fxn8=$(echo "$ldap_volList" | grep ^fxn8) # Place a check to ensure the Volumelist is not empty and then loop over the volume array's if [[ ! -z "$ldap_volList" ]]; then for i in $array_fxn1; do volstyle=$(ssh dbcl101 "row 0; vol show $i -fields volume-style-extended" |awk '/fxn/{print $3}') if [[ "$volstyle" == "flexvol" ]];then echo -n "$ldap_proj:$i " ssh dbcl101 "row 0;set -unit MB; \ vol show-footprint $i -fields volume-blocks-footprint-bin0,volume-blocks-footprint-bin1,total-footprint"||awk '/^stv/{s1+=$3; s2+=$4; s3+=$5 } END {print s1, s2, s3}' fi done for i in $array_fxn2; do volstyle=$(ssh dbcl201 "row 0; vol show $i -fields volume-style-extended" |awk '/fxn/{print $3}') if [[ "$volstyle" == "flexvol" ]];then echo -n "$ldap_proj:$i " ssh dbcl201 "row 0;set -unit MB; \ vol show-footprint $i -fields volume-blocks-footprint-bin0,volume-blocks-footprint-bin1,total-footprint"||awk '/^stv/{s1+=$3; s2+=$4; s3+=$5 } END {print s1, s2, s3}' fi done for i in $array_fxn3; do volstyle=$(ssh dbcl301 "row 0; vol show \$i -fields volume-style-extended" |awk '/fxn/{print $3}') if [[ "$volstyle" == "flexvol" ]];then echo -n "$ldap_proj:$i " ssh dbcl301 "row 0;set -unit MB; \ vol show-footprint \$i -fields volume-blocks-footprint-bin0,volume-blocks-footprint-bin1,total-footprint"|awk '/fxn/{print $3,$4,$5}' fi done for i in $array_fxn4; do volstyle=$(ssh fxn401 "row 0; vol show \$i -fields volume-style-extended" |awk '/fxn/{print $3}') if [[ "$volstyle" == "flexvol" ]];then echo -n "$ldap_proj:$i " ssh dbcl401 "row 0;set -unit MB; \ vol show-footprint \$i -fields volume-blocks-footprint-bin0,volume-blocks-footprint-bin1,total-footprint"|awk '/fxn/{print $3,$4,$5}' fi done for i in $array_fxn6; do volstyle=$(ssh dbcl601 "row 0; vol show \$i -fields volume-style-extended" |awk '/fxn/{print $3}') if [[ "$volstyle" == "flexvol" ]];then echo -n "$ldap_proj:$i " ssh dbcl601 "row 0;set -unit MB; \ vol show-footprint \$i -fields volume-blocks-footprint-bin0,volume-blocks-footprint-bin1,total-footprint"|awk '/fxn/{print $3,$4,$5}' fi done for i in $array_fxn7; do volstyle=$(ssh dbcl701 "row 0; vol show \$i -fields volume-style-extended" |awk '/fxn/{print $3}') if [[ "$volstyle" == "flexvol" ]];then echo -n "$ldap_proj:$i " ssh dbcl701 "row 0;set -unit MB; \ vol show-footprint \$i -fields volume-blocks-footprint-bin0,volume-blocks-footprint-bin1,total-footprint"|awk '/fxn/{print $3,$4,$5}' fi done for i in $array_fxn8; do volstyle=$(ssh dbcl804 "row 0; vol show \$i -fields volume-style-extended" |awk '/fxn/{print $3}') if [[ "$volstyle" == "flexvol" ]];then echo -n "$ldap_proj:$i " ssh dbcl804 "row 0;set -unit MB; \ vol show-footprint \$i -fields volume-blocks-footprint-bin0,volume-blocks-footprint-bin1,total-footprint"|awk '/fxn/{print $3,$4,$5}' fi done fi
Output:
$ bash new_test.sh Please Enter LDAP Project Name: web3 web3:fxn1002_web3_wiki 14030MB 13292MB 0MB web3:fxn1002_web3_wvg 690194MB 679021MB 0MB web3:fxn1002_web3_wso 70MB 38MB 0MB
UPDATE ON THE POST:
CVS DATA Sample:
Location,ProjectName,NisMap,"Total(GB)","Used(GB)","Used_Percentage",Quality,Id,volumeName "DC_oregon-DC01",dnpc,"dbc2002:/proj/dbc2002_dnpc_eaeagle_pd2/q",1000,"761.87",76,economy,"eaeagle_pd2","dbc2002_dnpc_eaeagle_pd2" "DC_oregon-DC01",dnpc,"dbc2002:/proj/dbc2002_dnpc_eaeagle2_7/q",4096,"3536.99",86,scratch,"eaeagle2_7","dbc2002_dnpc_eaeagle2_7" "DC_oregon-DC01",dnpc,"dbc2002:/proj/dbc2002_dnpc_eaeagle2_6/q",4096,"2976.74",73,scratch,"eaeagle2_6","dbc2002_dnpc_eaeagle2_6" "DC_oregon-DC01",dnpc,"dbc2002:/proj/dbc2002_dnpc_eaeagle2_5/q",4096,"3111.61",76,economy,"eaeagle2_5","dbc2002_dnpc_eaeagle2_5" "DC_oregon-DC01",dnpc,"dbc2002:/proj/dbc2002_dnpc_eaeagle2_4/q",4096,"3595.16",88,economy,"eaeagle2_4","dbc2002_dnpc_eaeagle2_4" "DC_oregon-DC01",dnpc,"dbc2002:/proj/dbc2002_dnpc_eaeagle2_3/q",4096,"2296.67",56,economy,"eaeagle2_3","dbc2002_dnpc_eaeagle2_3"
NetAPP command data:
1)
$ ssh dbcl101 "row 0; vol show fxn1002_dbdesk_scratch -fields volume-style-extended" |awk '/fxn/{print $3}' flexvol
the above command I am making sure if the output returns flexvol
.
$ ssh dbcl101 "row 0; vol show fxn1002_dbdesk_scratch -fields volume-style-extended" Last login time: 7/17/2022 21:38:02 (rows) vserver volume volume-style-extended ------- -------------------------- --------------------- fxn1002 fxn1002_dbdesk_scratch flexvol
The above one is full command which have volume type
like we have in previous command which contains the flexvol
and that's what we are getting store via awk
to a variable.
- The below NetApp command returns the size of a volume.
--
$ ssh dbcl101 "row 0; vol show-footprint fxn1002_dbdesk_scratch -fields volume-blocks-footprint-bin0,volume-blocks-footprint-bin1,total-footprint" Last login time: 7/17/2022 21:39:26 (rows) vserver volume total-footprint volume-blocks-footprint-bin0 volume-blocks-footprint-bin1 ------- -------------------------- --------------- ---------------------------- ---------------------------- fxn1002 fxn1002_dbdesk_scratch 880.4GB 18.91GB 854.8GB fxn1002 fxn1002_dbdesk_scratch 880.4GB 18.91GB 854.8GB
$ ssh dbcl101 "row 0; vol show-footprint fxn1002_dbdesk_scratch -fields volume-blocks-footprint-bin0,volume-blocks-footprint-bin1,total-footprint"|awk '/fxn/{print $3,$4,$5}' 880.4GB 18.91GB 854.8GB
The above one again the same command which does the print the field 2, 3 & 4
.