--- lib/PXElator/httpd.pm 2009/08/28 19:39:54 326
+++ lib/PXElator/httpd.pm 2009/08/30 15:54:30 379
@@ -19,8 +19,25 @@
use IO::Socket::INET;
use Regexp::Common qw/net/;
-sub menu {qq{
+our $title;
+sub html_start {
+qq{
+
+
+
home
server
@@ -56,6 +73,8 @@
use wireshark;
use syslogd;
use nmap;
+use ping;
+use wol;
use CouchDB;
@@ -107,7 +126,7 @@
}
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 {
@@ -123,6 +142,8 @@
CouchDB::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 '/' ) {
@@ -209,6 +230,7 @@
;
} elsif ( $path =~ m!^/client(?:/$RE{net}{IPv4}{-keep})?! ) {
my $ip = $1;
+ $title = $ip;
if ( $param->{action} eq 'remove' ) {
client::remove( $param->{change_ip} );
@@ -224,38 +246,40 @@
if ( ! $ip ) {
my $peer_ip = $client->peerhost;
- my $netmask = ip::to_int $server::netmask;
- my $network = ip::to_int $server::ip & $netmask;
- my ( $from, $to ) = ( $network | $server::ip_from, $network | $server::ip_to );
- my $ip_int = ip::to_int $peer_ip;
+ my $netmask = ip::to_int $server::netmask;
+ my $network = ip::to_int($server::ip) & $netmask;
+ my $from_int = $network | $server::ip_from;
+ my $to_int = $network | $server::ip_to;
+ my $ip_int = ip::to_int $peer_ip;
# show edit for clients in our dhcp range
- if ( $ip_int >= ( $network | $server::ip_from ) && $ip_int <= ( $network | $server::ip_to ) ) {
+ if ( $ip_int >= $from_int && $ip_int <= $to_int ) {
$ip = $peer_ip;
}
}
if ( $ip && $ip ne $server::ip ) {
- my $hostname = client::conf( $ip, 'hostname' => $param->{hostname} );
+ my @editable = ( qw/hostname config homepage/ );
+
+ client::conf( $ip, $_ => $param->{$_} ) foreach @editable;
+
+ my $conf = client::all_conf( $ip );
+ my $config = delete $conf->{config};
+
+ my $nmap = qq|
nmap|;
my @table = (
+ 'ping' => ping::host($ip)
+ ? qq|
up $nmap|
+ : qq|
down wol $nmap|
+ ,
'ip' => qq|
old: $ip|,
- 'hostname' => qq||,
+ 'mac' => format::mac( delete $conf->{mac}, 'html' ),
+ 'hostname' => qq||,
+ 'config' => html::select( 'config', $config, config::available ),
+ html::conf( $ip, $conf, 'edit', @editable )
);
- my $deploy;
-
- if ( my $mac = client::mac_from_ip( $ip ) ) {
- $deploy = client::conf( $ip, 'deploy' => $param->{deploy} );
- push @table, (
- 'mac' => format::mac( $mac => 'html' ),
- 'deploy' => html::select( 'deploy', $deploy, config::available )
- );
- if ( my $pxelinux = config::for_ip( $ip ) ) {
- $deploy = qq|PXElinux
| . html::pre( $pxelinux );
- }
- }
-
print $client ok
, qq||
- , $deploy
;
- if ( my $amt = client::conf( $ip, 'amt' ) ) {
- print $client qq|AMT
|, amt::info( $amt, $ip );
+ if ( $config ) {
+ if ( my $for_ip = config::for_ip( $ip ) ) {
+ print $client qq|config::for_ip
| . html::pre( $for_ip );
+ }
}
+
+ print $client qq|amt info
|, amt::info( $ip ) if $conf->{amt};
+
} else {
- my $arp = {
- map {
- my @c = split(/\s+/,$_);
- if ( $#c == 5 ) {
- client::save_ip_mac( $c[0], $c[3] );
- ( uc $c[3] => [ $c[0] , $c[5] ] )
- } else {
- }
- } read_file('/proc/net/arp')
- };
+ my @ping;
+ if ( my $host = $param->{ping_target} ) {
+ @ping = ( $host );
+ } elsif ( $param->{ping} ) {
+ @ping = client::all_ips;
+ }
- warn "# arp ",dump( $arp );
+ my $ping = ping::fping( @ping ) if @ping;
+ my $arp = client::arp_mac_dev;
print $client ok
, qq|Clients on $server::ip
|
- , html::table( -6,
- 'ip', 'mac', 'arp', 'hostname', 'deploy', 'conf',
+ , html::table( -5,
+ 'ip', 'mac', 'dev', 'hostname', 'conf',
map {
my $ip = $_;
my $conf = client::all_conf( $ip );
my $mac = delete $conf->{mac} || '';
+ my $style;
+ $style
+ = 'style="color:'
+ . ( $ping->{$ip} ? 'green' : 'red' )
+ . '"'
+ if $ping;
+ $style ||= '';
(
- qq|$ip|
+ qq|$ip|
, format::mac( $mac => 'html' )
- , ( $arp->{$mac} ? $arp->{$mac}->[1] : '' )
+ , $arp->{$mac}
, delete $conf->{hostname}
- , delete $conf->{deploy}
- , ( %$conf ? html::pre_dump( $conf ) : qq|nmap| )
- );
+ , html::conf( $ip, $conf, 'inline' )
+ )
} client::all_ips
)
;
+ print $client qq|
+
+ |;
}
} elsif ( $path =~ m{^/brctl} ) {
- print $client ok
- ,html::pre( `brctl show` )
- ;
+ print $client ok, html::table( -4,
+ map {
+ my @c = split(/\t+/,$_,4);
+ if ( $#c == 1 ) {
+ ( '', '', '', $c[1] )
+ } else {
+ @c
+ }
+ } split(/\n/, `brctl show`)
+ );
} elsif ( $path =~ m{^/ip/?(\w+)?} ) {
print $client ok
, join("\n", map { qq|$_| } ( qw/link addr route neigh ntable tunnel maddr mroute xfrm/ ))
@@ -325,6 +369,11 @@
|;
}
+ } elsif ( $path =~ m{^/wol/(\S+)} ) {
+ print $client redirect( "$url/client/$1" ), wol::power_on($1);
+ } elsif ( $path =~ m!^/amt/(\w+)/$RE{net}{IPv4}{-keep}! ) {
+ my ( $run, $ip ) = ( $1, $2 );
+ print $client redirect( "$url/client/$ip" ), amt::RemoteControl( $ip, $run );
} elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) {
eval 'our $' . $1 . ' = ' . $2;
warn $@ if $@;
@@ -363,6 +412,8 @@
print "url $url\n";
+ syslogd::install_local;
+
while (1) {
my $client = $server->accept() || next; # ALARM trickle us
my $request = <$client>;
@@ -371,6 +422,7 @@
if ($request =~ m{^GET (/.*) HTTP/1.[01]}) {
my $path = $1;
+ $path =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/ge;
my $param;
if ( $path =~ s{\?(.+)}{} ) {
foreach my $p ( split(/[&;]/, $1) ) {
@@ -385,7 +437,7 @@
warn "500 $request";
}
- print $client menu() if $client->connected;
+ print $client menu() . html_end() if $client->connected;
}