-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaws-s3-size.sh
4093 lines (4089 loc) · 161 KB
/
aws-s3-size.sh
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#! /bin/bash
#
#
#
# ------------------------------------------------------------------------------------
#
# MIT License
#
# Copyright (c) 2018 Enterprise Group, Ltd.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# ------------------------------------------------------------------------------------
#
# File: aws-s3-size.sh
#
script_version=0.0.45
#
# Dependencies:
# - 'aws-services-snapshot-driver.txt' or custom driver file containing AWS describe/list commands
# - 'aws-services-snapshot-driver-global.txt' containing AWS global services (not limited to an AWS region)
# - bash shell
# - jq - JSON wrangler https://stedolan.github.io/jq/
# - AWS CLI tools (pre-installed on AWS AMIs)
# - AWS CLI profile with IAM permissions for the AWS CLI command:
# - aws sts get-caller-identity (used to pull account number )
# - aws iam list-account-aliases (used to pull account alias )
# - AWS CLI profile with IAM permissions for the AWS CLI command:
# 'cloudwatch get-metric-statistics' (used to pull the S3 bucket sizes)
#
# Sample IAM policy JSON for "sts:GetCallerIdentity"
#
# {
# "Version": "2012-10-17",
# "Statement":
# {
# "Effect": "Allow",
# "Action": "sts:GetCallerIdentity",
# "Resource": "*"
# }
# }
#
#
# Sample IAM policy JSON for "iam:ListAccountAliases"
#
# {
# "Version": "2012-10-17",
# "Statement":
# {
# "Effect": "Allow",
# "Action": "iam:ListAccountAliases",
# "Resource": "*"
# }
# }
#
#
#
# Tested on:
# Windows Subsystem for Linux (WSL)
# OS Build: 15063.540
# bash.exe version: 10.0.15063.0
# Ubuntu 16.04
# GNU bash, version 4.3.48(1)
# jq 1.5-1-a5b5cbe
# aws-cli/1.11.134 Python/2.7.12 Linux/4.4.0-43-Microsoft botocore/1.6.1
#
# AWS EC2
# Amazon Linux AMI release 2017.03
# Linux 4.9.43-17.38.amzn1.x86_64
# GNU bash, version 4.2.46(2)
# jq-1.5
# aws-cli/1.11.133 Python/2.7.12 Linux/4.9.43-17.38.amzn1.x86_64 botocore/1.6.0
#
#
# By: Douglas Hackney
# https://github.com/dhackney
#
# Type: AWS utility
# Description:
# This shell script snapshots the current state of AWS resources and writes it to JSON files
#
#
# Roadmap:
# * summary report
#
#
###############################################################################
#
# set the environmental variables
#
set -o pipefail
#
###############################################################################
#
#
# initialize the script variables
#
echo ""
echo "initialize the script variables"
echo ""
aws_account=""
aws_account_alias=""
bucket_bytes=""
bucket_gigabytes=0
bucket_list=""
bucket_megabytes=0
bucket_name=""
bucket_name_2=""
bucket_name_feed=""
bucket_size_json_edit=""
bucket_size_json_results=""
bucket_size_name_bytes=""
bucket_size_TB_GB_MB_B_build=""
bucket_terabytes=0
cli_profile=""
cli_profile_file_check_line=""
cli_profile_file_check_line_strip=""
count_bucket_list=0
count_cli_profile=0
count_cli_profile_file_check_line=0
count_script_version_length=0
count_storage_type=0
count_tasks_loop=0
count_tasks_this_file=0
count_tasks_this_file_all=0
count_tasks_this_file_end=0
count_tasks_this_file_increment=0
count_tasks_this_file_increment_all=0
count_tasks_this_file_non_loop=0
count_text_bar_header=0
count_text_bar_menu=0
count_text_block_length=0
count_text_header_length=0
count_text_side_length_header=0
count_text_side_length_menu=0
count_text_width_header=0
count_text_width_menu=0
counter_bucket_list=0
counter_cli_profile=0
counter_cli_profile_task_display=0
counter_storage_type=0
counter_tasks_this_file=0
date_file="$(date +"%Y-%m-%d-%H%M%S")"
date_now="$(date +"%Y-%m-%d-%H%M%S")"
_empty=""
_empty_task=""
_empty_task_sub=""
error_line_aws=""
error_line_jq=""
error_line_pipeline=""
error_line_psql=""
feed_write_log=""
file_driver=""
_fill=""
_fill_task=""
_fill_task_sub=""
full_path=""
let_done=""
let_done_task=""
let_done_task_sub=""
let_left=""
let_left_task=""
let_left_task_sub=""
let_progress=""
let_progress_task=""
let_progress_task_sub=""
log_suffix=""
logging=""
parameter1=""
paramter2=""
storage_type=""
tasks_code=""
tasks_loop_line_start=0
tasks_loop_line_end=0
text_bar_header_build=""
text_bar_menu_build=""
text_header=""
text_header_bar=""
text_menu=""
text_menu_bar=""
text_side_header=""
text_side_menu=""
this_file=""
this_log=""
this_log_file=""
this_log_file_errors=""
this_log_file_errors_full_path=""
this_log_file_full_path=""
this_log_temp_file_full_path=""
this_path=""
this_path_temp=""
this_summary_report=""
this_summary_report_full_path=""
this_user=""
this_utility_acronym=""
this_utility_filename_plug=""
thislogdate=""
timestamp_end=""
timestamp_period=""
timestamp_start=""
verbose=""
write_file=""
write_file_clean=""
write_file_full_path=""
write_file_raw=""
write_file_service_names=""
write_file_service_names_unique=""
write_path=""
#
#
#
#
#
##############################################################################################################33
# Function definition begin
##############################################################################################################33
#
#
# Functions definitions
#
#######################################################################
#
#
#
#######################################################################
#
#
# function to display the Usage
#
#
function fnUsage()
{
echo ""
echo " ----------------------------------------- AWS S3 Bucket Size utility usage ------------------------------------------"
echo ""
echo " This utility reports the the size of AWS S3 buckets "
echo ""
echo " This script will: "
echo " * Capture the average size of AWS S3 buckets for a given time period "
echo " * Write the average size of AWS S3 buckets to a JSON file for each account "
echo " * Write the size of AWS S3 buckets in Bytes, Megabytes, Gigabytes, and Terabytes to a CSV file for each account "
echo ""
echo "----------------------------------------------------------------------------------------------------------------------"
echo ""
echo " Usage:"
echo " aws-s3-size.sh -p AWS_CLI_profile -s start timestamp -e end timestamp "
echo ""
echo " Optional parameters: -t storage type -d period "
echo ""
echo " Example: aws-s3-size.sh -p prod -s 2018-02-12T02:00:00 -e 2018-02-12T03:59:59"
echo ""
echo " Note: AWS accounts are determined by the AWS CLI profile "
echo ""
echo " Where: "
echo " -p - Name of the AWS CLI cli_profile (i.e. what you would pass to the --profile parameter in an AWS CLI command)"
echo " or the name of a text file containing a list of AWS CLI profiles for multiple accounts"
echo " Example: -p myAWSCLIprofile "
echo " Example: -p s3-size-profile-driver-file.txt "
echo ""
echo " -s - The GMT/UTC/Zulu timestamp to begin the bucket average size calculation in the format: YYYY-MM-DDTHH:MM:SS "
echo " Example: -s 2018-02-12T00:00:00"
echo ""
echo " -e - The GMT/UTC/Zulu timestamp to end the bucket average size calculation in the format: YYYY-MM-DDTHH:MM:SS "
echo " Example: -e 2018-02-12T01:00:00"
echo ""
echo " -d - The period in seconds to sample the S3 bucket size. Default is 3600. Valid entries follow."
echo " Example: -d 60"
echo " Example: -d 300"
echo " Example: -d 3600"
echo " Documentation is here: https://docs.aws.amazon.com/cli/latest/reference/cloudwatch/get-metric-statistics.html"
echo ""
echo " -t - The S3 storage type to measure. Default is 'all'. Valid entries follow. "
echo " Example: -t all"
echo " Example: -t StandardStorage"
echo " Example: -t StandardIAStorage"
echo " Example: -t ReducedRedundancyStorage"
echo " Documentation is here: https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/s3-metricscollected.html"
echo ""
echo ""
echo " -b - Verbose console output. Set to 'y' for verbose console output. Temp files are not deleted. "
echo " Example: -b y "
echo ""
echo " -g - Logging on / off. Default is off. Set to 'y' to create an info log. Set to 'z' to create a debug log. "
echo " Note: logging mode is slower and debug log mode will be very slow and resource intensive on large jobs. "
echo " Example: -g y "
echo ""
echo " -h - Display this message"
echo " Example: -h "
echo ""
echo " ---version - Display the script version"
echo " Example: --version "
echo ""
echo ""
exit 1
}
#
#######################################################################
#
#
# function to echo the progress bar to the console
#
# source: https://stackoverflow.com/questions/238073/how-to-add-a-progress-bar-to-a-shell-script
#
# 1. Create ProgressBar function
# 1.1 Input is currentState($1) and totalState($2)
function fnProgressBar()
{
#
# Process data
let _progress=(${1}*100/"${2}"*100)/100
let _done=(${_progress}*4)/10
let _left=40-"$_done"
# Build progressbar string lengths
_fill="$(printf "%${_done}s")"
_empty="$(printf "%${_left}s")"
#
# 1.2 Build progressbar strings and print the ProgressBar line
# 1.2.1 Output example:
# 1.2.1.1 Progress : [########################################] 100%
printf "\r Overall Progress : [${_fill// /#}${_empty// /-}] ${_progress}%%"
}
#
#######################################################################
#
#
# function to update the task progress bar
#
# source: https://stackoverflow.com/questions/238073/how-to-add-a-progress-bar-to-a-shell-script
#
# 1. Create ProgressBar function
# 1.1 Input is currentState($1) and totalState($2)
function fnProgressBarTask()
{
#
# Process data
let _progress_task=(${1}*100/"${2}"*100)/100
let _done_task=(${_progress_task}*4)/10
let _left_task=40-"$_done_task"
# Build progressbar string lengths
_fill_task="$(printf "%${_done_task}s")"
_empty_task="$(printf "%${_left_task}s")"
#
# 1.2 Build progressbar strings and print the ProgressBar line
# 1.2.1 Output example:
# 1.2.1.1 Progress : [########################################] 100%
printf "\r Account Progress : [${_fill_task// /#}${_empty_task// /-}] ${_progress_task}%%"
}
#
#######################################################################
#
#
# function to update the subtask progress bar
#
# source: https://stackoverflow.com/questions/238073/how-to-add-a-progress-bar-to-a-shell-script
#
# 1. Create ProgressBar function
# 1.1 Input is currentState($1) and totalState($2)
function fnProgressBarTaskSub()
{
#
# Process data
let _progress_task_sub=(${1}*100/"${2}"*100)/100
let _done_task_sub=(${_progress_task_sub}*4)/10
let _left_task_sub=40-"$_done_task_sub"
# Build progressbar string lengths
_fill_task_sub="$(printf "%${_done_task_sub}s")"
_empty_task_sub="$(printf "%${_left_task_sub}s")"
#
# 1.2 Build progressbar strings and print the ProgressBar line
# 1.2.1 Output example:
# 1.2.1.1 Progress : [########################################] 100%
printf "\r Account Bucket Progress : [${_fill_task_sub// /#}${_empty_task_sub// /-}] ${_progress_task_sub}%%"
}
#
#######################################################################
#
#
# function to display the task progress bar on the console
#
# parameter 1 = counter
# paramter 2 = count
#
function fnProgressBarTaskDisplay()
{
#
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} "in function: 'fnProgressBarTaskDisplay' "
fnWriteLog ${LINENO} ""
#
fnWriteLog ${LINENO} level_0 "$text_header_bar"
fnWriteLog ${LINENO} level_0 ""
fnProgressBarTask "$1" "$2"
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 "$text_header_bar"
fnWriteLog ${LINENO} level_0 ""
}
#
#######################################################################
#
#
# function to display the task progress bar on the console
#
# parameter 1 = counter
# paramter 2 = count
#
function fnProgressBarTaskSubDisplay()
{
#
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} "in function: 'fnProgressBarTaskSubDisplay' "
fnWriteLog ${LINENO} ""
#
fnWriteLog ${LINENO} level_0 "$text_header_bar"
fnWriteLog ${LINENO} level_0 ""
fnProgressBarTaskSub "$1" "$2"
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 "$text_header_bar"
fnWriteLog ${LINENO} level_0 ""
}
#
#######################################################################
#
#
# function to echo the header to the console
#
function fnHeader()
{
#
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} "in function: 'fnHeader' "
fnWriteLog ${LINENO} ""
#
clear
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} "--------------------------------------------------------------------------------------------------------------------"
fnWriteLog ${LINENO} "--------------------------------------------------------------------------------------------------------------------"
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 "$text_header"
fnWriteLog ${LINENO} level_0 ""
fnProgressBar ${counter_tasks_this_file} ${count_tasks_this_file}
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 "$text_header_bar"
fnWriteLog ${LINENO} level_0 ""
}
#
#######################################################################
#
#
# function to echo to the console and write to the log file
#
function fnWriteLog()
{
# clear IFS parser
IFS=
# write the output to the console
fnOutputConsole "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9"
# if logging is enabled, then write to the log
if [[ ("$logging" = "y") || ("$logging" = "z") || ("$logging" = "x") ]]
then
# write the output to the log
fnOutputLog "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9"
fi
# reset IFS parser to default values
unset IFS
}
#
#######################################################################
#
#
# function to echo to the console
#
function fnOutputConsole()
{
#
# console output section
#
# test for verbose
if [ "$verbose" = "y" ] ;
then
# if verbose console output then
# echo everything to the console
#
# strip the leading 'level_0'
if [ "$2" = "level_0" ] ;
then
# if the line is tagged for display in non-verbose mode
# then echo the line to the console without the leading 'level_0'
echo " Line: "$1" "$3" "$4" "$5" "$6" "$7" "$8" "$9""
else
# if a normal line echo all to the console
echo " Line: "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9""
fi
else
# test for minimum console output
if [ "$2" = "level_0" ] ;
then
# echo ""
# echo "console output no -v: the logic test for level_0 was true"
# echo ""
# if the line is tagged for display in non-verbose mode
# then echo the line to the console without the leading 'level_0'
echo " "$3" "$4" "$5" "$6" "$7" "$8" "$9""
fi
fi
#
#
}
#
#######################################################################
#
#
# function to write to the log file
#
function fnOutputLog()
{
# log output section
#
# load the timestamp
thislogdate="$(date +"%Y-%m-%d-%H:%M:%S")"
#
# ----------------------------------------------------------
#
# normal logging
#
# append the line to the log variable
# the variable is written to the log file on exit by function fnWriteLogFile
#
#
if [ "$2" = "level_0" ] ;
then
# if the line is tagged for logging in non-verbose mode
# then write the line to the log without the leading 'level_0'
this_log+="$(echo "${thislogdate} Line: "$1" "$3" "$4" "$5" "$6" "$7" "$8" "$9"" 2>&1)"
else
# if a normal line write the entire set to the log
this_log+="$(echo "${thislogdate} Line: "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9"" 2>&1)"
fi
#
# append the new line
# do not quote this variable
this_log+=$'\n'
#
#
# ---------------------------------------------------------
#
# 'use this for debugging' - debug logging
#
# if the script is crashing and you cannot debug it from the 'info' mode log produced by -g y,
# then enable 'verbose' console output and 'debug' logging mode
#
# note that the 'debug' form of logging is VERY slow on big jobs
#
# use parameters: -b y -g z
#
# if the script crashes before writing out the log you can scroll back in the console to
# identify the line number where the problem is located
#
#
}
#
#######################################################################
#
#
# function to append the log variable to the temp log file
#
function fnWriteLogTempFile()
{
#
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} "in function: 'fnWriteLogTempFile' "
fnWriteLog ${LINENO} ""
#
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} "Appending the log variable to the temp log file"
fnWriteLog ${LINENO} ""
feed_write_log="$(echo "$this_log" >> "$this_log_temp_file_full_path" 2>&1)"
#
# check for command / pipeline error(s)
if [ "$?" -ne 0 ]
then
#
# set the command/pipeline error line number
error_line_pipeline="$((${LINENO}-7))"
#
#
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 "value of variable 'feed_write_log':"
feed_write_log="$(echo "$feed_write_log")"
fnWriteLog ${LINENO} level_0 "$feed_write_log"
fnWriteLog ${LINENO} level_0 ""
#
# call the command / pipeline error function
fnErrorPipeline
#
#
fi
#
fnWriteLog ${LINENO} "$feed_write_log"
fnWriteLog ${LINENO} ""
#
# empty the temp log variable
this_log=""
#
}
#
#######################################################################
#
#
# function to write log variable to the log file
#
function fnWriteLogFile()
{
#
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} "in function: 'fnWriteLogFile' "
fnWriteLog ${LINENO} ""
#
# append the temp log file onto the log file
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} "Writing temp log to log file"
fnWriteLog ${LINENO} "Value of variable 'this_log_temp_file_full_path': "
fnWriteLog ${LINENO} "$this_log_temp_file_full_path"
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} "Value of variable 'this_log_file_full_path': "
fnWriteLog ${LINENO} "$this_log_file_full_path"
fnWriteLog ${LINENO} ""
# write the contents of the variable to the temp log file
#
fnWriteLogTempFile
#
if [[ "$aws_account" != "" ]]
then
cat "$this_log_temp_file_full_path" >> "$this_log_file_full_path"
echo "" >> "$this_log_file_full_path"
echo "Log end" >> "$this_log_file_full_path"
# delete the temp log file
rm -f "$this_log_temp_file_full_path"
elif [[ "$aws_account" = "" ]]
then
# echo "AWS account is empty. Preserving temp log file."
# echo "Temp log file is here: "
# echo "$this_log_temp_file_full_path"
echo ""
fi
}
#
##########################################################################
#
#
# function to delete the work files
#
function fnDeleteWorkFiles()
{
#
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} "in function: 'fnDeleteWorkFiles' "
fnWriteLog ${LINENO} ""
#
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} "in delete work files "
fnWriteLog ${LINENO} "value of variable 'verbose': "$verbose" "
fnWriteLog ${LINENO} ""
if [ "$verbose" != "y" ] ;
then
# if not verbose console output then delete the work files
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} "In non-verbose mode: Deleting work files"
fnWriteLog ${LINENO} ""
feed_write_log="$(rm -f "$this_path_temp"/"$this_utility_acronym"-* 2>&1)"
fnWriteLog ${LINENO} "$feed_write_log"
feed_write_log="$(rm -f "$this_path_temp"/"$this_utility_acronym"_* 2>&1)"
fnWriteLog ${LINENO} "$feed_write_log"
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} "value of variable 'this_log_file_full_path' "$this_log_file_full_path" "
fnWriteLog ${LINENO} "$feed_write_log"
fnWriteLog ${LINENO} ""
feed_write_log="$(rm -f "$write_path_snapshots"/"$this_utility_acronym"* 2>&1)"
fnWriteLog ${LINENO} "$feed_write_log"
feed_write_log="$(rm -f "$write_path_snapshots"/"$this_utility_acronym"* 2>&1)"
fnWriteLog ${LINENO} "$feed_write_log"
fnWriteLog ${LINENO} ""
feed_write_log="$(rm -r -f "$this_path_temp" 2>&1)"
fnWriteLog ${LINENO} "$feed_write_log"
#
# check for error file; if exists
# if no errors, then delete the error log file
if [[ -f "$this_log_file_errors_full_path" ]]
then
count_error_lines="$(cat "$this_log_file_errors_full_path" | wc -l)"
if (( "$count_error_lines" < 3 ))
then
rm -f "$this_log_file_errors_full_path"
fi
fi
else
# in verbose mode so preserve the work files
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 "In verbose mode: Preserving work files "
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 "work files are here: "$this_path" "
fnWriteLog ${LINENO} level_0 ""
fi
}
#
##########################################################################
#
#
# function to handle command or pipeline errors
#
function fnErrorPipeline()
{
#
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} "in function: 'fnErrorPipeline' "
fnWriteLog ${LINENO} ""
#
fnWriteLog ${LINENO} level_0 "-----------------------------------------------------------------------------------------------------"
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 " Command or Command Pipeline Error "
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 "-----------------------------------------------------------------------------------------------------"
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 " System Error while running the previous command or pipeline "
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 " Please check the error message above "
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 " Error at script line number: "$error_line_pipeline" "
fnWriteLog ${LINENO} level_0 ""
if [[ ("$logging" = "y") || ("$logging" = "z") ]]
then
fnWriteLog ${LINENO} level_0 " The log will also show the error message and other environment, variable and diagnostic information "
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 " The log is located here: "
fnWriteLog ${LINENO} level_0 " "$this_log_file_full_path" "
fi
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 " Exiting the script"
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 "-----------------------------------------------------------------------------------------------------"
fnWriteLog ${LINENO} level_0 ""
# append the temp log onto the log file
fnWriteLogTempFile
# write the log variable to the log file
fnWriteLogFile
exit 1
}
#
##########################################################################
#
#
# function for AWS CLI errors
#
function fnErrorAws()
{
#
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} "in function: 'fnErrorAws' "
fnWriteLog ${LINENO} ""
#
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 " AWS Error while executing AWS CLI command"
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 " Please check the AWS error message above "
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 " Error at script line number: "$error_line_aws" "
fnWriteLog ${LINENO} level_0 ""
if [[ ("$logging" = "y") || ("$logging" = "z") ]]
then
fnWriteLog ${LINENO} level_0 " The log will also show the AWS error message and other diagnostic information "
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 " The log is located here: "
fnWriteLog ${LINENO} level_0 " "$write_path"/ "
fnWriteLog ${LINENO} level_0 " "$this_log_file" "
fi
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 " Exiting the script"
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 "--------------------------------------------------------------------------------------------------"
fnWriteLog ${LINENO} level_0 ""
# append the temp log onto the log file
fnWriteLogTempFile
# write the log variable to the log file
fnWriteLogFile
exit 1
}
#
##########################################################################
#
#
# function for jq errors
#
function fnErrorJq()
{
#
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} "in function: 'fnErrorJq' "
fnWriteLog ${LINENO} ""
#
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 " Error at script line number: "$error_line_jq" "
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 " There was a jq error while processing JSON "
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 " Please check the jq error message above "
fnWriteLog ${LINENO} level_0 ""
if [[ ("$logging" = "y") || ("$logging" = "z") ]]
then
fnWriteLog ${LINENO} level_0 " The log will also show the AWS error message and other diagnostic information "
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 " The log is located here: "
fnWriteLog ${LINENO} level_0 " "$write_path"/ "
fnWriteLog ${LINENO} level_0 " "$this_log_file" "
fi
fnWriteLog ${LINENO} level_0 " The log will also show the jq error message and other diagnostic information "
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 " The log is located here: "
fnWriteLog ${LINENO} level_0 " "$this_log_file_full_path" "
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 " Exiting the script"
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 "--------------------------------------------------------------------------------------------------"
fnWriteLog ${LINENO} level_0 ""
# append the temp log onto the log file
fnWriteLogTempFile
# write the log variable to the log file
fnWriteLogFile
exit 1
}
#
##########################################################################
#
#
# function to check for valid date prameter entry
#
fnDateValid() {
if [[ "$1" =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}$ ]] && date -d "$1">/dev/null 2>&1
then
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} "Valid date entry"
fnWriteLog ${LINENO} ""
else
fnWriteLog ${LINENO} "Invalid date entry"
fnWriteLog ${LINENO} ""
clear
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 "-------------------------------------------------------------------------------"
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 " ERROR: You entered an invalid date: "$1" "
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 " Dates must be in the foramt YYYY-MM-DDTHH:mm:SS "
fnWriteLog ${LINENO} level_0 " Example: 2018-02-18T23:59:59 "
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 "-------------------------------------------------------------------------------"
fnWriteLog ${LINENO} level_0 ""
fnUsage
fi;
}
#
##########################################################################
#
#
# function to log non-fatal errors
#
function fnErrorLog()
{
#
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} "in function: 'fnErrorLog' "
fnWriteLog ${LINENO} ""
#
fnWriteLog ${LINENO} level_0 "-----------------------------------------------------------------------------------------------------"
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 " Error message: "
fnWriteLog ${LINENO} level_0 " "$feed_write_log""
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 "-----------------------------------------------------------------------------------------------------"
echo "-----------------------------------------------------------------------------------------------------" >> "$this_log_file_errors_full_path"
echo "" >> "$this_log_file_errors_full_path"
echo " Error message: " >> "$this_log_file_errors_full_path"
echo " "$feed_write_log"" >> "$this_log_file_errors_full_path"
echo "" >> "$this_log_file_errors_full_path"
echo "-----------------------------------------------------------------------------------------------------" >> "$this_log_file_errors_full_path"
}
#
##########################################################################
#
#
# function to increment the task counter
#
function fnCounterIncrementTask()
{
#
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} "in function: 'fnCounterIncrementTask' "
fnWriteLog ${LINENO} ""
#
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} "incrementing the task counter"
counter_tasks_this_file="$((counter_tasks_this_file+1))"
fnWriteLog ${LINENO} "value of variable 'counter_tasks_this_file': "$counter_tasks_this_file" "
fnWriteLog ${LINENO} "value of variable 'count_tasks_this_file': "$count_tasks_this_file" "
fnWriteLog ${LINENO} ""
}
#
##############################################################################################################33
# Function definition end
##############################################################################################################33
#
#
###########################################################################################################################
#
#
# enable logging to capture initial segments
#
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} "---------------------------------------------------------------------------------------------------------"
fnWriteLog ${LINENO} "enable logging to capture initial segments "
fnWriteLog ${LINENO} "---------------------------------------------------------------------------------------------------------"
fnWriteLog ${LINENO} ""
#
logging="y"
#
###########################################################################################################################
#
#
# build the menu and header text line and bars
#
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} "---------------------------------------------------------------------------------------------------------"
fnWriteLog ${LINENO} "build the menu and header text line and bars "
fnWriteLog ${LINENO} "---------------------------------------------------------------------------------------------------------"
fnWriteLog ${LINENO} ""
#
text_header='AWS S3 Bucket Size utility v'
count_script_version_length=${#script_version}
count_text_header_length=${#text_header}
count_text_block_length=$(( count_script_version_length + count_text_header_length ))
count_text_width_menu=104
count_text_width_header=83
count_text_side_length_menu=$(( (count_text_width_menu - count_text_block_length) / 2 ))
count_text_side_length_header=$(( (count_text_width_header - count_text_block_length) / 2 ))
count_text_bar_menu=$(( (count_text_side_length_menu * 2) + count_text_block_length + 2 ))
count_text_bar_header=$(( (count_text_side_length_header * 2) + count_text_block_length + 2 ))
#
# source and explanation for the following use of printf is here: https://stackoverflow.com/questions/5799303/print-a-character-repeatedly-in-bash
text_bar_menu_build="$(printf '%0.s-' $(seq 1 "$count_text_bar_menu") )"
text_bar_header_build="$(printf '%0.s-' $(seq 1 "$count_text_bar_header") )"
text_side_menu="$(printf '%0.s-' $(seq 1 "$count_text_side_length_menu") )"
text_side_header="$(printf '%0.s-' $(seq 1 "$count_text_side_length_header") )"
text_menu="$(echo "$text_side_menu"" ""$text_header""$script_version"" ""$text_side_menu")"
text_menu_bar="$(echo "$text_bar_menu_build")"
text_header="$(echo " ""$text_side_header"" ""$text_header""$script_version"" ""$text_side_header")"
text_header_bar="$(echo " ""$text_bar_header_build")"
#
###########################################################################################################################
#
#
# display initializing message
#
#
fnWriteLog ${LINENO} ""
fnWriteLog ${LINENO} "---------------------------------------------------------------------------------------------------------"
fnWriteLog ${LINENO} " display initializing message "
fnWriteLog ${LINENO} "---------------------------------------------------------------------------------------------------------"
fnWriteLog ${LINENO} ""
#
clear
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 "$text_header"
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 " This utility writes the size of AWS S3 buckets to JSON and CSV files "
fnWriteLog ${LINENO} level_0 ""
fnWriteLog ${LINENO} level_0 " This script will: "
fnWriteLog ${LINENO} level_0 " * Capture the average size of AWS S3 buckets for a given time period "
fnWriteLog ${LINENO} level_0 " * Write the average size of AWS S3 buckets to a JSON file for each account "
fnWriteLog ${LINENO} level_0 " * Write the size of AWS S3 buckets in Bytes, Megabytes, Gigabytes, and "
fnWriteLog ${LINENO} level_0 " Terabytes to a CSV file for each account "