rendered paste body#! /usr/bin/perl
use DBI;
use Switch;
#Feb 1 01:47:35 fran dhcpd: DHCPREQUEST for xxx.xxx.xxx.xxx from 00:d0:96:d2:8e:1b via eth1
#Feb 1 01:47:35 fran dhcpd: DHCPACK on xxx.xxx.xxx.xxx to 00:d0:96:d2:8e:1b via eth1
#Feb 1 01:54:44 fran dhcpd: DHCPDISCOVER from 00:00:00
# Update these when I get a sample -msh 2/1/04
#
sub fixup {
switch ($data) {
case (/\:/) { $mac = $data }
case (/\./) { $ip = $data }
}
switch ($data2) {
case (/\:/) { $mac = $data2 }
case (/\./) { $ip = $data2 }
}
}
sub db_connect {
$dbh = DBI->connect( "DBI:Pg:dbname=noc;host=xxx.xxx.xxx.xxx","noc");
}
open(FD,"/usr/local/noc/dhcpfifo");
#open(FD,"/var/log/all.log");
while (<FD>) {
print $_;
if (! ($dbh) || !(($sth = $dbh->prepare("select now()")) && $sth->execute())) {
if (! db_connect() ) {
print "Unable to connect to database.\n";
next;
} else {
print "Reconnected to DB\n";
}
}
(undef,undef,undef,undef,$daemon,$type,undef,$data,undef,$data2,$data3) = split();
$data = uc($data);
$data2 = uc($data2);
$data3 = uc($data3);
if ($daemon =~ /^dhcpd/) {
$ip = "";
$mac = "";
switch ($type) {
case 'DHCPDISCOVER' { $type = 0; $mac = $data; }
case 'DHCPOFFER' { $type = 1; $mac = $data2; $ip = $data; }
case 'DHCPREQUEST' { $type = 2; $mac = $data2; $ip = $data; if ($mac =~ /FROM/) { $mac = $data3; } }
case 'DHCPACK' { $type = 3; $mac = $data2; $ip = $data; }
case 'DHCPNAK' { $type = 4; fixup(); }
case 'DHCPDECLINE' { $type = 5; fixup(); }
case 'DHCPRELEASE' { $type = 6; $mac = $data2; $ip = $data; }
case 'DHCPINFORM' { $type = 7; $ip = $data; }
else { $type = -1 };
}
if ($mac eq "00:00:00:00:00:00" || $mac eq "00:07:E9:17:87:6F") { # First is random brzcom equip, second is the rogue detector.
$type = -1;
}
if ($type >= 0) {
print "$type . $ip . $mac ($data - $data2)\n";
$prepared = "INSERT INTO dhcp_log (host, timestamp, type, ip, mac) VALUES('fran', NOW(), ?, ?, ?)";
print "$prepared\n" + " what is this site about?. I came here from a blind link";
$sth = $dbh->prepare($prepared) or print "huh?\n";
$sth->execute($type,$ip,$mac) or print "Unable to execute query: $dbh->errstr\n";
}
}
}