diff --git a/.github/workflows/update_maps.yml b/.github/workflows/update_maps.yml index f10d48d3e..501f6ac32 100644 --- a/.github/workflows/update_maps.yml +++ b/.github/workflows/update_maps.yml @@ -28,7 +28,6 @@ jobs: cpanm --local-lib=~/perl5 local::lib && eval $(perl -I ~/perl5/lib/perl5/ -Mlocal::lib) export MIBHOME=`pwd` EXTRAS/scripts/mkoidmap - gzip -9f EXTRAS/reports/all_oids - name: Commit files run: | diff --git a/EXTRAS/scripts/mkoidmap b/EXTRAS/scripts/mkoidmap index 2a910a986..d907126f0 100755 --- a/EXTRAS/scripts/mkoidmap +++ b/EXTRAS/scripts/mkoidmap @@ -35,124 +35,140 @@ if ($arg and (!exists $vendor_mibs->{$arg} and !exists $mib_files->{$arg})) { exit(1); } -my @mibs = (qw(SNMPv2-MIB SNMPv2-TC SNMPv2-SMI), - (reverse sort grep {m/^RFC/} @{ $vendor_mibs->{rfc} })); +my $mibcount = 0; +my %oidcount = (); +my %target_mibs = (); -if (exists $vendor_mibs->{$arg}) { - push @mibs, sort @{$vendor_mibs->{$arg}}; +if ($arg and exists $vendor_mibs->{$arg}) { + $target_mibs{$arg} = [sort @{ $vendor_mibs->{$arg} }]; } -elsif (exists $mib_files->{$arg}) { - @mibs = ($arg); +elsif ($arg and exists $mib_files->{$arg}) { + $target_mibs{$arg} = [$arg]; } else { - push @mibs, map {(@{$vendor_mibs->{$_}})} (qw(rfc net-snmp cisco), sort keys %{$vendor_mibs}); + %target_mibs = map {$_ => [sort @{ $vendor_mibs->{$_} }]} keys %{$vendor_mibs}; } -print "\N{EYES} Translating MIBs\n"; -my (%oidmap, %descrmap) = ((), ()); - -MIB: foreach my $m (@mibs) { - my $out = File::Temp->new(); - my $err = File::Temp->new(); - status("Parsing $m"); - - defined(my $pid = fork) or die "Couldn't fork: $!"; - if (!$pid) { # Child - exec(qq{snmptranslate -m '$m' -On -Td -TB '.1*' 2>'$err' 1>'$out'}) - or die "Couldn't exec: $!"; - } else { # Parent - my $slept = 0.5; - while (! waitpid($pid, WNOHANG)) { - status("Parsing $m"); - sleep 0.05; - $slept -= 0.05; - } - sleep $slept if $slept > 0; - } - - if (-s $err) { - blank(); - print RED, "\N{HEAVY BALLOT X} ", CYAN, 'Errors from ', - MAGENTA, $m, CYAN, " MIBs\n", RESET; - while (<$err>) { print } - print "\n"; - next MIB; - } - - my @report = read_lines($out, 'latin-1'); - my %details = (); - - LINEOUT: foreach my $line (@report) { - if ($line =~ m/^::=/) { # end of details - $oidmap{$details{oid}} ||= join ',', - ($details{mib} .'::'. $details{leaf}), - map {$details{$_} || ''} (qw/type mode index status syntax descr/); - %details = (); - next LINEOUT; - } - - if ($line =~ m/^\.\d/) { # oid - $details{oid} = $line; - next LINEOUT; - } - - if ($line =~ m/^(\S+)\s/) { # leaf name - $details{leaf} = $1; - next LINEOUT; - } +use DDP; p %target_mibs; - if ($line =~ m/^\s+-- FROM\s+([-#\w]+)/) { - $details{mib} = $1; - next LINEOUT; - } +foreach my $target (reverse sort keys %target_mibs) { + my %oidmap = parse_mibs($target, @{ $target_mibs{$target} }); + $mibcount += scalar @{ $target_mibs{$target} }; + ++$oidcount{$_} for keys %oidmap; - if ($line =~ m/^\s+MAX-ACCESS\s+([-\w]+)/) { - $details{mode} = $1; - next LINEOUT; - } + my $report = catfile($ENV{MIBHOME}, 'EXTRAS', 'reports', "${target}_oids"); + write_text($report, join '', map {"$_,$oidmap{$_}\n"} + sort {lxoid($a) cmp lxoid($b)} + grep {m/^\.1/} keys %oidmap); +} - if ($line =~ m/^\s+INDEX\s+{([^}]+)}/) { - $details{index} = join ':', - map {s/\s+//g; $_} - split m/,/, $1; - next LINEOUT; - } +blank(); +print sprintf "\N{BLACK FLAG} %d mibs translated into %d objects.\n", $mibcount, scalar keys %oidcount; +exit(0); - if ($line =~ m/^\s+STATUS\s+([-\w]+)/) { - $details{status} = $1; - next LINEOUT; +sub parse_mibs { + my ($target, @mibs) = @_; + my %oidmap = (); + + blank(); + print "\N{EYES} Translating $target MIB(s)\n"; + + MIB: foreach my $m (@mibs) { + my $out = File::Temp->new(); + my $err = File::Temp->new(); + status("Parsing $m"); + + defined(my $pid = fork) or die "Couldn't fork: $!"; + if (!$pid) { # Child + exec(qq{snmptranslate -m '$m' -On -Td -TB '.1*' 2>'$err' 1>'$out'}) + or die "Couldn't exec: $!"; + } else { # Parent + my $slept = 0.5; + while (! waitpid($pid, WNOHANG)) { + status("Parsing $m"); + sleep 0.05; + $slept -= 0.05; + } + sleep $slept if $slept > 0; } - if ($line =~ m/^\s+SYNTAX\s+([-\w]+)(?:\s+(.+))?/) { - $details{type} = $1; - my $syntax = $2; - next LINEOUT unless $syntax and $syntax =~ m/{/; - $details{syntax} = join ':', - map {s/\s+//g; $_} - map {s/[{}]//g; $_} - split m/,/, $syntax; - next LINEOUT; + if (-s $err) { + blank(); + print RED, "\N{HEAVY BALLOT X} ", CYAN, 'Errors from ', + MAGENTA, $m, CYAN, " MIBs\n", RESET; + while (<$err>) { print } + print "\n"; + next MIB; } - if ($line =~ m/^(?: DESCR| )/) { - $details{descr} .= join '', - map {s/\s+/ /g; $_} - map {s/"//g; $_} - map {s/^\s+DESCRIPTION\s+//; $_} ($line); - next LINEOUT; + my @report = read_lines($out, 'latin-1'); + my %details = (); + + LINEOUT: foreach my $line (@report) { + if ($line =~ m/^::=/) { # end of details + $oidmap{$details{oid}} ||= join ',', + ($details{mib} .'::'. $details{leaf}), + map {$details{$_} || ''} (qw/type mode index status syntax descr/); + %details = (); + next LINEOUT; + } + + if ($line =~ m/^\.\d/) { # oid + $details{oid} = $line; + next LINEOUT; + } + + if ($line =~ m/^(\S+)\s/) { # leaf name + $details{leaf} = $1; + next LINEOUT; + } + + if ($line =~ m/^\s+-- FROM\s+([-#\w]+)/) { + $details{mib} = $1; + next LINEOUT; + } + + if ($line =~ m/^\s+MAX-ACCESS\s+([-\w]+)/) { + $details{mode} = $1; + next LINEOUT; + } + + if ($line =~ m/^\s+INDEX\s+{([^}]+)}/) { + $details{index} = join ':', + map {s/\s+//g; $_} + split m/,/, $1; + next LINEOUT; + } + + if ($line =~ m/^\s+STATUS\s+([-\w]+)/) { + $details{status} = $1; + next LINEOUT; + } + + if ($line =~ m/^\s+SYNTAX\s+([-\w]+)(?:\s+(.+))?/) { + $details{type} = $1; + my $syntax = $2; + next LINEOUT unless $syntax and $syntax =~ m/{/; + $details{syntax} = join ':', + map {s/\s+//g; $_} + map {s/[{}]//g; $_} + split m/,/, $syntax; + next LINEOUT; + } + + if ($line =~ m/^(?: DESCR| )/) { + $details{descr} .= join '', + map {s/\s+/ /g; $_} + map {s/"//g; $_} + map {s/^\s+DESCRIPTION\s+//; $_} ($line); + next LINEOUT; + } } } -} - -delete $oidmap{'.1'}; -my $report = catfile($ENV{MIBHOME}, 'EXTRAS', 'reports', ($arg ? "${arg}_oids" : 'all_oids')); -write_text($report, join '', map {"$_,$oidmap{$_}\n"} - sort {lxoid($a) cmp lxoid($b)} - grep {m/^\.1/} keys %oidmap); -blank(); -print sprintf "\N{BLACK FLAG} %d mibs translated into %d objects.\n", scalar @mibs, scalar keys %oidmap; -exit(0); + delete $oidmap{'.1'}; + return %oidmap; +} # take oid and make comparable sub lxoid {