This Oracle Monitoring script was written as a collaborative effort
between an Oracle Certified Professional and a software engineer
at the University of Akron.
This open source code is provided as-is, but we are looking for
ways to continually improve upon it.
#!/usr/bin/perl
use strict;
use warnings;
use Net::Ping;
use DBI;
use LWP::UserAgent;
use Net::FTP;
use IO::Socket;
use Net::POP3;
use Getopt::Long;
use Mail::Sendmail;
use Sys::Hostname;
my $VERSION = "3.01";
my $EMAIL_FROM = "system\@oraclegiants.com";
my $EMAIL_TO1 = "editor\@oraclegiants.com";
my $EMAIL_TO2 = "dba_manager\@oraclegiants.com";
my $FREQUENCY_TO_CHECK
= "";
my $HOST_AND_PORT = "";
my %Config;
Configure( \%Config );
if (($Config{help}) ||
($HOST_AND_PORT eq '')) {
Syntax();
exit();
}
my $myhost = hostname;
sub Configure {
my( $Config ) = @_;
my $Result;
Getopt::Long::Configure( "prefix_pattern=(-|\/)" );
$Result = GetOptions( $Config,
"h=s"=>\$HOST_AND_PORT,
"f=s"=>\$FREQUENCY_TO_CHECK,
"help|?"
);
$Config->{help} =
1 unless( $Result );
}
sub sendEmailAlert {
my $message = $_[0];
my $host = $_[1];
my $sService = $_[2];
my $to1 = $EMAIL_TO1;
if ($to1 ne '') {
my $to2 = $EMAIL_TO2;
my $to = $to1 . ';' . $to2;
#print "to: $to\n";
#$to =~ s/@/\\@/g;
my %mail = ( To => $to,
From => $EMAIL_FROM,
Subject => $host,
Message => $message
);
$mail{smtp} = 'smtp-server';
sendmail(%mail) or die $Mail::Sendmail::error;
print "OK. Log says:\n", $Mail::Sendmail::log, "\n";
}
}
sub tcpHost (@) {
#**********************************************
# Connect to a TCP socket/port for each of the
# hosts provided in the passed in array
#**********************************************
my @host_array = @_;
my $timeout = 10;
my $sService = "SOCKET";
my $hostPool;
my $host;
foreach $host (@host_array) {
my $tcp = IO::Socket::INET->new(
Proto => 'tcp', # protocol
PeerAddr=> $host, # Address of server and port
Reuse => 1,
Timeout => $timeout);
if ($tcp) {
#$tcp->autoflush(1); # Send immediately
registerSuccess($sService, $host);
close $tcp; # Close socket
} else {
registerFailure($sService, $host);
}
}
}
sub registerSuccess() {
my $sService = $_[0];
my $host = $_[1];
print "\n$host
is reachable";
#update timestamp in database
sendEmailAlert("Host: $host Service: $sService is reachable.",
$host, $sService);
}
sub registerFailure()
{
my $sService = $_[0];
my $host = $_[1];
sendEmailAlert("Host: $host Service: $sService is unreachable
(confirmed from 2 sources).", $host, $sService);
}
sub Syntax {
my( $Script ) = ( $0 =~ m#([^\\/]+)$# );
my $Line = "-" x length( $Script );
print STDERR << "EOT";
$Script
$Line
Monitors network services for availablity.
Version: $VERSION
Syntax: $0 [-h HOST_AND_PORT]
-h HOST_AND_PORT..........hostname/ip and port
-f FREQUENCY..............frequency in seconds to check
Examples:
perl $0 -h 127.0.0.1:1521 -f 5
EOT
}
#***************************************
# Main - call functions to perform tests
#***************************************
print "Starting application...";
while (1 == 1){
#Test - Oracle service
print "\n\n ### Oracle Port Check ###";
my @hostList;
unshift(@hostList, $HOST_AND_PORT);
tcpHost (@hostList);
sleep $FREQUENCY_TO_CHECK;
}
#***************************************
# End Main
#***************************************