PowerControl.pl

Page last edited hace 2148 días 23 horas
De Solaris-ES
Saltar a: navegación, buscar
 #!/usr/bin/perl -w                                                         
                                                                            
 # *********************************************************************** #
 #                                                                         #
 #  SCRIPT: powercontrol                                                   #
 #  AUTHOR: Mariano J. Obarrio Miles <mariano.obarrio@gmail.com>           #
 #    DATE: 31/05/10                                                       #
 #     REV: 0.1                                                            #
 #     LIC: Uso y reproduccion TOTALMENTE LIBRE. Si tienen un seg,         #
 #          envien un mail para saber en que lo estan utilizando.          #
 #          Solo por curiosidad ;)                                         #
 #                                                                         #
 #  PLATFORM: Solaris 10                                                   #
 #                                                                         #
 #  PROPOSITO: Control de apagado y encendido de terminales Sunray         #
 #                                                                         #
 #  ESTE ESCRIPT ES LLAMADO DESDE:                                         #
 #                                                                         #
 #     Linea de comando                                                    #
 #                                                                         #
 #                                                                         #
 #  EXIT CODES:                                                            #
 #                                                                         #
 #  NOTE: You must have root privileges to execute this script.            #
 #                                                                         #
 # Historico:                                                              #
 #    31/05/2010 Rev. 0.1                                                  #
 #    Mariano ->                                                           #
 #            ->                                                           #
 #                                                                         #
 # *********************************************************************** #
                                                                            
 #use strict;                                                               
 #use diagnostics;                                                          
 #use warnings;                                                             
 use Data::Dumper;                                                          
 use Getopt::Std;                                                           
                                                                            
                                                                            
 ###                                                                        
 ### Subrutinas                                                             
 ###                                                                        
 sub cargaHorarios                                                          
 {                                                                          
   my @POWER = @_;                                                          
   my %TERM = ();                                                           
   my $term;                                                                
   my $rango;                                                               
   my $desde;                                                               
   my $hasta;                                                               
   foreach my $on (@POWER){                                                 
         chop($on);                                                         
         $on =~ s/^\ +//g;                                                  
         $on =~ s/\ +$//g;                                                  
         #  pseudo.080020f5abc8:0800-2000                                   
         ($term,$rango) = split(/:/,$on);                                   
         ($desde,$hasta) = split(/-/,$rango);                               
         $TERM{"$term"}{'desde'}=$desde;                                    
         $TERM{"$term"}{'hasta'}=$hasta;                                    
  }                                                                         
   return(%TERM);                                                           
 }                                                                          
                                                                            
 sub listarHorarios                                                         
 {                                                                          
   my (%H) = @_;                                                            
   foreach my $k (keys %H){ print "POWERON : [$k]  $H{$k}{'desde'} $H{$k}{'h
 }                                                                          
                                                                            
 sub PowerControl{                                                          
   my ($term, %H) = @_;                                                     
   my $DATE=`/usr/bin/date +'%H%M'`;                                        
   my $display = listarUT($term);                                           
   my $ton  = "/tmp/$term.on";                                              
   my $toff = "/tmp/$term.off";                                             
   return if(!exists($H{$term}));                                           
   $DATE =~ s/\n//g;                                                        
   if(($DATE > $H{$term}{'desde'}) && ($DATE < $H{$term}{'hasta'})){        
         ### Procedimiento de ENCENDIDO                                     
         if(! -e $ton){                                                     
            system("/usr/bin/touch $ton");                                  
            system("/usr/bin/rm $toff") if( -e $toff);                      
            system("/usr/openwin/bin/xset -display $display -dpms s blank s 
         }                                                                  
   }else{                                                                   
         ### Procedimiento de APAGADO                                       
         if(! -e $toff){                                                    
            system("/usr/bin/touch $toff");                                 
            system("/usr/bin/rm $ton") if( -e $ton);                        
            system("/usr/openwin/bin/xset -display $display dpms force off")
         }                                                                  
   }                                                                        
 }                                                                          
                                                                            
 sub SetPowerControl{                                                       
   my ($term, $estado) = @_;                                                
   return(0) if(!$term || !$estado);                                        
   my $display = listarUT($term);                                           
   my $ton     = "/tmp/$term.on";                                           
   my $toff    = "/tmp/$term.off";                                          
   if("$estado" eq 'on'){                                                   
         ### Procedimiento de ENCENDIDO                                     
         system("/usr/bin/touch $ton");                                     
         system("/usr/bin/rm $toff") if( -e $toff);                         
         system("/usr/openwin/bin/xset -display $display -dpms s blank s off
   }else{                                                                   
         ### Procedimiento de APAGADO                                       
         system("/usr/bin/touch $toff");                                    
         system("/usr/bin/rm $ton") if( -e $ton);                           
         system("/usr/openwin/bin/xset -display $display dpms force off");  
   }                                                                        
 }                                                                          
                                                                            
 sub listarUT{                                                              
   my $term = shift;                                                        
   my $ldisp;                                                               
   my $lterm;                                                               
   my $luser;                                                               
   my ($nada,$cid)=split(/\./,$term);                                       
   my @UTWHO=`/opt/SUNWut/bin/utwho -c`;                                    
   foreach my $who (@UTWHO){                                                
         chop($who);                                                        
         $who =~ s/^\ +//g;                                                 
         $who =~ s/\ +$//g;                                                 
         $who =~ s/\ +/ /g;                                                 
         $who =~ s/\ /:/g;                                                  
         my ($ldisp,$lterm,$luser,$lip,$lcid) = split(/\:/,$who);           
         return("localhost:${ldisp}") if($lcid =~ /$cid/);                  
   }                                                                        
   return("");                                                              
 }                                                                          
                                                                            
 sub usage{                                                                 
         print "\nUsage: PowerControl [ -h | -t {terminal} [-o {on|off}] | -
         print "\t-h     Help\n";                                           
         print "\t-t     Terminal\n";                                       
         print "\t-o     Set Display (ON/OFF)\n";                           
         print "\t-d     Default (segun archivo de configuracion)\n";       
         print "\n\t# PowerControl -t pseudo.080020f5abc8 -o on\n";         
         print "\t# PowerControl -d\n";                                     
         print "\n\tSEE: /var/adm/sun/etc/PowerControl\n";                  
         exit;                                                              
 }                                                                          
                                                                            
 ###                                                                        
 ### MAIN                                                                   
 ###                                                                        
                                                                            
 use vars qw/ %args /;                                                      
 getopts("dht:o:", \%args);                                                 
 if(defined $args{h}){ usage(); exit; }                                     
                                                                            
 if(defined $args{d}){                                                      
    my @POWER_CONTROL=`/usr/bin/cat /var/adm/sun/etc/PowerControl|/usr/bin/e
    my %POWER  = &cargaHorarios(@POWER_CONTROL);                            
    my @UTWHO=`/opt/SUNWut/bin/utwho`;                                      
    foreach my $who (@UTWHO){                                               
         chop($who);                                                        
         $who =~ s/^\ +//g;                                                 
         $who =~ s/\ +$//g;                                                 
         $who =~ s/\ +/ /g;                                                 
         $who =~ s/\ /:/g;                                                  
         my ($ldisp,$lterm,$luser) = split(/\:/,$who);                      
         PowerControl($lterm,%POWER);                                       
    }                                                                       
 }else{                                                                     
   if(defined $args{t} && !defined $args{o}){                               
     SetPowerControl($args{t},'on');                                        
   }else{                                                                   
     SetPowerControl($args{t},$args{o});                                    
   }                                                                        
 }                                                                          
                                                                            
 exit;