source: trunk/cgi-bin/IPDB.pm@ 5

Last change on this file since 5 was 4, checked in by Kris Deugau, 20 years ago

Import "new" IPDB development:

March 2004 through end of August 2004

Changes include:

-> Entirely new method of allocating IP space; which should

hopefully reduce the amount of messiness in allocations.

-> IP address processing provided by NetAddr::IP rather than

homebrew code

-> Change DB to PostgreSQL to eliminate some of the problems

caused by using MySQL, and to gain native RDBMS support for
IP addresses.

-> Using NetAddr::IP and Postgres allows (eventually, with

PG >= 7.4) IPV6 without any code changes. In theory.

-> Logging so that if someone makes a change that turns out

to have been wrong for some reason, Blame Can Be Assigned.

-> General code cleanups (split IPDB.pm from CommonWeb.pm,

for instance)

File size: 1.6 KB
Line 
1# IPDB.pm
2# Contains DB-related functions for IPDB
3# Split from CommonWeb.pm 08/13/2004 kdeugau@vianet
4
5package IPDB;
6
7use strict;
8use warnings;
9use Exporter;
10use 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.
25sub 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.
40sub 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.
631;
Note: See TracBrowser for help on using the repository browser.