#!/bin/sh ## ## SCRIPT NAME: get_fileSystems_spaceData.sh ## ## PURPOSE: ## This script gets 'allocated' and 'used' data (in Gigabytes) via output ## from the 'df -k' command, where '-k' means the data is shown in 1K-blocks. ## ## This shell script is meant to be capable of returning info of 3 types: ## 1) a report of the status of ALL the file systems known to 'df' ## 2) a list of the file-system mount-points ## 3) %-used, allocated-Gigabytes, used-Gigabytes for a user-specified ## mount-point. ## ## This script takes a single parameter --- a text string --- to indicate ## which of these 3 types of info to return: ## 1) 'all' ## 2) 'mounts' ## 3) the name of a mount point such as '/' or '/home' or '/var', ## if these are among the mount points returned by option-2. ## ## Example output from the 'df -k' command: ## ## Filesystem 1K-blocks Used Available Use% Mounted on ## /dev/sda1 76896316 3468088 69522028 5% / ## udev 507896 280 507616 1% /dev ## none 507896 184 507712 1% /dev/shm ## none 507896 80 507816 1% /var/run ## none 507896 0 507896 0% /var/lock ## none 507896 0 507896 0% /lib/init/rw ## /dev/sda5 75972964 31460380 40653372 44% /home ## ## 1) ## If 'all' is passed, all the data from the 'df -k' command is used, ## although it is reformatted into Gigabytes in the '1K-blocks', 'Used', and ## 'Available' columns. The data is SORTED by the PERCENT-USED column. ## The reformatted, sorted columnar data is returned by this script --- ## to stdout (standard-out). ## ## 2) ## If 'mounts' is passed, a list of the mount points is passed. ## For a computer with only a 'root' file system, then only '/' will be ## in the list that is returned (to stdout). ## For a computer with a separate filesystem mounted at '/home', ## then the two lines ## ## / ## /home ## ## will be in the list that is returned (to stdout). ## ## 3) ## If a mount point is passed (like '/' or '/home'), ## the %-used, allocated-Gigabytes, and used-Gigabytes data ## from the 'df" output line for that mount point is returned. Example: ## A line like ## ## /home 46 75.973 32.530 39.584 /dev/sda5 ## ## may be returned to stdout, where the items are ## mount-point, %-used, Allocated-Gig, Used-Gig, Free-Gig, Filesys-device-name. ## ##+######### ## CALLED BY: a Tk GUI script that shows filesystem 'Allocated' and 'Used' ## data as PERCENT-used needle readings on one or more meters ## (dials) drawn on a Tk canvas --- Tk script name: ## meters_forFileSystems_usage.tk ## ## The 'all' output is intended to be used to show ALL the output ## from 'df -k' (reformatted) in a text-window of the Tk script GUI. ## ## The 'slash' output is intended to be used to position the ## needle of a tachometer-style needle of the Tk script GUI. ## ##+################### ## MAINTENANCE HISTORY: ## Updated by: Blaise Montandon 2013sep02 Started this script on Linux, ## using Ubuntu 9.10 (2009 October, ## 'Karmic Koala'). ## Updated by: Blaise Montandon 2013oct31 Added to comments and changed-fixed ## some 'awk' parms. ## Updated by: Blaise Montandon 2013nov01 Added 'mounts' option. ##+######################################################################### ## FOR TESTING: (show statements as they execute) # set -x if test "$1" = "all" then ########################################################################## ## SET REPORT HEADING. ########################################################################## HOST_ID="`hostname`" echo "\ ......................... `date '+%Y %b %d %a %T%p %Z'` ........................ DISK USAGE (in Gigabytes) IN FILE SYSTEMS ON HOST *** $HOST_ID *** SORTED BY *PERCENT-USED* --- HIGHEST %-USED AT THE TOP ************ ****************** LOCAL FILE-SYSTEMS: ****************** FileSystem Directory ****** ALLOCATED USED AVAILABLE Device-partition, (Filesystem Mount Point) % USED Gigabytes Gigabytes Gigabytes if any ------------------------- ------ ---------- ---------- ---------- ---------------- " ########################################################################## ## GENERATE THE REFORMATTED, SORTED DATA from the 'df -kl' command. ## ('l' for local file systems, not NFS-mounted). ## ## Note that the utilities 'sed', 'sort', and 'awk' are used. ########################################################################## ## FOR TESTING: (show statements as they execute) # set -x df -kl | sed '1d;s/\%/ /g' | sort -n -r -k5 | awk \ '{printf ("%-25s %6s %11.3f %10.3f %10.3f %s \n\n", $6, $5, $2/1000000, $3/1000000, $4/1000000, $1)}' ########################################################################## ## ADD REPORT 'TRAILER'. ########################################################################## SCRIPT_BASENAME=`basename $0` SCRIPT_DIRNAME=`dirname $0` echo " ......................... `date '+%Y %b %d %a %T%p'` ........................ NOTE1: This file-systems report is SORTED by %-Used .... LARGEST %-Used FIRST. Hence the file-system on the first line may be the one of most immediate concern, if the %-Used is greater than 85%, say. ............................................................................. The output above is from script $SCRIPT_BASENAME in directory $SCRIPT_DIRNAME It ran the 'df' command on host $HOST_ID . The script uses a 'pipe' of several commands (df, sed, sort, awk) like: df -kl | sed ... | sort -n -r -k5 | awk '{printf ( ... ) }' ............................................................................. NOTE2: The 'l' option of the 'df' command specifies that info is requested only for 'local' file systems. This utility could be enhanced to also show 'df' data for 'nfs' mounted file systems. NOTE3: This utility provides columnar formatting (in Gigabytes only) and sorting that is not available by use of the 'df' command by itself. ............................................................................. ......................... `date '+%Y %b %d %a %T%p %Z'` ........................ " exit fi ## END OF the if "$1" = "all" SECTION. ##+############################################################# ## THE FOLLOWING IS EXECUTED IF $1 CONTAINS 'mounts'. ## ## The output goes to standard-out. ## ## The following pipe of commands is used: ## 1) Uses 'grep' to get the lines that start with a slash (/) ## in the first ('Filesystem') column. ## 2) Uses 'awk' to reformat the output --- retrieving only ## column 6 (mount-point name) from each line. ## 3) Uses 'grep' to get the mount-point names that start with ## slash (/). ##+############################################################ if test "$1" = "mounts" then df -kl | grep '^/' | awk '{printf ("%s \n", $6)}' | grep '^/' exit fi ## END OF the if "$1" = "mounts" SECTION. ##+############################################################# ## THE FOLLOWING IS EXECUTED IF $1 CONTAINS ANYTHING other than, ## 'all' or 'mounts'. It is assumed that $1 contains a mount-point ## name such as '/' or '/home'. ## ## The output goes to standard-out. ## ## The following pipe of commands is used: ## 1) Uses 'grep' to get the lines that start with a slash (/) ## in the first ('Filesystem') column. ## 2) Uses 'grep' to get the line containing a string (mount-point name) ## matching the string in $1. ## 3) Uses 'awk' to reformat the output --- changing kilobytes ## to gigabytes, and re-ordering the columns to 6,5,2,3,4,1. ## 4) Uses 'sed' to change the '%' character to a space. ## 5) Uses 'head' to take the first line --- in case more than ## one line is selected by the 2 grep's. ##+############################################################# df -kl | grep '^/' | grep "$1" | awk \ '{printf ("%s %6s %11.3f %10.3f %10.3f %s \n", $6, $5, $2/1000000, $3/1000000, $4/1000000, $1)}' \ | sed 's/\%/ /g' | head -1 ## EXAMPLE OUTPUT FROM THE 'PIPE' ABOVE: ## ## /home 46 75.973 32.530 39.584 /dev/sda5 ##