#! /usr/bin/perl
use DBI;
use POSIX;
use strict;
use warnings;

# WARNING: CHANGE THIS! :-P
my $year = 2006;

my %months = (
	Jan => 1,
	Feb => 2,
	Mar => 3,
	Apr => 4,
	May => 5,
	Jun => 6,
	Jul => 7,
	Aug => 8,
	Sep => 9,
	Oct => 10,
	Nov => 11,
	Dec => 12
);

my ($dbh, $q);
while (<>) {
	/(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s+(\d+)\s+(\d+:\d+:\d+).*DHCPACK on (\d+\.\d+\.\d+\.\d+)/ or next;
	my $date = $year . "-" . $months{$1} . "-" . $2 . " " . $3;

	if (!defined($dbh) || !$dbh->ping) {
		$dbh = DBI->connect("dbi:Pg:dbname=snmpfetch;host=violet.tg05.gathering.org", "snmpfetch", "removed")
			or die "Couldn't connect to database";
		$q = $dbh->prepare("UPDATE dhcp SET last_ack=? WHERE inet ? << network AND ( last_ack < ? OR last_ack IS NULL )")
			or die "Couldn't prepare query";
	}

	print STDERR "$date $4\n";
	$q->execute($date, $4, $date)
		or die "Couldn't push $1 into database";
}
