| 1 | package Watchdog::Mysql; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use Alias; | ||
| 5 | use base qw(Watchdog::Base); | ||
| 6 | use DBI; | ||
| 7 | use vars qw($VERSION $DB $HOST $PORT $NAME); | ||
| 8 | |||
| 9 | $VERSION = '0.01'; | ||
| 10 | |||
| 11 | =head1 NAME | ||
| 12 | |||
| 13 | Watchdog::Mysql - Test status of Mysql service | ||
| 14 | |||
| 15 | =head1 SYNOPSIS | ||
| 16 | |||
| 17 | use Watchdog::Mysql; | ||
| 18 | $h = new Watchdog::Mysql($name,$host,$port,$db); | ||
| 19 | ($alive,$error) = $h->is_alive; | ||
| 20 | print $h->id, $alive ? ' is alive' : " is dead: $error", "\n"; | ||
| 21 | |||
| 22 | =head1 DESCRIPTION | ||
| 23 | |||
| 24 | B<Watchdog::Mysql> is an extension for monitoring a Mysql server. | ||
| 25 | |||
| 26 | =cut | ||
| 27 | |||
| 28 | ## CLASS DATA | ||
| 29 | |||
| 30 | my %fields = ( DB => 'test', ); | ||
| 31 | |||
| 32 | =head1 CLASS METHODS | ||
| 33 | |||
| 34 | =head2 new($name,$host,$port,$db) | ||
| 35 | |||
| 36 | Returns a new B<Watchdog::Mysql> object. I<$name> is a string which | ||
| 37 | will identify the service to a human (default is 'mysql'). I<$host> | ||
| 38 | is the hostname which is running the service (default is 'localhost'). | ||
| 39 | I<$port> is the port on which the service listens (default is 3306). | ||
| 40 | I<$db> is a database with no access control (default is 'test'). | ||
| 41 | |||
| 42 | =cut | ||
| 43 | |||
| 44 | sub new($$$$) { | ||
| 45 | my $DEBUG = 0; | ||
| 46 | my $proto = shift; | ||
| 47 | my $class = ref($proto) || $proto; | ||
| 48 | my($name,$host,$port) = ('mysqld','localhost',3306); | ||
| 49 | my $arg; | ||
| 50 | for ( \$name,\$host,\$port ) { | ||
| 51 | $$_ = $arg if $arg = shift; | ||
| 52 | } | ||
| 53 | print STDERR "Watchdog::Mysql::new() $name $host $port\n" if $DEBUG; | ||
| 54 | my $self = bless($class->SUPER::new($name,$host,$port),$class); | ||
| 55 | |||
| 56 | print STDERR "Watchdog::Mysql::new() $NAME $HOST $PORT\n" if $DEBUG; | ||
| 57 | for my $element (keys %fields) { | ||
| 58 | $self->{_PERMITTED}->{$element} = $fields{$element}; | ||
| 59 | } | ||
| 60 | @{$self}{keys %fields} = values %fields; | ||
| 61 | $self->{DB} = $_[0] if defined($_[0]); | ||
| 62 | print STDERR "Watchdog::Mysql::new() $NAME $HOST $PORT $DB\n" if $DEBUG; | ||
| 63 | return $self; | ||
| 64 | } | ||
| 65 | |||
| 66 | #------------------------------------------------------------------------------ | ||
| 67 | |||
| 68 | =head1 OBJECT METHODS | ||
| 69 | |||
| 70 | =head2 id() | ||
| 71 | |||
| 72 | Return a string describing the Mysql service. | ||
| 73 | |||
| 74 | =cut | ||
| 75 | |||
| 76 | sub id() { | ||
| 77 | my $self = attr shift; | ||
| 78 | return $self->SUPER::id . ":$DB"; | ||
| 79 | } | ||
| 80 | |||
| 81 | #------------------------------------------------------------------------------ | ||
| 82 | |||
| 83 | =head2 is_alive() | ||
| 84 | |||
| 85 | Returns (1,undef) if the mysql server can be 'pinged' or (0,I<$error>) | ||
| 86 | where I<$error> is a DBI error string if it can't. | ||
| 87 | |||
| 88 | =cut | ||
| 89 | |||
| 90 | sub is_alive() { | ||
| 91 | my $DEBUG = 0; | ||
| 92 | my $self = attr shift; | ||
| 93 | print STDERR "Mysql::is_alive()\n" if $DEBUG; | ||
| 94 | print STDERR "\$DB = $DB\n" if $DEBUG; | ||
| 95 | |||
| 96 | # driver should use the Unix domain socket if $PORT is undef | ||
| 97 | my $dsn = "DBI:mysql:database=$DB;host=$HOST"; | ||
| 98 | $dsn .= ";port=$PORT" if defined($PORT); | ||
| 99 | print STDERR "\$dsn = $dsn\n" if $DEBUG; | ||
| 100 | my $dbh = DBI->connect($dsn,undef,undef,{ PrintError => 0 } ); | ||
| 101 | |||
| 102 | my($alive,$error); | ||
| 103 | if ( defined($dbh) ) { | ||
| 104 | ($alive,$error) = ($dbh->ping,$dbh->errstr); | ||
| 105 | $dbh->disconnect; | ||
| 106 | } else { | ||
| 107 | ($alive,$error) = (0,$DBI::errstr); | ||
| 108 | } | ||
| 109 | |||
| 110 | return($alive,$error); | ||
| 111 | } | ||
| 112 | |||
| 113 | #------------------------------------------------------------------------------ | ||
| 114 | |||
| 115 | =head1 SEE ALSO | ||
| 116 | |||
| 117 | L<Watchdog::Base> | ||
| 118 | |||
| 119 | L<DBI> | ||
| 120 | |||
| 121 | =head1 AUTHOR | ||
| 122 | |||
| 123 | Paul Sharpe E<lt>paul@miraclefish.comE<gt> | ||
| 124 | |||
| 125 | =head1 COPYRIGHT | ||
| 126 | |||
| 127 | Copyright (c) 1998 Paul Sharpe. England. All rights reserved. This | ||
| 128 | program is free software; you can redistribute it and/or modify it | ||
| 129 | under the same terms as Perl itself. | ||
| 130 | |||
| 131 | =cut |