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