Concord/autoDiscover

From S23Wiki
Jump to: navigation, search

autoDiscover

this is a ruff Perl script for "auto discovering" Cisco routers in a ATM network. it has basic support for one wan port and a number of ATM PVS for which it sets groop, main name, alias name, and speed.

i need to clean this up a bit i have an extra cli varible thats not needed (or should be but =)


NOTE: you will have to be the network health user to use this script, and you will ahve to have "run" the relavent nethealth rc file ie `/$NH_HOME/nethealthrc.ksh`

#!/usr/local/bin/perl
#

use strict;
use warnings;

############################################################
# main discover 
# Parameters : 
#		autoDiscover.pl discover.csv "Service"
#		csv file must be in the correct format ie:- (with as many PVC's as needed)
#	IPAddress,String,SiteName,Group,WanAliasName,WanSpeed:NumberOfPVCs:DLCI1,Alias1,Name1,Speed1;DLCI2,Alias2,Name2,Speed2;
#
# ver0.1.2 04/03/2004:	Removed ( -nmsSource \"NH:Discover\") from the nhAutoDiscover command
############################################################


my ($dataFileName)=$ARGV[0] || die "Missing Args Should be: autoDiscover.pl discover.csv \"Service\"  $!";  	# get file name of discover file
my ($SdtArfName)=$ARGV[1] || die "Missing Args Should be: autoDiscover.pl discover.csv \"Service\"  $!";	# get  std rule name


open DFILE, "$dataFileName"  # open datafile and read in  or die
	or die "no file specified command format is \"autoDiscover FILE-NAME Server \" $!";
my (@dataFile) = <DFILE>;
close DFILE;

foreach my $site (@dataFile) {  # loop  for each line/site in dataFile
	chomp $site ;
	my ($siteIP,$siteString)=makeArf("$site","$SdtArfName"); # make ARF file then return IP and Cominity String
	
	print " Discovering $siteIP with $siteString \n ";
	
	`nhAutoDiscover -l \"$siteIP\" -c \"$siteString\" -mode \"lanwan,router\" -dciRule \"tmp/$siteIP.arf\" > tmp\/$siteIP.Results.temp`; # this is the command used to do the discover
	checkResults("$siteIP");	# run the results sub
	};


sub checkResults {
############################################################
# Function Check Discover Results
# Parameters :
#		Ip Address of site to be found : x.x.x.x
#		Poller Cfg file : this is the data file 
#			should be in the $NH_HOME/poller dir
# Returns :
#		
# Ver0.1.2 04/03/2004:	clear out the $segmentFields vars at start of loop ( to stop KeyValue passing on to next segment )
#			put Speach marks around $siteIPAdd (next unless /\"$siteIPAdd\"/;) as it was finging extra segments ie not just .5 but .51
############################################################
my $NH_HOME= "/nethealth1/poller/";  # point to the poller CFG file NOTE replace this with ENV[NH_HOME]  
my($segmentFieldKey, $segmentFieldValue, %segmentFields, $nullVar);
my($siteIPAdd)=$_[0];  # read in  IP  from parent NOTE make a check for this

local $/ = "}\n"; 			# set delimiter
open(DISCOVERLOG, ">>tmp/discover.log.temp") || die "can not open : $!";
open(POLLER, "$NH_HOME/poller.cfg") || die "can not open : $!";

while(<POLLER>) {
($segmentFields{segment}, $segmentFields{deviceSpeed2}, $segmentFields{sysName}, $segmentFields{aliasName}, $segmentFields{mibTranslationFile})=("","","","","");
  next unless /\s+segment/;
  next unless /\"$siteIPAdd\"/;
  s/\n\s+\}\n//g;
  s/["{]//g;
 	foreach (split(/\n/)) {
		($nullVar,$segmentFieldKey,$segmentFieldValue)=split(/\s+/,$_,3);
		
		$segmentFields { $segmentFieldKey } = $segmentFieldValue ; 
	}
print DISCOVERLOG "$segmentFields{segment}, $segmentFields{deviceSpeed2}, $segmentFields{sysName}, $segmentFields{aliasName}, $segmentFields{mibTranslationFile}\n" ; 
}
close POLLER;
close DISCOVERLOG;
}

	

sub makeArf {
# Start of sub that makes arf files
############################################################
# this function creates a arf rule file from input Data
# Version 0.1 6/11/03 original
# 0.2 21/11/03 tryed to convert to sub
# 0.3 42/11/03 finished converting to sub
# 0.4 12/03 worked on results sub
# 0.5 20/1/2004 tweeked rule order 
############################################################

my ($csvData)="$_[0]";
my ($siteLink,$siteNoOfPVCs,$siteAllPVCs)=split(/:/,$csvData,3); #split up main / pvc info
my ($siteIP,$siteString,$siteName,$siteGroup,$siteCCTReff,$siteACRate)=split(/,/,$siteLink,6); #split up main info
my @sitePVCs = split(/;/,$siteAllPVCs,$siteNoOfPVCs);

my $siteARFFILE = "$siteIP.arf";
open ARFFILE, ">tmp/$siteARFFILE" 
	or die "can not open '$siteARFFILE': $!";
	
# print header
print ARFFILE <<EOF;
######################################################################
# Discover Rule auto genarated by autoDiscover.pl
# IP:  $siteIP 
# SiteName: $siteName 
# Companny: $siteGroup 
# Energis CCT Reff: $siteCCTReff
###################################################################### \n
EOF

# open standard file and print 
my ($standardFile)="standard.$_[1].txt";
open IN, "$standardFile" # open file with standard bits
	or die "can not open standard arf rules file: $! ";
my @standardRules = <IN>;
close IN;
print ARFFILE "@standardRules\n"; #print standard bits

# print -BRI rule
print ARFFILE <<EOF;
name matches  ".*-BRI.*": {
    exclude () ;
}\n
EOF

# print -BVI rule
print ARFFILE <<EOF;
name matches  ".*-BVI.*": {
    exclude () ;
}\n
EOF

# print -Dialer rule
print ARFFILE <<EOF;
name matches  ".*-Dialer.*": {
    exclude () ;
}\n
EOF

# print -PPP rule
print ARFFILE <<EOF;
ifType matches "ppp": {
    exclude () ;
}\n
EOF

# print -Cpu- rule
print ARFFILE <<EOF;
name matches  ".*-Cpu-.*": {
	setName ("$siteName-cpu");
	setGroup ("$siteGroup");
    setAlias ("\$1-\$2") ;
}\n
EOF

# print Ethernet rule
print ARFFILE <<EOF;
name matches  ".*Ethernet.*":
{
    setName ("$siteName-lan\$2") ;
    setGroup ("$siteGroup") ;
    setAlias ("\$1Lan\$2") ;
}\n
EOF

foreach my $PVC (@sitePVCs){ # loop for each PVC
		
	my ($PVCdlci,$PCVreff,$PVCname,$PVCcir)=split(/,/,$PVC,4); # split out pvc info

# print PVC rules
print ARFFILE <<EOF;
name matches  ".*-Serial.*-dlci-$PVCdlci": {
	setName ("$siteName-$PVCname") ;
	setGroup ("$siteGroup") ;
	setAlias ("\$1-S\$2-$PCVreff") ;
	setSpeedIn ("$PVCcir") ;
	setSpeedOut ("$PVCcir") ;
	setDeviceSpeedIn ("$PVCcir") ;
	setDeviceSpeedOut ("$PVCcir") ;
}\n
EOF

}

# print dlci drop out rule
print ARFFILE <<EOF;
name matches  ".*-dlci-.*":
{
    exclude () ;
}\n
EOF

# print -Serial*:* rule
print ARFFILE <<EOF;
name matches  ".*-Serial.*:.*":{
	setName ("$siteName-wan\$2:\$3") ;
	setGroup ("$siteGroup") ;
	setAlias ("\$1-s\$2:\$3-$siteCCTReff") ;
	setSpeedIn ("$siteACRate") ;
	setSpeedOut ("$siteACRate") ;
	setDeviceSpeedIn ("$siteACRate") ;
	setDeviceSpeedOut ("$siteACRate") ;
}\n
EOF

# print -Serial rule
print ARFFILE <<EOF;
name matches  ".*-Serial.*":{
	setName ("$siteName-wan\$2") ;
	setGroup ("$siteGroup") ;
	setAlias ("\$1-S\$2-$siteCCTReff") ;
	setSpeedIn ("$siteACRate") ;
	setSpeedOut ("$siteACRate") ;
	setDeviceSpeedIn ("$siteACRate") ;
	setDeviceSpeedOut ("$siteACRate") ;
}\n
EOF

# print -RH rule
print ARFFILE <<EOF;
name matches  ".*-RH\$": {
	setName ("$siteName-rh") ;
	setGroup ("$siteGroup") ;
    	setAlias ("\$1-rh") ;
}\n
EOF

# print drop out rule
print ARFFILE <<EOF;
name matches  ".*":
{
    exclude () ;
}\n
EOF

# print footer rule
print ARFFILE <<EOF;
######################################################################
# End of Discover Rule 
######################################################################\n
EOF

close(ARFFILE) 
	or die("can not close '$siteARFFILE': $!");
	
return ($siteIP,$siteString);      # return the IP and comunity string to main ruteen
};  #end of makeArf Sub


Back to Concord Index