toggle power state using amt
[pxelator] / lib / PXElator / amt.pm
index 3324403..3f4426f 100644 (file)
@@ -12,22 +12,39 @@ use Regexp::Common qw/net/;
 use html;
 use ping;
 use CouchDB;
+use Carp qw/confess/;
 
-sub info {
-       my ( $amt, $client_ip ) = @_;
-       my ( $passwd, $ip ) = split(/\s+/, $amt);
+sub ip {
+       my $client_ip = shift || confess "no ip";
+       my ( $passwd, $ip ) = split(/\s+/, client::conf( $client_ip => 'amt' ));
        ( $ip, $passwd ) = ( $passwd, $ip ) if $passwd =~ m/$RE{net}{IPv4}/;
 
        $ip ||= $client_ip;
 
        if ( ! ping::host( $ip ) ) {
-               warn "$ip unreachable";
+               warn "W: ping $ip unreachable\n";
        }
 
        warn "amt $ip ", '*' x length($passwd), "\n";
 
-       $ENV{AMT_HOST} = $ip;
        $ENV{AMT_PASSWORD} = $passwd;
+       $ENV{AMT_HOST} = $ip;
+
+}
+
+sub power_on {
+       ip @_;
+       power_state() == 0;
+}
+
+sub power_state {
+       my $state = eval { Intel::AMT::RemoteControl::SystemPowerState };
+       CouchDB::audit('SystemPowerState', { ip => $ENV{AMT_HOST}, SystemPowerState => $state });
+       return $state;
+}
+
+sub info {
+       ip @_;
 
        my $amt;
        eval {
@@ -38,6 +55,7 @@ sub info {
        warn "ERROR amt $@" if $@;
 
        my $out;
+       my $ip = $ENV{AMT_HOST};
 
        $out = 
                  qq| <a href="http://$ip:16992/" target="$ip">amt</a>|
@@ -49,4 +67,13 @@ sub info {
        return $out;
 }
 
+
+sub RemoteControl {
+       ip shift;
+       my $command = shift;
+       eval { Intel::AMT::RemoteControl::run( $command ) };
+       CouchDB::audit( $command, { ip => $ENV{AMT_HOST}, error => $@ } );
+       warn "ERROR $@" if $@;
+}
+
 1;