Skip to main content
Question

Disk Usage Report Self Service Policy


dan-snelson
Forum|alt.badge.img+28

To aid end-users in determining where all their hard drive space has gone, we have a Self Service policy which leverages du and outputs text files of the top 75 directories of the root volume and the user's home folder. The utilitarian results are sorted by size, in gigabytes, and saved to the user's desktop.

Updated (28-May-2022)

Please see this post for a macOS Monterey-compatible version.

Self Service Description

Disk Usage Report Outputs two text files to your Desktop, which includes a listing of the top 75 directories of the root volume and the top 75 directories of your user folder. The results are saved to your desktop as: Computer-DiskUsage.txt and {YourUserName}-DiskUsage.txt.

Disk Usage: Root Volume

#!/bin/sh
####################################################################################################
#
# ABOUT
#
#   Disk Usage
#
####################################################################################################
#
# HISTORY
#
#   Version 1.0, 8-Dec-2014, Dan K. Snelson
#
####################################################################################################
# Import logging functions
source /path/to/logging/script/logging.sh
####################################################################################################

loggedInUser=`/bin/ls -l /dev/console | /usr/bin/awk '{ print $3 }'`
loggedInUserHome=`dscl . -read /Users/$loggedInUser | grep NFSHomeDirectory: | cut -c 19- | head -n 1`
machineName=`scutil --get LocalHostName`

/bin/echo "`now` *** Calculate Disk Usage for / ***" >> $logFile

/usr/bin/du -axrg / | sort -nr | head -n 75 > $loggedInUserHome/Desktop/$machineName-ComputerDiskUsage.txt


exit 0      ## Success
exit 1      ## Failure

Disk Usage: Home Directory

#!/bin/sh
####################################################################################################
#
# ABOUT
#
#   Disk Usage
#
####################################################################################################
#
# HISTORY
#
#   Version 1.0, 8-Dec-2014, Dan K. Snelson
#
####################################################################################################
# Import logging functions
source /path/to/logging/script/logging.sh
####################################################################################################

loggedInUser=`/bin/ls -l /dev/console | /usr/bin/awk '{ print $3 }'`
loggedInUserHome=`dscl . -read /Users/$loggedInUser | grep NFSHomeDirectory: | cut -c 19- | head -n 1`

/bin/echo "`now` *** Calculate Disk Usage for $loggedInUserHome  ***" >> $logFile

/usr/bin/du -axrg "$loggedInUserHome" | sort -nr | head -n 75 > "$loggedInUserHome"/Desktop/"$loggedInUser"-DiskUsage.txt


exit 0      ## Success
exit 1      ## Failure

 

38 replies

Forum|alt.badge.img+12
  • Contributor
  • 115 replies
  • June 3, 2015

This looks fantastic, I will definitely use this. Thanks for sharing it.


Forum|alt.badge.img+8
  • Contributor
  • 56 replies
  • June 3, 2015

@dan.snelson how did you get the end of the text bold in your self service item? I tried to use html code thinking that might do it but it didn't. Thanks for sharing this.


dan-snelson
Forum|alt.badge.img+28
  • Author
  • Honored Contributor
  • 632 replies
  • June 3, 2015

Forum|alt.badge.img+8
  • Contributor
  • 56 replies
  • June 3, 2015

@dan.snelson thanks that worked.


Forum|alt.badge.img+12
  • Contributor
  • 115 replies
  • June 4, 2015

@dan.snelson Dan, dumb question, can you explain the purpose of the source command at the start of the script? Where do you set the path?


mm2270
Forum|alt.badge.img+16
  • Legendary Contributor
  • 7880 replies
  • June 4, 2015

Silly question, but why did you decide to split these into 2 scripts? I assume they are both in the same policy given the Self Service description, so why not have both du commands run in one script, one after the other?

Also, for getting the loggedInUserHome, if you want, you can cut that command down to this:

dscl . -read /Users/$loggedInUser NFSHomeDirectory | awk '{print $NF}'

No need for grep, cut and head.


dan-snelson
Forum|alt.badge.img+28
  • Author
  • Honored Contributor
  • 632 replies
  • June 4, 2015

@dferrara The source command imports an external script (in this case, a logging script which we put on machines during imaging / enrollment).

@mm2270 Our Tier 1, 2 and 3 support personnel sometimes run this for end-users via Casper Remote and they like the flexibility of running one, the other, or both. (I like your loggedInUserHome better than the one I'm using; thanks.)


russeller
Forum|alt.badge.img+15
  • Valued Contributor
  • 215 replies
  • June 6, 2015

@dan.snelson Thanks for sharing. Can you elaborate on your logging script?


acodega
Forum|alt.badge.img+15
  • Valued Contributor
  • 383 replies
  • June 7, 2015

This is great, easier to do than deploying OmniDiskSweeper or the like. Thanks for the contribution!


Forum|alt.badge.img+16
  • Valued Contributor
  • 1002 replies
  • June 8, 2015

Just created a variation on this as well, thanks for the idea.

Added this bit at the end to make it open automatically in the users browser (obviously you will need to work out the variables first).

if [ -f "/Users/$CURRENT_USER/$FILE_NAME" ]; then
su - $CURRENT_USER -c "open -a safari /Users/$CURRENT_USER/$FILE_NAME"
fi

dan-snelson
Forum|alt.badge.img+28
  • Author
  • Honored Contributor
  • 632 replies
  • June 8, 2015

@ssrussell Our logging script was inspired by Stack Overflow and is installed on each client during imaging or when enrollment is completed. (My personal preference is to not write anything to jamf.log so that JAMF support isn't slowed down by my nonsensical gibberish when debugging their code.)

#!/bin/sh
####################################################################################################
#
# ABOUT
#
#   Standard logging functions which are imported into other scripts
#
####################################################################################################
#
# HISTORY
#
#   Version 1.0, 22-Nov-2014, Dan K. Snelson
#
####################################################################################################
#
# LOGGING
#
# Logging variables
    logFile="/var/log/com.company.division.department.log"
    alias now="date '+%Y-%m-%d %H:%M:%S'"
#
# Check for / create logFile
    if [ ! -f "${logFile}" ]; then
        ### logFile not found; Create logFile
        /usr/bin/touch "${logFile}"
    fi
# Save standard output and standard error
    exec 3>&1 4>&2
# Redirect standard output to logFile
    exec 1>>"${logFile}"
# Redirect standard error to logFile
    exec 2>>"${logFile}"
#
# Example
#   /bin/echo "`now` Hello, world!" >> $logFile
#
# Enable all logging from this point forward
#   set -xv; exec 1>"${logFile}" 2>&1
#
####################################################################################################

dan-snelson
Forum|alt.badge.img+28
  • Author
  • Honored Contributor
  • 632 replies
  • June 8, 2015

@Look Nice. Simplifies the end-user finding the output.


Forum|alt.badge.img+7
  • Contributor
  • 66 replies
  • April 13, 2016

This is exactly what I'm looking for to help my company slim down their hard drives, we have too many users using 90% +

Is there a simple way you can think to get the results sent to someone else or back to the JAMF server?


mm2270
Forum|alt.badge.img+16
  • Legendary Contributor
  • 7880 replies
  • April 13, 2016

@rgranholm You could look at sending the resulting data in an email to yourself, or a dist from the command line using Unix mail.

echo "$some_data" | mail -s "My Subject" rgranholm@company.com

There are a few other more advanced options you can throw in there, but the above is the basic idea. For example, since we're talking about a very simple text (log) file, you can try attaching it using uuencode:

uuencode "${logFile}" com.company.division.department.log | mail -s "My Subject" rgranholm@company.com

Or include a message body along with the attachment:

(echo "This is my message body"; uuencode "${logFile}" com.company.division.department.log) | mail -s "My Subject" rgranholm@company.com

Edit: forgot to mention that, using the JSS API, you can also attach the log file as an attachment to the computer record in the JSS. That way anyone with access to the detailed computer records could download and view it.
And finally, there's including the report information in an EA. Meaning, pipe the results of the policy into the log file as per above, then have an EA script pick up the contents so its viewable directly within the record. Only issue with the latter approach is it may be a lot of info to have in an EA field, so you may want to try some of the other approaches.


Forum|alt.badge.img+7
  • Contributor
  • 66 replies
  • April 14, 2016

@mm2270 .. thanks! I'm a complete novice when it comes to scripting so I'm copying and pasting and testing my way there.

I'm testing adding the uuencode line to the end of the script, but it spit an error that said "uuencode: : No such file or directory....I'm unsure how to define the location of that log file.

I'm really interested in attaching the log file to the computer record, would it be too much trouble to show me how I'd need to edit the script to do this? ... the e-mail option works as well.

Thank you!


mm2270
Forum|alt.badge.img+16
  • Legendary Contributor
  • 7880 replies
  • April 14, 2016

@rgranholm See modified version of Dan's script from above. This is the one that does the top level disk usage, but the principle could be used for the other script as well. Keep in mind I copied his logging script into /private/var/scripts/ on my Mac, so its referenced in the source import line below. You'd need to customize that if you put it in a different location and name it something else.
The other items that need to be changed would be the emailRec variable, which would be either a single email address, or any number of email addresses separated by a comma (no spaces) And also any text in the echo that creates the body of the email.
The end result will be an email with the attachment with whatever you specify as the name, and a subject and body that reflect what machine its coming from.
See screenshot below (with some items blocked out). As you can see, it includes the computer name in both the file name as well as subject and body text. I also sent it both to my company email and my personal gmail account as seen below.

Here's the modified script:

#!/bin/sh
####################################################################################################
#
# ABOUT
#
#   Disk Usage
#
####################################################################################################
#
# HISTORY
#
#   Version 1.0, 8-Dec-2014, Dan K. Snelson
#
####################################################################################################
# Import logging functions
source /var/scripts/logging.sh
####################################################################################################

loggedInUser=$(stat -f%Su /dev/console)
loggedInUserHome=$(dscl . read /Users/$loggedInUser NFSHomeDirectory | awk '{print $NF}')
machineName=$(scutil --get LocalHostName)

/bin/echo "`now` *** Calculate Disk Usage for / ***" >> $logFile

/usr/bin/du -axrg / | sort -nr | head -n 75 > $loggedInUserHome/Desktop/$machineName-ComputerDiskUsage.txt

## var of path to log file
userLogFile="$loggedInUserHome/Desktop/$machineName-ComputerDiskUsage.txt"
logFileName="$machineName-ComputerDiskUsage.txt"

## Email recipient address
emailRec="somebody@mycompany.com"

(echo "Disk usage report attached for ${machineName}"; uuencode "$userLogFile" "$logFileName") | mail -s "Disk Usage Report - ${machineName}" "$emailRec"

Few other items: you can also add both cc's and bcc's to any email by doing something like:

-c somebody2@company.com -b somebody3@company.com

The -c is CC and -b is BCC

One last thing on the unix email option. YMMV on whether it will actually work. I've seen some environments that block sending email from the command line, and the email ends up stuck in the local machine's outbox. So you'll need to test this under as many conditions as you can to ensure it works.

As for attaching to the computer record in the JSS, give me a few and I'll post some code to do that as well. In the interim, feel free to search around on here because I know there are existing examples of using the API to attach a file. In fact, I think I may have already posted an example on another older thread somewhere on how to do that.


Forum|alt.badge.img+7
  • Contributor
  • 66 replies
  • April 14, 2016

Hmm immediately fails with uuencode: /Users/rgranholm/Desktop/RFG-MbProR15-ComputerDiskUsage.txt: No such file or directory

Seems as though it tries to execute uuencode first, I'll keep tinkering


Forum|alt.badge.img+7
  • Contributor
  • 66 replies
  • April 14, 2016

Ah nevermind, saw the comments out line for generating the .txt file, removed that and made progress.


mm2270
Forum|alt.badge.img+16
  • Legendary Contributor
  • 7880 replies
  • April 14, 2016

Oops, yeah, I did comment that out toward the end and forgot to remove it before posting. Sorry for the confusion. Post above updated.

Edit: To be clear though, you should only remove the comment hash sign, not the entire line, since that actually does the bulk of the work of generating the report file.


dan-snelson
Forum|alt.badge.img+28
  • Author
  • Honored Contributor
  • 632 replies
  • April 14, 2016

@rgranholm: Thanks for the idea …@mm2270: Thanks for pulling it off; I'll have to try it out.


Forum|alt.badge.img+7
  • Contributor
  • 66 replies
  • April 14, 2016

No problem, it executed, but no email was received.

I've been testing it by executing it as a shell .sh file in terminal, you mentioned some environments wouldn't play nice with e-mail so I'll look into that next.

The terminal gave me the below output, and several more denied folders...but otherwise produced the text file.

RFG-MbProR15:Desktop rgranholm$ sh diskusage.sh 
diskusage.sh: line 23: now: command not found
diskusage.sh: line 23: $logFile: ambiguous redirect
du: /.DocumentRevisions-V100: Permission denied
du: /.fseventsd: Permission denied
du: /.Spotlight-V100: Permission denied
du: /.Trashes: Permission denied

dan-snelson
Forum|alt.badge.img+28
  • Author
  • Honored Contributor
  • 632 replies
  • April 14, 2016

@rgranholm Since the JAMF binary executes scripts as root, you may want to elevate your permissions in your shell to simulate how the JSS will execute scripts.


Forum|alt.badge.img+7
  • Contributor
  • 66 replies
  • April 14, 2016

Thanks Dan, I'll give that a try now.


mm2270
Forum|alt.badge.img+16
  • Legendary Contributor
  • 7880 replies
  • April 14, 2016

@rgranholm As Dan mentioned, you need to run the script as root since du is going to need to scan a bunch of locations that may otherwise have permissions protections on them.
Plus, it needs to be able to both create and write to the log file, which I think by default is going into a normally root protected location. I was not able to run the script normally in my user context and have it work. Only when I put sudo in front of it did it work.

Also, that ambiguous redirect makes me think you didn't set up the logging script that @dan.snelson posted up earlier in the thread. Its going to try to reference that for logging purposes and if it can't locate it it won't really work.

Hope that helps.


Forum|alt.badge.img+7
  • Contributor
  • 66 replies
  • April 14, 2016

Hmm using sudo eliminated all the permission issues.

I put a blank txt file in the var/scripts location named logging.sh see here: http://take.ms/VSGjo

Howver I still do get the below

diskusage.sh: line 23: now: command not found
diskusage.sh: line 23: $logFile: ambiguous redirect


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings