LJ Archive

Listing 2

#! /usr/bin/perl -w
use Ace 1.51;
open(LOG,"report.data") ||
die "Can't open logfile\n";
my $DB = Ace->connect(-port => 20000100,
   -host => 'localhost') ||
   die "Can't connect to database\n",
   Ace->error;
$beginrecord = 0;
for (;;){
   while (<LOG>) {
     if (/^\s+hostname: (.*)/) { $host_name = $1;
        $beginrecord = 1}
     elsif (/^\s+ip address: (.*)/) {$ip = $1}
     elsif (/^\s+ethernet address: (.*)/)
        {$c_ea = $1}
     elsif (/^\s+old Ethernet address: (.*)/)
        {$p_ea = $1}
     elsif (/^\s+ethernet vendor: (.*)/)
        {$c_ev = $1}
     elsif (/^\s+old Ethernet vendor: (.*)/)
        {$p_ev = $1}
     elsif (/^\s+timestamp: (.*)/) {$c_ts = $1}
     elsif (/^\s+previous timestamp: (.*)/)
        {$p_ts = $1}
     elsif (/^\s+delta: (.*)/) {$delta_ts = $1}
     else { &process if ($beginrecord) };
   }
sleep 300;  # pause 5 minutes
seek (LOG,0,1);
}
sub process {
   # remove whitespace
   foreach ($host_name,$ip,$c_ea,$p_ea,$c_ev,
      $p_ev,$c_ts,$p_ts,$delta_ts)
   {
      s/^\s+//;
      s/\s+$//;
   }
   # give "unknown" records a unique name
   if ($host_name =~ /<unknown>/)
   { $host_name = "unknown on $c_ts $c_ea" }
   # check to see if the object already exists
   my $existing_host = $DB->fetch(
      Host => "$host_name");
   if (!$existing_host){
      # create new Host object
      my $new_host = $DB->new(-class=>'Host',
         -name=>"$host_name");
   if ($host_name){
      $new_host->add(host_name => "$host_name");
      $host_name = "" }
      if ($ip){ $new_host->add(ip_address =>
         "$ip"); $ip = "" }
      if ($c_ea){ $new_host->add(current_ea =>
         "$c_ea"); $c_ea = ""}
      if ($p_ea){ $new_host->add(previous_ea =>
         "$p_ea"); $p_ea = ""}
      if ($c_ev){ $new_host->add(current_ev =>
         "$c_ev"); $c_ev = ""}
      if ($p_ev){ $new_host->add(previous_ev =>
         "$p_ev"); $p_ev = ""}
      if ($c_ts){ $new_host->add(current_ts =>
         "$c_ts"); $c_ts = ""}
      if ($p_ts){ $new_host->add(previous_ts =>
         "$p_ts"); $p_ts = ""}
      if ($delta_ts){ $new_host->add(delta_ts =>
         "$delta_ts");
      $delta_ts = "" }
      # commit record to the database
      $new_host->commit;
   } else {
      # update existing Host object
      if ($c_ea){ $existing_host->add(current_ea
      => "$c_ea"); $c_ea = ""}
      if ($p_ea){ $existing_host->add(previous_ea
      => "$p_ea"); $p_ea = ""}
      if ($c_ev){ $existing_host->add(current_ev
      => "$c_ev"); $c_ev = ""}
      if ($p_ev){ $existing_host->add(previous_ev
      => "$p_ev"); $p_ev = ""}
      if ($c_ts){ $existing_host->add(current_ts
      => "$c_ts"); $c_ts = ""}
      if ($p_ts){ $existing_host->add(previous_ts
      => "$p_ts"); $p_ts = ""}
      if ($delta_ts){ $existing_host->add(delta_ts
      => "$delta_ts");
      $delta_ts = "" }
      # commit changes to the database
      $existing_host->commit;
   }
   #reset got record flag
   $beginrecord = 0;
}
LJ Archive