--- lib/PXElator/httpd.pm 2009/08/11 15:13:19 199 +++ lib/PXElator/httpd.pm 2010/01/31 15:22:34 496 @@ -18,15 +18,23 @@ #use JSON; use IO::Socket::INET; use Regexp::Common qw/net/; +use POSIX qw(strftime); -sub menu {qq{ +our $title; -
-home -server -client -
+sub html_start { +qq{ + + +$title + + +}} +sub html_end { +qq{ + + }} our $port = 7777; @@ -43,12 +51,40 @@ use log; use x11; use amt; -use boolean; use daemons; use kvm; use browser; use network; +use ip; +use wireshark; +use syslogd; +use nmap; +use ping; +use wol; + +use store; + + +sub menu { + my $store_url = $url; + $store_url =~ s{:\d+.+}{:28017}; +qq{ +
+home +| +server +brctl +ip +| +MongoDB +latest +| +nmap +client +
+ +}} sub static { @@ -58,10 +94,11 @@ return if ! -f $full; + return if $full =~ m{\.ico$}; + if ( my $pid = fork ) { # parent close($client); - print "http static child $pid\n"; $static_pids->{$pid} = $path; return 1; } @@ -81,7 +118,7 @@ my $buff; my $pos = 0; - warn "static $path $type $size block: $block\n"; + store::audit( 'static', { pid => $$, path => $path, type => $type, size => $size, block => $block, peerhost => $client->peerhost }); progress_bar::start; @@ -96,13 +133,11 @@ print STDERR "\n"; - warn "exit static child"; - exit(0); } sub ok { - qq|HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n| . menu() + qq|HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n| . html_start() . menu() } sub redirect { @@ -111,25 +146,32 @@ qq|HTTP/1.1 302 Found\r\nContent-type: text/html\r\nLocation: $to\r\n\r\n| } +sub toggle { + my $v = shift; + return $v ? 0 : 1; +} + sub get_request { my ( $client, $path, $param ) = @_; server->refresh; - warn "get_request $path ", $param ? dump( $param ) : '', "\n"; + store::audit( 'request', { path => $path, param => $param, peerhost => $client->peerhost } ); + + $title = $path; if ( my $found = static( $client,$path ) ) { warn "static $found" if $debug; } elsif ( $path eq '/' ) { - my @rows = ( - 'debug', qq|$debug|, - ); + my @rows; my $debug_proc = ''; +warn "XXX pids = ", dump( $daemons::pids ); + foreach my $name ( sort keys %$daemons::pids ) { - my $pid = $daemons::pids->{$name} || next; + my $pid = $daemons::pids->{$name}; # || next; my $html; @@ -138,7 +180,7 @@ if ( -e $proc ) { $html .= qq|$pid|; if ( $debug ) { - $html .= qq| ?|; + $html .= qq| ?| if $name->can('start'); $debug_proc .= qq|$proc
|
@@ -147,17 +189,30 @@
 						;
 				}
 
-				if ( $name->can('start_fork') ) {
-					$html .= qq| $_| foreach $name->start_fork;
+				my $class = $name;
+				$class =~ s{\.\d+$}{};
+
+				if ( $class->can('fork_if_active') ) {
+					$html .= qq| $_| foreach $class->fork_if_active;
 				}
 
-				if ( $name->can('actions') ) {
-					$html .= qq| $_| foreach $name->actions;
+				if ( $class->can('actions') ) {
+					$html .= qq| $_| foreach $class->actions;
 				}
 			} else {
-				$html .= qq|restart $pid exited|;
+				if ( $pid =~ m{^\d+$} ) {
+					$html .= qq|$pid exited |
+				} else {
+					$html .= qq|$pid |;
+				}
+				$html .= qq|restart| if $pid || $name->can('start');
+				if ( $name->can('fork_actions') ) {
+					$html .= qq| $_| foreach $name->fork_actions;
+				}
 			}
 
+			die "no html generated" unless $html;
+
 			push @rows, ( $name => $html );
 		}
 
@@ -176,100 +231,295 @@
 			}
 		}
 
+		my $kvm = kvm::next_nr;
+		$kvm = qq|
create new kvm $kvm
|; + print $client ok , html::table( 2, @rows ) , $below_table + , $kvm , html::tabs( log::mac_changes ) , $debug_proc ; } elsif ( $path =~ m{^/server} ) { + foreach my $name ( keys %$param ) { + eval '$server::' . $name . '= $param->{$name}'; + } + my @table = ( + 'debug' => qq|$debug|, + , 'new_clients' => qq|| + ); + + foreach my $editable ( 'ip', 'bcast', 'netmask', 'ip_from', 'ip_to', 'domain' ) { + my $v = eval '$server::' . $editable; + push @table, ( $editable, qq|| ); + } + + foreach my $readonly ( 'base_dir', 'conf' ) { + my $v = eval '$server::' . $readonly; + push @table, ( $readonly, html::tt $v ); + } + print $client ok - , html::table( 2, map { ( $_, html::tt eval '$server::'.$_ ) } ( 'ip', 'netmask', 'ip_from', 'ip_to', 'domain_name', 'base_dir', 'conf' ) ) + , qq|
| + , html::table( 2, @table ) + , qq| + +
+ | + ; + + } elsif ( $path =~ m{^/store/latest} ) { + print $client ok + , qq| + + | + , qq|| + ; + my ( $s1,$s2 ) = ( ' class=z', '' ); + my @cols; + + my $from_t = $param->{from_t}; + + store::query( $from_t, sub { + my $o = shift; + my $p = delete( $o->{package} ); + delete( $o->{_id} ); + + if ( ! @cols ) { + #@cols = keys %$p; + @cols = qw( time name ); + print $client qq|| + ; + } + + # XXX sigh, dump dies if we don't do this +# delete $o->{$_} foreach ( grep { ! defined $o->{$_} } keys %$o ); + + print $client qq|| + , strftime( qq||, localtime($p->{time}) ) + , map { qq|\n| ; + ( $s1, $s2 ) = ( $s2, $s1 ); + $from_t = $p->{time}; + }); + print $client qq|
| + , join(qq||, @cols) + , qq|
%H:%M:%S$_| } ( $p->{name} , html::pre_dump($o) ) + , qq|
|, + qq|more| + ; + } 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 $ip = $1; + $title = $ip if $ip; - my @table = ( - 'ip' => qq|