fix spelling of netmask
[pxelator] / lib / PXElator / log.pm
1 package log;
2
3 use warnings;
4 use strict;
5 use autodie;
6
7 use server;
8
9 our $dir = "$server::base_dir/log";
10 mkdir $dir unless -e $dir;
11
12 foreach ( glob("$dir/pxelator.*.log") ) {
13         warn "unlink $_ ", -s $_;
14         unlink $_;
15 }
16
17 my $mac_dir = "$dir/mac";
18 mkdir $mac_dir unless -e $mac_dir;
19
20 use Time::HiRes qw/time/;
21 my $start_t = time();
22
23 sub mac {
24         my ( $mac, $message ) = @_;
25         print "MAC $mac $message\n";
26         open(my $fh, '>>', "$mac_dir/$mac");
27         print $fh time() - $start_t, "\t$message\n";
28         close($fh);
29 }
30
31 our $mac_pos;
32
33 use File::Slurp;
34
35 sub mac_changes {
36         my $mac = shift;
37
38         my @changes;
39
40         foreach my $mac_path ( sort glob("$mac_dir/*") ) {
41
42                 open(my $fh, '<', $mac_path);
43
44                 my $pos = $mac_path;
45                 $pos =~ s{/([^/]+$)}{/\.$1\.pos};
46                 my $mac = uc($1);
47                 $mac =~ s{(..)}{$1:}g;
48                 $mac =~ s{:$}{};
49                 seek $fh, read_file($pos), 0 if -e $pos;
50
51                 while(<$fh>) {
52                         chomp;
53                         push @changes, "$_\t$mac";
54                 }
55
56                 warn "pos $pos ",tell($fh) if $server::debug;
57                 write_file $pos, tell($fh);
58         }
59
60         return @changes;
61 }
62
63 1;