Hello knowledgeable perl type people
im still very new to perl and, iv got my script "working", what
is does in searches through a large data file finds the segments i
need and outputs
the data in a more use-full format. As i said id does work but im
no to convinced the structure is very clean
so i thort maybe some one could give me some pointers.
yours
most gratefully RichT
scanPoller.cfg.pl==================================================
#!/usr/local/bin/perl
use strict;
use warnings;
use Getopt::Long;
my($inFile,$USAGE,$showKey,$site,$found,$foundKeys,@dataFile,@foundSegments,$value);
my($opt_inFile, $opt_showAll, $opt_help ) = (0,0,0);
my $feildName = "agentAddress";
my $showFields = "segment,agentAddress,community";
$USAGE = <<USAGE_TEXT;
usage: $0 [-inFile input filename] [-findFeild feildName (default is
agentAddress)]
[-showFields feilds to output (default is
segment,agentAddress,community)] [-showAll show all fields]
[-help this help text]
USAGE_TEXT
GetOptions ( "inFile=s",
"findFeild=s" => \$feildName,
"showFeilds=s" => \$showFields,
"showAll",
"help|h|H|?|HELP"
);
if ($opt_help == 1) {print $USAGE; exit; } # print out help and exit
# start finding results
if ($inFile){ # find results if we have in -inFile
open DFILE, "$opt_inFile" # open $inFile and read in or die
or die " could not open $opt_inFile";
@dataFile = <DFILE>;
close DFILE;
foreach $value (@dataFile) { # loop for each line/site in dataFile
chomp $value ;
@foundSegments=findVars($feildName,$value);
}
} elsif ($ARGV[0]) { # read in value from comandline
foreach $value ($ARGV[0]) { # loop for each line/site in dataFile
@foundSegments=findVars($feildName,$value);
}
} else {print $USAGE; exit; } #quit if no values are supplyed...
# start show results
if ($opt_showAll) { # if show all option then print out every thing
for $found ( @foundSegments ) {
print "\n" ;
for $foundKeys (keys %$found) {
print "$foundKeys,$found->{$foundKeys}\n";
}
}
} else { # show chosen fields
for $found ( @foundSegments ) {
foreach $showKey (split /,/, $showFields) {
print "$found->{$showKey},";
}
print "\n";
}
}
sub findVars {
############################################################
# Function Check Discover Results
#
############################################################
my($findKey, $findValue, $segmentFieldKey, $segmentFieldValue,
%segmentFields, $nullVar, @foundSegments);
# read in Search Key and Value from parent NOTE make a check for this
$findKey=$_[0] || die "Missing Args $findKey $!" ;
$findValue=$_[1] || die "Missing Args $findValue $!" ;
chomp $findValue;
chomp $findKey;
#my $NH_HOME= $ENV[NH_HOME]; # point to the poller CFG file
my $NH_HOME= "."; # point to the poller CFG file NOTE this is temp
for testing use above line in live
local $/ = "}\n"; # set delimiter
open(POLLER, "$NH_HOME/poller.cfg") || die "can not open : $!";
while(<POLLER>) {
next unless /^\s+segment/;
s/\n\s+\}\n//g;
s/["{]//g;
foreach (split(/\n/)) {
($nullVar,$segmentFieldKey,$segmentFieldValue) = split(/\s+/,$_,3);
$segmentFields{ $segmentFieldKey } = $segmentFieldValue ;
}
if ( $segmentFields{$findKey} eq $findValue ) {
push @foundSegments, {%segmentFields } ;
}
undef %segmentFields;
my %segmentFields;
}
close POLLER;
return (@foundSegments); # return the data
};
/scanPoller.cfg.pl==================================================
exampleExtract form poller.cfg==================================
segment "customer-site-Bend" {
agentAddress "x.x.x.x"
uniqueDeviceId "dfofdkskhjkldsf"
mibTranslationFile "cisco-frameRelay-cir.mtf"
index "2"
index2 "400"
deviceSpeed "64000.0"
deviceSpeed2 "64000.0"
discoverMtf "cisco-frameRelay-cir.mtf"
index3 "7"
community "public"
sysDescr "Cisco Internetwork Operating System Software
IOS (tm) C1700 Software (C1700-Y-M), Version 12.1 blar blar..."
sysName "routerName"
sysLoc "siteLocation"
ifDescr "Serial0"
ifType "frame-relay"
aliasName "PVCreffNumber"
nmsKey "routerName Serial0 400"
enterpriseId "9"
possibleLatencySources "concord, ciscoPing"
fullDuplex "1"
mediaSpeed "64000.0"
mediaSpeed1 "64000.0"
statistics "1"
}
exampleExtract form poller.cfg==================================
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>