#!/usr/bin/perl
#----------------------------------------------------------------------------------------------
#
# Copyright (c) 2005 Ericsson Australia Pty Ltd
#
# This program may be used and/or copied only 
# with the prior written permission of Ericsson 
# Australia or in accordance with the terms and 
# conditions stipulated in the contract or agreement 
# under which the program has been supplied.
#
#----------------------------------------------------------------------------------------------
#
# pmList: List counters that exist in a PM File
#
# usage: pmList -m [<mo_filter>] -c [<counter_filter>] [<file1> [<file2...] ]
#
# 	<mo_filter> = Regular expression to select which managed objects 
#                     for which counters will be listed
#	<counter_filter> = Regular expression to select which counters 
#                          will be listed
#	<file1>... = rop files to read. If none specified then std input will 
#                    be read.
#
# M. Harris (eharmic), michael.harris@ericsson.com
# GSDC Australia
#-----------------------------------------------------------------------------
# Copyright (c) 2005 Ericsson Australia Pty Ltd
#
# This program may be used and/or copied only 
# with the prior written permission of Ericsson 
# Australia or in accordance with the terms and 
# conditions stipulated in the contract or agreement 
# under which the program has been supplied.
#-----------------------------------------------------------------------------
# Revision History
# v 1.0 24/06/2005 eharmic    Initial Version
#-------------------------------------------------------------------------------

use strict;
use Getopt::Long;
use Time::Local;

my $version = "v1.0, 2005-06-24";

sub printUsage {

	if (@_) {print STDERR "@_\n";};
	die <<_EOTXT_;
pmList : List counters available in an XML File

$version

usage: pmList -m [<mo_filter>] -c [<counter_filter>] [<file1> [<file2...] ]

 	<mo_filter> = Regular expression to select which managed objects 
                      for which counters will be listed
	<counter_filter> = Regular expression to select which counters 
                           will be listed
	<file1>... = rop files to read. If none specified then std input will 
                     be read.
_EOTXT_

}
	
my ($help, $mo_filter, $counter_filter);

GetOptions(
	"m=s"		=> \$mo_filter,
	"c=s"		=> \$counter_filter,
	"help"         => \$help
) or printUsage("Invalid Options");

printUsage if ($help);

my $counter_re=qr/$counter_filter/i if $counter_filter;
my $mo_re = qr/(.*$mo_filter.*)/i if $mo_filter;
my @counters;
my $mo_class;
my @fdn_components;
my $ctr;

while (<>) {
	
	if ((($ctr)=(/<mt>(\w+)<\/mt>/))) {
		next if ($counter_filter and $ctr !~ $counter_re);
		push @counters,$ctr;
		next;
	}
	
	if (@counters and /<moid>(.+)<\/moid>/) {
		next if ($mo_filter and $1 !~ $mo_re);
		@fdn_components=(split ",",$1);
		$mo_class = (split "=", (@fdn_components)[$#fdn_components])[0];
		print "MO Class: $mo_class\n";
		print "     $_\n" foreach (@counters);
		@counters=();
		next;
	}
	
	if (/<\/md>/) {
		@counters=();
	}
	
}
