Friday, July 29, 2011

Wednesday, July 27, 2011

Perl script to run any UNIX/LINUX command and email the output



The following script would run command "lsof -u oracle | wc -l" and then check for
the threshold value and if the threshold is exceeded, it will email the output.

#!/usr/bin/perl -w
use POSIX 'strftime';
my $date = strftime '%m-%d-%Y %H:%M:%S', localtime;
my $command = `/usr/sbin/lsof -u oracle | wc -l `;
my $host = `hostname`; chomp($host);
my $to = "abc\@yahoo.com";
my $title = "LSOF Threshold Exceeded" ;
my $from = "DBA\@yahoo.com";
my $subject = "Threshold lsof exceeded";
my $thresh = 10;

if( $command ge $thresh ) {
open(MAIL, "|/usr/sbin/sendmail -t ");

print MAIL "To: $to\n";
print MAIL "From: $from\n";
print MAIL "Subject: $title for host : $host\n";

print MAIL "$date\n HOSTNAME: $host\n LSOF Count: $command\n\n";
print MAIL "LSOF Count has Exceeded the threshold of $thresh";

close(MAIL);

}