Welcome to UltratechHost Forum, Guest. Please take the time to register if you haven't, or click

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5

[-]
Tags
monitor unix linux server disk space with shell script

Monitor UNIX / Linux Server Disk Space with Shell Script
#1
Shell script to monitor or watch the disk space and send an email alert if the (free avilable) percentage of space is >= 80%

Create a file, say, /root/serverspace.sh by running the command

Code:
nano /root/loadmonitor.sh

and add the bellow script

PHP Code:
#!/bin/sh
# Shell script to monitor or watch the disk space
# It will send an email to $ADMIN, if the (free avilable) percentage
# of space is >= 80%
# -------------------------------------------------------------------------
# Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# ----------------------------------------------------------------------
# Linux shell script to watch disk space (should work on other UNIX oses )
# SEE URL: http://www.cyberciti.biz/tips/shell-script-to-watch-the-disk-space.html
# set admin email so that you can get email
ADMIN="me@somewher.com"
# set alert level 90% is default
ALERT=80
df 
--grep -vE '^Filesystem|tmpfs|cdrom' awk '{ print $5 " " $1 }' | while read output;
do
  
#echo $output
  
usep=$(echo $output awk '{ print $1}' cut -d'%' -f1  )
  
partition=$(echo $output awk '{ print $2 }' )
  if [ 
$usep -ge $ALERT ]; then
    
echo "Running out of space \"$partition ($usep%)\" on $(hostname) as on $(date)" |
     
mail -"Alert: Almost out of disk space $usep$ADMIN
  fi
done 

Now, schedule a cronjob to execute the script on per minute basis. Edit the cronjob file


Code:
# crontab -e

or

nano /etc/crontab

and add the line at the end as

Code:
* * * * * /bin/sh /root/loadmonitor.sh

restart the crond service


Code:
# service crond restart
In order to use “mutt” to send emails, you need to install the mutt package on the server. It allows you to send emails with attachments.


Code:
# yum install mutt

Credit: Origional Auther for the Bash Shell script

Regards
Ultra Web Solutions Pvt Ltd



Dinesh Kumar Mohanty
Ultra Web Solutions®
Bhubaneswar, Odisha, India.

UltratechHost - Your One Stop Offshore & Onshore Hosting Provider
Server Locations : Netherlands, Germany, Russia, Singapore & USA
Service Offers: Shared hosting | Reseller hosting | Cloud server | Dedicated server | VPS Hosting



Forum Jump:


Users browsing this thread: 1 Guest(s)