ITPub博客

首页 > Linux操作系统 > Linux操作系统 > perl for dba :example:send a mail to dba when can't connect to db

perl for dba :example:send a mail to dba when can't connect to db

原创 Linux操作系统 作者:xhailiang 时间:2007-03-15 00:00:00 0 删除 编辑

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: }

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/308563/viewspace-171972/,如需转载,请注明出处,否则将追究法律责任。

上一篇: sql*load
请登录后发表评论 登录
全部评论

注册时间:2008-02-17

  • 博文量
    270
  • 访问量
    425012