report error without nmap output
[pxelator] / lib / PXElator / httpd.pm
index b811617..6f72a5a 100644 (file)
@@ -18,7 +18,7 @@ use File::Slurp;
 #use JSON;
 use IO::Socket::INET;
 use Regexp::Common qw/net/;
-use YAML;
+use POSIX qw(strftime);
 
 our $title;
 
@@ -77,8 +77,8 @@ qq{
 <a target=server href=/brctl >brctl</a>
 <a target=server href=/ip >ip</a>
 |
-<a target=store href=$store_url >store</a>
-<a target=store href=/store/query >query</a>
+<a target=store href=$store_url >MongoDB</a>
+<a target=store href=/store/latest >latest</a>
 |
 <a target=client href=/nmap >nmap</a>
 <a target=client href=/client >client</a>
@@ -270,23 +270,65 @@ warn "XXX pids = ", dump( $daemons::pids );
                        |
                        ;
 
-       } elsif ( $path =~ m{^/store/query} ) {
+       } elsif ( $path =~ m{^/store/latest} ) {
                print $client ok
+                       , qq|
+<style type=text/css>
+       .z {
+               background: #eee;
+       }
+       td > pre {
+               margin: 0;
+               max-height: 3em;
+               overflow: hidden;
+       }
+       td:hover > pre {
+               max-height: 100%;
+               overflow: show;
+       }
+</style>
+                       |
                        , qq|<table>|
                ;
-               my ( $s1,$s2 ) = ( ' style="background: #eee"', '' );
-               store::query( sub {
+               my ( $s1,$s2 ) = ( ' class=z', '' );
+               my @cols;
+
+               my $from_t = $param->{from_t};
+
+               my $q;
+               $q->{'package.time'} = { '$lt' => $from_t * 1 } if $from_t;
+#              $q->{'package.name'} = { '$ne' => 'syslogd' };
+               $q->{'package.name'} = { '$ne' => 'dhcpd' };
+#              $q->{'package.name'} = { '$ne' => [ 'dhcpd', 'dnsd' ] };
+               $q->{'tag'} = { '$ne' => 'CRON' };
+               store::query( $q, sub {
                        my $o = shift;
                        my $p = delete( $o->{package} );
                        delete( $o->{_id} );
 
+                       if ( ! @cols ) {
+                               #@cols = keys %$p;
+                               @cols = qw( time name );
+                               print $client qq|<tr><th>|
+                                       , join(qq|</th><th>|, @cols)
+                                       , qq|</th><th></th></tr>|
+                               ;
+                       }
+
                        # XXX sigh, dump dies if we don't do this
-                       delete $o->{$_} foreach ( grep { ! defined $o->{$_} } keys %$o );
+#                      delete $o->{$_} foreach ( grep { ! defined $o->{$_} } keys %$o );
 
-                       print $client qq|<tr$s1><td>|, join(qq|</td><td>|, map { $p->{$_} } keys %$p ), qq|</td><td><pre>|, dump( $o ), qq|</pre></td></tr>\n|;
+                       print $client qq|<tr$s1>|
+                               , strftime( qq|<td title="%Y-%m-%d">%H:%M:%S</td>|, localtime($p->{time}) )
+                               , map { qq|<td>$_<td>| } ( $p->{name} , html::pre_dump($o) )
+                               , qq|</tr>\n|
+                       ;
                        ( $s1, $s2 ) = ( $s2, $s1 );
+                       $from_t = $p->{time};
                });
-               print $client qq|</table>|;
+               print $client qq|</table>|,
+                       qq|<a href="/store/latest?from_t=$from_t">more</a>|
+               ;
 
        } elsif ( $path =~ m!^/client(?:/$RE{net}{IPv4}{-keep})?! ) {
                my $ip = $1;
@@ -320,13 +362,19 @@ warn "XXX pids = ", dump( $daemons::pids );
 
                if ( $ip && $ip ne $server::ip ) {
 
-                       my @editable = ( qw/hostname config homepage/ );
-
-                       client::conf( $ip, $_ => $param->{$_} ) foreach @editable;
-
                        my $conf = client::all_conf( $ip );
                        my $config = delete $conf->{config};
 
+                       my @editable = ( qw/hostname config homepage/ );
+                       # add params with config prefix if config exists
+                       push @editable, grep { m{^\Q$config\E} } keys %$conf if $config;
+                       my @update = grep { defined $param->{$_} } @editable;
+                       if ( @update ) {
+                               client::conf( $ip, $_ => $param->{$_} ) foreach @update;
+                               print $client redirect("$url/client/$ip");
+                               return;
+                       }
+
                        my $nmap = qq|<a href=/nmap?scan=$ip>nmap</a>|;
                        my @table = (
                                'ping' => ping::host($ip)
@@ -382,7 +430,9 @@ warn "XXX pids = ", dump( $daemons::pids );
                                my $mac = delete $conf->{mac} || '';
                                my $dev = $arp->{$mac};
 
-                               next unless $dev || $param->{all};
+                               my $in_dhcp_range = ip::in_dhcp_range($ip);
+
+                               next unless $dev || $param->{all} || $in_dhcp_range;
 
                                my $style
                                        = 'style="color:'
@@ -392,7 +442,7 @@ warn "XXX pids = ", dump( $daemons::pids );
 
                                $style ||= '';
                                my $ip_text = qq|<tt>$ip</tt>|;
-                               $ip_text = qq|<tt><b>$ip</b></tt>| if ip::in_dhcp_range($ip);
+                               $ip_text = qq|<tt><b>$ip</b></tt>| if $in_dhcp_range;
 
                                $dev = qq|<tt>$dev</tt>| if $dev;