Simple Shell Script to Monitoring Disk Space on a Linux Machine
#Reference: df
echo “$df” | while read percent fs
do
#If Disk Usage increases above 90% send an email.
if [ $percent -ge 90 ] ; then
#Calling a perl file with parameters for sending an email.
`/opt/lampp/bin/perl DiskSpace_Alert.pl $fs is $percent percent full`
fi
done
This article explain a way to get a mail as soon as the disk usage reaches to its critical level to avoid issues later. To set a simple monitor on Linux / Unix, I have two simple scripts:
- DSAlert.sh : Shell script for retrieving disk space percentages and put in to a cron job.
- DiskSpace_Alert.pl : Perl script for sending an email.
Following is the disk monitor shell script which will execute the perl file for sending alert email.
#DSAlert.sh
#!/bin/sh
#Retrive disk space info
df=`df -Pl | grep "^/dev" | awk '{print $5, $6}' | sed "s/%//"`
#Reference: df
echo “$df” | while read percent fs
do
#If Disk Usage increases above 90% send an email.
if [ $percent -ge 90 ] ; then
#Calling a perl file with parameters for sending an email.
`/opt/lampp/bin/perl DiskSpace_Alert.pl $fs is $percent percent full`
fi
done
Perl Script for sending an email:
# DiskSpace_Alert.pl
#!perl
use MIME::Lite;
#!perl
use MIME::Lite;
#Reference : Mime-Lite
# set up email
$to = “my-mail-id\@domain.com”;
$from = “Diskmonitor\@ServerName.com”;
$subject = “Disk_Alert”;
$message = “Disk Space issue.\nActions Required:\n”.”@ARGV”;
# send email
email($to, $from, $subject, $message, $file);
$to = “my-mail-id\@domain.com”;
$from = “Diskmonitor\@ServerName.com”;
$subject = “Disk_Alert”;
$message = “Disk Space issue.\nActions Required:\n”.”@ARGV”;
# send email
email($to, $from, $subject, $message, $file);
# email function
sub email
{
# get incoming parameters
local ($to, $from, $subject, $message, $file) = @_;
# create a new message
$msg = MIME::Lite->new(
From => $from,
To => $to,
Subject => $subject,
Data => $message);
# send the email
sub email
{
# get incoming parameters
local ($to, $from, $subject, $message, $file) = @_;
# create a new message
$msg = MIME::Lite->new(
From => $from,
To => $to,
Subject => $subject,
Data => $message);
# send the email
MIME::Lite->send(‘smtp’, ‘localhost’, Timeout => 60);
$msg->send();
}
Setting up a crontab for monitoring:
$msg->send();
}
Setting up a crontab for monitoring:
Crontab –e
* * * * * sh full/path/to/DSAlert.sh
And we’re done with the Linux/Unix Disk Monitoring alerts.
Instructions for setting up timing for crontab:
+----------------> minute (0 - 59)
| +-------------> hour (0 - 23)
| | +----------> day of month (1 - 31)
| | | +-------> month (1 - 12)
| | | | +----> day of week (0 - 6) (Sunday=0 or 7)
| | | | |
* * * * *
0 comments:
Post a Comment