--- lib/PXElator/httpd.pm 2009/08/06 18:55:51 166 +++ lib/PXElator/httpd.pm 2009/08/10 16:36:16 193 @@ -29,17 +29,6 @@ }} -our $pids; -$pids = { httpd => $$ } unless defined $pids; # keep pids on refresh - -sub DESTROY { - warn "pids ",dump( $pids ); - foreach ( values %$pids ) { - warn "kill $_"; - kill 1,$_ || kill 9, $_; - } -} - our $port = 7777; use server; @@ -52,6 +41,15 @@ use config; use client; use log; +use x11; +use amt; +use boolean; +use daemons; + +use kvm; +use browser; +use network; + sub static { my ($client,$path) = @_; @@ -103,53 +101,6 @@ exit(0); } -use boolean; - -use kvm; -use qemu; $pids->{qemu} = 'stopped'; - -$SIG{CHLD} = 'IGNORE'; - -sub start_stop { - my $daemon = shift; - my $pid = $pids->{$daemon} || 'not started'; - - warn "start_stop $daemon $pid\n"; - - if ( $pid =~ m{^\d+$} ) { - my $pstree = `pstree -p $pid`; - my @pids = $pstree =~ m{\((\d+)\)}g; - warn "pstree $pstree pids ",dump( @pids ); - kill 1, $_ foreach reverse @pids; - $pids->{$daemon} = 'stopped'; - return qq|$daemon pid $pid stopped|; - } else { - if ( $pid = fork ) { - # parent - $pids->{$daemon} = $pid; - warn "forked $daemon $pid\n"; - return qq|$daemon pid $pid started|; - } elsif ( defined $pid ) { - # child - my $invoke = 'start'; - $invoke = $1 if $daemon =~ s{/(.+)}{}; - if ( $daemon =~ m{dhcpd|tftpd|dnsd} ) { - my $exec = "perl -I$server::base_dir/lib -I$server::base_dir/lib/PXElator -M$daemon -e ${daemon}::${invoke}"; - warn "exec $exec"; - exec "xterm -T $daemon -n $daemon -e $exec"; - } else { - my $eval = $daemon . '::' . $invoke . '(' . ( @_ ? dump(@_) : '' ) . ')'; - warn "eval $eval"; - eval $eval; - warn "can't start $daemon: $@" if $@; - } - exit; - } else { - die "fork error $!"; - } - } -} - my $ok = qq|HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n| . menu(); my $redirect = qq|HTTP/1.1 302 Found\r\nContent-type: text/html\r\nLocation: $url\r\n\r\n|; @@ -170,15 +121,15 @@ my $debug_proc = ''; - warn 'pids: ', dump( $pids ) if $debug; - foreach my $name ( sort keys %$pids ) { - my $pid = $pids->{$name} || next; + foreach my $name ( sort keys %$daemons::pids ) { + my $pid = $daemons::pids->{$name} || next; - my $html = qq|$pid|; + my $html; my $proc = "/proc/$pid/status"; if ( -e $proc ) { + $html .= qq|$pid|; if ( $debug ) { $html .= qq| ?|; @@ -196,6 +147,8 @@ if ( $name->can('actions') ) { $html .= qq| $_| foreach $name->actions; } + } else { + $html .= qq|restart $pid exited|; } push @rows, ( $name => $html ); @@ -225,39 +178,80 @@ } elsif ( $path =~ m{^/server} ) { print $client $ok - , html::table( 2, map { ( $_, eval '$server::'.$_ ) } ( 'ip', 'netmask', 'ip_from', 'ip_to', 'domain_name', 'base_dir' ) ) + , html::table( 2, map { ( $_, html::tt eval '$server::'.$_ ) } ( 'ip', 'netmask', 'ip_from', 'ip_to', 'domain_name', 'base_dir', 'conf' ) ) ; } elsif ( $path =~ m!^/client(?:/$RE{net}{IPv4}{-keep})?! ) { my $ip = $1 || $client->peerhost; if ( $ip ne $server::ip ) { my $hostname = client::conf( $ip, 'hostname' => $param->{hostname} ); - my $deploy = client::conf( $ip, 'deploy' => $param->{deploy} ); + + my @table = ( + 'ip' => $ip, + 'hostname' => qq||, + ); + + my $deploy; + + if ( my $mac = client::mac( $ip ) ) { + $deploy = client::conf( $ip, 'deploy' => $param->{deploy} ); + push @table, ( + 'mac' => $mac, + 'deploy' => html::select( 'deploy', $deploy, config::available ) + ); + $deploy = qq|

PXElinux

| . html::pre( config::for_ip( $ip ) ); + } + print $client $ok , qq|
| - , html::table( 2, - 'ip' => $ip, - 'mac' => client::mac( $ip ), - 'hostname' => qq||, - 'deploy' => html::select( 'deploy', $deploy, config::available ), - ) + , html::table( 2, @table ), , qq|
| - , qq|

PXElinux $deploy

|
-				, config::for_ip( $ip )
-				, qq|
| + , $deploy ; + + if ( my $amt = client::conf( $ip, 'amt' ) ) { + print $client qq|

AMT

|, amt::info( $amt ); + } } else { + + my $arp = { + map { + my @c = split(/\s+/,$_); + if ( $#c == 5 ) { + ( uc $c[3] => [ $c[0] , $c[5] ] ) + } else { + } + } read_file('/proc/net/arp') + }; + + warn "# arp ",dump( $arp ); + print $client $ok , qq|

Clients on $server::ip

| - , qq|| + , qq|

ARP

| + , html::table( -3, 'mac', 'dev', 'ip', + map { + my $c = $arp->{$_}; + ( html::tt( $_ ), $c->[1], $c->[0] ) + } sort keys %$arp + ) ; } } elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) { @@ -266,7 +260,7 @@ print $client $redirect, qq|$1 = $2
Location: $url|; server::debug( $debug ) if $1 eq 'debug'; } elsif ( $path =~ m{^/start_stop/(\S+)} ) { - print $client $redirect, start_stop($1); + print $client $redirect, daemons::start_stop($1); } elsif ( $path =~ m{^/action/([^/]+)/(.+)} ) { $1->$2(); print $client $redirect; @@ -280,15 +274,17 @@ } -use browser; -use network; - sub start { warn 'tap ', network::tap(); + daemons::start_stop 'browser', $url; + daemons::start_stop $_ foreach ( qw/dhcpd tftpd dnsd/ ); + daemons::start_stop 'kvm' unless $ENV{DEV}; # skip kvm statup when running on real device + my $server = IO::Socket::INET->new( Proto => 'tcp', + LocalAddr => $server::ip, LocalPort => $httpd::port, Listen => SOMAXCONN, Reuse => 1 @@ -296,12 +292,6 @@ print "url $url\n"; - start_stop 'browser', $url; - start_stop 'dhcpd'; - start_stop 'tftpd'; - start_stop 'dnsd'; - start_stop 'kvm'; - while (1) { my $client = $server->accept() || next; # ALARM trickle us my $request = <$client>;