diff options
| author | Joachim Tingvold <joachim@tingvold.com> | 2016-03-29 23:13:09 +0200 | 
|---|---|---|
| committer | root <root@wat.gathering.org> | 2016-03-29 23:13:09 +0200 | 
| commit | 7275a8c8b51792c167dab9e052e4abc2423828f4 (patch) | |
| tree | ed994d6763336da78be24887c654e533fcd032d5 | |
| parent | 3409227f931d89f7cbb3395c67fdbf7334cee7bb (diff) | |
Optimize DHCP-tailer.
| -rwxr-xr-x | clients/dhcptail.pl | 26 | 
1 files changed, 20 insertions, 6 deletions
| diff --git a/clients/dhcptail.pl b/clients/dhcptail.pl index 2b7e6bf..9c2111f 100755 --- a/clients/dhcptail.pl +++ b/clients/dhcptail.pl @@ -4,6 +4,7 @@ use POSIX;  use lib '../include';  use nms;  use strict; +use Data::Dumper;  use warnings;  BEGIN { @@ -31,15 +32,28 @@ my %months = (  	Dec => 12  ); -my ($dbh, $q); +my $realtime = 0; +my ($dbh, $q,$check);  $dbh = nms::db_connect(); -$q = $dbh->prepare("INSERT INTO dhcp (switch,time,mac) VALUES((SELECT switch FROM switches WHERE ?::inet << network),?,?)"); +$q = $dbh->prepare("INSERT INTO dhcp (switch,time,ip,mac) VALUES((SELECT switch FROM switches WHERE ?::inet << subnet4 ORDER BY sysname LIMIT 1),?,?,?)"); +$check = $dbh->prepare("SELECT max(time)::timestamp - ?::timestamp < '0s'::interval as doit FROM dhcp;"); +  open(SYSLOG, "tail -n 9999999 -F /var/log/syslog |") or die "Unable to tail syslog: $!";  while (<SYSLOG>) { -	/(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s+(\d+)\s+(\d+:\d+:\d+).*DHCPACK on (\d+\.\d+\.\d+\.\d+) to (\S+)/ or next; -	my $date = $year . "-" . $months{$1} . "-" . $2 . " " . $3; +	/(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s+(\d+)\s+(\d+:\d+:\d+).*DHCPACK on (\d+\.\d+\.\d+\.\d+) to (\S+) / or next; +	my $date = $year . "-" . $months{$1} . "-" . $2 . " " . $3 . " Europe/Oslo";  	my $machine = $5; -	$q->execute($4,$date,$machine); -	$q->commit; +	my $via = $6; +	$check->execute($date); +	$dbh->commit; +	my $cond = $check->fetchrow_hashref(); +	if (!defined($cond) or !defined($cond->{'doit'}) or $cond->{'doit'} eq '1') { +		if ($realtime != 1) { +			$realtime = 1; +			print "real time achieved...\n"; +		} +		$q->execute($4,$date,$4,$machine); +		$dbh->commit; +	}  }  close SYSLOG; | 
