Home > General > Shell script to find the disk space and report when disk is full

Shell script to find the disk space and report when disk is full

September 29, 2011

#!/bin/bash

# Set thresholds
min_free_space=2000
max_in_use_space=75

# Get a list of all file systems.
filesystems=`df -k | grep -v Use | grep -v none | awk ‘{ print $6 }’`

for filesystem in $filesystems
do
# Results for this file system.
entry=`df -k $filesystem | tail -1`

# Split out the amount of space free as well as in-use percentage.
free_space=`echo $entry | cut -d’ ‘ -f4`
in_use_space=`echo $entry | cut -d’ ‘ -f5 | cut -d’%’ -f1 `

# Check the file system percent in use.
if [ $(expr “$in_use_space > $max_in_use_space” ) ]
then
echo “$filesystem has only $free_space KB free at $in_use_space%.”
else
# Check the available space against threshold.
# Only make this check if the in use is OK.

result=$( echo ”
scale=2 /* two decimal places */
print $free_space < $min_free_space” | bc)

#if [ $(expr “$free_space < $min_free_space” ) ]
if [ $(expr “$result != 0” ) ]
then
echo “$filesystem has only $free KB free.”
fi
fi

done

 

URL : http://www.togotutor.com/forums/shell/58-shell-script-find-disk-space-report-when-disk-full.html

Advertisements
Categories: General
%d bloggers like this: