#!/usr/bin/perl -s

#
# Copyright (c) 2001, 2011, Oracle and/or its affiliates. All rights reserved. 
#
#  $Id:oracle_sage_resp.pl 
#
#
# NAME  
#   oracle_sage_resp.pl
#
# DESC 
#
# This script gets all the static configuration of the Oracle Sage Cell.   
#
# NOTES
#
# MODIFIED  (MM/DD/YY)
#    spalapu 12/11/09 - add flag for invoking with TestVersion metric
#    spalapu 08/28/08 - 
#    spalapu 08/28/08 - 
#    spalapu 08/27/08 - 
#    spalapu 08/27/08 - replace sagemonitor with cellmonitor
#    spalapu 08/26/08 - 
#    spalapu 08/26/08 - removed sage from plug-in and added exadata. Also,
#                       fixed realm reports to remove targer_guid from realm
#                       reports. Creating new files from oracle_sage_cell
#    spalapu 03/28/08 - change ifs to elsifs where possible
#    spalapu 03/19/08 - Add Copyright statements
#    spalapu 03/18/08 - 
#    spalapu 03/17/08 - 
#    spalapu 02/26/08 - 
#    spalapu 02/13/08 - new file
#   spalapud 10/07/06 	Created the file.
# 

use XML::Parser;
require emx::oracle_cell::oracle_cell_common;
require "emd_common.pl";

$ipaddress=doPing(@ARGV);

if($ipaddress eq "") {
  if ($test != 1) {
    print "em_result=0";
    exit;
  }
  else {
    print "target is unreachable over the network or is down or non-existent. Please check if cell can be pinged with ttl value 100/n";
    exit -1;
  }
}
my $command = "ssh -l cellmonitor $ipaddress cellcli  -xml -e ' list cell attributes msStatus '";
EMD_PERL_DEBUG("command = $command \n");

my $str;
use Data::Dumper;

      eval {
        local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required
        alarm $timeout;
        $xmlout = `$command`;
        $str = Dumper($xmlout);
        EMD_PERL_DEBUG($str);
        alarm 0;
      };
      if ($@) {
           die unless $@ eq "alarm\n"; # propagate unexpected errors
           if ($test != 1) {
             print "em_result=0\n";
             exit;
           } else {
           print "please check on the agent host,if ssh access is setup according to the installation guide and is working as expected";
           exit -1; # Indicate timeout and unavailabilty.
           }
 # timed out
      }
#my $xmlout = `$command`;

my $parser = XML::Parser->new( Handlers => {
                                      Start=>\&handle_start,
                                      End=>\&handle_end,
				      Char=> \&characterData,
                                     });
$str = Dumper($parser);
EMD_PERL_DEBUG($str);

my $result;
my $myinf;
$parser->parse( $xmlout);
#print "em_result=$result\n";


#-----------------------------------------------------------------
# FUNCTIONS :  handle_start
#
#
# DESC : Does node based parsing of the XML output.
#
# ARGUMENTS :
#		elements : Node elements in the XML 
#  		attrs 	 : Attributes of the elements.
#
#
#-----------------------------------------------------------------

sub handle_start {
    my( $expat, $element, %attrs  ) = @_;
    $myinf="";
    if ($element eq "msStatus")
    {
       $myinf="status_ms";
    }
    else
    {
       EMD_PERL_DEBUG("invisible element = $element \n");
    }
}

sub handle_end {
    my( $expat, $element, %attrs  ) = @_;
    if ($element eq "msStatus")
    {
      if ($status_ms eq "running ") {
        $status_ms="1";
      } else {
        $status_ms="0";
      }
      print "em_result=$status_ms";
    }
    else
    {
       EMD_PERL_DEBUG("find not cell element: $element \n");
    }
}

sub characterData {
    my( $parseinst, $data ) = @_;
    $space=" ";
    $data =~ s/\n//g;
    if (($data !~ m/^\s/) and ($data ne "\""))
    {
       if ($myinf eq "status_ms" )
       {
         $status_ms=$status_ms.$data;
       }
    }
}

