📄 makecricketconfig.pl
字号:
#!/usr/bin/env perl## $Id: makecricketconfig.pl 3976 2007-04-24 08:39:16Z mortenv $# This script makes the config to Cricket based on data in the# manage-db.## Copyright 2001-2004 Norwegian University of Science and Technology## This file is part of Network Administration Visualized (NAV)## NAV is free software; you can redistribute it and/or modify# it under the terms of the GNU General Public License as published by# the Free Software Foundation; either version 2 of the License, or# (at your option) any later version.## NAV is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# GNU General Public License for more details.## You should have received a copy of the GNU General Public License# along with NAV; if not, write to the Free Software# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA### Authors: John Magne Bredal <john.m.bredal@itea.ntnu.no>## TODO:# - use config.db to see target-types too.# - make views automatically# Loglevels:# 1 -> silent# 2 -> default# 3 -> debuguse warnings;BEGIN { $cricketdir = 0; $ll = 2; use vars qw ($opt_h $opt_l $opt_c); use Pg; use Cwd; use Getopt::Std; getopts('hl:c:'); # Checking for cricket-conf.pl if c-option not set. my @defaultcricketdirs = ("/home/navcron/cricket/cricket","/etc/cricket","/etc/","/usr/local/nav/cricket/cricket/"); if ($opt_c) { print "Setting cricketdir to $opt_c.\n"; $cricketdir = $opt_c; } else { print "No path to cricket-conf.pl specified, searching default paths...\n" if $ll >= 2; foreach my $dir (@defaultcricketdirs) { print "Searching in $dir.\n" if $ll >= 2; if (-e $dir."/cricket-conf.pl") { print "Found cricket-conf.pl in $dir.\n" if $ll >= 2; $cricketdir = $dir; last; } } unless ($cricketdir) { print "Didn't find Cricket, if you know where it is use the -c parameter.\n"; exit; } } eval "require '$cricketdir/cricket-conf.pl'";}our $ll;our $cricketdir;our ($opt_l,$opt_h,$opt_c);# Imports the config-db from Cricketuse lib "$Common::global::gInstallRoot/lib";use ConfigTree::Cache;use Common::global;$Common::global::gCT = new ConfigTree::Cache;$gCT = $Common::global::gCT;$gCT->Base($Common::global::gConfigRoot);$gCT->Warn(\&Warn);if (! $gCT->init()) { die("Failed to open compiled config tree from gConfigRoot/config.db: $!\n If this is the first time you run makecricketconfig, try compile cricket ($cricketdir/cricket/compile)\n");}use strict;use NAV;use NAV::Path;my $usage = "USAGE: $0 [-h] [-l loglevel] [-c pathtocricket]This script makes the config-tree for Cricket\th: help, prints this\tc: location of cricket-conf, if not set we search in default directories\tl: loglevel (1 - silent, 2 - default, 3 - debug)Made by John Magne Bredal - ITEA NTNU 2003";if ($opt_h) { print $usage; exit;}# some vars#my $cricketconfigdir = "$cricketdir/cricket-config";my $cricketconfigdir = "$Common::global::gConfigRoot";my $configfile = ".nav";my $changelog = $NAV::Path::localstatedir."/log/cricket-changelog";my $viewfile = $NAV::Path::sysconfdir."/cricket-views.conf";my %config; # stores navconfig for the configtreemy %dshash; # stores the mapping between ds and ds-typemy %targetoidhash;my %rtargetoidhash; # temporar hash for serversmy %rrdhash;my $step = 300;my $subsystem = "cricket";if ($opt_l && $opt_l =~ m/\d/) { $ll = $opt_l; print "Setting loglevel to $ll\n" if $ll >= 2;}# DB-varsmy $dbh = &NAV::connection('statTools', 'manage');# Must have the cricket-rows of the snmpoid-table in memory.my %oidhash;my %roidhash;my $q = "SELECT snmpoidid,oidkey FROM snmpoid WHERE oidsource LIKE 'Cricket' OR oidsource LIKE 'mib-II'";my $r = $dbh->exec($q);print "Filling hashes\n";while (my ($id,$txt) = $r->fetchrow) { $oidhash{$id} = $txt; $roidhash{$txt} = $id; print "\t$id <-> $txt\n" if $ll >= 3;}print "Done\n" if $ll >= 3;# Read the different view-groups into a hashmy %viewarr;if (-e $viewfile) { open (VIEW, $viewfile) or die ("Could not open $viewfile: $!\n"); while (<VIEW>) { next unless /^view/; m/view (\w+):(.*)/; my $view = $1; my @ds = split (" ", $2); $viewarr{$view} = [ @ds ]; } close VIEW;} else { printf "Could not find $viewfile, it should be in %s...no views will be made.\n", $NAV::Path::sysconfdir;}######################################### Description of hashes######################################### config# --------------------------------------# Contains the info read from the .nav-config-files# dirs: contains all the dirs that we will edit defaults-files# and make targets for# $dir->$key: we parse the .nav-files for config for each dir# specified in {dirs}. The possible values are specified# in the .nav config-file. We just fetch everything here,# no check on content# dshash{ds} = rrd-ds-type# --------------------------------------# for all the ds (that is everything in the config-files that come after datasource)# we know what rrd-ds-type that is defined. Filled by using the parseDefaults sub.# Example: dshash{ifinctets} = COUNTER# targetoidhash{targettype} = [datasources]# --------------------------------------# For each netbox we store the array of # datasources used to collect data.# Example (may be incorrect): kjemi-gsw->[cpu1min,cpu5min,mem5minUsed,mem5minFree]# oidhash{id} = txt# --------------------------------------# for all the id's in the snmpoid-table we know the textual description# Example (may be incorrect): oidhash{164} = ifinoctets# roidhash{txt} = id# --------------------------------------# The reverse of oidhash# Example (may be incorrect): oidhash{ifinoctets} = 164# rrdhash{path}{filename} with the following (possible) keys:# --------------------------------------# This array is used only when filling the rrd-database (rrd_file and rrd_datasource)# ds: an array consisting of the snmpoidid's that we are collecting for this target# netboxid: the netboxid of the unit we are collecting data from# interface: set to 1 if this is an interface# table: set only for interfaces, the table that we fetch interfaces from# id: set only for interfaces, the key-field of the table# Lets start working.# Rotating changelogsunless (-e $changelog) { `touch $changelog`;}for (my $counter = 8; $counter > 0;$counter--) { if (-e $changelog.".".$counter) { my $tmp = $counter+1; `mv $changelog.$counter $changelog.$tmp`; }}`mv $changelog $changelog.1`;open (CHANGELOG, ">$changelog") or warn ("Could not open $changelog for writing: $!\n");chdir ($cricketconfigdir);my $cwd = cwd;# parse main config so we know where to work.&parseMainConfig();# for each dir mentioned in the main config, treat themforeach my $dir (@{ $config{'dirs'} } ) { printf "---------- %s ----------\n", "$dir" if $ll >= 2; printf "Treating %s.\n", $dir if $ll >= 3; my $continue = &parseConfig($dir); next unless $continue; # Making serverconfig...it's still not fully tested. if ($dir eq 'servers') { &makeservers('servers'); next; } # interfaces are standard so we have a fixed config for them. if ($config{$dir}{'interface'}) { print "---------- MAKING TARGETS ----------\n" if $ll >= 2; &makeinterfaceTargets($dir); } else { print "---------- MAKING TARGETTYPES ----------\n" if $ll >= 2; &createTargetTypes($dir); print "---------- MAKING TARGETS ----------\n" if $ll >= 2; &makeTargets($dir); }}# Ok, we are done with editing config, making targettypes,# making targets and so on. Now lets fill the rrd-database# with the new info we have gathered. For this we use the# rrdhash that we have built.&fillRRDdatabase();close CHANGELOG;printf ("$0 executed in %s seconds.\n", time-$^T) if $ll >= 2;################################################### SUBS##################################################################################################### parseMainConfig# --------------------# Parses the main nav-configfile to see what dirs# we are supposed to look in. Stores that info# in the main hash.# INPUT: nada# RETURNS: nada, uses global hash.##################################################sub parseMainConfig { my $me = "parseMainConfig"; print "\n=> Running $me <=\n" if $ll >= 2; unless (-e $configfile) { printf "Could not find nav-configfile in %s, exiting.\n", $cricketconfigdir if $ll >= 2; exit(0); } unless (-r $configfile) { printf "nav-configfile is not readable in %s, exiting.\n", $cricketconfigdir if $ll >= 2; exit(0); } printf "Found nav-configfile in %s, good!\n", $cwd if $ll >= 2; open (NAV, $configfile) or die ("Could not open $cwd/$configfile.\n"); while (<NAV>) { my @dirs; next if /^\s*\#/; # Skipping comments # We find the dirs which we will descend into if (m/\s*dirs\s*=\s*(\S+)/) { my @dirs; my @tmp = split ",", $1; foreach my $dir (@tmp) { print "Pushing >$dir<\n" if $ll >= 3; push @dirs, $dir if (-e $dir); } $config{'dirs'} = [@dirs]; } # more to come? } close NAV; print "\n=> Done running $me<=\n" if $ll >= 2; return 1;}################################################### parseConfig# --------------------# Parses the nav-configfile found in the dir# specified. Puts info in the global hash $config.## INPUT: the dir we will work in# RETURNS: nada, uses global hash##################################################sub parseConfig { my $dir = shift; my $path = "$dir/$configfile"; my $me = "parseConfig"; print "\n=> Running $me with dir=$dir <=\n" if $ll >= 2; unless (-e $path) { printf "%s had no config-file.\n", $dir if $ll >= 3; print "\n=> Done running $me <=\n"; return 0; } open (HANDLE, $path) or die ("Could not open $path: $!"); while (<HANDLE>) { next if /^\s*\#/; if (m/^\s*(\S*)\s*=\s*(.+)$/) { $config{$dir}{$1} = $2; print "Setting $dir -> $1 = $2\n" if $ll >= 3; } } close HANDLE; print "\n=> Done running $me <=\n" if $ll >= 2; return 1;}################################################### parseDefaults# --------------------# parsing the existing defaults-file to find the# targettypes that already exists. Also store the# datasources found in a global hash $dshash, as# these are of interest later.## INPUT: the dir we work in# RETURNS: the name of the defaults file (scalar)##################################################sub parseDefaults { my $dir = shift; my $file; my $found = 0; my $me = "parseDefaults"; print "\n=> Running $me with dir=$dir <=\n" if $ll >= 2; my %returnhash; my @filenamearr = qw(Defaults defaults Default default); print "Searching for defaultsfile in $dir.\n" if $ll >= 3; foreach my $filename (@filenamearr) { if (-e "$dir/$filename") { $file = $filename; $found = 1; last; } } unless ($found) { print "Could not find defaults-file in $dir, returning.\n" if $ll >= 3; $returnhash{'found'} = 0; print "\n=> Done running $me <=\n" if $ll >= 2; return %returnhash; } my $tt; my $default;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -