Script for Filesystem Utilization Monitoring

October 9, 2016 by S4

Filed under Uncategorized

Last modified October 9, 2016

Script for Filesystem Utilization Monitoring

#! /bin/ksh

#

# This script checks the filesystem utilization of local filesystems

# and sends an email alert if disk space utilization is above the

# defined threshold.

#

# Script: filesys_spchk.ksh

# Version: 1.0

# Author: Santosh

# Email: sales@s4techno.com

 

# MAILTO=”mention email ids whom the alerts need to be sent”

#

# MAILTO=sales@s4techno.com

#

# You can also use aliases and define them in /etc/mail/aliases

MAILTO=”sys_admin”

 

HOSTNAME=`hostname`

TMPFILE=”/tmp/nkfstmp”

 

# Incase you want to exclude some filesystems from monitoring mention them below

# separated by  “|” (pipe)

 

Xclude=”cdrom|AFScache”

 

rm $TMPFILE

 

# Set the threshold value in the line below. Currently it is set to 80%

 

/usr/sbin/df -kl | egrep -v “Filesystem|$Xclude” | awk ‘BEGIN {THRESHOLD=”80″} { if ( $5 > THRESHOLD ) print $6 ” is ” $5 ” full ” }’ > $TMPFILE

 

if [ -s $TMPFILE ]

then

cat $TMPFILE | mailx -s “Filesystem Alert on $HOSTNAME ” $MAILTO

fi

 

 

1. Description

Above script filesys_spchk.ksh executes “df –kl “command and filters o/p through awk It compares the capacity column (#5 of the df o/p) with the threshold (THRESHOLD) defined.

If the filesystem capacity utilized is above the threshold it will send an email alert to the email id defined in variable MAILTO

You can also define an email alias in /etc/mail/aliases. Just append the existing file with a line as below.

sys_admin:admin1@s4techno.com,admin2@s4techno.com

Care has been taken to excludes remote filesystem which have been mounted from other server by using “-l” option in df.

If you want to exclude any of the filesystem from monitoring, enter them in the Exclude list  Ex:  Xclude=”cdrom|AFScache”

Note: Sendmail client need to be working for the system to send the mail to your mailbox.

 

2. How to set it up

1) Copy the above script as root (preferably) in to a directory, in this case say

/export/Scripts/filesys_spchk.ksh

2) Make sure the script is executable

 #chmod 755  /export/Scripts/filesys_spchk.ksh

3) Schedule the script in root cron to check the filesystem utilization as required in this case we check it at 7AM every day

0  7  *  *  *   /export/Scripts/filesys_spchk.ksh

Leave a Comment