#!/usr/bin/perl

#
# Copyright (c) 2001, 2011, Oracle and/or its affiliates. All rights reserved. 
#
#  $Id:oracle_sage_config.pl 
#
#
# NAME  
#   oracle_cell_fs_metrics.pl
#
# DESC 
#
# This script gets all the File System metrics of the CELL.   
#
# NOTES
#
# MODIFIED  (MM/DD/YY)
#    spalapu 11/30/09 - filesystem metrics
#   spalapud 10/04/09 	Created the file.
# 

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

our %filesystems;  # Hash of filesystems

$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_FILESYSTEM\"'."'";
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;
my $read_throughput;
my $write_throughput;
my $read_reqs;
my $write_reqs;
$parser->parse( $xmlout);

       foreach $filesystem (keys %filesystems)
       {
#Since the latency times are in microsecs it is required to convert it into millisecs.
          print "em_result=".$filesystems{$filesystem}{name}."|".$cellname."|".$realmname."|".$filesystems{$filesystem}{util}."|"."\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};
         $osstimestamp = $attrs{ossStartTimestamp};
      }
    }
    elsif ($element eq "cli-output")
    {
       $myinf="cli-output";
    }
    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" )
{
  if ($metric_type eq "Instantaneous")
  {
    $metric_value=$metric_value.$data;
    $filesystems{$object_name}{name} = $object_name;
    $filesystems{$object_name}{util} = $metric_value;
    $filesystems{$object_name}{collection_time} = $collection_time;
    $name="";
    $metric_type="";
    $metric_value="";
    $collection_time="";
    $object_name="";
  }
}
elsif ($myinf eq "collection_time" )
{
  #convert to secs from millsecs
  $data= $data; 
  $collection_time=$collection_time.$data;
}
elsif ($myinf eq "object_name" )
{
  $object_name=$object_name.$data;

}
}
}
