ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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;
}
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.
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;
}
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
#### 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
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.
@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!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.