first verision which boots virtual machine logging using new CouchDB::audit code
authorDobrica Pavlinusic <dpavlin@rot13.org>
Wed, 12 Aug 2009 22:56:45 +0000 (22:56 +0000)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Wed, 12 Aug 2009 22:56:45 +0000 (22:56 +0000)
it need some more work to make it less ugly (especially console output) and
less redundant/useful, but it's a start

lib/PXElator/CouchDB.pm
lib/PXElator/daemons.pm
lib/PXElator/dhcpd.pm
lib/PXElator/dnsd.pm
lib/PXElator/httpd.pm
lib/PXElator/shell.pm
lib/PXElator/tftpd.pm

index 2b3357f..3396eda 100644 (file)
@@ -74,7 +74,6 @@ sub get {
 
 sub put {
        my ($self, $url, $json) = @_;
-       warn "put $url ",dump($json);
 
        if ( ! defined $json->{_rev} ) {
                my $old = eval { $self->get( $url )->{_rev} };
@@ -103,18 +102,22 @@ sub audit {
        $url =~ s/-\S+//g;
        $url =~ s/\W+/-/g;
 
-       my ( $package, undef, $line, $sub ) = caller(1);
+       my ( $package, $file, $line, $sub ) = caller(1);
        ( $package, undef, $line ) = caller(0) if $package eq 'main';
 
+       my $t = time();
+
        $data->{x_meta} = {
-               'ident' => [ @_ ],
-               'time' => time(),
+               'time' => $t,
                'package' => $package,
                'line' => $line,
                'sub' => $sub,
        };
+       $data->{'ident'} = [ @_ ] if @_;
+
+       warn 'audit ', dump($data), "at $file +$line\n";
 
-       $audit->put( "pxelator/$package.$url", $data );
+       $audit->put( "pxelator/$t.$package.$url", $data );
 }
 
 1;
index 5bc6889..28aa8b2 100644 (file)
@@ -9,7 +9,7 @@ use Data::Dump qw/dump/;
 use x11;
 
 our $pids;
-$pids = { httpd => $$ } unless defined $pids; # keep pids on refresh
+$pids = { httpd => $$, kvm => 'not started' } unless defined $pids; # keep pids on refresh
 
 sub DESTROY {
        warn "pids ",dump( $pids );
index 4ecbbb3..2c3c9a7 100644 (file)
@@ -23,6 +23,8 @@ use lib '..';
 use Net::DHCP::Packet;
 use Net::DHCP::Constants 0.67;
 
+use CouchDB;
+
 use server;
 my $debug = server::debug;
 
@@ -87,6 +89,9 @@ sub process_packet {
        my $hostname = $dhcp->getOptionValue(DHO_HOST_NAME);
        print "$ip ", client::conf( $ip => 'hostname', default => $hostname ), " >> /etc/hosts\n";
 
+       my $audit = { mac => $mac, ip => $ip, hostname => $hostname };
+       CouchDB::audit( 'request', $audit );
+
 =for later
 
        my $user_class = $dhcp->getOptionValue(DHO_USER_CLASS());
@@ -139,6 +144,8 @@ sub process_packet {
        }
 
        warn "W: options requested but missing: ",dump( @missing ),$/;
+       $audit->{requested} = [ @requested ];
+       $audit->{missing}   = [ @missing   ];
 
        foreach my $opt ( 'magic', 'config_file', 'path_prefix', 'reboot_time' ) {
                my $DH0 = eval 'DHO_PXELINUX_' . uc $opt;
@@ -153,12 +160,12 @@ sub process_packet {
        my $messagetype = $dhcp->getOptionValue(DHO_DHCP_MESSAGE_TYPE());
 
        if ($messagetype eq DHCPDISCOVER()) {
-               log::mac $mac, "DHCP DISCOVER";
+               $audit->{type} = 'discover';
                $packet->{Comment} = $dhcp->comment();
                $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPOFFER();
        } elsif ($messagetype eq DHCPREQUEST()) {
                my $requested_ip = $dhcp->getOptionValue(DHO_DHCP_REQUESTED_ADDRESS());
-               log::mac $mac, "DHCP REQUEST $requested_ip $ip $file";
+               $audit->{type} = 'request';
                if ( $ip eq $requested_ip ) {
                        $packet->{DHO_DHCP_MESSAGE_TYPE()}      = DHCPACK();
                        $packet->{DHO_DHCP_LEASE_TIME()}        = 5 * 60; # 5 min
@@ -168,16 +175,19 @@ sub process_packet {
                        $packet->{DHO_DHCP_MESSAGE()} = "Bad request, expected $ip";
                }
        } elsif ($messagetype eq DHCPINFORM()) {
-               log::mac $mac, "DHCP INFORM ignored";
+               $audit->{type} = 'inform';
        } else {
-               log::mac $mac, "$messagetype igored (bootp?)";
+               $audit->{type} = sprintf('ignored %x', $messagetype);
        }
 
        warn ">> $mac == $ip server: $server::ip", $file ? " file: $file\n" : "\n" if $debug;
+       $audit->{response} = $packet;
 
        $packet = new Net::DHCP::Packet( %$packet );
        warn "send ",$packet->toString() if $debug;
 
+       CouchDB::audit( 'response', $audit );
+
        my $reply = IO::Socket::INET->new(
                LocalAddr => $server::ip,
                LocalPort => 67,
@@ -211,6 +221,8 @@ sub start {
 
        print "DHCP listen on ",$sock->sockhost,":",$sock->sockport,"\n";
 
+       CouchDB::audit( 'start', { 'listen' => { addr => $sock->sockhost, port => $sock->sockport } } );
+
        while (1) {
                process_packet $sock;
        }
index f94e80a..b82664f 100644 (file)
@@ -6,6 +6,7 @@ use strict;
 use Net::DNS::Nameserver;
 use Net::DNS::Resolver;
 use Data::Dump qw/dump/;
+use CouchDB;
 
 use server;
 our $debug = server::debug;
@@ -30,7 +31,14 @@ sub reply_handler {
        server->refresh;
        $debug = server::debug;
 
-       print "$qname $qclass $qtype $peerhost to ". $conn->{"sockhost"}. "\n";
+       CouchDB::audit( 'request', {
+               qname => $qname,
+               qclass => $qclass,
+               qtype => $qtype,
+               peerhost =>  $peerhost,
+               sockhost => $conn->{"sockhost"}
+       });
+
        $query->print if $debug;
 
        my $local = $1     if $qname =~ m{^(.+)\.\Q$server::domain_name\E$};
@@ -44,6 +52,7 @@ sub reply_handler {
                my $rdata;
                if ( $qtype eq "A" && $local eq "server" ) {
                        $rdata = name_ip( $local, $server::ip );
+                       CouchDB::audit('local', $rdata);
                } else {
                        $rcode = "NXDOMAIN";
                }
@@ -54,12 +63,14 @@ sub reply_handler {
                        if ( my $rdata = $ptr_cache->{$1} ) {
                                $rdata .= '.' . $server::domain_name;
                                push @ans, Net::DNS::RR->new("$qname $ttl $qclass $qtype $rdata");
+                               CouchDB::audit('ptr', $rdata );
                        } else {
 warn "## ",dump( $ptr_cache );
                                $rcode = "NXDOMAIN";
                        }
        } elsif ( my $packet = $res->query( $qname, $qtype ) ) {
 
+               CouchDB::audit( 'upstream', $packet );
                $packet->print;
                push @ans, $_ foreach $packet->answer;
                $rcode = "NOERROR";
@@ -71,6 +82,8 @@ warn "## ",dump( $ptr_cache );
 
        warn "rcode: $rcode ",dump( @ans );
 
+       CouchDB::audit('response', { rcode => $rcode, ans => [ @ans ], auth => [ @auth ], add => [ @add ] });
+
        # mark the answer as authoritive (by setting the 'aa' flag
        return ($rcode, \@ans, \@auth, \@add, { aa => 1 });
 }
@@ -82,6 +95,7 @@ sub start {
                Verbose      => $debug,
        ) || die "couldn't create nameserver object\n";
 
+       CouchDB::audit('start', { listen => { port => 53, domain_name => $server::domain_name } });
        warn "DNS $server::domain_name";
 
        $ns->main_loop;
index 7f47640..84cb16a 100644 (file)
@@ -50,6 +50,7 @@ use kvm;
 use browser;
 use network;
 
+use CouchDB;
 
 sub static {
        my ($client,$path) = @_;
@@ -61,8 +62,8 @@ sub static {
        if ( my $pid = fork ) {
                # parent
                close($client);
-               print "http static child $pid\n";
                $static_pids->{$pid} = $path;
+               CouchDB::audit('static', 'parent', { pid => $pid, path => $path });
                return 1;
        }
 
@@ -81,7 +82,7 @@ sub static {
        my $buff;
        my $pos = 0;
 
-       warn "static $path $type $size block: $block\n";
+       CouchDB::audit( 'static', 'child', { pid => $$, path => $path, type => $type, size => $size, block => $block });
 
        progress_bar::start;
 
@@ -96,7 +97,7 @@ sub static {
 
        print STDERR "\n";
 
-       warn "exit static child";
+       CouchDB::audit( 'static', 'child', 'exit', { pid => $$ } );
 
        exit(0);
 }
@@ -116,7 +117,7 @@ sub get_request {
 
        server->refresh;
 
-       warn "get_request $path ", $param ? dump( $param ) : '', "\n";
+       CouchDB::audit( 'request', { path => $path, param => $param } );
 
        if ( my $found = static( $client,$path ) ) {
                warn "static $found" if $debug;
index bda5486..e6eeef6 100644 (file)
@@ -5,6 +5,7 @@ use strict;
 
 use autodie;
 use Data::Dump qw/dump/;
+use CouchDB;
 
 our $command;
 
@@ -46,6 +47,7 @@ warn "command $command key $key";
                if ( $key ) {
                        my $k = $cols[$key - 1];
                        $info->{$k} = $hash;
+                       CouchDB::audit( $command, $k, $hash );
                } else {
                        push @$info, $hash;
                }
index b0845cd..eb20fce 100644 (file)
@@ -6,6 +6,7 @@ use strict;
 use Net::TFTPd 0.03 qw(%OPCODES);
 use IO::Socket::INET;
 use Data::Dump qw/dump/;
+use CouchDB;
 
 use server;
 
@@ -46,7 +47,8 @@ sub tftp_request {
 
        warn 'request: ', dump( $request ) if $debug;
 
-       config::for_ip( $request->{_REQUEST_}->{PeerAddr} );
+       my $ip = $request->{_REQUEST_}->{PeerAddr};
+       config::for_ip( $ip );
 
        if ( $request->{RootDir} ne $dir ) {
                $request->{RootDir} = $dir;
@@ -54,16 +56,22 @@ sub tftp_request {
        }
 
        my $file = $request->{'_REQUEST_'}{'FileName'};
-       # received request
-       print $OPCODES{$request->{'_REQUEST_'}{'OPCODE'}}, " $file\n";
+       my $opcode = $OPCODES{$request->{'_REQUEST_'}{'OPCODE'}};
+
+       CouchDB::audit('request', {
+               ip => $ip,
+               opcode => $opcode,
+               file => $file,
+       });
 
        progress_bar::start;
 
        # process the request
        if( $request->processRQ() ) {
-               print "\nOK completed $file ", -s "$dir/$file", "\n";
+               my $size = -s "$dir/$file";
+               CouchDB::audit('completed', { file => $file, size => $size });
        } else {
-               print "ERROR ", Net::TFTPd->error, "\n";
+               CouchDB::audit('error', { file => $file, error => Net::TFTPd->error });
                $request->processRQ();
        }
 
@@ -107,10 +115,12 @@ sub start {
 
        warn 'listener: ',dump( $listener ) if $debug;
 
-       printf "TFTP listen %s:%d timeout: %d dir: $dir\n",
-               $listener->{LocalAddr},
-               $listener->{LocalPort},
-               $listener->{Timeout};
+       CouchDB::audit( 'start', {
+               addr => $listener->{LocalAddr},
+               port => $listener->{LocalPort},
+               timeout => $listener->{Timeout},
+               params => { %params },
+       });
 
        while(1) {