-
Notifications
You must be signed in to change notification settings - Fork 2
/
list_fsxn_filesystems
executable file
·265 lines (254 loc) · 11.8 KB
/
list_fsxn_filesystems
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#!/bin/bash
#
#################################################################################
# THIS SOFTWARE IS PROVIDED BY NETAPP "AS IS" AND ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
# EVENT SHALL NETAPP BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR'
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
################################################################################
#
################################################################################
# This script will list all the AWS FSxN file systems the user has access to.
# It will list:
# o Region
# o File System ID
# o File Systesm "name"
# o Status
# o Management IP
# o VPC ID - optional
# o Subnet ID - optional
# o ARN - optional
# o Backup - The Backup retention period.
#
# In the case of the Management IP and Subnet ID, it will only show the first
# one defined. Based on the potential output from the API call, there could
# be more than one.
#
# If the '-c' option is provided, instead of providing the information above
# it will display a hierarchical view of each file system, meaning it will
# display all the SVMs, and under each SVM, all the volumes that are under it.
################################################################################
################################################################################
# This function is used to output the usage information for the script and exit.
################################################################################
usage () {
cat 1>&2 <<EOF
Usage $(basename $0) [-r region] [-a] [-n] [-c] [-s] [-b] [-i fileSystemId] [-f fileSystemName] [-x] [pattern]
Where: pattern - means to only show file systems whos name match the pattern.
-r region allows you to specify the region you want the list from.
-a means all regions.
-c means to display a hierarchical view of each filesystem including svms and volumes.
-i allows you to limit the display to the file system with the id provided.
-f allows you to limit the display to the file system with the name provided.
-b means to display the backup retenion period. Not compoatible with the -x or -c options.
-s means to display the current status of a volume. Only relative with the -c option.
-n means to show the AWS ARN for the file system. Not compatible with -x or -c options.
-x means include additional information: vpc, subnet, Size, Deployement Type, Throughput, provisioned IOPS.
EOF
exit 1
}
################################################################################
# Main logic starts here.
################################################################################
tmpout=/tmp/list_fss-out.$$
fileSystemsFile=/tmp/list_fss-fss.$$
svmsFile=/tmp/list_fss-svms.$$
volumesFile=/tmp/list_fss-volumes.$$
trap 'rm -f $tmpout $fileSystemsFile $svmsFile $volumesFile' exit
#
# Ensure all the required ultilities are installed.
if which aws jq > /dev/null 2>&1; then
:
else
echo "Error, both the 'aws' and 'jq' commands are required to run this script." 1>&2
exit 1
fi
#
# Set defaults.
allRegions=false
includeExtraInfo=false
contents=false
showStatus=false
showARN=false
showBackup=false
region=$(aws configure list | egrep '^.*egion ' | awk '{print $2}')
#
# Process command line arguments.
while getopts "bhcxsanr:i:f:" option; do
case "$option" in
r) region="$OPTARG"
;;
a) allRegions=true
;;
b) showBackup=true
;;
x) includeExtraInfo=true
;;
c) contents=true
;;
i) fsid=$OPTARG
;;
f) fileSystemName="$OPTARG"
;;
s) showStatus=true
;;
n) showARN=true
;;
*) usage
;;
esac
done
shift $((OPTIND-1))
#
# Check for invalid options.
if [ "$showBackup" == "true" -a \( "$contents" == "true" -o "$includeExtraInfo" == "true" \) ]; then
echo "Error, the -b option is not compatiable with the -c or -x options." 1>&2
echo ""
usage
fi
if [ "$showARN" == "true" -a \( "$contents" == "true" -o "$includeExtraInfo" == "true" \) ]; then
echo "Error, the -n option is not compatiable with the -c or -x options." 1>&2
echo ""
usage
fi
if [ "$showStatus" == "true" -a "$contents" != "true" ]; then
echo "Error, the -s option is only compatiable with the -c option." 1>&2
echo ""
usage
fi
declare -a regions
if [ "$allRegions" == "true" ]; then
#
# Generate a list of all the valid regions the user can search. That is the
# intersection of all the regions they have enabled, and the regions that
# support FSxN
allEndabledRegions=$(aws ec2 describe-regions --query "Regions[].RegionName" --output=json | jq -r '.[]')
allFsxnRegions=$(curl -s https://api.regional-table.region-services.aws.a2z.com/index.json | jq -r '.prices[] | select(.attributes."aws:serviceName" == "Amazon FSx for NetApp ONTAP") .attributes."aws:region"')
for reg in $allEndabledRegions; do
for fsxnReg in $allFsxnRegions; do
if [ $reg == $fsxnReg ]; then
regions+=($reg)
fi
done
done
if [ -z "$regions" ]; then
echo "Error, failed to get the list of regions that support FSxN" 1>&2
exit 1
fi
else
regions=($region)
fi
if [ ! -z "$fsid" -a ! -z "$fileSystemName" ]; then
echo "Error, you can't specify both a file system ID and file system name." 1>&2
exit 1
fi
#
# Get the regions that support the FSxN for ONTAP service.
#
# Define the query string that is used to get only the fields needed to generate the output.
# It also makes the 'jq' commands below easily, since it flattens the JSON structure.
queryString="FileSystems[*].{
FileSystemType: FileSystemType,
FileSystemId: FileSystemId,
Lifecycle:Lifecycle,
Name:Tags[?Key=='Name']|[0].Value,
ManagementIp: OntapConfiguration.Endpoints.Management.IpAddresses[0],
VpcId: VpcId,
SubnetId: SubnetIds[0],
ResourceARN: ResourceARN,
AutomaticBackupRetentionDays: OntapConfiguration.AutomaticBackupRetentionDays,
DeploymentType: OntapConfiguration.DeploymentType,
DiskIopsConfiguration: OntapConfiguration.DiskIopsConfiguration.Iops,
ThroughputCapacity: OntapConfiguration.ThroughputCapacity,
StorageCapacity: StorageCapacity
}"
#
# Loop on all the requested regions.
for region in ${regions[*]}; do
if [ ! -z "$fileSystemName" ]; then
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)
if [ ! -z "$fsid" ]; then
aws fsx describe-file-systems --file-system-ids $fsid --region=$region --query "$queryString" --output=json > $fileSystemsFile 2>&1
else
echo "Error, failed to get the file system ID based on a file system name of '$fileSystemName'." 1>&2
exit 1
fi
else
if [ -z "$fsid" ]; then
aws fsx describe-file-systems --region=$region --query "$queryString" --output=json > $fileSystemsFile 2>&1
else
aws fsx describe-file-systems --file-system-ids $fsid --region=$region --query "$queryString" --output=json > $fileSystemsFile 2>&1
fi
fi
if [ $? -ne 0 ]; then
echo "Error, failed to get the list of file systems." 1>&2
cat $fileSystemsFile 1>&2
exit 1
fi
if [ $contents == "true" ]; then
aws fsx describe-storage-virtual-machines --region=$region --output=json > $svmsFile 2>&1
if [ $? -ne 0 ]; then
echo "Error, failed to get the list of SVMs." 1>&2
cat $svmsFile 1>&2
exit 1
fi
aws fsx describe-volumes --region=$region --output=json > $volumesFile 2>&1
if [ $? -ne 0 ]; then
echo "Error, failed to get the list of volumes." 1>&2
cat $volumesFile 1>&2
exit 1
fi
printf "$region\n"
jq -r '.[] | if(.FileSystemType != "ONTAP") then empty else .FileSystemId + " " + .Lifecycle + " =" + .Name + "=" end' $fileSystemsFile | while read fs fsStatus fsName; do
x="${fsName#=}"
fsName="${x%=}"
[ "$showStatus" == "true" ] && printf "\t$fs($fsStatus) - '$fsName'\n"
[ "$showStatus" != "true" ] && printf "\t$fs - '$fsName'\n"
jq -r '.StorageVirtualMachines[] | if(.FileSystemId == "'$fs'") then .StorageVirtualMachineId + " " + .Lifecycle + " " + .Name else empty end' $svmsFile | while read svm svmStatus svmName; do
[ "$showStatus" == "true" ] && printf "\t\t$svm($svmStatus) - '$svmName'\n"
[ "$showStatus" != "true" ] && printf "\t\t$svm - '$svmName'\n"
jq -r '.Volumes[] | if(.FileSystemId == "'$fs'" and .OntapConfiguration.StorageVirtualMachineId == "'$svm'") then .VolumeId + " " + .Lifecycle + " " + .Name else empty end' $volumesFile | while read volume volStatus volumeName; do
[ "$showStatus" == "true" ] && printf "\t\t\t$volume($volStatus) - '$volumeName'\n"
[ "$showStatus" != "true" ] && printf "\t\t\t$volume - '$volumeName'\n"
done
done
done
else
#
# Convert JSON into a CSV format.
# 1 = fsid
# 2 = arn
# 3 = name
# 4 = lifecycle
# 5 = management ip
# 6 = vpc id
# 7 = subnet id
# 8 = backup retention
# 9 = deployment type
# 10 = iops
# 11 = throughput
# 12 = size
if [ ! -z "$1" ]; then
jq -r '.[] | if(.FileSystemType == "ONTAP") then .FileSystemId + "," + .ResourceARN + "," + (if((.Name | tostring) | test("'$1'")) then .Name else empty end) + "," + .Lifecycle + "," + .ManagementIp + "," + .VpcId + "," + .SubnetId + "," + (if(.AutomaticBackupRetentionDays == null) then "Dissabled" else (.AutomaticBackupRetentionDays | tostring) end) + "," + .DeploymentType + "," + (.DiskIopsConfiguration | tostring) + "," + (.ThroughputCapacity | tostring) + "," + (.StorageCapacity | tostring) else empty end' $fileSystemsFile > $tmpout
else
jq -r '.[] | if(.FileSystemType == "ONTAP") then .FileSystemId + "," + .ResourceARN + "," + .Name + "," + .Lifecycle + "," + .ManagementIp + "," + .VpcId + "," + .SubnetId + "," + if(.AutomaticBackupRetentionDays == null) then "Dissabled" else (.AutomaticBackupRetentionDays | tostring) end + "," + .DeploymentType + "," + (.DiskIopsConfiguration | tostring) + "," + (.ThroughputCapacity | tostring) + "," + (.StorageCapacity | tostring) else empty end' $fileSystemsFile > $tmpout
fi
if [ "$includeExtraInfo" == "true" ]; then
awk -F, -v region=$region 'BEGIN {first=1; formatStr="%12s %23s %35s %10s %15s %22s %25s %6s %12s %11s %6s\n"}; {if(first) {printf "\n"; printf formatStr, "Region", "FileSystem ID", "Name", "Status", "Management IP", "VPC ID", "Subnet ID", "Size", "Deployment", "Throughput", "Iops"; first=0}; printf formatStr, region, $1, "\"" $3 "\"", $4, $5, $6, $7, $(12), $9, $(11), $(10)}' < $tmpout
else
if [ "$showARN" == "true" ]; then
awk -F, -v region=$region 'BEGIN {first=1; formatStr="%12s %23s %70s %35s %10s %15s\n"}; {if(first) {printf "\n"; printf formatStr, "Region", "FileSystem ID", "ARN", "Name", "Status", "Management IP"; first=0}; printf formatStr, region, $1, $2, "\"" $3 "\"", $4, $5}' < $tmpout
else
formatStr='%12s %23s %35s %10s %15s\n'
[ "$showBackup" == "true" ] && formatStr='%12s %23s %35s %10s %15s %17s\n'
awk -F, -v region=$region 'BEGIN {first=1; formatStr="'"${formatStr}"'"}; {if(first) {printf "\n"; printf formatStr, "Region", "FileSystem ID", "Name", "Status", "Management IP", "Backup Retention"; first=0}; printf formatStr, region, $1, "\"" $3 "\"", $4, $5, $8}' < $tmpout
fi
fi
fi
done