Archive

Archive for May, 2018

Monitoring disk usage in Linux

May 10th, 2018 No comments

The below script can be used to automate disk usage checks on a Linux machine.
It sends an email if the usage per device is above 80% (warning) and 90%(critical).
It’s written in bash, it’s not set in stone, can be done in various ways, a lot more checks to be added, but hope it helps.

#!/bin/bash

mountdev=($(df -h | awk -F" " '{print $1}' | tail -n +2))
usage=($(df -h | awk -F" " '{print $5}' | tail -n +2 | awk -F"%" '{print $1}'))
warning=0
critical=0
workpath=`dirname $0`

function send_email {
    MAILTO="yourname@yourdomain.com"
    MAILFROM="notifications@yourdomain.com"
    SUBJECT=$1
    bodyString=`cat $2`
    echo -e "From: ${MAILFROM}\nTo: ${MAILTO}\nSubject:${SUBJECT}\n\n${bodyString}" | /usr/sbin/sendmail -f ${MAILFROM} ${MAILTO}
}


echo > ${workpath}/warning.txt
echo > ${workpath}/critical.txt

for ((i=0;i<${#usage[@]};i++)); do
    if [ ${usage[$i]} -ge 80 ] && [ ${usage[$i]} -lt 90 ]; then
        echo "${mountdev[$i]} disk usage is above 80% - Current usage at ${usage[$i]}" >> ${workpath}/warning.txt
        warning=1
    else
        if [ ${usage[$i]} -ge 90 ]; then
            echo "${mountdev[$i]} disk usage is above 90% - Current usage at ${usage[$i]}" >> ${workpath}/critical.txt
            critical=1
        fi
    fi
done
if [ ${critical} -eq 1 ]; then
    send_email  "[Critical] Disk usage above 90%" ${workpath}/critical.txt
else
    if [ ${warning} -eq 1 ]; then
        send_email  "[Warning] Disk usage above 80%" ${workpath}/warning.txt
    fi
fi
Categories: Bash, IT, Scripting Tags:

Welcome!

May 10th, 2018 No comments
#!/bin/bash
echo "Hello World!"
Categories: General Tags:
%d bloggers like this: