Bash Monitor Disk Usage

shell script to monitor disk usage

How about this start:

du -sk <full-path-to-dir-to-watch>/* | sort -nr | head -n 10

du command will give you disk usage in blocks for all files/folders in that place you should be watching, sort with -n will sort by first column (usage figures), -r will reverse (print largest numbers 1st), then head will give you the first 10 items, the largest.

Put this in the shell script, etc... (you know better what else you wish to do)

Bash monitor disk usage

#!/bin/bash
source /etc/profile

# Device to check
devname="/dev/sdb1"

let p=`df -k $devname | grep -v ^File | awk '{printf ("%i",$3*100 / $2); }'`
if [ $p -ge 90 ]
then
df -h $devname | mail -s "Low on space" my@email.com
fi

Crontab this to run however often you want an alert

EDIT: For multiple disks

#!/bin/bash
source /etc/profile

# Devices to check
devnames="/dev/sdb1 /dev/sda1"

for devname in $devnames
do
let p=`df -k $devname | grep -v ^File | awk '{printf ("%i",$3*100 / $2); }'`
if [ $p -ge 90 ]
then
df -h $devname | mail -s "$devname is low on space" my@email.com
fi
done

Check free disk space for current partition in bash

Yes:

df -k .

for the current directory.

df -k /some/dir

if you want to check a specific directory.

You might also want to check out the stat(1) command if your system has it. You can specify output formats to make it easier for your script to parse. Here's a little example:

$ echo $(($(stat -f --format="%a*%S" .)))

Is there a shell script that can monitor partition usage?

don't know if there's already one, but it's not too hard to write. Just put this into your crontab:

df | awk 'NR>1 && $5>80 {print $1 " " $5}'

You should replace 80 with the threshold (percent used) you want to be alerted on. If will mail you the df output for all partitions that cross that usage level.



Related Topics



Leave a reply



Submit