diff options
author | Kristian Lyngstol <kristian@bohemians.org> | 2016-04-12 18:01:32 +0200 |
---|---|---|
committer | Kristian Lyngstol <kristian@bohemians.org> | 2016-04-12 18:01:32 +0200 |
commit | 09ea945c3908fd42e90eb64c194d9af11d174206 (patch) | |
tree | 8bcf0f98113ea4a92effa1c39344730c7e6f50b0 /clients/dhcptail.pl | |
parent | c3ebdbda43c23ade1e49dfd9f546fba1d15f9941 (diff) |
Actual initial import
Fetched from tgmanage.
Diffstat (limited to 'clients/dhcptail.pl')
-rwxr-xr-x | clients/dhcptail.pl | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/clients/dhcptail.pl b/clients/dhcptail.pl new file mode 100755 index 0000000..9c2111f --- /dev/null +++ b/clients/dhcptail.pl @@ -0,0 +1,59 @@ +#! /usr/bin/perl +use DBI; +use POSIX; +use lib '../include'; +use nms; +use strict; +use Data::Dumper; +use warnings; + +BEGIN { + require "../include/config.pm"; + eval { + require "../include/config.local.pm"; + }; +} + +my $year = $nms::config::tgname; +$year =~ s/tg/20/; # hihi + +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 $realtime = 0; +my ($dbh, $q,$check); +$dbh = nms::db_connect(); +$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 . " Europe/Oslo"; + my $machine = $5; + 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; |