| 1 | #============================================================================== | ||
| 2 | # Filename: DB.pm | ||
| 3 | # Description: Database Logger Delegate. | ||
| 4 | # Original Authoer: Dale M. Amon | ||
| 5 | # Revised by: $Author: amon $ | ||
| 6 | # Date: $Date: 2008-08-28 23:20:19 $ | ||
| 7 | # Version: $Revision: 1.10 $ | ||
| 8 | # License: LGPL 2.1, Perl Artistic or BSD | ||
| 9 | # | ||
| 10 | #==============================================================================use strict; | ||
| 11 | use Fault::Dbh; | ||
| 12 | use Fault::DebugPrinter; | ||
| 13 | use Fault::ErrorHandler; | ||
| 14 | use Fault::Delegate; | ||
| 15 | use Fault::Msg; | ||
| 16 | |||
| 17 | package Fault::Delegate::DB; | ||
| 18 | use vars qw{@ISA}; | ||
| 19 | @ISA = qw( Fault::Delegate ); | ||
| 20 | |||
| 21 | #============================================================================= | ||
| 22 | # Family internal methods | ||
| 23 | #============================================================================= | ||
| 24 | # *** Not much to do if it fails, but think about it anyway. | ||
| 25 | |||
| 26 | sub _write ($$) { | ||
| 27 | my ($self,$msg) = @_; | ||
| 28 | my ($stamp,$priority,$type,$p,$m) = $msg->get; | ||
| 29 | |||
| 30 | $self->{'logins'}->execute($stamp,$priority,$p,$type,$m); | ||
| 31 | return 1; | ||
| 32 | } | ||
| 33 | |||
| 34 | #------------------------------------------------------------------------------ | ||
| 35 | |||
| 36 | sub _connect ($) { | ||
| 37 | my $self = shift; | ||
| 38 | my ($DBH,$dbh,$sth_del,$sth_ins,$sth_lkp); | ||
| 39 | |||
| 40 | return 1 if (defined $self->{'dbh'}); | ||
| 41 | |||
| 42 | $DBH = Fault::Dbh->open ($self->{'dbname'}, | ||
| 43 | $self->{'user'}, | ||
| 44 | $self->{'pass'}); | ||
| 45 | |||
| 46 | $dbh = (defined $DBH) ? $DBH->dbh : undef; | ||
| 47 | if (!defined $dbh) { | ||
| 48 | $self->warn ("Failed to connect to db \'$self->{'dbname'}\' " . | ||
| 49 | "for user \'$self->{'user'}\'"); | ||
| 50 | return 0; | ||
| 51 | } | ||
| 52 | |||
| 53 | @$self{'dbh','logins','faultins','sthdel','sthlist','sthexist'} = | ||
| 54 | ($DBH, | ||
| 55 | $dbh->prepare ("INSERT INTO log VALUES ('',?,?,?,?,?)"), | ||
| 56 | $dbh->prepare ("INSERT INTO faults VALUES ('',?,?,?,?,?)"), | ||
| 57 | $dbh->prepare ("DELETE FROM faults WHERE Message=? and Process=?"), | ||
| 58 | $dbh->prepare ("SELECT * FROM faults WHERE Process=?"), | ||
| 59 | $dbh->prepare ("SELECT * FROM faults WHERE Message=? and Process=?"), | ||
| 60 | ); | ||
| 61 | return 1; | ||
| 62 | } | ||
| 63 | |||
| 64 | #------------------------------------------------------------------------------ | ||
| 65 | # NOTE: The undef is really important. Otherwise a pointer with a valid open | ||
| 66 | # db handle exists before processes get spawned. If a dbh gets passed through | ||
| 67 | # to two different running processes... mysql gets really confused when both | ||
| 68 | # try to access the same connection. | ||
| 69 | # | ||
| 70 | # NOTE: I hope that moving it all here instead of doing this to objects | ||
| 71 | # will be just as effective. | ||
| 72 | # | ||
| 73 | |||
| 74 | sub _disconnect ($) { | ||
| 75 | my $self = shift; | ||
| 76 | if (defined $self->{'dbh'}) {$self->{'dbh'}->close;} | ||
| 77 | |||
| 78 | @$self{'dbh','logins','faultins','sthdel','sthlist','sthexist'} = | ||
| 79 | (undef,undef,undef,undef,undef,undef); | ||
| 80 | return 1; | ||
| 81 | } | ||
| 82 | |||
| 83 | #============================================================================= | ||
| 84 | # Local internal methods | ||
| 85 | #============================================================================= | ||
| 86 | |||
| 87 | sub _dump ($$) { | ||
| 88 | my ($self,$p) = @_; | ||
| 89 | my @list = (); | ||
| 90 | |||
| 91 | Fault::DebugPrinter->dbg (3, "Dump of fault table."); | ||
| 92 | $self->{'sthlist'}->execute ($p) || (return (@list)); | ||
| 93 | |||
| 94 | my $cnt = $self->{'sthlist'}->rows; | ||
| 95 | Fault::DebugPrinter->dbg (3, "Found $cnt faults."); | ||
| 96 | |||
| 97 | while ($cnt-- > 0 ) {push @list, ($self->{'sthlist'}->fetchrow_hashref);} | ||
| 98 | $self->{'sthlist'}->finish; | ||
| 99 | return (@list); | ||
| 100 | } | ||
| 101 | |||
| 102 | #============================================================================== | ||
| 103 | # CLASS METHODS | ||
| 104 | #============================================================================== | ||
| 105 | |||
| 106 | sub new ($$$$$) { | ||
| 107 | my ($class,$host,$dbname,$user,$pass) = @_; | ||
| 108 | my $self = bless {}, $class; | ||
| 109 | defined $host or ($host="localhost"); | ||
| 110 | |||
| 111 | if ((ref $host) or !POSIX::isalnum $host) { | ||
| 112 | $self->warn | ||
| 113 | ("Database server name invalid: defaulting to localhost."); | ||
| 114 | $host = "localhost"; | ||
| 115 | } | ||
| 116 | |||
| 117 | if (!defined $dbname or (ref $dbname) or !POSIX::isalnum $dbname) { | ||
| 118 | $self->warn | ||
| 119 | ("Fail: dbname string is invalid or undefined!"); | ||
| 120 | return undef; | ||
| 121 | } | ||
| 122 | |||
| 123 | if (!defined $user or (ref $user) or !POSIX::isalnum $user) { | ||
| 124 | $self->warn | ||
| 125 | ("Fail: user name string is invalid or undefined!"); | ||
| 126 | return undef; | ||
| 127 | } | ||
| 128 | |||
| 129 | if (!defined $pass or (ref $pass) or !POSIX::isprint $pass) { | ||
| 130 | $self->warn | ||
| 131 | ("Fail: password string is invalid or undefined!"); | ||
| 132 | return undef; | ||
| 133 | } | ||
| 134 | |||
| 135 | @$self{'host','dbname','user','pass'} = ($host,$dbname,$user,$pass); | ||
| 136 | |||
| 137 | return ($self->test) ? $self : undef; | ||
| 138 | } | ||
| 139 | |||
| 140 | #============================================================================== | ||
| 141 | # INSTANCE METHODS | ||
| 142 | #============================================================================== | ||
| 143 | # Logger Internal Hook Callback Methods | ||
| 144 | #============================================================================== | ||
| 145 | # Callback from Logger when it raises a fault | ||
| 146 | |||
| 147 | sub trans01 ($$) { | ||
| 148 | my ($self,$msg) = @_; | ||
| 149 | my ($stamp,$priority,$type,$p,$m) = $msg->get; | ||
| 150 | my $val = 0; | ||
| 151 | |||
| 152 | if ($self->_connect) { | ||
| 153 | |||
| 154 | # *** Not much to do if it fails, but think about it anyway. | ||
| 155 | $self->{'sthexist'}->execute($m,$p); | ||
| 156 | ($self->{'sthexist'}->rows == 0) || (return 0); | ||
| 157 | |||
| 158 | # *** Not much to do if it fails, but think about it anyway. | ||
| 159 | $self->{'faultins'}->execute($stamp,$priority,$p,$type,$m); | ||
| 160 | } | ||
| 161 | $self->_disconnect; | ||
| 162 | return 0; | ||
| 163 | } | ||
| 164 | |||
| 165 | #------------------------------------------------------------------------------ | ||
| 166 | # Callback from Logger when it clears a fault | ||
| 167 | # | ||
| 168 | sub trans10 ($$) { | ||
| 169 | my ($self,$msg) = @_; | ||
| 170 | my $val = 0; | ||
| 171 | |||
| 172 | if ($self->_connect) { | ||
| 173 | # *** Not much to do if it fails, but think about it anyway. | ||
| 174 | $self->{'sthdel'}->execute($msg->msg,$msg->priority); | ||
| 175 | } | ||
| 176 | $self->_disconnect; | ||
| 177 | return 0; | ||
| 178 | } | ||
| 179 | |||
| 180 | #------------------------------------------------------------------------------ | ||
| 181 | # Callback from Logger when it initializes it's in-memory fault table. | ||
| 182 | |||
| 183 | sub initfaults ($) { | ||
| 184 | my ($self) = @_; | ||
| 185 | my @msglist = (); | ||
| 186 | |||
| 187 | if ($self->_connect) { | ||
| 188 | foreach my $fault ($self->_dump($self->processname)) { | ||
| 189 | push @msglist, ($fault->{'Message'}); | ||
| 190 | } | ||
| 191 | } | ||
| 192 | $self->_disconnect; | ||
| 193 | return @msglist; | ||
| 194 | } | ||
| 195 | |||
| 196 | #============================================================================== | ||
| 197 | # POD DOCUMENTATION | ||
| 198 | #============================================================================== | ||
| 199 | # You may extract and format the documention section with the 'perldoc' cmd. | ||
| 200 | |||
| 201 | =head1 NAME | ||
| 202 | |||
| 203 | Fault::Delegate::DB - Database Logger Delegate. | ||
| 204 | |||
| 205 | =head1 SYNOPSIS | ||
| 206 | |||
| 207 | use Fault::Delegate::DB; | ||
| 208 | $self = Fault::Delegate::DB->new ($host,$host,$dbname,$user,$pass); | ||
| 209 | $okay = $self->log ($msg); | ||
| 210 | $zero = $self->trans01 ($msg); | ||
| 211 | $zero = $self->trans10 ($msg); | ||
| 212 | @list = $self->initfaults; | ||
| 213 | |||
| 214 | =head1 Inheritance | ||
| 215 | |||
| 216 | UNIVERSAL | ||
| 217 | Fault::Delegate | ||
| 218 | Fault::Delegate::DB | ||
| 219 | |||
| 220 | =head1 Description | ||
| 221 | |||
| 222 | This is a Logger delegate for database logging. | ||
| 223 | |||
| 224 | This Class manages a database logging connection to a local Mysql database | ||
| 225 | server. | ||
| 226 | |||
| 227 | Remote database access is not supported at this time. It will require changes | ||
| 228 | to the Fault::Dbh module, or improvements in MySQL (and total disappearance | ||
| 229 | of old versions) which prevent exhaustion of connections if your processes | ||
| 230 | rapidly create and destroy connections. | ||
| 231 | |||
| 232 | Also note that some attempt has been made to be thread-safe. I discovered that | ||
| 233 | if a handle was created in a parent there was a possibility it could be | ||
| 234 | reused in a child process. The parent and child then had interleaved | ||
| 235 | communications with the server which completely confused it. | ||
| 236 | |||
| 237 | =head1 Database requirements | ||
| 238 | |||
| 239 | There should be two tables defined on the MySQL database server under the | ||
| 240 | same database: log and fault. You must have a username and password that | ||
| 241 | give you rights to select, insert and delete from them. | ||
| 242 | |||
| 243 | use Mydatabase; | ||
| 244 | CREATE TABLE log ( \ | ||
| 245 | LogId bigint unsigned auto_increment, \ | ||
| 246 | Time datetime NOT NULL, \ | ||
| 247 | Priority enum('emerg','alert','crit','err','warning','notice','info','debug') NOT NULL DEFAULT 'err', \ | ||
| 248 | Process varchar(20) NOT NULL DEFAULT 'Unspecified', \ | ||
| 249 | Type enum('BUG','SRV','NET','DATA','NOTE') NOT NULL DEFAULT 'BUG', \ | ||
| 250 | Message blob NOT NULL, \ | ||
| 251 | UNIQUE (LogId), \ | ||
| 252 | PRIMARY KEY (LogId)); | ||
| 253 | |||
| 254 | |||
| 255 | CREATE TABLE faults ( \ | ||
| 256 | FaultId bigint unsigned auto_increment, \ | ||
| 257 | Time datetime NOT NULL, \ | ||
| 258 | Priority enum('emerg','alert','crit','err','warning','notice','info','debug') NOT NULL DEFAULT 'err', \ | ||
| 259 | Process varchar(20) NOT NULL DEFAULT 'Unspecified', \ | ||
| 260 | Type enum('BUG','SRV','NET','DATA','NOTE') NOT NULL DEFAULT 'BUG', \ | ||
| 261 | Message blob NOT NULL, \ | ||
| 262 | UNIQUE (FaultId), \ | ||
| 263 | PRIMARY KEY (FaultId)); | ||
| 264 | |||
| 265 | =head1 Examples | ||
| 266 | |||
| 267 | use Fault::Delegate::DB; | ||
| 268 | use Fault::Msg; | ||
| 269 | use Fault::Logger; | ||
| 270 | |||
| 271 | my $msg = Fault::Msg ("Arf!"); | ||
| 272 | my $baz = Fault::Delegate::DB->new ($host,$db,$user,$pass); | ||
| 273 | my $waslogged = $baz->log ($msg); | ||
| 274 | |||
| 275 | Fault::Logger->new ($baz); | ||
| 276 | my $waslogged = Fault::Logger->log ("Bow! Wow!"); | ||
| 277 | |||
| 278 | [See Fault::Logger for a detailed example.] | ||
| 279 | |||
| 280 | =head1 Class Variables | ||
| 281 | |||
| 282 | None. | ||
| 283 | |||
| 284 | =head1 Instance Variables | ||
| 285 | |||
| 286 | host Name of host computer. | ||
| 287 | dbname Name of database. | ||
| 288 | user Name of user. | ||
| 289 | pass Password string. | ||
| 290 | |||
| 291 | Transient instance variables. | ||
| 292 | dbh database handle. | ||
| 293 | logins prepared log insert statement handle | ||
| 294 | faultins prepared fault insert statement handle | ||
| 295 | sthdel prepared fault delete statement handle | ||
| 296 | sthlist prepared fault select all statement handle | ||
| 297 | sthexist prepared fault select one statement handle | ||
| 298 | |||
| 299 | =head1 Class Methods | ||
| 300 | |||
| 301 | =over 4 | ||
| 302 | |||
| 303 | =item B<$self = Fault::Delegate::DB-E<gt>new ($host,$dbname,$user,$pass)> | ||
| 304 | |||
| 305 | Create an object to mediate log and fault communications with a | ||
| 306 | database server. It has only been tested with MySQL thus far. | ||
| 307 | |||
| 308 | $host can only be "localhost" at this time. If undef, $host will be set | ||
| 309 | to localhost. If $host is invalid in any other way, a warning will be | ||
| 310 | issued and $host will be set to localhost. | ||
| 311 | |||
| 312 | $dname is the name of the MySQL database in which the tables log and fault | ||
| 313 | exist; $user is a user name that can read and write those tables; $pass | ||
| 314 | is that users MySQL password. | ||
| 315 | |||
| 316 | Returns undef on failure to create the object and connect to the db. | ||
| 317 | |||
| 318 | =back 4 | ||
| 319 | |||
| 320 | =head1 Instance Methods | ||
| 321 | |||
| 322 | =over 4 | ||
| 323 | |||
| 324 | =item B<$okay = $self-E<gt>log ($msgobj)> | ||
| 325 | |||
| 326 | Inserts a log message in the database and returns true if we succeeded in | ||
| 327 | doing so. | ||
| 328 | |||
| 329 | =item B<$zero = $self-E<gt>trans01 ($msgobj)> | ||
| 330 | |||
| 331 | Inserts a fault record for the current process name and the $msg string. | ||
| 332 | If one already exists, do nothing. It always returns 0. | ||
| 333 | |||
| 334 | =item B<$zero = $self-E<gt>trans10 ($msgobj)> | ||
| 335 | |||
| 336 | Removes a fault record that matches the current process name and | ||
| 337 | the $msg string. If there is not, do nothing. It always returns 0. | ||
| 338 | |||
| 339 | =item B<@list = $self-E<gt>initfaults> | ||
| 340 | |||
| 341 | Requests a current list of faults from the database when Logger initializes. | ||
| 342 | @list contains a simple list of strings, where each string represents a | ||
| 343 | unique active fault condition belonging to the current process. | ||
| 344 | |||
| 345 | ("fault message 1", "fault message 2", ...) | ||
| 346 | |||
| 347 | If it cannot connect to the database, an empty list is returned. | ||
| 348 | |||
| 349 | =back 4 | ||
| 350 | |||
| 351 | =head1 Private Class Methods | ||
| 352 | |||
| 353 | None. | ||
| 354 | |||
| 355 | =head1 Private Instance Methods | ||
| 356 | |||
| 357 | =over 4 | ||
| 358 | |||
| 359 | =item B<$bool = $self-E<gt>_write ($msg)> | ||
| 360 | |||
| 361 | =item B<$bool = $self-E<gt>_connect> | ||
| 362 | |||
| 363 | =item B<$bool = $self-E<gt>_disconnect> | ||
| 364 | |||
| 365 | Impliments the above overrides to the internal family protocol utilized by | ||
| 366 | the Fault:Delegate log and test methods. | ||
| 367 | |||
| 368 | =back 4 | ||
| 369 | |||
| 370 | =head1 Errors and Warnings | ||
| 371 | |||
| 372 | Local warning messages are issued if the db server cannot be reached or has | ||
| 373 | any problems whatever. | ||
| 374 | |||
| 375 | =head1 KNOWN BUGS | ||
| 376 | |||
| 377 | See TODO. | ||
| 378 | |||
| 379 | =head1 SEE ALSO | ||
| 380 | |||
| 381 | Fault::Logger, Fault::Delegate, Fault::Msg, Fault::Dbh, | ||
| 382 | Fault::ErrorHandler, Fault::DebugPrinter | ||
| 383 | |||
| 384 | =head1 AUTHOR | ||
| 385 | |||
| 386 | Dale Amon <amon@vnl.com> | ||
| 387 | |||
| 388 | =cut | ||
| 389 | |||
| 390 | #============================================================================= | ||
| 391 | # CVS HISTORY | ||
| 392 | #============================================================================= | ||
| 393 | # $Log: DB.pm,v $ | ||
| 394 | # Revision 1.10 2008-08-28 23:20:19 amon | ||
| 395 | # perldoc section regularization. | ||
| 396 | # | ||
| 397 | # Revision 1.9 2008-08-17 21:56:37 amon | ||
| 398 | # Make all titles fit CPAN standard. | ||
| 399 | # | ||
| 400 | # Revision 1.8 2008-07-24 21:17:24 amon | ||
| 401 | # Moved all todo notes to elsewhere; made Stderr the default delegate instead of Stdout. | ||
| 402 | # | ||
| 403 | # Revision 1.7 2008-05-09 18:24:55 amon | ||
| 404 | # Bugs and changes due to pre-release testing | ||
| 405 | # | ||
| 406 | # Revision 1.6 2008-05-08 20:22:50 amon | ||
| 407 | # Minor bug fixes; shifted fault table and initfault from Logger to List | ||
| 408 | # | ||
| 409 | # Revision 1.5 2008-05-07 18:14:55 amon | ||
| 410 | # Simplification and standardization. Much more is inherited from Fault::Delegate. | ||
| 411 | # | ||
| 412 | # Revision 1.4 2008-05-05 19:25:49 amon | ||
| 413 | # Catch any small changes before implimenting major changes | ||
| 414 | # | ||
| 415 | # Revision 1.3 2008-05-04 14:43:18 amon | ||
| 416 | # Updates to perl doc; minor code changes. | ||
| 417 | # | ||
| 418 | # Revision 1.2 2008-05-03 00:56:57 amon | ||
| 419 | # Changed standard argument ordering. | ||
| 420 | # | ||
| 421 | # Revision 1.1.1.1 2008-05-02 16:38:11 amon | ||
| 422 | # Fault and Log System. Pared off of DMA base lib. | ||
| 423 | # | ||
| 424 | # Revision 1.5 2008-04-18 13:37:59 amon | ||
| 425 | # Added some fault bailout code and warning. | ||
| 426 | # | ||
| 427 | # Revision 1.4 2008-04-18 13:05:13 amon | ||
| 428 | # Added call to Dbh close. | ||
| 429 | # | ||
| 430 | # Revision 1.3 2008-04-18 12:52:08 amon | ||
| 431 | # Minor changes. | ||
| 432 | # | ||
| 433 | # Revision 1.2 2008-04-18 12:48:14 amon | ||
| 434 | # Added arg checking and bail out to new method. | ||
| 435 | # | ||
| 436 | # Revision 1.1 2008-04-18 11:32:32 amon | ||
| 437 | # I combined three old modules into one simplified database logger delegate | ||
| 438 | # class. | ||
| 439 | # | ||
| 440 | # 20041012 Dale Amon <amon@vnl.com> | ||
| 441 | # Created starting from Document::LogFile as a template. | ||
| 442 | # | ||
| 443 | # DONE * Make docs similar to docs in SimpleHttp. [DMA20080416-20080416] | ||
| 444 | # * Bring in args to new method for host, user and password | ||
| 445 | # [DMA20080416-20080416] | ||
| 446 | # * I can move some internal methods like _process_name and | ||
| 447 | # _timestamp up to a Superclass Delegate.pm | ||
| 448 | # [DMA20080416-20080416] | ||
| 449 | # * Create a Delegate.pm which defines the full protocol | ||
| 450 | # for this set of classes. [DMA20080416-20080416] | ||
| 451 | # * If a superclass defines stubs, then my use of 'can' methods | ||
| 452 | # to check the API from Logger won't work! | ||
| 453 | # (So I simply comment them out to indicate they are part of the | ||
| 454 | # optional protocol.) [DMA20080416-20080416] | ||
| 455 | # * The LogTable and LogFault need the timestamp and | ||
| 456 | # processname methods. (I will fold them into this module. | ||
| 457 | # [DMA20080416-20080416] | ||
| 458 | # * Add arg lists including marks for optional args. | ||
| 459 | # [DMA20080416-20080417] | ||
| 460 | # * Add in all the connection checking and failsafe code. | ||
| 461 | # [DMA20080416-20080418] | ||
| 462 | 1; |