LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-12-2009, 12:05 PM   #1
cmontr
Member
 
Registered: Sep 2007
Posts: 175

Rep: Reputation: 15
script for disk space alert


Could someone help me to automate disk space alert at 70 file system and send an email or text msg on solaris server? I tried this perl script but it has failed. If you have another versions please let me know. Thank you so much.

Script I tried:

#!/usr/bin/perl -w
# Script to check free diskspace and email notifications. Change the email and alert levels and you should be good to go.
# created by lb
use strict;

# Alert levels Warning and Critical - Below what percent level of free disk space do you want an alert?
my $alert1 = 40; #Warning level free space below 30%
my $alert2 = 20; #Critical level free space below 10%

# Put the email address to notify here
my $email = 'abc@abcn.com';

my ($size,$used,$avail,$use,$mounted);
my $message;
my @list;
my $sysname = `/bin/uname -n`;
chomp $sysname;
my @df = `/bin/df`;
my $df;
foreach $df (@df) {
if ($df =~ /\/\n/) {
@list = split(/\s+/, $df);
}
else {next;}
}

# Check the usage
my $diskfree = (($list[3]) / ($list[2]+$list[3])) * 100.00;

# Round the number off to 2 decimals
$diskfree = sprintf("%.2f", $diskfree);

# See if free disk space is below any of our levels
if ( ($diskfree < $alert1) && ($diskfree > $alert2) ) {
$message = "Warning Diskspace threshold reached...free space below $alert1% at $diskfree%\n";
&mailer;
}

elsif ( ($diskfree < $alert1) && ($diskfree < $alert2) ) {
$message = "Critical Diskspace threshold reached...free space below $alert2% at $diskfree%\n";
&mailer;
}

else {
$message = "Free diskspace is good at $diskfree%\n";
}

#Output to terminal (comment out if you wish)
print $message;
print "~" x 75, "\n@df","~" x 75,"\n","From system: $sysname\n";

#Subroutine for Mail, notifies on warning and critical levels.
sub mailer {
open(MAIL, "|/usr/sbin/sendmail -t") or die "Cannot open sendmail!: $!";
print MAIL "To: $email\n";
print MAIL "From: $sysname\n";
print MAIL "Subject: $message\n\n";
print MAIL "$message";
print MAIL "~" x 75, "\n@df","~" x 75,"\n","From system: $sysname";
close(MAIL);
}



Error I got was:

Use of uninitialized value in addition (+) at ./g.pl line 28.
Use of uninitialized value in addition (+) at ./g.pl line 28.
Use of uninitialized value in division (/) at ./g.pl line 28.
Illegal division by zero at ./g.pl line 28.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 01-12-2009, 12:26 PM   #2
cmontr
Member
 
Registered: Sep 2007
Posts: 175

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by cmontr View Post
Could someone help me to automate disk space alert at 70 file system and send an email or text msg on solaris server? I tried this perl script but it has failed. If you have another versions please let me know. Thank you so much.

Script I tried:

#!/usr/bin/perl -w
# Script to check free diskspace and email notifications. Change the email and alert levels and you should be good to go.
# created by lb
use strict;

# Alert levels Warning and Critical - Below what percent level of free disk space do you want an alert?
my $alert1 = 40; #Warning level free space below 30%
my $alert2 = 20; #Critical level free space below 10%

# Put the email address to notify here
my $email = 'abc@abcn.com';

my ($size,$used,$avail,$use,$mounted);
my $message;
my @list;
my $sysname = `/bin/uname -n`;
chomp $sysname;
my @df = `/bin/df`;
my $df;
foreach $df (@df) {
if ($df =~ /\/\n/) {
@list = split(/\s+/, $df);
}
else {next;}
}

# Check the usage
my $diskfree = (($list[3]) / ($list[2]+$list[3])) * 100.00;

# Round the number off to 2 decimals
$diskfree = sprintf("%.2f", $diskfree);

# See if free disk space is below any of our levels
if ( ($diskfree < $alert1) && ($diskfree > $alert2) ) {
$message = "Warning Diskspace threshold reached...free space below $alert1% at $diskfree%\n";
&mailer;
}

elsif ( ($diskfree < $alert1) && ($diskfree < $alert2) ) {
$message = "Critical Diskspace threshold reached...free space below $alert2% at $diskfree%\n";
&mailer;
}

else {
$message = "Free diskspace is good at $diskfree%\n";
}

#Output to terminal (comment out if you wish)
print $message;
print "~" x 75, "\n@df","~" x 75,"\n","From system: $sysname\n";

#Subroutine for Mail, notifies on warning and critical levels.
sub mailer {
open(MAIL, "|/usr/sbin/sendmail -t") or die "Cannot open sendmail!: $!";
print MAIL "To: $email\n";
print MAIL "From: $sysname\n";
print MAIL "Subject: $message\n\n";
print MAIL "$message";
print MAIL "~" x 75, "\n@df","~" x 75,"\n","From system: $sysname";
close(MAIL);
}



Error I got was:

Use of uninitialized value in addition (+) at ./g.pl line 28.
Use of uninitialized value in addition (+) at ./g.pl line 28.
Use of uninitialized value in division (/) at ./g.pl line 28.
Illegal division by zero at ./g.pl line 28.


#### I also tried this code but got error as well on solaris...Please let me know any ideas...thanks much...

#!/bin/ksh
TEMPDIR=$HOME
FILE=mail.log
sendemail=-1
HOSTNAME="IP of the SERVER" ###ur unix server name
space=`df -bhk /users/directory |awk '{print$5}'` ## dir were u want to monitor
echo "$space is the file size percent"
#capacity=${space%?}
#capacity=$space|/usr/xpg4/bin/awk -F"[%]" '{ print $1}'
capacity=`df -bhk /users/directory| grep directory |awk '{print $5}'|sed 's/\%//'`

echo "capacity in $HOSTNAME is $capacity " >> $TEMPDIR/$FILE
if [ $capacity -gt 50 ]; then
echo "alert space in $HOSTNAME getting full "
echo > $TEMPDIR/$FILE
echo "*************************************************" >> $TEMPDIR/$FILE
echo "Problem with disk space on $HOSTNAME" >> $TEMPDIR/$FILE
/usr/bin/mailx -s "ALERT: Problem with file space on $HOSTNAME " "abc@abcd.com" < $TEMPDIR/$FILE
else
echo "enough space available"
fi
 
Old 01-12-2009, 12:27 PM   #3
cmontr
Member
 
Registered: Sep 2007
Posts: 175

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by cmontr View Post
#### I also tried this code but got error as well on solaris...Please let me know any ideas...thanks much...

#!/bin/ksh
TEMPDIR=$HOME
FILE=mail.log
sendemail=-1
HOSTNAME="IP of the SERVER" ###ur unix server name
space=`df -bhk /users/directory |awk '{print$5}'` ## dir were u want to monitor
echo "$space is the file size percent"
#capacity=${space%?}
#capacity=$space|/usr/xpg4/bin/awk -F"[%]" '{ print $1}'
capacity=`df -bhk /users/directory| grep directory |awk '{print $5}'|sed 's/\%//'`

echo "capacity in $HOSTNAME is $capacity " >> $TEMPDIR/$FILE
if [ $capacity -gt 50 ]; then
echo "alert space in $HOSTNAME getting full "
echo > $TEMPDIR/$FILE
echo "*************************************************" >> $TEMPDIR/$FILE
echo "Problem with disk space on $HOSTNAME" >> $TEMPDIR/$FILE
/usr/bin/mailx -s "ALERT: Problem with file space on $HOSTNAME " "abc@abcd.com" < $TEMPDIR/$FILE
else
echo "enough space available"
fi

ERROR GOT:

df: (/users/directory) not a block device, directory or mounted resource
is the file size percent
df: (/users/directory) not a block device, directory or mounted resource
./disk.sh[13]: test: argument expected
enough space available
 
Old 02-06-2019, 09:44 AM   #4
etcetera
Member
 
Registered: Aug 2004
Posts: 436

Rep: Reputation: 17
I will post one once I find a smooth-running one and debugged.

I have a KSH that runs well but don't want to deal with KSH anymore.
 
Old 02-06-2019, 10:17 AM   #5
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by etcetera View Post
I will post one once I find a smooth-running one and debugged. I have a KSH that runs well but don't want to deal with KSH anymore.
If you have one, why are you asking others to write one for you, in your other thread? And did you not notice that THIS thread has been closed for TEN YEARS now?

And if by some chance the OP comes back, the error in their Perl script is easily fixed, since it has to do with df on Solaris vs Linux, and the entire error is on line 28 as they stated.
 
2 members found this post helpful.
Old 02-06-2019, 01:27 PM   #6
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
@etcetera: Please do not wake up old threads without adding any useful information specific to that thread. If you have a similar question please start your own thread and provide the details relevant to your particular problem so that others may quickly inderstand your specific case and give specific responses.

Please review the Site FAQ for guidance in posting your questions and general forum usage. Especially, read the link in that page, How To Ask Questions The Smart Way. The more effort you put into understanding your problem and framing your questions, the better others can help!
 
Old 02-09-2019, 04:05 AM   #7
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
df and an old derived shell script called dfspace (used to be standard on sco and perhaps solaris) will do it.

ok
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
script for disk space monitoring jmmo1981 Red Hat 1 05-24-2008 11:25 AM
LXer: Perl script to monitor disk space and send an email alert LXer Syndicated Linux News 1 02-23-2007 01:12 PM
3Gb of disk space lost! Disk space problem or mother board conflicts with HDD Mistreated Linux - Hardware 4 12-06-2004 03:58 PM
Disk space checking script? jimieee Linux - General 2 04-01-2004 02:57 AM
script for checking disk space pralaydesai Linux - Networking 3 03-25-2003 03:34 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:57 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration