Bash monitor disk usage
Asked Answered
V

5

8

I bought a NAS box which has a cut down version of debian on it.

It ran out of space the other day and I did not realise. I am basically wanting to write a bash script that will alert me whenever the disk gets over 90% full.

Is anyone aware of a script that will do this or give me some advice on writing one?

Villiers answered 13/4, 2011 at 11:7 Comment(1)
possible duplicate of Is there a shell script that can monitor partition usage?Impend
F
10
#!/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" [email protected]
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" [email protected]
  fi
done
Finecut answered 13/4, 2011 at 11:14 Comment(5)
That works. How would it be done for multiple disks? if you do not mind :)Villiers
Excellent works like a charm. I'll figure out how to automatically get the devices to check. Thanks!!Villiers
When you have long device names it helps to use 'df -Pk' which collapses the output down to one line otherwise you get a bug in thisGamesome
Erik, made some updates - feel free to integrate back into your answer and ill delete mineGamesome
Avoiding grep: df -k $devname | awk 'NR > 1 {printf ("%i",$5); }'Benkley
G
7

I tried to use Erik's answer but had issues with devices having long names which wraps the numbers and causes script to fail, also the math looked wrong to me and didn't match the percentages reported by df itself.

Here's an update to his script:

#!/bin/bash
source /etc/profile

# Devices to check
devnames="/dev/sda1 /dev/md1 /dev/mapper/vg1-mysqldisk1 /dev/mapper/vg4-ctsshare1 /dev/mapper/vg2-jbossdisk1 /dev/mapper/vg5-ctsarchive1 /dev/mapper/vg3-muledisk1"


for devname in $devnames
do
  let p=`df -Pk $devname | grep -v ^File | awk '{printf ("%i", $5) }'`
  if [ $p -ge 70 ]
  then
    df -h $devname | mail -s "$devname is low on space" [email protected]
  fi
done

Key changes are changed df -k to df -Pk to avoid line wrapping and simplified the awk to use pre-calc'd percent instead of recalcing.

Gamesome answered 19/3, 2013 at 15:43 Comment(1)
(changed percent to 70% for testing)Gamesome
H
0

You could also use Monit for this kind of job. It's a "free open source utility for managing and monitoring, processes, programs, files, directories and filesystems on a UNIX system".

Heliometer answered 5/1, 2012 at 13:29 Comment(0)
S
0

Based on @Erik answer, here is my version with variables :

#!/bin/bash

DEVNAMES="/ /home"
THRESHOLD=80
[email protected]

host=$(hostname)
for devname in $DEVNAMES
do
    current=$(df $devname | grep / | awk '{ print $5}' | sed 's/%//g')
    if [ "$current" -gt "$THRESHOLD" ] ; then
        mail -s "Disk space alert on $host" "$EMAIL" << EOF
WARNING: partition $devname on $host is $current% !!

To list big files (>100Mo) :
  find $devname -xdev -type f -size +100M
EOF
    fi
done

And if you do not have the mail command on your server, you can send email via SMPT with swaks :

swaks --from "$EMAIL" --to "$EMAIL" --server "TheServer" --auth LOGIN --auth-user "TheUser" --auth-password "ThePasswrd" --h-Subject "Disk space alert on $host" --body - << EOF
Sleight answered 20/1, 2021 at 15:31 Comment(0)
A
0
#!/bin/bash

DEVNAMES=$(df --output=source | grep ^/dev)
THRESHOLD=90
EMAIL=your@email
HOST=$(hostname)

for devname in $DEVNAMES
do
    current=$(df $devname | awk 'NR>1 {printf "%i",$5}')
    [ "$current" -gt "$THRESHOLD" ] && warn="WARNING: partition $devname on $HOST is $current% !! \n$warn"
done

[ "$warn" ] && echo -e "$warn" | mail -s "Disk space alert on $HOST" $EMAIL

Based on previous answers, here's my version with following changes:

  1. Automatically checks all mounted devices
  2. Sends only one mail per check, regardless of how many devices are over the threshold
  3. Code generally tidied up
Antisana answered 13/7, 2022 at 10:48 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.