The lines of interest here are 18-28 and 32-44. Lines 18 enters an eval block. This is code that Perl is to evaluate and execute at runtime, much like EXECUTE IMMEDIATE in PL/SQL. Lines 20 and 21 setup the timeout functionality. If the connection to the database at line 23 is not made before 60 seconds have passed as specified by the $timeOut variable, the alarm will cause the eval block to be exited.
1: #!/usr/bin/perl -w
2:
3: use warnings;
4: use strict;
5: use DBI;
6: use Mail::Sendmail qw(sendmail);
7:
8: my $timeOut = 60; # wait 60 seconds for connection
9: my $interval = 300; # 5 minutes between connectivity checks
10:
11: my @databases = ('ts01','ts02','ts03');
12: my ($username, $password) = ('scott','tiger');
13:
14: while(1) { # loop forever
15: foreach my $db ( @databases ) {
16: print "checking $dbn";
17: my $dbh='';
18: eval {
19: # set alarm to timeout current operation
20: local $SIG{ALRM} = sub {die "connection timeoutn"};
21: alarm $timeOut;
22:
23: $dbh = DBI->connect(
24: 'dbi:Oracle:' . $db,
25: $username, $password,
26: {RaiseError => 1}
27: );
28: };
29:
30: alarm 0; # reset the alarm
31:
32: if ($dbh) { # success
33: print "Connection succeeded for $dbn";
34: $dbh->disconnect;
35: } else { # failure
36: print "Error connecting to $dbn";
37: my %mail = (
38: To => 'thedba@somewhere.com',
39: From => 'oracle@somewhere.com',
40: Subject => "Database $db is down!",
41: );
42: unless (sendmail %mail) { print "Error sending mail: $Mail::Sendmail::error n" }
43: }
44: }
45:
46: sleep $interval; # wait for next db check
47: }