-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathDisk Usage with swiftDialog.bash
821 lines (574 loc) · 28.8 KB
/
Disk Usage with swiftDialog.bash
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
#!/bin/bash
####################################################################################################
#
# ABOUT
#
# Disk Usage with swiftDialog
# Help users determine what's occupying all their hard drive space
#
# See: https://snelson.us/2022/11/disk-usage-with-swiftdialog-0-0-2/
#
####################################################################################################
#
# HISTORY
#
# Version 0.0.1, 09-Nov-2022, Dan K. Snelson (@dan-snelson)
# Original swiftDialog, proof-of-concept version
#
# Version 0.0.2, 12-Nov-2022, Dan K. Snelson (@dan-snelson)
# Removed `--ontop` from Progress dialog (for longer execution times)
# Hard-coded estimated execution time for user's home folder to 60 percent
# Opened the macOS built-in Storage information
#
# Version 0.0.3, 14-Nov-2022, Dan K. Snelson (@dan-snelson)
# Modified du's stderr redirection (thanks, @Pico!)
#
# Version 0.0.4, 09-Sep-2023, Dan K. Snelson (@dan-snelson)
# Updated `dialogURL`
#
# Version 0.0.5, 03-Jan-2025, Dan K. Snelson (@dan-snelson)
# - Updated to latest standard
# - Updates for swiftDialog 2.5.5
#
####################################################################################################
####################################################################################################
#
# Global Variables
#
####################################################################################################
export PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin/
# Script Version
scriptVersion="0.0.5"
# Client-side Log
scriptLog="/var/log/org.churchofjesuschrist.log"
# Timestamp
timestamp=$( date '+%Y-%m-%d-%H%M%S' )
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Jamf Pro Script Parameters
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Parameter 4: Debug Mode
debugMode="${4:-"false"}"
# Parameter 5: Estimated Total Seconds
estimatedTotalSeconds="${5:-"120"}"
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Organization Variables
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Script Human-readabale Name
humanReadableScriptName="Disk Usage"
# Organization's Script Name
organizationScriptName="DU"
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Logged-in User Variables
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
loggedInUser=$( echo "show State:/Users/ConsoleUser" | scutil | awk '/Name :/ { print $3 }' )
loggedInUserFullname=$( id -F "${loggedInUser}" )
loggedInUserFirstname=$( echo "$loggedInUserFullname" | sed -E 's/^.*, // ; s/([^ ]*).*/\1/' | sed 's/\(.\{25\}\).*/\1…/' | awk '{print ( $0 == toupper($0) ? toupper(substr($0,1,1))substr(tolower($0),2) : toupper(substr($0,1,1))substr($0,2) )}' )
loggedInUserID=$( id -u "${loggedInUser}" )
loggedInUserHome=$( dscl . read /Users/"${loggedInUser}" NFSHomeDirectory | awk -F ": " '{print $2}' )
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Operating System, Machine and Volume Names
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
osVersion=$( sw_vers -productVersion )
osMajorVersion=$( echo "${osVersion}" | awk -F '.' '{print $1}' )
machineName=$( scutil --get LocalHostName )
volumeName=$( diskutil info / | grep "Volume Name:" | awk '{print $3,$4}' )
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Dialog binary (and enable swiftDialog's `--verbose` mode with script's operationMode)
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# swiftDialog Binary Path
dialogBinary="/usr/local/bin/dialog"
# swiftDialog Minimum Required Version
swiftDialogMinimumRequiredVersion="2.5.5.4802"
# swiftDialog Command Files
dialogWelcomeLog=$( mktemp /var/tmp/dialogWelcomeLog.XXXX )
dialogProgressLog=$( mktemp /var/tmp/dialogProgressLog.XXXX )
dialogCompleteLog=$( mktemp /var/tmp/dialogCompleteLog.XXXX )
# Set Permissions on Dialog Command Files
chmod -vv 644 "${dialogWelcomeLog}" | tee -a "${scriptLog}"
chmod -vv 644 "${dialogProgressLog}" | tee -a "${scriptLog}"
chmod -vv 644 "${dialogCompleteLog}" | tee -a "${scriptLog}"
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Disk Usage-related Variables
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
diskUsageEntireVolumeTop50=$( mktemp /var/tmp/diskUsageEntireVolumeTop50.XXXX )
diskUsageUsersHomeTop50=$( mktemp /var/tmp/diskUsageUsersHomeTop50.XXXX )
outputFileNameEntireVolume="$loggedInUserHome/Desktop/$machineName-Volume-Usage-$timestamp.txt"
outputFileNameUsersHome="$loggedInUserHome/Desktop/$loggedInUser-Home-Usage-$timestamp.txt"
freeSpace=$( diskutil info / | grep -E 'Free Space|Available Space|Container Free Space' | awk -F ":\s*" '{ print $2 }' | awk -F "(" '{ print $1 }' | xargs )
freeBytes=$( diskutil info / | grep -E 'Free Space|Available Space|Container Free Space' | awk -F "(\\\(| Bytes\\\))" '{ print $2 }' )
diskBytes=$( diskutil info / | grep -E 'Total Space' | awk -F "(\\\(| Bytes\\\))" '{ print $2 }' )
freePercentage=$( echo "scale=2; ( $freeBytes * 100 ) / $diskBytes" | bc )
diskSpace="$freeSpace free ( ${freePercentage}% available )"
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Welcome Dialog Title, Message and Icon
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
welcomeTitle="Disk Usage ($scriptVersion)"
welcomeMessage="Happy $( date +'%A' ), ${loggedInUserFirstname}! \n \nThis script analyzes the following locations and outputs text files to your Desktop, which list the 50 largest directories for both: \n- **${volumeName}** (non-system files) \n- **${loggedInUserHome}** \n\nPlease be patient as execution time can be in excess of ${estimatedTotalSeconds} seconds. \n\nClick **Continue** to proceed."
welcomeIcon="/System/Library/Extensions/IOStorageFamily.kext/Contents/Resources/Internal.icns"
overlayIcon=$( defaults read /Library/Preferences/com.jamfsoftware.jamf.plist self_service_app_path )
button1text="Continue …"
button2text="Quit"
infobuttontext="KB8675309"
infobuttonaction="https://servicenow.company.com/support?id=kb_article_view&sysparm_article=${infobuttontext}"
welcomeProgressText="Waiting; click Continue to proceed"
# Debug Mode
if [[ ${debugMode} == "true" ]]; then
welcomeTitle="DEBUG MODE | $welcomeTitle"
button1text="DEBUG MODE | $button1text"
fi
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Welcome Dialog Settings and Features
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
dialogWelcome="$dialogBinary \
--title \"$welcomeTitle\" \
--message \"$welcomeMessage\" \
--icon \"$welcomeIcon\" \
--overlayicon \"$overlayIcon\" \
--button1text \"$button1text\" \
--button2text \"$button2text\" \
--infobuttontext \"$infobuttontext\" \
--infobuttonaction \"$infobuttonaction\" \
--progress \
--progresstext \"$welcomeProgressText\" \
--moveable \
--ontop \
--titlefont size=22 \
--messagefont size=14 \
--iconsize 135 \
--width 700 \
--height 375 \
--commandfile \"$dialogWelcomeLog\" "
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Progress Dialog Title, Message and Icon
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
progressTitle="Disk Usage ($scriptVersion)"
progressMessage="Analyzing ${volumeName} …"
progressIcon="/System/Applications/Utilities/Disk Utility.app"
progressProgressText="Initializing …"
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Progress Dialog Settings and Features
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
dialogProgress="$dialogBinary \
--title \"$progressTitle\" \
--message \"$progressMessage\" \
--icon \"$progressIcon\" \
--progress \
--progresstext \"$progressProgressText\" \
--mini \
--moveable \
--commandfile \"$dialogProgressLog\" "
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Complete Dialog Title, Message and Icon
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
completeTitle="Disk Usage ($scriptVersion)"
if [[ ${debugMode} == "true" ]]; then
completeTitle="DEBUG MODE | $completeTitle"
fi
completeMessage="### Analysis complete \n\nPlease review the following files, which have been saved to your Desktop: \n- **$machineName-Volume-Usage-$timestamp.txt** \n- **$loggedInUser-Home-Usage$timestamp.txt**"
completeIcon="/System/Library/Extensions/IOStorageFamily.kext/Contents/Resources/Internal.icns"
overlayIcon=$( defaults read /Library/Preferences/com.jamfsoftware.jamf.plist self_service_app_path )
completeButton1text="Close"
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Complete Dialog Settings and Features
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
dialogComplete="$dialogBinary \
--title \"$completeTitle\" \
--message \"$completeMessage\" \
--icon \"$completeIcon\" \
--overlayicon \"$overlayIcon\" \
--button1text \"$completeButton1text\" \
--infobuttontext \"$infobuttontext\" \
--infobuttonaction \"$infobuttonaction\" \
--moveable \
--ontop \
--titlefont size=22 \
--messagefont size=14 \
--iconsize 135 \
--width 700 \
--height 325 \
--commandfile \"$dialogCompleteLog\" "
####################################################################################################
#
# Functions
#
####################################################################################################
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Client-side Logging
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
function updateScriptLog() {
echo "${organizationScriptName} ($scriptVersion): $( date +%Y-%m-%d\ %H:%M:%S ) - ${1}" | tee -a "${scriptLog}"
}
function preFlight() {
updateScriptLog "[PRE-FLIGHT] ${1}"
}
function logComment() {
updateScriptLog " ${1}"
}
function notice() {
updateScriptLog "[NOTICE] ${1}"
}
function info() {
updateScriptLog "[INFO] ${1}"
}
function debug() {
if [[ "$operationMode" == "debug" ]]; then
updateScriptLog "[DEBUG] ${1}"
fi
}
function errorOut(){
updateScriptLog "[ERROR] ${1}"
}
function error() {
updateScriptLog "[ERROR] ${1}"
let errorCount++
}
function warning() {
updateScriptLog "[WARNING] ${1}"
let errorCount++
}
function fatal() {
updateScriptLog "[FATAL ERROR] ${1}"
exit 1
}
function quitOut(){
updateScriptLog "[QUIT] ${1}"
}
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Validate / install swiftDialog (Thanks big bunches, @acodega!)
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
function dialogInstall() {
# Get the URL of the latest PKG From the Dialog GitHub repo
dialogURL=$(curl -L --silent --fail "https://api.github.com/repos/swiftDialog/swiftDialog/releases/latest" | awk -F '"' "/browser_download_url/ && /pkg\"/ { print \$4; exit }")
# Expected Team ID of the downloaded PKG
expectedDialogTeamID="PWA5E9TQ59"
preFlight "Installing swiftDialog..."
# Create temporary working directory
workDirectory=$( /usr/bin/basename "$0" )
tempDirectory=$( /usr/bin/mktemp -d "/private/tmp/$workDirectory.XXXXXX" )
# Download the installer package
/usr/bin/curl --location --silent "$dialogURL" -o "$tempDirectory/Dialog.pkg"
# Verify the download
teamID=$(/usr/sbin/spctl -a -vv -t install "$tempDirectory/Dialog.pkg" 2>&1 | awk '/origin=/ {print $NF }' | tr -d '()')
# Install the package if Team ID validates
if [[ "$expectedDialogTeamID" == "$teamID" ]]; then
/usr/sbin/installer -pkg "$tempDirectory/Dialog.pkg" -target /
sleep 2
dialogVersion=$( /usr/local/bin/dialog --version )
preFlight "swiftDialog version ${dialogVersion} installed; proceeding..."
else
# Display a so-called "simple" dialog if Team ID fails to validate
osascript -e 'display dialog "Please advise your Support Representative of the following error:\r\r• Dialog Team ID verification failed\r\r" with title "Error" buttons {"Close"} with icon caution'
quitScript
fi
# Remove the temporary working directory when done
/bin/rm -Rf "$tempDirectory"
}
function dialogCheck() {
# Check for Dialog and install if not found
if [ ! -e "/Library/Application Support/Dialog/Dialog.app" ]; then
preFlight "swiftDialog not found. Installing..."
dialogInstall
else
dialogVersion=$(/usr/local/bin/dialog --version)
if [[ "${dialogVersion}" < "${swiftDialogMinimumRequiredVersion}" ]]; then
preFlight "swiftDialog version ${dialogVersion} found but swiftDialog ${swiftDialogMinimumRequiredVersion} or newer is required; updating..."
dialogInstall
else
preFlight "swiftDialog version ${dialogVersion} found; proceeding..."
fi
fi
}
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Run command as logged-in user (thanks, @scriptingosx!)
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
function runAsUser() {
notice "Run \"$@\" as \"$loggedInUserID\" … "
launchctl asuser "$loggedInUserID" sudo -u "$loggedInUser" "$@"
}
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Quit Script (thanks, @bartreadon!)
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
function quitScript() {
notice "Quitting …"
updateProgressDialog "quit: "
sleep 1
logComment "Exiting …"
# Remove dialogWelcomeLog
if [[ -e ${dialogWelcomeLog} ]]; then
logComment "Removing ${dialogWelcomeLog} …"
rm "${dialogWelcomeLog}"
fi
# Remove dialogProgressLog
if [[ -e ${dialogProgressLog} ]]; then
logComment "Removing ${dialogProgressLog} …"
rm "${dialogProgressLog}"
fi
# Remove dialogCompleteLog
if [[ -e ${dialogCompleteLog} ]]; then
logComment "Removing ${dialogCompleteLog} …"
rm "${dialogCompleteLog}"
fi
# Remove diskUsageEntireVolumeTop50
if [[ -e ${diskUsageEntireVolumeTop50} ]]; then
logComment "Removing ${diskUsageEntireVolumeTop50} …"
rm "${diskUsageEntireVolumeTop50}"
fi
# Remove diskUsageUsersHomeTop50
if [[ -e ${diskUsageUsersHomeTop50} ]]; then
logComment "Removing ${diskUsageUsersHomeTop50} …"
rm "${diskUsageUsersHomeTop50}"
fi
logComment "Goodbye!"
exit "${1}"
}
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Update Progress Dialog
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
function updateProgressDialog() {
sleep 0.35
echo "${1}" >> "${dialogProgressLog}"
}
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Analyze Disk Usage for the entire volume
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
function analyzeDiskUsageEntireVolume() {
notice "Output disk usage statistics of \"${volumeName}\" to: ${diskUsageEntireVolumeTop50}"
du -I System -axrg / 2>/dev/null | sort -nr | head -n 50 >> "$diskUsageEntireVolumeTop50"
}
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Analyze Disk Usage for the user's home folder
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
function analyzeDiskUsageUsersHome() {
notice "Output disk usage statistics of \"$loggedInUserHome\" to: ${diskUsageUsersHomeTop50}"
du -axrg "$loggedInUserHome" 2>/dev/null | sort -nr | head -n 50 >> "$diskUsageUsersHomeTop50"
}
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Open Storage Information
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
function openStorageInformation() {
if [[ "${osMajorVersion}" -ge 13 ]] ; then
runAsUser open x-apple.systempreferences:com.apple.settings.Storage
else
runAsUser open /System/Library/CoreServices/Applications/Storage\ Management.app
fi
}
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Output Results for the entire volume
# shellcheck disable=SC2129
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
function outputResultsEntireVolume() {
notice "Disk usage for volume \"$volumeName\" on computer \"$machineName\" "
logComment "Report Location: $outputFileNameEntireVolume"
echo "--------------------------------------------------------------------------------------------------" > "$outputFileNameEntireVolume"
echo "Disk usage for volume \"$volumeName\" on computer \"$machineName\" " >> "$outputFileNameEntireVolume"
echo "Disk Space: $diskSpace" >> "$outputFileNameEntireVolume"
echo "Report Location: $outputFileNameEntireVolume" >> "$outputFileNameEntireVolume"
echo "--------------------------------------------------------------------------------------------------" >> "$outputFileNameEntireVolume"
echo " " >> "$outputFileNameEntireVolume"
echo " " >> "$outputFileNameEntireVolume"
echo " " >> "$outputFileNameEntireVolume"
echo "GBs Directory or File" >> "$outputFileNameEntireVolume"
echo " " >> "$outputFileNameEntireVolume"
cat "${diskUsageEntireVolumeTop50}" >> "$outputFileNameEntireVolume"
echo " " >> "$outputFileNameEntireVolume"
echo "--------------------------------------------------------------------------------------------------" >> "$outputFileNameEntireVolume"
}
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Output Results or the user's home folder
# shellcheck disable=SC2129
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
function outputResultsUsersHome() {
notice "Disk usage for \"$loggedInUserHome\" for volume \"$volumeName\" on computer \"$machineName\" "
logComment "Report Location: $outputFileNameUsersHome"
echo "--------------------------------------------------------------------------------------------------" > "$outputFileNameUsersHome"
echo "Disk usage for \"$loggedInUserHome\" for volume \"$volumeName\" on computer \"$machineName\" " >> "$outputFileNameUsersHome"
echo "Disk Space: $diskSpace" >> "$outputFileNameUsersHome"
echo "Report Location: $outputFileNameUsersHome" >> "$outputFileNameUsersHome"
echo "--------------------------------------------------------------------------------------------------" >> "$outputFileNameUsersHome"
echo " " >> "$outputFileNameUsersHome"
echo " " >> "$outputFileNameUsersHome"
echo " " >> "$outputFileNameUsersHome"
echo "GBs Directory or File" >> "$outputFileNameUsersHome"
echo " " >> "$outputFileNameUsersHome"
cat "${diskUsageUsersHomeTop50}" >> "$outputFileNameUsersHome"
echo " " >> "$outputFileNameUsersHome"
echo "--------------------------------------------------------------------------------------------------" >> "$outputFileNameUsersHome"
echo "
###
# Time Machine Information
###
" >> "$outputFileNameUsersHome"
tmDestinationInfo=$( tmutil destinationinfo )
if [[ "${tmDestinationInfo}" == *"No destinations configured"* ]]; then
echo "WARNING: Time Machine destination NOT configured." >> "$outputFileNameUsersHome"
else
tmutil listlocalsnapshots / >> "$outputFileNameUsersHome"
echo "
---
- Thin Local Time Machine Snapshots
---
Thinning local Time Machine snapshots can quickly free up disk space by PERMANENTLY deleting local Time Machine snapshots.
man tmutil
thinlocalsnapshots mount_point [purge_amount] [urgency]
Thin local Time Machine snapshots for the specified volume.
When purge_amount and urgency are specified, tmutil will attempt (with urgency level 1-4)
to reclaim purge_amount in bytes by thinning snapshots.
If urgency is not specified, the default urgency will be used.
ABSOLUTELY UNSUPPORTED EXAMPLES TO BE USED AT YOUR OWN RISK:
# Free 20 GB of snapshots stored on the boot drive (with maximum urgency)
tmutil thinlocalsnapshots / 21474836480 4
# Free 36 GB of snapshots stored on the boot drive (with maximum urgency)
tmutil thinlocalsnapshots / 38654705664 4" >> "$outputFileNameUsersHome"
fi
}
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Analyze Disk Usage with Progress
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
function analyzeDiskUsageWithProgress() {
notice "Disk Space: $diskSpace"
logComment "Analyze Disk Usage with Progress (1 of 2)"
eval "$dialogProgress" & sleep 0.35
if [[ ${debugMode} == "true" ]]; then
debug "DEBUG MODE ENABLED"
updateProgressDialog "title: DEBUG MODE | $progressTitle"
updateProgressDialog "message: DEBUG MODE. Please wait for 10 seconds …"
sleep 2
updateProgressDialog "progresstext: Processing …"
sleep 2
updateProgressDialog "progresstext: Analyzing …"
sleep 2
updateProgressDialog "progresstext: Thinking …"
sleep 2
updateProgressDialog "progresstext: Almost done …"
sleep 2
updateProgressDialog "quit: "
else
###
# Analyze Disk Usage for entire volume
###
notice "Analyze Disk Usage for \"${volumeName}\""
updateProgressDialog "message: Analyze Disk Usage for \"${volumeName}\""
updateProgressDialog "progress: 0"
updateProgressDialog "progresstext: Initializing …"
SECONDS="0"
analyzeDiskUsageEntireVolume &
updateProgressDialog "progresstext: Analyzing …"
while [[ -n $(pgrep -x "du|sort|head") ]]; do
progressPercentage=$( echo "scale=2 ; ( $SECONDS / $estimatedTotalSeconds ) * 100" | bc | sed 's/\.00$//')
updateProgressDialog "progress: ${progressPercentage}"
updateProgressDialog "progresstext: Analyzing: ${progressPercentage}%"
done
updateProgressDialog "progress: 100"
updateProgressDialog "progresstext: Elapsed Time: $(printf '%dh:%dm:%ds\n' $((SECONDS/3600)) $((SECONDS%3600/60)) $((SECONDS%60)))"
info "Elapsed Time for \"${volumeName}\": $(printf '%dh:%dm:%ds\n' $((SECONDS/3600)) $((SECONDS%3600/60)) $((SECONDS%60)))"
outputResultsEntireVolume
###
# Analyze Disk Usage for user's home directory
###
notice "Analyze Disk Usage with Progress (2 of 2)"
logComment "Analyze Disk Usage for \"${loggedInUserHome}\""
updateProgressDialog "message: Analyze Disk Usage for \"${loggedInUserHome}\""
updateProgressDialog "progress: 0"
updateProgressDialog "progresstext: Initializing …"
SECONDS="0"
estimatedTotalSecondsUsersHome=$( echo "scale=2 ; ( $estimatedTotalSeconds * 0.6 )" | bc )
analyzeDiskUsageUsersHome &
updateProgressDialog "progresstext: Analyzing …"
while [[ -n $(pgrep -x "du|sort|head") ]]; do
progressPercentage=$( echo "scale=2 ; ( $SECONDS / $estimatedTotalSecondsUsersHome ) * 100" | bc | sed 's/\.00$//')
updateProgressDialog "progress: ${progressPercentage}"
updateProgressDialog "progresstext: Analyzing: ${progressPercentage}%"
done
updateProgressDialog "progress: 100"
updateProgressDialog "progresstext: Elapsed Time: $(printf '%dh:%dm:%ds\n' $((SECONDS/3600)) $((SECONDS%3600/60)) $((SECONDS%60)))"
info "Elapsed Time for \"${loggedInUserHome}\": $(printf '%dh:%dm:%ds\n' $((SECONDS/3600)) $((SECONDS%3600/60)) $((SECONDS%60)))"
outputResultsUsersHome
updateProgressDialog "quit: "
fi
}
####################################################################################################
#
# Pre-flight Checks
#
####################################################################################################
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Pre-flight Check: Client-side Logging
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
if [[ ! -f "${scriptLog}" ]]; then
touch "${scriptLog}"
if [[ -f "${scriptLog}" ]]; then
preFlight "Created specified scriptLog: ${scriptLog}"
else
fatal "Unable to create specified scriptLog '${scriptLog}'; exiting.\n\n(Is this script running as 'root' ?)"
fi
else
preFlight "Specified scriptLog '${scriptLog}' exists; writing log entries to it"
fi
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Pre-flight Check: Logging Preamble
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
preFlight "\n\n###\n# $humanReadableScriptName (${scriptVersion})\n# Operation Mode: ${operationMode}\n###\n"
preFlight "Initiating …"
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Pre-flight Check: Confirm script is running as root
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
if [[ $(id -u) -ne 0 ]]; then
fatal "This script must be run as root; exiting."
fi
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Pre-flight Check: Validate swiftDialog is installed
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
preFlight "Validate swiftDialog is installed"
dialogCheck
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Pre-flight Check: Complete
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
preFlight "Complete!"
####################################################################################################
#
# Program
#
####################################################################################################
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Create Welcome Dialog
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
notice "Create Welcome Dialog …"
eval "$dialogWelcome"
welcomeReturncode=$?
case ${welcomeReturncode} in
0) ## Process exit code 0 scenario here
info "${loggedInUser} clicked ${button1text};"
analyzeDiskUsageWithProgress
eval "$dialogComplete"
# shellcheck disable=SC2086
if [[ -f "${outputFileNameEntireVolume}" || "${outputFileNameUsersHome}" ]]; then
runAsUser open $outputFileNameEntireVolume
runAsUser open $outputFileNameUsersHome
openStorageInformation
quitScript "0"
else
warning "Something went sideways; couldn't find ${outputFileNameEntireVolume} or ${outputFileNameUsersHome}"
quitScript "1"
fi
;;
2) ## Process exit code 2 scenario here
info "${loggedInUser} clicked ${button2text};"
quitScript "0"
;;
3) ## Process exit code 3 scenario here
info "${loggedInUser} clicked ${infobuttontext};"
;;
4) ## Process exit code 4 scenario here
info "${loggedInUser} allowed timer to expire;"
quitScript "1"
;;
*) ## Catch all processing
info "Something else happened; Exit code: ${welcomeReturncode};"
quitScript "1"
;;
esac
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Exit
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
quitOut "End-of-line."
quitScript "0"