makecricketconfig.pl
来自「Network Administration Visualized 网络管理可」· PL 代码 · 共 1,641 行 · 第 1/4 页
PL
1,641 行
my $datasource; my $dstype; print "Parsing file $dir/$file\n" if $ll >= 3; open (HANDLE, "$dir/$file") or die ("Could not open $dir/$file, exiting: $!\n"); while (<HANDLE>) { next if /^\s*\#/; # This is the targettype part # --------------------------- # we first look for a targettype if (m/^\s*targettype\s*(\S+)/i) { print "Found targettype >$1<.\n" if $ll >= 3; $tt = $1; } # we assume that ds will come after a targettype # as we control the config-files this should not be a problem if (m/^\s*ds\s*=\s*\"(.+)\"/i) { print "Found ds's: $1.\n" if $ll >= 3; my @tmp = split (",", $1); foreach my $ds (@tmp) { $ds =~ s/^\s*(.*)?\s*$/$1/; } my @dsarr = map $roidhash{$_}, @tmp; print "Pushing @dsarr on $tt\n" if $ll >= 3; $targetoidhash{$tt} = [@dsarr]; $rtargetoidhash{$tt} = [@tmp]; @dsarr = (); } } close HANDLE; print "\n=> Done running $me <=\n" if $ll >= 2; return $file;}################################################### createTargetTypes# --------------------# fetches all the netboxes we are to make config# for and makes a targetType for every type based# on the data we find in the netboxsnmpoid-table.## Help functions: &parseDefaults, &compare, &makeTTs## INPUT: Directory of work (scalar)##################################################sub createTargetTypes { our $gCT; my $dir = shift; my $type = $config{$dir}{'type'}; my $me = "createTargetTypes"; print "\n=> Running $me with dir=$dir<=\n" if $ll >= 2; my %newtts; # fetching the existing targettypes my $filename = &parseDefaults($dir); # We know that type may be several catid's my @types = split (",", $config{$dir}{'type'}); foreach my $type (@types) { $type =~ s/^\s*(\w+)?\s*$/$1/; $type = "catid='$type'"; } # Todo for new system - try 1 # - find all netboxes of give type (category) # - foreach netbox, find all snmpoids (oidkey) # - check if it is printf "Creating targetTypes for %s, based on %s .\n", $dir, join (",", @types) if $ll >= 2; my $query = "SELECT netboxid,sysname FROM netbox WHERE (" . join ( " OR ", @types ) . ")"; print "$query\n" if $ll >= 3; my $res = $dbh->exec($query); # For all the types, make a targetType # Use only the oids that are not interface-specific while (my($netboxid, $sysname)=$res->fetchrow) { print "\nFound netbox $sysname.\n" if $ll >= 2; print "---------------------\n" if $ll >= 2; # Fetch the oids for this netbox my $q = "SELECT snmpoidid FROM netboxsnmpoid WHERE netboxid=$netboxid"; printf "%s\n", $q if $ll >= 3; my $fetchoids = $dbh->exec($q); # fetches all the oid's that exists in this part of the config-tree my $purepath = "/".$dir; my $oidinconfig = $gCT->configHash($purepath,'oid'); # for each oid, check if it should be used in a targettype my @newtt; while (my $snmpoidid = $fetchoids->fetchrow) { print "Found snmpoidid $snmpoidid " if $ll >= 3; unless ($oidhash{$snmpoidid}) { print "- skipping because not in oidhash.\n" if $ll >= 3; next; } elsif ($oidhash{$snmpoidid} =~ m/^if/) { # here we do a weak test for interface-oids print "- skipping because it is an interface oid.\n" if $ll >= 3; next; } else { print "\n" if $ll >= 3; } # if the oid is not in the config-file we cannot collect data from it if ($oidinconfig->{lc($oidhash{$snmpoidid})}) { printf "%s should be integrated as a datasource.\n", $oidhash{$snmpoidid} if $ll >= 2; push @newtt, $snmpoidid; } else { printf "Could not find %s in the config-tree, skipping it.\n", $oidhash{$snmpoidid} if $ll >= 3; } } next if $#newtt < 0; # checking is this targettype already exists in the config-file # We temporarily disable this and make new targettypes every time# if ($targetoidhash{$sysname}) {# print "This targettype already exists, checking if it's equal.\n" if $ll >= 3;# if (&compare($targetoidhash{$sysname}, [ @newtt ] )) {# print "They are equal.\n" if $ll >= 3;# } else {# print "The new targettype does not match with the old, making new.\n" if $ll >= 3;# $newtts{$sysname} = [@newtt];# $targetoidhash{$sysname} = [@newtt];# }# } else { print "This targettype does not exist, making new.\n" if $ll >= 3; $newtts{$sysname} = [@newtt]; $targetoidhash{$sysname} = [@newtt];# } @newtt = (); } if (&makeTTs($filename, $dir, %newtts)) { print "targettypes made successfully.\n" if $ll >= 2; } else { print "There was an error when making the targettypes.\n" if $ll >= 2; } print "\n=> Done running $me <=\n" if $ll >= 2; return 1; }################################################### makeTTs# --------------------# INPUT: filename, directory of work and a hash# of the new targettypes to add.# RETURNS: 0 on error, else 1.##################################################sub makeTTs { my ($filename, $dir, %input) = @_; my $path = "$dir/$filename"; my $me = "makeTTs"; print "\n=> Running $me with filename=$filename, dir=$dir <=\n" if $ll >= 2; print "Editing file $path.\n" if $ll >= 3; unless (-w $path) { print "The file is not writeable, returning.\n" if $ll >= 3; print "\n=> Done running $me<=\n" if $ll >= 2; return 0; } # We read the entire defaults file into memory, then rename it for backup. open (HANDLE, $path) or die ("Could not open $path for reading: $!\n"); my @lines = <HANDLE>; close HANDLE; unless (rename ($path, "$path~")) { print "Could not rename file: $!\n" if $ll >= 2; } my $delete = 0; # a bool my $write = 0; # a bool my $tt; # Walks through the file, deleting the old tt's that we don't want, and # creating new ones after the special "mark". It only deletes lines # starting with "targettype" and thereafter lines starting with "ds" and # "view". open (HANDLE, ">$path") or die ("Could not open $path for writing: $!\n "); foreach my $line (@lines) { if ($write) { # Printing the new targettypes my @keys = keys %input; my $numberofkeys = @keys; if ($numberofkeys > 0) { for my $tt (@keys) { print "Adding targettype $tt to file.\n" if $ll >= 3; printf CHANGELOG "Adding targettype %s to %s.\n", $tt, $path; print HANDLE "targetType $tt\n"; print HANDLE "\tds\t= \"", join (",", sort( map ( $oidhash{$_}, @{ $input{$tt} } ) ) ), "\"\n"; print HANDLE &makeView( @{ $input{$tt} } ); print HANDLE "\n"; } } else { print "No new targettypes added.\n" if $ll >= 3; } $write = 0; print HANDLE $line; } elsif ($line =~ m/^\s*targettype\s*(.*)/i) { print "Deleting targettype $1\n" if $ll >= 3; printf CHANGELOG "Deleting targettype %s from %s\n", $1, $path; $delete = 1; } elsif ($delete && $line =~ m/^\s*ds/) { # delete print "Deleting line: $line" if $ll >= 3; } elsif ($delete && $line =~ m/^\s*view/) { # delete this line and be happy for now $delete = 0; print "Deleting line: $line" if $ll >= 3; } elsif ($line =~ m/\#!\#!\#!/) { $write = 1; print "Found special mark - setting the write-bit.\n" if $ll >= 3; print HANDLE $line; } else { print HANDLE $line; } } close HANDLE; print "\n=> Done running $me<=\n" if $ll >= 2; return 1;}################################################### makeTargets# --------------------# Makes the target-files in the specified directory.# INPUT: The dir to work in# RETURNS: nada##################################################sub makeTargets { my $dir = shift; my $file = "targets"; my $me = "makeTargets"; print "\n=> Running $me with dir=$dir <=\n" if $ll >= 2; print "$config{$dir}{'type'}\n" if $ll >= 3; # We know that type may be several catid's my @types = split (",", $config{$dir}{'type'}); foreach my $type (@types) { $type =~ s/^\s*(\w+)?\s*$/$1/; $type = "catid='$type'"; } my $query = "SELECT netboxid,ip,sysname,ro, type.descr as typedescr , room.descr as roomdescr FROM netbox LEFT JOIN type USING (typeid) LEFT JOIN room USING (roomid) WHERE (" . join ( " OR ", @types ) . ") AND up='y' ORDER BY sysname"; print "$query\n" if $ll >= 3; my $res = $dbh->exec($query); my $filetext; my %changes = (); my @changes = (); while (my ($netboxid,$ip,$sysname,$ro,$typedescr,$roomdescr) = $res->fetchrow) { # If we failed to make a targettype for this one, skip it. unless ($targetoidhash{$sysname}) { print "Could not find a targettype for $sysname, skipping.\n" if $ll >= 2; next; } # format: # target $sysname # snmp-host = $ip # snmp-community = $ro # target-type = $sysname # short-desc = # We let Cricket do the sorting atm # Making description - perhaps we should be more flexible here? my $descr; if ($roomdescr) { $descr = join (", ", $typedescr,$roomdescr); } else { $descr = $typedescr; } # Make sure " in for instance url's don't mess up the config $descr =~ s/\"/\\\"/g; $descr = "\"$descr\""; # Storing info that we need later when we are going to # fill the rrd-db. $rrdhash{"$cricketconfigdir/$dir"}{$sysname}{'netboxid'} = $netboxid; $rrdhash{"$cricketconfigdir/$dir"}{$sysname}{'ds'} = $targetoidhash{$sysname}; push @changes, $sysname; $filetext .= "target \"$sysname\"\n"; $filetext .= "\tsnmp-host\t=\t$ip\n"; $filetext .= "\tsnmp-community\t=\t$ro\n"; $filetext .= "\ttarget-type\t=\t$sysname\n"; $filetext .= "\tshort-desc\t=\t$descr\n\n"; print "Adding target \"$sysname\"\n" if $ll >= 2; } open (TARGETS, ">$dir/$file") or die ("Could not open $dir/$file for writing: $!\n"); print TARGETS $filetext; close TARGETS; # Printing changes $changes{"$cricketconfigdir/$dir"} = [@changes]; &checkChanges(%changes); print "\n=> Done running $me <=\n" if $ll >= 2; return 1;}################################################### makeinterfaceTargets# --------------------# Makes targets for interfaces. These are standard# therefore we treat them for themselves. There# is no need to edit the defaults-file for these# either.# INPUT: the dir to work in# RETURNS: nada, just makes a file##################################################sub makeinterfaceTargets { my $dir = shift; my $file = "targets"; my $me = "makeinterfaceTargets"; my %changes = (); print "\n=> Running $me with dir=$dir <=\n" if $ll >= 2; my @types = split ",",$config{$dir}{'type'}; my @nameparameters = split (",", $config{$dir}{'name'}); my $descrsentence = $config{$dir}{'descr'}; my $joinparam = $config{$dir}{'join'} || "-"; my $table = $config{$dir}{'table'}; my $giga = $config{$dir}{'giga'}; # Stripping whitespace foreach my $a (@nameparameters) { $a =~ s/^\s*(\w+)?\s*$/$1/; } foreach my $type (@types) { $type =~ s/^\s*(\w+)?\s*$/$1/; $type = "catid='$type'"; } # first we kill all the prior config here. # There should not be anything besides a defaults-file and the .nav-file here. printf "Deleting all the directories in %s\n", $cricketconfigdir."/".$dir if $ll >= 3; `rm -rf $cricketconfigdir/$dir/*/`; my $query = "SELECT netboxid,ip,sysname,ro,vendorid,snmp_version FROM netbox LEFT JOIN type USING (typeid) WHERE (". join ( " OR " , @types ) . ") AND up='y' AND ro IS NOT NULL ORDER BY sysname"; my $res = $dbh->exec($query); # For each unit, check if it has any interfaces to gather data from, make # a subdir for it and place the targets there. while (my($netboxid,$ip,$sysname,$ro,$vendor,$snmpversion) = $res->fetchrow) { my %ifindexhash = (); # to make sure we don't create a target for the same if twice my @changes = (); my $filetext = ""; my $path = "$dir/$sysname"; my $targetfile = "$path/$file"; my $q; $q = "SELECT ".$table."id,ifindex,interface,". join (",",@nameparameters) . " FROM $table LEFT JOIN module USING (moduleid) WHERE netboxid=$netboxid"; # Check if 64-bits counters are supported on this netbox. my $support64bits = 0; my $snmpq = "SELECT oidkey FROM netboxsnmpoid LEFT JOIN snmpoid USING (snmpoidid) WHERE netboxid=$netboxid AND oidkey ~* 'ifHC'"; my $snmpqres = $dbh->exec($snmpq); if ($snmpqres->ntuples > 0) { $support64bits = 1; } foreach my $parameter (@nameparameters) { $q .= " AND $parameter IS NOT NULL"; } $q .= " ORDER BY ".join (",",@nameparameters);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?