|
From: <buc...@us...> - 2013-03-02 09:10:14
|
Revision: 242
http://devmon.svn.sourceforge.net/devmon/?rev=242&view=rev
Author: buchanmilne
Date: 2013-03-02 09:10:06 +0000 (Sat, 02 Mar 2013)
Log Message:
-----------
Improve templatebuilder.pl:
-Dont add branches and leaves to oids file
-Imprve threshold generation
-Create a table in message for repeaters, or oid: value lines for leaves
-Write the template files if they dont exist in the current directory
Modified Paths:
--------------
trunk/extras/templatebuilder.pl
Modified: trunk/extras/templatebuilder.pl
===================================================================
--- trunk/extras/templatebuilder.pl 2013-01-03 14:44:41 UTC (rev 241)
+++ trunk/extras/templatebuilder.pl 2013-03-02 09:10:06 UTC (rev 242)
@@ -15,6 +15,7 @@
my %trans = ();
my %enum = ();
my @walkproc= ();
+my $debug = 1;
sub OIDNumLookup {
my ($mib,$var,$tail) = @_;
@@ -32,13 +33,15 @@
my $c = -1;
foreach my $l (@walk) {
if(my ($mib,$var,$tail) = ($l =~ /^([^:]+)::([^.]+)(.*) = /)) {
+ push @walkproc,($l);
+ $c++;
+ next if $trans{"${mib}::$var"};
$trans{"${mib}::$var"} = "";
# $trans{"${mib}::$var$tail"} = "";
- if($l =~ /= INTEGER: .*\(\d+\)$/) {
+ if($l =~ /= INTEGER: .*(\d+)$/) {
+ print "Adding ${mib}::$var to integers to map\n" if $debug;
$enum{"${mib}::$var"} = "";
}
- push @walkproc,($l);
- $c++;
} else {
$walkproc[$c] .= "\n$l";
}
@@ -54,46 +57,111 @@
}
my @enumlist = keys %enum;
$transcmd = "snmptranslate -Tp ".join " ",@enumlist;
-@translist = grep / (EnumVal|Values:) /,`$transcmd`;
+#@translist = grep / (EnumVal|Values:) /,`$transcmd`;
chomp @translist;
-for(my $i=0;$i<=$#enumlist;$i++) {
- $enum{$enumlist[$i]} = $translist[1+2*$i];
- $enum{$enumlist[$i]} =~ s/^\s+//;
+foreach (keys %enum) {
+ my $snmpcmd = "snmptranslate -Tp $_|grep Values:";
+ print "Trying to translate $_ using $snmpcmd\n" if $debug;
+ my @snmpvals = `$snmpcmd`;
+ print "Received from snmptranslate: ",join "\n",@snmpvals;
+ #my $transform = grep /Values:/,@snmpvals;
+ my $transform = $snmpvals[0];
+ #$transform =~ s/\s+//g;
+ print "Transform for $_: $transform\n" if $debug;
+ $enum{$_} = $transform;
}
+print Dumper(\%enum) if $debug;
+#for(my $i=0;$i<=$#enumlist;$i++) {
+# $enum{$enumlist[$i]} = $translist[1+2*$i];
+# $enum{$enumlist[$i]} =~ s/^\s+//;
+#}
my %varprint;
my %enumprint;
my @oidprint = ();
my @transformprint = ();
my @threshprint = ();
+my %thresh;
+my @messageprint = ();
+my @branches;
+my @leaves;
foreach my $l (@walkproc) {
+ print "Assessing $l\n" if $debug;
my ($mib,$var,$tail,$rest) = ($l =~ /^([^:]+)::([^.]+)(.*)( = .*)$/s);
$tail ||= "";
- #printf ("mib: %s var: %s tail: %s rest: $rest\n",$mib,$var,$tail,$rest);
+ printf ("mib: %s var: %s tail: %s rest: $rest\n",$mib,$var,$tail,$rest) if $debug;
unless( defined $varprint{"${mib}::$var"} ) {
- print "$var\t: ".$trans{"${mib}::$var"}."\t: branch\n";
- print "$var\t: ".$trans{"${mib}::$var"}.".0\t: leaf\n" if $tail eq ".0";
- push @oidprint,("$var\t: ".$trans{"${mib}::$var"}."\t: branch\n");
- push @oidprint,("$var\t: ".$trans{"${mib}::$var"}.".0\t: leaf\n") if $tail eq ".0";
+ if ($tail eq '.0') {
+ print "$var\t: ".$trans{"${mib}::$var"}.".0\t: leaf\n";
+ push @oidprint,("$var\t: ".$trans{"${mib}::$var"}.".0\t: leaf\n");
+ push @leaves,$var;
+ } else {
+ print "$var\t: ".$trans{"${mib}::$var"}."\t: branch\n";
+ push @oidprint,("$var\t: ".$trans{"${mib}::$var"}."\t: branch\n");
+ push @branches,$var;
+ }
}
$varprint{"${mib}::$var"} = 1;
- if($l =~ /= INTEGER: .*\(\d+\)$/) {
+ if($l =~ /= INTEGER: (\d+)/) {
unless( $enumprint{"${mib}::$var"} ) {
- printf " ## %s\n",$enum{"${mib}::$var"};
+ print "Checking if I can interpret values for $var from the MIB\n" if $debug;
+ printf " ##ENUM %s\n",$enum{"${mib}::$var"};
my $el = $enum{"${mib}::$var"};
$el =~ s/^\s*Values:\s*//;
my @elv = map {s/^(.*)\((\-?\d+)\)/$2=$1/; $_;} split /,\s*/,$el;
- my @thv = map {s/^(.*)\(\-?\d+\)/$1/; $_;} split /,\s*/,$el;
- #@elv =~ s/^(.*)\((\d+)\)/$2=$1/;
- printf "${var}Txt\t: SWITCH\t: {$var} %s\n",join ",",@elv;
- push @transformprint, (sprintf "${var}Txt\t: SWITCH\t: {$var} %s\n",join ",",@elv);
- push @threshprint, (sprintf "${var}Txt\t: green : %s\t:\n",join "|",grep /(ok|good|online|closed|locked)/i,@thv);
- push @threshprint, (sprintf "${var}Txt\t: yellow : %s\t:\n",join "|",grep !/(ok|good|online|closed|locked|fail|degrade|offline)/i,@thv);
- push @threshprint, (sprintf "${var}Txt\t: red : %s\t:\n",join "|",grep /(fail|degrade|offline)/i,@thv);
+ if ( my @thv = map {s/^(.*)\(\-?\d+\)/$1/; $_;} split /,\s*/,$el) {
+ #@elv =~ s/^(.*)\((\d+)\)/$2=$1/;
+ printf "${var}Txt\t: SWITCH\t: {$var} %s\n",join ",",@elv;
+ push @transformprint, (sprintf "${var}Txt\t: SWITCH\t: {$var} %s\n",join ",",@elv);
+ push @threshprint, (sprintf "${var}Txt\t: green : %s\t:\n",join "|",grep /(ok|good|online|closed|locked|green)/i,@thv);
+ push @threshprint, (sprintf "${var}Txt\t: yellow : %s\t:\n",join "|",grep !/(ok|good|online|closed|locked||green|fail|degrade|offline|alarm|red|off)/i,@thv);
+ push @threshprint, (sprintf "${var}Txt\t: red : %s\t:\n",join "|",grep /(fail|degrade|offline|alarm|red|off)/i,@thv);
+ $thresh{$var} = 1;
+ }
}
$enumprint{"${mib}::$var"} = 1;
}
- print "${mib}::$var$tail (".OIDNumLookup($mib,$var,$tail).")$rest\n";
+ print "${mib}::$var$tail (".OIDNumLookup($mib,$var,$tail).")$rest\n" if $debug;
}
-print "\noids file::\n",@oidprint;
-print "\ntransforms file::\n",@transformprint;
-print "\nthresholds file::\n",@threshprint;
+if (@leaves gt 1 ) {
+ foreach my $leaf (@leaves) {
+ print @messageprint,"{${leaf}.errors}\n" if $thresh{$leaf};
+ }
+ foreach my $leaf (@leaves) {
+ push @messageprint,"$leaf: {$leaf}\n";
+ }
+}
+if (@branches gt 1) {
+ push @messageprint,(sprintf "TABLE:\n");
+ push @messageprint, join('|',@branches), "\n";
+ foreach my $branch (@branches) {
+ if ( $thresh{$branch} ) {
+ push @messageprint, "{${branch}Txt.color}{${branch}Txt}{${branch}Txt.errors}"
+ } else {
+ push @messageprint, "{$branch}";
+ }
+ push @messageprint, '|';
+ }
+ pop @messageprint;
+ push @messageprint,"\n";
+}
+write_file("oids",\@oidprint);
+write_file("transforms",\@transformprint);
+write_file("thresholds",\@threshprint);
+write_file("exceptions",[]);
+write_file("message",\@messageprint);
+
+sub write_file {
+ my ($file,$contents) = @_;
+ print Dumper($contents) if $debug;
+ if ( -e "$file" ) {
+ print STDERR "#$file file exists, not overwriting it\n";
+ print "===== start $file =====\n",@{$contents},"====== end $file ======\n";
+ } else {
+ open TMPL,">$file" or warn "Could not open file: $!" and return 0;
+ print TMPL @{$contents};
+ close TMPL;
+ }
+ return 1;
+}
+
+
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|