Server status.pl

Page last edited hace 2117 días 3 horas
De Solaris-ES
Saltar a: navegación, buscar
#!/usr/bin/perl

# *********************************************************************** #
#                                                                         #
#  SCRIPT: server_status                                                  #
#  AUTHOR: mariano.obarrio@gmail.com                                      #
#    DATE: 11/05/10                                                       #
#     REV: 0.1                                                            #
#                                                                         #
#  PLATFORM: Solaris 10                                                   #
#                                                                         #
#  PROPOSITO: Reporta todo cambio registrado en un sistema.               #
#                                                                         #
#  ESTE ESCRIPT ES LLAMADO DESDE:                                         #
#                                                                         #
#     Linea de comando                                                    #
#                                                                         #
#                                                                         #
#  EXIT CODES:                                                            #
#                                                                         #
#  NOTE: You must have root privileges to execute this script.            #
#                                                                         #
# Historico:                                                              #
#    11/05/2010 Rev. 0.1                                                  #
#                                                                         #
# *********************************************************************** #
# *********************************************************************************************** #
# Paquetes necesarios                                                                             #
#   Algorithm/Diff                                                                                #
#   Mail/Sendmail                                                                                 #
#   Text/Diff                                                                                     #
#                                                                                                 #
# Controles del server                                                                            #
#   0,5,10,15,20,25,30,35,40,45,50 * * * * /var/adm/sun/bin/server_status >/dev/null 2>&1         #
#                                                                                                 #
# Linea de comandos para instalar los paquetes adisionales                                        #
#   cd /var/adm/sun/pkg;gunzip *gz;                                                               #
#   for i in `ls *.tar`; do tar xvf $i; rm $i; done                                               #
#   for i in `ls`; do cd $i; perl Makefile.PL; make; make install; cd ..; rm -fr ./$i; done       #
#                                                                                                 #
# *********************************************************************************************** #

use Text::Diff;
use POSIX qw(strftime);
use Mail::Sendmail;

my $DEBUG=0;
my $IS_SYSTEMCONTROLER=0;
my $IS_ORACLESERVER=0;
my $IS_CONSOLA=0;
my $MAILS="user\@domain.com,user1\@domain.com";
my $TMPFILE="/var/adm/sun/tmp/tempfile01";
my $HOSTNAME=`hostname`;
my $OUTFILES="/var/adm/sun/out";
my $DIRSUMFILE="/var/adm/sun/tmp/checksum";
my $CMDSCSTAT="/usr/cluster/bin/scstat";
my $CMDEGREP="/usr/bin/egrep";
my $DEVNUL="2>/dev/null";
my @DOMS = ("A".."R");
my @ALM = ("sf15k_alm_1","sf15k_alm_2","sf15k_alm_3","sf15k_alm_4","sf15k_alm_5");

#process ARGV
foreach (@ARGV) {
        if (/(-h|--help)/)              { &usage(); exit; }
        if (/(-m|--mailto)/)            { $_ =~ s/(-m|--mailto)//ig; $MAILS=$_; next;}
        if (/(-d|--debug)/) { 
                $_ =~ s/(-d|--debug)//ig; 
                if($_){ $DEBUG=$_;}
                else{$DEBUG=1;} 
                if($DEBUG>=5){ $DEVNUL=""; }
                print "DEBUG LEVEL: $DEBUG\n\n"; 
                next;
        }
        if (/(-o|--oracle_server)/)     { $IS_ORACLESERVE=1; next; }
        if (/(-c|--consola)/)           { $IS_CONSOLA=1; next; }
        if (/(-s|--syscon)/)            { $IS_SYSTEMCONTROLER=1; next; }
}

if($DEBUG>=4){
        print "ARGUMENTOS:\n";
        print "\tIS_ORACLESERVE: [$IS_ORACLESERVE]\n";
        print "\tIS_CONSOLA:[$IS_CONSOLA]\n";
        print "\tIS_SYSTEMCONTROLER:[$IS_SYSTEMCONTROLER]\n";
        print "MAILS:\n";
        print "\t$MAILS\n";
}

# usage - print usage and exit.
#
sub usage {
        print STDERR <<END;
USAGE: server_status [-h] 
   eg, server_status                            # Controla el estado de los equipos
       server_status [-d|--debug][1-5]          # habilita el debug con un nivel de 1 a 5
       server_status [-c|--consola]             # habilita los controles como consola 
       server_status [-s|--syscon]              # habilita los controles como system controller 
       server_status [-o|--oracle_server]       # habilita los controles en oracle
       server_status [-m|--mailto]mail1,mailN   # envia la salida a las siguientes direcciones default "root"
       server_status [-h|--help]                # muestra la ayuda
END
        exit 1;
}

sub _isSystemConrtroller{
        if($IS_SYSTEMCONTROLER == 1){
                $IS_MAIN=`su - sms-svc -c "showfailover -r" 2>/dev/null`;
                chop($IS_MAIN);
                print "_isSystemConrtroller::$IS_MAIN\n" if($DEBUG>=2);
                if($IS_MAIN =~ /MAIN/){ 
                        print "_isSystemConrtroller::SC-MAIN\n" if($DEBUG>=2);
                        return 1; 
                }else{
                        print "_isSystemConrtroller::SC-SPARE\n" if($DEBUG>=2);
                        return 0; 
                }
        }
}

sub _check{
        $LOGS=shift(@_);
        $SUMFILE  = $LOGS;
        $SUMFILE  =~ s/(\/)/_/g;
        $SUMFILE  = "$DIRSUMFILE$SUMFILE";
        $SUMFILE  =~ s/\ /_/g;
        $SUMFILE  =~ s/__/_/g;
        $LOCALTIME = strftime "%Y%m%d%H%M%S", localtime;
        $OUTFILE = $SUMFILE;
        $OUTFILE =~ s/.*(checksum_)//g;
        $STRFILE = $OUTFILE;
        $OUTFILE = "$OUTFILES/$LOCALTIME.$OUTFILE.out";
        $TIPO   = shift(@_);
        $TIPO   = (!$TIPO?"c":$TIPO); 
        $FEC    = strftime "%b %e", localtime;
        my $GETSUM=0;
        #
        print "_check::TIPO DE BUSQUEDA: [$TIPO] ".($TIPO=~ /(C|c)/?"Comando\n":"Log File\n") if($DEBUG>=4);
        #
        # Restricciones en la busqueda
        #
        $restrict  = "mail.crit|550.*User unknown|";
        $restrict .= ".*(SRS Proxy).*(daemon.error).*(Could not upload bulk message).*|";
        $restrict .= ".*(SRS Proxy).*(daemon.error).*(Could not send message from queue).*|";
        $restrict .= ".*(SRS Proxy).*(daemon.error).*(File upload failure).*|";
        #
        #
        # Busqueda del checksum en los comandos/logs
        #
        $GETSUM=`cat $SUMFILE $DEVNUL`; 
        chop($GETSUM); 
        if($TIPO =~ /(C|c)/) {
                print "_check::TIPO:[COMANDO]\n" if($DEBUG>=3);
                $CMDEXEC = "$LOGS $DEVNUL"
        }else{
                print "_check::TIPO:[LOG]\n" if($DEBUG>=3);
                $CMDEXEC = "grep -i error $LOGS $DEVNUL|egrep '".$FEC."' $DEVNUL|egrep -v '".$restrict."' $DEVNUL";
        }
        $SUM = `$CMDEXEC|sum $DEVNUL`;
        chop($SUM);

        if($GETSUM == $SUM || $SUM == "0 0"){
                print "_check::NO HAY CAMBIOS EN :[$LOGS]\n" if($DEBUG>=1);
               #print "_check::NO HAY CAMBIOS EN :[$LOGS]\n" if($DEBUG==1);
               #print "_check::NO HAY CAMBIOS\n\tARCHIVO:[$LOGS]\n\tSUMFILE:[$SUMFILE]\n\tCHECKSUM:[$SUM][$GETSUM]\n" if($DEBUG>=2);
        }else{
                my $genmail = "";
                my $ret     = "";
                my $ok      = 0;
                print "_check::HAY CAMBIOS EN :[$LOGS]\n" if($DEBUG==1);
                print "_check::HAY CAMBIOS\n"             if($DEBUG>=2);
                print "\tARCHIVO:[$LOGS]\n"               if($DEBUG>=2);
                print "\tCMDEXEC:[$CMDEXEC]\n"            if($DEBUG>=2);
                print "\tSUMFILE:[$SUMFILE]\n"            if($DEBUG>=2);
                print "\tSUM:[$SUM]\n"                    if($DEBUG>=2);
                print "\tGETSUM:[$GETSUM]\n"              if($DEBUG>=2);
                print "\tMAILS:[$MAILS]\n"                if($DEBUG>=2);
                print "\tOUTFILES:[$OUTFILES]\n"          if($DEBUG>=2);
                print "\tLOCALTIME:[$LOCALTIME]\n"        if($DEBUG>=2);
                print "\tOUTFILE:[$OUTFILE]\n"            if($DEBUG>=2);
                print "\tSTRFILE:[$STRFILE]\n"            if($DEBUG>=2);
                system("echo $SUM >$SUMFILE");
                system("$CMDEXEC > $OUTFILE");
                # Agregado 20080414
                $TEMP =~ s/^.*($LOCALTIME).//g;
                $TEMP = "/usr/bin/ls -ltr $OUTFILES/*$TEMP*$STRFILE* $DEVNUL|/usr/bin/tail -2 $DEVNUL|awk '{print \$9}' $DEVNUL";
                @DIFF = `$TEMP`;
                $len = scalar @DIFF;
                print "\tSIZEOF \@DIFF:[$len]\n"          if($DEBUG>=2);
                if($len > 1 ){
                        chop($DIFF[0]); 
                        chop($DIFF[1]);
                        if (-e $DIFF[0] && -e $DIFF[1]) {
                           $ret .= diff  $DIFF[0], $DIFF[1], { STYLE => "OldStyle" };
                           $ok = (length($ret) == 0?0:1);
                           print "\tDIFF1:[$DIFF[0]]\n"           if($DEBUG>=2);
                           print "\tDIFF2:[$DIFF[1]]\n"           if($DEBUG>=2);
                           print "\tDIFF: [NO HAY DIFERENCIAS]\n" if($DEBUG>=2);
                        }else{
                           $ret .= "";
                           print "\tDIFF1:[$DIFF[0]]\n"     if($DEBUG>=2);
                           print "\tDIFF2:[$DIFF[1]]\n"     if($DEBUG>=2);
                           print "\tFILE NOT FOUND:[$STRFILE]\n" if($DEBUG>=1);
                        }
                }else{  
                        $ret .= getContent($OUTFILE); 
                        $ok = (length($ret) == 0?0:1);
                }
                ### DEBUGS
                print "\tOK: [$ok]\n"                  if($DEBUG>=2);
                $timeinfo   = substr($LOCALTIME,0,4)."-".substr($LOCALTIME,4,2)."-".substr($LOCALTIME,6,2)." ";
                $timeinfo  .= substr($LOCALTIME,8,2).":".substr($LOCALTIME,10,2);
                $genmail   .= "=============================================================\n";
                $genmail   .= "====      Reporte cambio de estado en la plataforma      ====\n";
                $genmail   .= "=============================================================\n";
                $genmail   .= "==== HOSTNAME      : $HOSTNAME";
                $genmail   .= "==== FILENAME      : $LOGS\n";
                $genmail   .= "==== LOCALTIME     : $timeinfo\n";
                $genmail   .= "==== VALOR ANTIGUO : <\n";
                $genmail   .= "==== VALOR NUEVO   : >\n";
                $genmail   .= "=============================================================\n";
                $genmail   .= $ret;
                $genmail   .= "=============================================================\n";
                $genmail   .= "This is Unix-Land\n";
                $genmail   .= "In quiet nights you can hear the Windows machines reboot...\n";
                $genmail   .= "=============================================================\n";
                %mail = ( To      => "$MAILS",
                          From    => '"Super-User - SERVER_STATUS" <user@domain.com>',
                          Subject => "Subject: $HOSTNAME [$LOGS]",
                          Message => "$genmail"
                        );
                if($ok == 1){
                        sendmail(%mail) or die $Mail::Sendmail::error;
                        print "\tSENDMAIL:[ENVIADO]\n"   if($DEBUG>=2);
                }else{ print "\tSENDMAIL:[NO ENVIADO]\n" if($DEBUG>=2); }

                # Fin agregado 20080414
                # system("$CMDEXEC|/usr/bin/mailx -s 'SERVERSTAT: $HOSTNAME - [$LOGS]' $MAILS");
        } # Fin de la busqueda de checksums
}

sub _check_SystemController{
        return if(!_isSystemConrtroller());
        if($IS_SYSTEMCONTROLER){
                foreach $dom (@DOMS){
                        $cmd = "/var/opt/SUNWSMS/adm/$dom/console";                                     _check($cmd,"l");
                        $cmd = "/var/opt/SUNWSMS/adm/$dom/messages";                                    _check($cmd,"l");
                        $cmd = "/var/opt/SUNWSMS/adm/$dom/syslog";                                      _check($cmd,"l");
                        $cmd = "/usr/bin/cat -s /etc/opt/SUNWSMS/config/$dom/blacklist";                _check($cmd);
                        $cmd = "/usr/bin/egrep file.system.full /var/opt/SUNWSMS/adm/$dom/console";     _check($cmd);
                }
                _check("/var/opt/SUNWSMS/adm/platform/messages","l");
                _check("/var/opt/SUNWSMS/adm/anonymous/syslog","l");
                _check("/usr/bin/cat -s /etc/opt/SUNWSMS/config/asr/blacklist");
                _check("/usr/bin/cat -s /etc/opt/SUNWSMS/config/platform/blacklist");
        }
}
sub _check_SE3510{
        if($IS_CONSOLA){
                foreach $storage (@ALM){
                        _check("/usr/sbin/sccli -w sun $storage show logical");
                        _check("/usr/sbin/sccli -w sun $storage show disks");
                        _check("/usr/sbin/sccli -w sun $storage show battery");
                        _check("/usr/sbin/sccli -w sun $storage show auto-write-through-trigger");
                        _check("/usr/sbin/sccli -w sun $storage show events");
                        #_check("/usr/sbin/sccli -w sun $storage show peripheral-device-status");
                        #_check("/usr/sbin/sccli -w sun $storage show enclosure-status");
                }
        }
}

sub _check_SolarisHost{
        _check("/var/adm/messages","l");
        #_check("/var/log/syslog","l");
        #_check("/usr/sbin/prtconf -v");
        #_check("/usr/sbin/psrinfo");
        _check("/usr/sbin/ifconfig -a");
        _check("/usr/sbin/eeprom");
        _check("/var/adm/sun/sbin/getdf-k");
        _check("/usr/bin/uname -a");
        _check("/usr/sbin/dumpadm");
        _check("/usr/bin/isainfo -kv");
        _check("/usr/bin/priocntl -l");
        _check("/usr/sbin/dispadmin -l");
        _check("/usr/bin/uname -X");
        _check("/usr/bin/cat -s /etc/system");
        _check("/usr/bin/cat -s /etc/path_to_inst");
        _check("/usr/bin/cat -s /etc/vfstab");
        _check("/usr/bin/cat -s /etc/inittab");
        _check("/usr/bin/cat -s /etc/init.d/rutas");
        _check("/usr/bin/cat -s /etc/hosts");
        _check("/usr/bin/cat -s /var/spool/cron/crontabs/root");
        _check("/usr/bin/cat -s /etc/inet/ipnodes");
        _check("/usr/bin/cat -s /etc/inet/ntp.conf");
        _check("/usr/bin/cat -s /etc/inet/netmasks");
        _check("/usr/bin/cat -s /etc/inet/networks");
        _check("/usr/bin/cat -s /etc/inet/protocols");
        _check("/usr/bin/cat -s /etc/inet/routing.conf");
        _check("/usr/bin/cat -s /etc/inet/services");
        _check("/usr/bin/cat -s /etc/ssh/sshd_config");
        _check("/usr/bin/cat -s /etc/nsswitch.conf");
        _check("/usr/bin/cat -s /etc/resolv.conf");
        _check("/usr/bin/cat -s /etc/rmmount.conf");
        _check("/usr/bin/cat -s /etc/nodename");
        _check("/usr/bin/cat -s /etc/mail/aliases");
        _check("/usr/bin/cat -s /etc/mail/relay-domains");
        _check("/usr/bin/cat -s /etc/mail/sendmail.cf");
        _check("/var/adm/sun/sbin/cat_recursivo /etc/hostname");
        _check("/var/adm/sun/sbin/cat_recursivo /var/spool/cron/crontabs/");
        _check("/usr/bin/cat -s /etc/rc2.d/S69staticroutes");
        _check("/usr/bin/cat -s /etc/passwd");
        _check("/usr/bin/cat -s /etc/printers.conf");
        #_check("/usr/bin/ls -la /usr/bin");
        #_check("/usr/bin/ls -la /usr/sbin");
        _check("/usr/bin/pkginfo");
        _check("/usr/bin/showrev -p");
        _check("/usr/bin/egrep -i \'file.system.full\' /var/adm/messages");
        _check("/usr/bin/svcs");
        _check("/usr/sbin/metadb -i");
        _check("/usr/sbin/metastat");
        _check("/usr/sbin/metastat -p");
}

sub getContent{
        $filename = shift;
        print "\tgetContent::FILENAME:[$filename]\n"   if($DEBUG>=2);
        my $ret = "";
        open (FD, "$filename") || die "couldn't open the file!";
        while ($record = <FD>) { $ret .= $record; }
        close(FD);
        return $ret;
}

_check_SolarisHost();           # Controles estandares en cualquier host Solaris
_check_SE3510();                # Controles sobre los SE3510
_check_SystemController();      # Controles sobre la System Controller del SF15K


  • SCRIPT: /var/adm/sun/sbin/cat_recursivo
### #!/usr/bin/sh
### 
### RET=`/usr/bin/ls $1*`
### for i in $RET
### do
###         /usr/bin/echo ::::::::::::::
###         /usr/bin/echo $i
###         /usr/bin/echo ::::::::::::::
###         /usr/bin/cat -s $i
###         /usr/bin/echo 
### done

### chmod 755 /var/adm/sun/sbin/cat_recursivo