1717# It will list:
1818# o Region
1919# o File System ID
20- # o File System Name - optional
20+ # o File System Name - optional with -n option
21+ # o Storage Virtual Machine ID
22+ # o Storage Virtual Machine Name - optional with -n option
2123# o Volume ID
22- # o Volume Name
2324# o Volume Status
25+ # o Volume Size in MB - optional - optional with -x option
26+ # o Volume Used in MB - optional - optional with -x option
27+ # o Volume Name
2428#
2529# ###############################################################################
2630
2933# ###############################################################################
3034usage () {
3135 cat 1>&2 << EOF
32- Usage $( basename $0 ) [-r region] [-a] [-o] [-f fileSystemName] [-i fileSystemId] [-n] [-s svmID]
33- Where: -r region allows you to specify the region you want the list from.
36+ Usage $( basename $0 ) [-r region] [-a] [-o] [-f fileSystemName] [-i fileSystemId] [-n] [-s svmID] [-x] [volume_pattern]
37+ Where: volume_pattern - Only volumes that match the egrep pattern will be included.
38+ -r region allows you to specify the region you want the list from.
3439 -a means all regions.
35- -n means include the file system name .
40+ -n means include the file system ans SVM names. Not capable with the -x option .
3641 -f fileSystemName means to only include volumes that are under the named file system.
3742 -i fileSystemId means to only include volumes that are under the file system with the specified file system ID.
3843 -s svmID - Only show volumes that are under the specified SVM ID.
3944 -o means to exclude svm root volumes. Only works in conjunction with the -s option.
45+ -x means to provide capacity information. Not capable with the -n option.
4046EOF
4147 exit 1
4248}
4551# Main logic starts here.
4652# ###############################################################################
4753
48- tmpout=/tmp/list_aws_vol-out.$$
49- tmpout2=/tmp/list_aws_vol-out0.$$
50- trap ' rm -f $tmpout $tmpout2' exit
54+ volumeList=/tmp/list_aws_vol-out.$$
55+ fsNames=/tmp/list_aws_vol-fsNames.$$
56+ svmNames=/tmp/list_aws_vol-svmNames.$$
57+ cloudwatchOutputFile=/tmp/list_vol-cloudwatch-output.$$
58+ cloudwatchQueryFile=/tmp/list_vol-cloudwatch-query.$$
59+ volCapacityFile=/tmp/list_vol-capacity.$$
60+ trap ' rm -f $volumeList $fsNames $svmNames $cloudwatchOutputFile $cloudwatchQueryFile $volCapacityFile' exit
5161#
5262# Check that the required utilities are installed.
5363if which aws jq > /dev/null 2>&1 ; then
@@ -65,7 +75,9 @@ excludeRoot=false
6575filter=" "
6676fsid=" "
6777fileSystemName=" "
68- while getopts " hr:af:i:nos:" option; do
78+ extraInfo=false
79+ volumePattern=" *"
80+ while getopts " hr:af:i:nos:x" option; do
6981 case " $option " in
7082 r) region=" $OPTARG "
7183 ;;
@@ -82,16 +94,27 @@ while getopts "hr:af:i:nos:" option; do
8294 ;;
8395 s) svmID=" $OPTARG "
8496 ;;
97+ x) extraInfo=true
98+ ;;
8599 * ) usage
86100 ;;
87101 esac
88102done
103+ shift $(( OPTIND- 1 ))
104+ if [ ! -z " $1 " ]; then
105+ volumePattern=" $1 "
106+ fi
89107
90108if [ ! -z " $fileSystenName " -a ! -z " $fsid " ]; then
91109 echo " Error, you can't provide both -f and -n options." 1>&2
92110 exit 1
93111fi
94112
113+ if [ " $extraInfo " != " false" -a " $includeFsName " != " false" ]; then
114+ echo " Error, you can't provide both -n and -x options." 1>&2
115+ exit 1
116+ fi
117+
95118if [ ! -z " $fileSystemName " ]; then
96119 fsid=$( aws fsx describe-file-systems --region $region --output=json 2> /dev/null | jq -r " .FileSystems[] | if((.Tags[] | select(.Key == \" Name\" ) .Value) == \" ${fileSystemName} \" ) then .FileSystemId else empty end" 2> /dev/null)
97120 if [ -z " $fsid " ]; then
@@ -127,6 +150,8 @@ if [ "$allRegions" = "true" ]; then
127150else
128151 regions=($region )
129152fi
153+
154+ jqFields=' \(.FileSystemId),\(.Name),\(.VolumeId),\(.Lifecycle),\(.OntapConfiguration.SizeInMegabytes),\(.OntapConfiguration.StorageVirtualMachineId)'
130155#
131156# Loop on all the regions.
132157for region in ${regions[*]} ; do
@@ -135,23 +160,70 @@ for region in ${regions[*]}; do
135160 if [ ! -z " $( getent hosts fsx.$region .amazonaws.com) " ]; then
136161 if [ -z " $svmID " ]; then
137162 if [ " $excludeRoot " != " true" ]; then
138- aws fsx describe-volumes $filter --region=$region --output=json | jq -r ' .Volumes[] | .FileSystemId + "," + .Name + "," + .VolumeId + " ," + .Lifecycle ' | sort > $tmpout
163+ aws fsx describe-volumes $filter --region=$region --output=json | jq -r ' .Volumes[] | " ' $jqFields ' " ' | egrep " , $volumePattern ," | sort > $volumeList
139164 else
140- aws fsx describe-volumes $filter --region=$region --output=json | jq -r ' .Volumes[] | if(.OntapConfiguration.StorageVirtualMachineRoot | not) then .FileSystemId + "," + .Name + "," + .VolumeId + "," + .Lifecycle else empty end ' | sort > $tmpout
165+ aws fsx describe-volumes $filter --region=$region --output=json | jq -r ' .Volumes[] | if(.OntapConfiguration.StorageVirtualMachineRoot | not) then " ' $jqFields ' " else empty end ' | egrep " , $volumePattern , " | sort > $volumeList
141166 fi
142167 else
143168 if [ " $excludeRoot " != " true" ]; then
144- aws fsx describe-volumes $filter --region=$region --output=json | jq -r ' .Volumes[] | if(.OntapConfiguration.StorageVirtualMachineId == "' $svmID ' ") then .FileSystemId + "," + .Name + "," + .VolumeId + "," + .Lifecycle else empty end ' | sort > $tmpout
169+ aws fsx describe-volumes $filter --region=$region --output=json | jq -r ' .Volumes[] | if(.OntapConfiguration.StorageVirtualMachineId == "' $svmID ' ") then " ' $jqFields ' " else empty end ' | egrep " , $volumePattern , " | sort > $volumeList
145170 else
146- aws fsx describe-volumes $filter --region=$region --output=json | jq -r ' .Volumes[] | if(.OntapConfiguration.StorageVirtualMachineId == "' $svmID ' " and (.OntapConfiguration.StorageVirtualMachineRoot | not)) then .FileSystemId + "," + .Name + "," + .VolumeId + "," + .Lifecycle else empty end ' | sort > $tmpout
171+ aws fsx describe-volumes $filter --region=$region --output=json | jq -r ' .Volumes[] | if(.OntapConfiguration.StorageVirtualMachineId == "' $svmID ' " and (.OntapConfiguration.StorageVirtualMachineRoot | not)) then " ' $jqFields ' " else empty end ' | egrep " , $volumePattern , " | sort > $volumeList
147172 fi
148173 fi
149174
150175 if [ $includeFsName == " true" ]; then
151- aws fsx describe-file-systems --region=$region --output=json | jq -r ' .FileSystems[] | .FileSystemId + "," + (.Tags[] | select(.Key == "Name") .Value)' | fgrep " $fileSystemName " > $tmpout2
152- awk -F, -v region=$region ' BEGIN {first=1; maxNameLen=0; while(getline < "' $tmpout2 ' ") {fss[$1]=$2; if(length($2) > maxNameLen) {maxNameLen=length($2)}}; maxNameLen +=2; formatStr="%12s %21s%-"maxNameLen"s %24s %10s %s\n"}; {if(first) {printf "\n"; printf formatStr, "Region", "FileSystem ID", "(Name)", "Volume ID", "State", "Volume Name"; first=0}; name="("fss[$1]")"; printf formatStr, region, $1, name, $3, $4, $2}' < $tmpout
176+ aws fsx describe-file-systems --region=$region --output=json | jq -r ' .FileSystems[] | .FileSystemId + "," + (.Tags[] | select(.Key == "Name") .Value)' > $fsNames
177+ aws fsx describe-storage-virtual-machines --region=$region --output=json | jq -r ' .StorageVirtualMachines[] | "\(.StorageVirtualMachineId),\(.Name)"' > $svmNames
178+ awk -F, -v region=$region ' BEGIN {first=1; maxFsNameLen=0; maxSvmNameLen=0; while(getline < "' $fsNames ' ") {fss[$1]=$2; if(length($2) > maxFsNameLen) {maxFsNameLen=length($2)}}; maxFsNameLen +=2; while(getline < "' $svmNames ' ") {svm[$1]=$2; if(length($2) > maxSvmNameLen) {maxSvmNameLen=length($2)}}; maxSvmNameLen +=2; formatStr="%12s %21s%-"maxFsNameLen"s %21s-%-"maxSvmNameLen"s %24s %10s %s\n"}; {if(first) {printf "\n"; printf formatStr, "Region", "FileSystem ID", "(Name)", "SVM", "(Name)", "Volume ID", "State", "Volume Name"; first=0}; fsName="("fss[$1]")"; svmName="("svm[$6]")"; printf formatStr, region, $1, fsName, $6, svmName, $3, $4, $2}' < $volumeList
153179 else
154- awk -F, -v region=$region ' BEGIN {first=1; formatStr="%12s %21s %24s %10s %s\n"}; {if(first) {printf "\n"; printf formatStr, "Region", "FileSystem ID", "Volume ID", "State", "Volume Name"; first=0}; printf formatStr, region, $1, $3, $4, $2}' < $tmpout
180+ if [ $extraInfo == " true" ]; then
181+ volIds=$( awk -F, ' {print $3}' < $volumeList )
182+ if [ ! -z " $volIds " ]; then
183+ echo " [" > $cloudwatchQueryFile
184+ first=true
185+ for volId in $volIds ; do
186+ volId2=$( echo $volId | sed -e ' s/-/_/g' )
187+ if [ " $first " = " true" ]; then
188+ first=false
189+ else
190+ echo " ," >> $cloudwatchQueryFile
191+ fi
192+ cat << EOF >> $cloudwatchQueryFile
193+ {
194+ "Id": "m$volId2 ",
195+ "MetricStat": {
196+ "Metric": {
197+ "Namespace": "AWS/FSx",
198+ "MetricName": "StorageUsed",
199+ "Dimensions": [
200+ {
201+ "Name": "VolumeId",
202+ "Value": "$volId "
203+ },
204+ {
205+ "Name": "FileSystemId",
206+ "Value": "$( awk -F, -v volId=$volId ' $3 == volId {print $1}' < $volumeList ) "
207+ }]
208+ },
209+ "Period": 300,
210+ "Stat": "Average"
211+ }
212+ }
213+ EOF
214+ done
215+ echo " ]" >> $cloudwatchQueryFile
216+ aws cloudwatch get-metric-data --region $region --metric-data-queries file://$cloudwatchQueryFile --start-time=$( date -u -d ' 5 minutes ago' +" %Y-%m-%dT%H:%M:%SZ" ) --end-time=$( date -u +" %Y-%m-%dT%H:%M:%SZ" ) --output=json > $cloudwatchOutputFile 2>&1
217+ for volId in $volIds ; do
218+ volId2=$( echo $volId | sed -e ' s/-/_/g' )
219+ capacity=$( jq -r ' .MetricDataResults[] | select(.Id == "m' $volId2 ' ") | .Values[0]' < $cloudwatchOutputFile )
220+ echo " $volId ,$capacity " >> $volCapacityFile
221+ done
222+ awk -F, -v region=$region -v volCapacityFile=$volCapacityFile ' BEGIN {first=1; formatStr="%12s %21s %21s %24s %10s %16s %15s %s\n"; while (getline < volCapacityFile) {volidCapacities[$1]=sprintf("%10.0f",$2/1024/1024)}}; {if(first) {printf "\n"; printf formatStr, "Region", "FileSystem ID", "SVM", "Volume ID", "State", "Volume Size (MB)", "Used (MB)", "Volume Name"; first=0}; printf formatStr, region, $1, $6, $3, $4, $5, volidCapacities[$3], $2}' < $volumeList
223+ fi
224+ else
225+ awk -F, -v region=$region ' BEGIN {first=1; formatStr="%12s %21s %21s %24s %10s %s\n"}; {if(first) {printf "\n"; printf formatStr, "Region", "FileSystem ID", "SVM", "Volume ID", "State", "Volume Name"; first=0}; printf formatStr, region, $1, $6, $3, $4, $2}' < $volumeList
226+ fi
155227 fi
156228 else
157229 if [ $allRegions != " true" ]; then
0 commit comments