#!/usr/bin/perl

#
# Copyright (c) 2001, 2011, Oracle and/or its affiliates. All rights reserved. 
#
#  $Id:oracle_cell_cell_metrics.pl 
#
#
# NAME  
#   oracle_cell_cell_metrics.pl
#
# DESC 
#
# This script gets all the static configuration of the Oracle Cell.   
#
# NOTES
#
# MODIFIED  (MM/DD/YY)
#    loliu   03/04/11 - XbranchMerge loliu_exa_plugin from st_emtp_2.0.12.1
#    loliu   01/25/11 - Added workaround for bug 10261753
#    spalapu 12/10/09 - add memory utilization metrics
#    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 05/27/08 - changing rounding to show upto 2 decimals
#    spalapu 04/28/08 - move metric collection to metric_value bloc
#    spalapu 03/26/08 - Add NIC metrics
#    spalapu 03/19/08 - Add Copyright statements
#    spalapu 03/18/08 - 
#    spalapu 03/17/08 - 
#    spalapu 03/13/08 - change object_type to objectType
#    spalapu 02/13/08 - new file
#   spalapud 10/07/06 	Created the file.
# 

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

our %cells;  # Hash of cells

$ipaddress=doPing(@ARGV);

if($ipaddress eq "") {
  print "em_error=Failed to ping Cell\n";
  exit -1;
}
my $command = "ssh -l cellmonitor $ipaddress cellcli  -xml -e 'list metriccurrent where objectType=".'\"CELL\"'."'";
EMD_PERL_DEBUG("command = $command \n");

my $xmlout = `$command`;

my $str;
use Data::Dumper;
$str = Dumper($xmlout);
EMD_PERL_DEBUG($str);

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

my $myinf;
$parser->parse( $xmlout);

       foreach $cell (keys %cells)
       {
          print "em_result=".$cellname."|".$realmname."|".$cells{$cell}{CL_CPUT}."|".$cells{$cell}{CL_MEMUT}."|".$cells{$cell}{CL_FANS}."|".$cells{$cell}{CL_RUNQ}."|".$cells{$cell}{CL_TEMP}."|".$cells{$cell}{N_NIC_NW}."|".$cells{$cell}{N_NIC_RCV_SEC}."|".$cells{$cell}{N_NIC_TRANS_SEC}."|"."\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 "context")
    {
      if( %attrs )
      {
         $cellname = $attrs{cell};
         $realmname = $attrs{realm};
      }
    }
    elsif ($element eq "name")
    {
       $myinf="name";
    }
    elsif ($element eq "metricType")
    {
       $myinf="metric_type";
    }
    elsif ($element eq "metricValue")
    {
       $myinf="metric_value";
    }
    elsif ($element eq "collectionTime")
    {
       $myinf="collection_time";
    }
    elsif ($element eq "metricObjectName")
    {
       $myinf="object_name";
    }
    else
    {
       EMD_PERL_DEBUG("invisible element = $element \n");
    }
}

sub handle_end {
    my( $expat, $element, %attrs  ) = @_;
    if ($element eq "metric")
    {
      if ($realmname eq "") {
        $realmname="NONE";
      }
    }
    else
    {
       EMD_PERL_DEBUG("find not metric element: $element \n");
    }
}

sub characterData {
    my( $parseinst, $data ) = @_;
    $space=" ";
    $data =~ s/\n//g;
    if (($data !~ m/^\s/) and ($data ne "\""))
    {
       if ($myinf eq "name" )
       {
          $name=$name.$data;
       }
       elsif ($myinf eq "metric_type" )
       {
          $metric_type=$metric_type.$data;
       }
       elsif ($myinf eq "metric_value" )
       {
          $metric_value=$metric_value.$data;
          # This is a workaround for bug 10261753 which is a cell software bug
          # (bug 10186448). In this case, celldisk/flasdisk I/O load metrics
          # are reported as cell metrics. The metricObjectName is the 
          # celldisk/flashdisk name instead of cell name as expected.
          if (!($object_name eq "") && ($object_name eq $cellname))
          {
            $cells{$object_name}{name} = $object_name;
            $cells{$object_name}{$name} = sprintf("%.2f",$metric_value);
            $cells{$object_name}{$name."_time"} = $collection_time;
          }
          $name="";
          $metric_type="";
          $metric_value="";
          $collection_time="";
          $object_name="";
       }
       elsif ($myinf eq "collection_time" )
       {
         $collection_time=$collection_time.$data;
       }
       elsif ($myinf eq "object_name" )
       {
         $object_name=$object_name.$data;
       }
    }
}
