[8] | 1 | # ipdb/cgi-bin/IPDB.pm
|
---|
[4] | 2 | # Contains DB-related functions for IPDB
|
---|
[8] | 3 | ###
|
---|
| 4 | # SVN revision info
|
---|
| 5 | # $Date: 2004-10-26 17:03:16 +0000 (Tue, 26 Oct 2004) $
|
---|
| 6 | # SVN revision $Rev: 26 $
|
---|
| 7 | # Last update by $Author: kdeugau $
|
---|
| 8 | ###
|
---|
| 9 |
|
---|
[4] | 10 | # Split from CommonWeb.pm 08/13/2004 kdeugau@vianet
|
---|
| 11 |
|
---|
| 12 | package IPDB;
|
---|
| 13 |
|
---|
| 14 | use strict;
|
---|
| 15 | use warnings;
|
---|
| 16 | use Exporter;
|
---|
| 17 | use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
|
---|
| 18 |
|
---|
| 19 | $VERSION = 1.0;
|
---|
| 20 | @ISA = qw(Exporter);
|
---|
| 21 | @EXPORT_OK = qw(&connectDB &checkDBSanity);
|
---|
| 22 |
|
---|
| 23 | @EXPORT = (); # Export nothing by default.
|
---|
| 24 | %EXPORT_TAGS = ( ALL => [qw( &connectDB &checkDBSanity )]
|
---|
| 25 | );
|
---|
| 26 |
|
---|
| 27 | # Creates connection to IPDB.
|
---|
| 28 | # Default is a PostgreSQL db; could be any DBMS with the
|
---|
| 29 | # right changes. MySQL in comments. Note that some DBMS's don't
|
---|
| 30 | # support transactions, this is a Bad Thing!
|
---|
| 31 | # Returns a handle to the db.
|
---|
| 32 | sub connectDB {
|
---|
| 33 | my $dbh;
|
---|
| 34 | my $DSN = "DBI:Pg:dbname=ipdb";
|
---|
| 35 | my $user = 'ipdb';
|
---|
| 36 | my $pw = 'ipdbpwd';
|
---|
| 37 |
|
---|
| 38 | # Note that we want to autocommit by default, and we will turn it off locally as necessary.
|
---|
| 39 | $dbh = DBI->connect($DSN, $user, $pw, { AutoCommit => 1 } )
|
---|
| 40 | or return undef if(!$dbh);
|
---|
| 41 |
|
---|
| 42 | return $dbh;
|
---|
| 43 | } # end connectDB
|
---|
| 44 |
|
---|
| 45 | # Quick check to see if the db is responding. A full integrity
|
---|
| 46 | # check will have to be a separate tool to walk the IP allocation trees.
|
---|
| 47 | sub checkDBSanity {
|
---|
| 48 | my $dbh = connectDB();
|
---|
| 49 |
|
---|
| 50 | if (!$dbh) {
|
---|
[16] | 51 | print "Cannot connect to the database!";
|
---|
[4] | 52 | } else {
|
---|
| 53 | # it connects, try a stmt.
|
---|
| 54 | my $sth = $dbh->prepare('select cidr from masterblocks');
|
---|
| 55 | my $err = $sth->execute();
|
---|
| 56 |
|
---|
| 57 | if ($sth->fetchrow()) {
|
---|
| 58 | # all is well.
|
---|
| 59 | return 1;
|
---|
| 60 | } else {
|
---|
[16] | 61 | print "Connected to the database, but could not execute test statement. ".$sth->errstr();
|
---|
[4] | 62 | }
|
---|
| 63 | }
|
---|
| 64 | # Clean up after ourselves.
|
---|
| 65 | $dbh->disconnect;
|
---|
| 66 | } # end checkDBSanity
|
---|
| 67 |
|
---|
| 68 | # Indicates module loaded OK. Required by Perl.
|
---|
| 69 | 1;
|
---|