📄 makecricketconfig.pl
字号:
my $r = $dbh->exec($q); print "$q\n" if $ll >= 3; next if $r->ntuples == 0; # make a subdirectory for each sysname unless (-e $path) { print "Making dir $path\n" if $ll >= 3; mkdir ($path) or warn ("Could not make directory $path: $!"); } # create default target $filetext .= "target --default--\n"; $filetext .= "\tsnmp-host\t=\t$ip\n"; if ($snmpversion == 2 && $support64bits) { print "$sysname supports 64-bits counters\n" if $ll >= 3; $filetext .= "\tsnmp-version\t=\t2c\n"; $filetext .= "\ttarget-type\t=\tsnmpv2-interface\n"; } else { print "$sysname does not support 64-bits counters\n" if $ll >= 3; } $filetext .= "\tsnmp-community\t=\t$ro\n\n"; my $numberofports = $r->ntuples; my $numtargets = $numberofports+1; # While there are more interfaces left, fetch them, make a target out of it. while (my @params = $r->fetchrow) { my $id = $params[0]; my $ifindex = $params[1]; my $interface = $params[2]; if ($vendor eq 'hp') { $ifindex =~ s/^\d0?(.*)/$1/; } # Some interfaces exists more than once in the database, lets skip them next if $ifindexhash{$ifindex}; $ifindexhash{$ifindex}++; my $name = ""; my $descr = ""; my $order = $numberofports--; # In the config-file we specify how to make the target-name. my @tmp; foreach my $param (@nameparameters) { my $a = $r->fnumber($param); $params[$a] =~ s,/,_,g; push @tmp, $params[$a]; } $name = join $joinparam, @tmp; # In the config-file we also specify how to make the description. # This is basically a select-sentence, therefore we must filter on # select to avoid mischief. @tmp = (); my @descrarr = (); my $descrq = $descrsentence; if ($descrq =~ /^select/i) { $descrq =~ s/\;//; $descrq =~ s/\$id/$id/; print "\tQuerying for description: $descrq\n" if $ll >= 3; my $descrres = $dbh->exec($descrq); @tmp = $descrres->fetchrow; foreach my $m (@tmp) { if ($m) { push (@descrarr, $m)}; } } my $lengthoftmp = @descrarr; if ($lengthoftmp > 0) { $descr = join (", ", @descrarr); $descr = "\"$descr\""; } # Set name = ifindex if no name set and so on $name = $ifindex unless $name; $descr = "\"No description available\"" unless $descr; # create interface-targets # format: # target $name # interface-index = $ifindex # short-desc = $descr $rrdhash{"$cricketconfigdir/$dir/$sysname"}{$name}{'netboxid'} = $netboxid; $rrdhash{"$cricketconfigdir/$dir/$sysname"}{$name}{'interface'} = 1; $rrdhash{"$cricketconfigdir/$dir/$sysname"}{$name}{'id'} = $id; $rrdhash{"$cricketconfigdir/$dir/$sysname"}{$name}{'table'} = $table; $filetext .= "target \"$name\"\n"; if ($interface && $interface =~ m/(.*)\.\d+/) { $filetext .= "\ttarget-type\t=\tsub-interface\n"; } $filetext .= "\torder\t=\t$order\n"; $filetext .= "\tinterface-index\t=\t$ifindex\n"; $filetext .= "\tshort-desc\t=\t$descr\n\n"; print "Adding target $name to $targetfile\n" if $ll >= 2; push @changes, $name; } my @targets = @changes; @targets = map lc($_), @targets; # Adding the all-target $filetext .= "target all\n"; $filetext .= "\torder\t=\t$numtargets\n"; $filetext .= "\ttargets\t=\t\"".join(";",@targets)."\"\n\n"; $changes{"$cricketconfigdir/$dir/$sysname"} = [@changes]; # Write to file. open (HANDLE, ">$targetfile") or die ("Could not open $targetfile: $!"); print HANDLE $filetext; close HANDLE; } &checkChanges(%changes); print "\n=> Done running $me <=\n" if $ll >= 2; return 1;}################################################### compare# --------------------# Compares two arrays to see if they are equal.# Written only for my needs, I AM aware that such# things are made (better), but I don't want to # install more mods than necessary.## INPUT: ref to two arrays# RETURNS: true or false##################################################sub compare { my ($ref1, $ref2) = @_; my @a = sort @{ $ref1 }; my @b = sort @{ $ref2 }; my $asize = @a; my $bsize = @b; if ($asize == $bsize) { print "Same size.\n" if $ll >= 3; } else { print "Arrays are not equal (%s != %s).\n", $asize, $bsize if $ll >= 3; return 0; } for my $i (0 .. $#a) { printf "Comparing %s - %s => ", $a[$i], $b[$i] if $ll >= 3; if ($a[$i] eq $b[$i]) { print "equal.\n" if $ll >= 3; } else { print "not equal.\n" if $ll >= 3; return 0; } } return 1;}################################################### checkChanges# --------------------# Get as input a hash of path and targets from# makeTargets. Compares that to what we have in# the database and prints out the changes.##################################################sub checkChanges { my $me = "checkChanges"; print "=> Running $me <=\n" if $ll >= 2; print "--- CHANGELOG ---\n" if $ll >= 2; my %changehash = @_; foreach my $dir (keys (%changehash)) { print "\t$dir\n" if $ll >= 2; my @targets = @{ $changehash{$dir} }; $dir =~ s/cricket-config/cricket-data/; my $q = "SELECT filename FROM rrd_file WHERE path = '$dir' ORDER BY filename"; my $r = $dbh->exec($q); print "\t$q\n" if $ll >= 3; my %dbtargets = (); while (my ($filename) = $r -> fetchrow) { $dbtargets{$filename}++; } my @changearr = (); foreach my $target (@targets) { $target = lc ($target.".rrd"); if ($dbtargets{$target}) { print "\t$target exists in the db, not adding to changelog.\n" if $ll >= 3; } else { print "\tAdding $target to changelog.\n" if $ll >= 3; push @changearr, $target; } delete $dbtargets{$target}; } # Printing those who were added. my $numadded = @changearr; if ($numadded > 0) { printf CHANGELOG "Added %s new targets to %s/targets:\n", $numadded, $dir; foreach my $target (@changearr) { print "\t$target\n" if $ll >= 2; print CHANGELOG "\t$target\n"; } } # Printing those who were in the database but not in the new config. my @inactive = keys %dbtargets; my $numinactive = @inactive; if ($numinactive > 0) { printf CHANGELOG "%s inactive in %s:\n", $numinactive, $dir; foreach my $key (@inactive) { print CHANGELOG "\t$key\n"; } } } print "=> Done running $me <=\n" if $ll>= 2; return 1;}################################################### fillRRDdatabase# --------------------# We have a global hash that functions as a mini-db# of what files we have made. This sub uses that to# fill the rrd-db##################################################sub fillRRDdatabase { our $gCT; # This is the hardcoded oids that we collect for interfaces. # These are VERY standard and should by no means be altered. # I have yet to experience that these do not exist on any units. # You may argue that if we want to collect more than these, it # is hard to change. That is true. We may read this info from a # text-file in the future. my @interfacearr = qw(ifInOctets ifOutOctets ifInErrors ifOutErrors ifInUcastPackets ifOutUcastPackets); my $interfaceds = "COUNTER"; print "---------- FILLING THE RRD-DATABASE ----------\n" if $ll >= 2; my $me = "fillRRDdatabase"; print "=> Running $me <=\n" if $ll >= 2; # We now have some global hashes - summary follows: # Remember the vars $step and $subsystem that are defined # at the top. Also remember to look at the description of the hashes in the # beginning of the script. # First of all we want to find the path of all the files # we have made configuration for: my @allpaths = keys(%rrdhash); # Then we go through each and every one of these and fill the database: # The cricket datadir, according to Cricket's config tree my $cricketdatadir = $gCT->configHash('/','target','filler','HASH')->{'datadir'}; foreach my $path (@allpaths) { # Create datapath my $tmppath = $path; $tmppath =~ s/$cricketconfigdir//; my $newpath = $cricketdatadir . "/" . $tmppath; $newpath =~ s/\/{2,}/\//g; # Remove redundant slashes print "--- Creating query for $path ---\n" if $ll >= 3; # Next step in trying to solve this thing is to select a lot from the # db at once. Here we make a hash with all the neccesary information # in. my %bighash = (); my $getallinfo = "SELECT rrd_fileid, filename, netboxid, key, value FROM rrd_file WHERE path = '$newpath'"; my $dogetallinfo = $dbh->exec($getallinfo); while (my ($id, $fname, $netbox, $k, $v) = $dogetallinfo->fetchrow) { $bighash{$id}{'filename'} = $fname; $bighash{$id}{'netboxid'} = $netbox; $bighash{$id}{'key'} = $k; $bighash{$id}{'value'} = $v; $bighash{$fname}{1} = $id; } # For all rrd-files we have in this path, add them to the db. my @allfiles = keys ( %{ $rrdhash{$path} }); foreach my $filename (@allfiles) { my $exists = 0; my $newfilename = lc($filename.".rrd"); print "\tFound rrd-file $filename\n" if $ll >= 3; my $netboxid = $rrdhash{$path}{$filename}{'netboxid'}; # Check if it exists from before: my $rrd_fileid = ""; if ($bighash{$newfilename}{1}) { printf "%s/%s does already exist.\n", $path, $filename if $ll >= 3; $exists = 1; $rrd_fileid = $bighash{$newfilename}{1}; } # Based on whether this file exists or not, we do a update or # insert. To save time we just update the records where the # key/value pairs are wrong. my $rrdfileq = ""; if ($rrdhash{$path}{$filename}{'interface'}) { my $key = $rrdhash{$path}{$filename}{'table'}; my $value = $rrdhash{$path}{$filename}{'id'}; if ($exists) { if ($key ne $bighash{$rrd_fileid}{'key'} or $value != $bighash{$rrd_fileid}{'value'} or $netboxid != $bighash{$rrd_fileid}{'netboxid'}) { $rrdfileq = "UPDATE rrd_file SET step=$step, netboxid=$netboxid, subsystem='$subsystem', key='$key', value=$value WHERE rrd_fileid=$rrd_fileid"; } } else { $rrdfileq = "INSERT INTO rrd_file (path,filename,step,netboxid,subsystem,key,value) VALUES ('$newpath','$newfilename',$step,$netboxid,'$subsystem','$key',$value)"; } } else { if ($exists) { if ($netboxid != $bighash{$rrd_fileid}{'netboxid'}) { $rrdfileq = "UPDATE rrd_file SET step=$step, netboxid=$netboxid, subsystem='$subsystem' WHERE rrd_fileid=$rrd_fileid"; } } else { $rrdfileq = "INSERT INTO rrd_file (path,filename,step,netboxid,subsystem) VALUES ('$newpath','$newfilename',$step,$netboxid,'$subsystem')"; } } # If we have something to update or insert, do that, else skip. if ($rrdfileq) { $r = $dbh->exec($rrdfileq); unless ($r->resultStatus eq PGRES_COMMAND_OK) { printf "ERROR DURING INSERT/UPDATE: %s", $dbh->errorMessage if $ll >= 2; } print "\t$rrdfileq\n" if $ll >= 3; } else { print "\tNo rrdfile-query made.\n" if $ll >= 3; } # Finding the id of what we just inserted... unless ($exists) { my $findid = "SELECT rrd_fileid FROM rrd_file WHERE path='$newpath' AND filename='$newfilename'"; my $findidres = $dbh->exec($findid); next if $findidres->ntuples == 0; ($rrd_fileid) = $findidres->fetchrow; } # TEMP $path =~ m,$cricketconfigdir(/.*)$,; my $purepath = $1; # We have now inserted or updated in the rrd_file table. Now we # are going one step down inserting or updating in the # rrd_datasource table. # We know that Cricket uses dsx for every ds, where x is a value # between 0 and upwards. So we use that as key, and update the # other fields if the values differ. my %bigdshash = (); # Fetch the values from the database. if ($exists) { my $fetchds = "SELECT rrd_datasourceid, name, descr, dstype FROM rrd_datasource WHERE rrd_fileid=$rrd_fileid"; my $dofetchds = $dbh->exec($fetchds); while ( my ($dsid, $dbname, $dbdescr, $dbdstype) = $dofetchds->fetchrow) { $bigdshash{$dsid}{'descr'} = $dbdescr; $bigdshash{$dsid}{'dstype'} = $dbdstype; $bigdshash{$dsid}{'name'} = $dbname; $bigdshash{$dbname}{1} = $dsid; } } # IF it's an interface, we have some static things to do if ($rrdhash{$path}{$filename}{'interface'}) { print "\t\tINTERFACE:\n" if $ll >= 3; for my $i (0 .. $#interfacearr) { # We know that if we are talking about interfaces # the ds-type is automatically COUNTER. # finding the units variable my $units = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -