Skip to content

Commit 78bef03

Browse files
committed
change to separate vendor oid maps
1 parent 6425e1a commit 78bef03

File tree

3 files changed

+117
-102
lines changed

3 files changed

+117
-102
lines changed

.github/workflows/update_maps.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ jobs:
2828
cpanm --local-lib=~/perl5 local::lib && eval $(perl -I ~/perl5/lib/perl5/ -Mlocal::lib)
2929
export MIBHOME=`pwd`
3030
EXTRAS/scripts/mkoidmap
31-
gzip -9f EXTRAS/reports/all_oids
3231
3332
- name: Commit files
3433
run: |

EXTRAS/reports/all_oids.gz

-17.3 MB
Binary file not shown.

EXTRAS/scripts/mkoidmap

Lines changed: 117 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -35,124 +35,140 @@ if ($arg and (!exists $vendor_mibs->{$arg} and !exists $mib_files->{$arg})) {
3535
exit(1);
3636
}
3737

38-
my @mibs = (qw(SNMPv2-MIB SNMPv2-TC SNMPv2-SMI),
39-
(reverse sort grep {m/^RFC/} @{ $vendor_mibs->{rfc} }));
38+
my $mibcount = 0;
39+
my %oidcount = ();
40+
my %target_mibs = ();
4041

41-
if (exists $vendor_mibs->{$arg}) {
42-
push @mibs, sort @{$vendor_mibs->{$arg}};
42+
if ($arg and exists $vendor_mibs->{$arg}) {
43+
$target_mibs{$arg} = [sort @{ $vendor_mibs->{$arg} }];
4344
}
44-
elsif (exists $mib_files->{$arg}) {
45-
@mibs = ($arg);
45+
elsif ($arg and exists $mib_files->{$arg}) {
46+
$target_mibs{$arg} = [$arg];
4647
}
4748
else {
48-
push @mibs, map {(@{$vendor_mibs->{$_}})} (qw(rfc net-snmp cisco), sort keys %{$vendor_mibs});
49+
%target_mibs = map {$_ => [sort @{ $vendor_mibs->{$_} }]} keys %{$vendor_mibs};
4950
}
5051

51-
print "\N{EYES} Translating MIBs\n";
52-
my (%oidmap, %descrmap) = ((), ());
53-
54-
MIB: foreach my $m (@mibs) {
55-
my $out = File::Temp->new();
56-
my $err = File::Temp->new();
57-
status("Parsing $m");
58-
59-
defined(my $pid = fork) or die "Couldn't fork: $!";
60-
if (!$pid) { # Child
61-
exec(qq{snmptranslate -m '$m' -On -Td -TB '.1*' 2>'$err' 1>'$out'})
62-
or die "Couldn't exec: $!";
63-
} else { # Parent
64-
my $slept = 0.5;
65-
while (! waitpid($pid, WNOHANG)) {
66-
status("Parsing $m");
67-
sleep 0.05;
68-
$slept -= 0.05;
69-
}
70-
sleep $slept if $slept > 0;
71-
}
72-
73-
if (-s $err) {
74-
blank();
75-
print RED, "\N{HEAVY BALLOT X} ", CYAN, 'Errors from ',
76-
MAGENTA, $m, CYAN, " MIBs\n", RESET;
77-
while (<$err>) { print }
78-
print "\n";
79-
next MIB;
80-
}
81-
82-
my @report = read_lines($out, 'latin-1');
83-
my %details = ();
84-
85-
LINEOUT: foreach my $line (@report) {
86-
if ($line =~ m/^::=/) { # end of details
87-
$oidmap{$details{oid}} ||= join ',',
88-
($details{mib} .'::'. $details{leaf}),
89-
map {$details{$_} || ''} (qw/type mode index status syntax descr/);
90-
%details = ();
91-
next LINEOUT;
92-
}
93-
94-
if ($line =~ m/^\.\d/) { # oid
95-
$details{oid} = $line;
96-
next LINEOUT;
97-
}
98-
99-
if ($line =~ m/^(\S+)\s/) { # leaf name
100-
$details{leaf} = $1;
101-
next LINEOUT;
102-
}
52+
use DDP; p %target_mibs;
10353

104-
if ($line =~ m/^\s+-- FROM\s+([-#\w]+)/) {
105-
$details{mib} = $1;
106-
next LINEOUT;
107-
}
54+
foreach my $target (reverse sort keys %target_mibs) {
55+
my %oidmap = parse_mibs($target, @{ $target_mibs{$target} });
56+
$mibcount += scalar @{ $target_mibs{$target} };
57+
++$oidcount{$_} for keys %oidmap;
10858

109-
if ($line =~ m/^\s+MAX-ACCESS\s+([-\w]+)/) {
110-
$details{mode} = $1;
111-
next LINEOUT;
112-
}
59+
my $report = catfile($ENV{MIBHOME}, 'EXTRAS', 'reports', "${target}_oids");
60+
write_text($report, join '', map {"$_,$oidmap{$_}\n"}
61+
sort {lxoid($a) cmp lxoid($b)}
62+
grep {m/^\.1/} keys %oidmap);
63+
}
11364

114-
if ($line =~ m/^\s+INDEX\s+{([^}]+)}/) {
115-
$details{index} = join ':',
116-
map {s/\s+//g; $_}
117-
split m/,/, $1;
118-
next LINEOUT;
119-
}
65+
blank();
66+
print sprintf "\N{BLACK FLAG} %d mibs translated into %d objects.\n", $mibcount, scalar keys %oidcount;
67+
exit(0);
12068

121-
if ($line =~ m/^\s+STATUS\s+([-\w]+)/) {
122-
$details{status} = $1;
123-
next LINEOUT;
69+
sub parse_mibs {
70+
my ($target, @mibs) = @_;
71+
my %oidmap = ();
72+
73+
blank();
74+
print "\N{EYES} Translating $target MIB(s)\n";
75+
76+
MIB: foreach my $m (@mibs) {
77+
my $out = File::Temp->new();
78+
my $err = File::Temp->new();
79+
status("Parsing $m");
80+
81+
defined(my $pid = fork) or die "Couldn't fork: $!";
82+
if (!$pid) { # Child
83+
exec(qq{snmptranslate -m '$m' -On -Td -TB '.1*' 2>'$err' 1>'$out'})
84+
or die "Couldn't exec: $!";
85+
} else { # Parent
86+
my $slept = 0.5;
87+
while (! waitpid($pid, WNOHANG)) {
88+
status("Parsing $m");
89+
sleep 0.05;
90+
$slept -= 0.05;
91+
}
92+
sleep $slept if $slept > 0;
12493
}
12594

126-
if ($line =~ m/^\s+SYNTAX\s+([-\w]+)(?:\s+(.+))?/) {
127-
$details{type} = $1;
128-
my $syntax = $2;
129-
next LINEOUT unless $syntax and $syntax =~ m/{/;
130-
$details{syntax} = join ':',
131-
map {s/\s+//g; $_}
132-
map {s/[{}]//g; $_}
133-
split m/,/, $syntax;
134-
next LINEOUT;
95+
if (-s $err) {
96+
blank();
97+
print RED, "\N{HEAVY BALLOT X} ", CYAN, 'Errors from ',
98+
MAGENTA, $m, CYAN, " MIBs\n", RESET;
99+
while (<$err>) { print }
100+
print "\n";
101+
next MIB;
135102
}
136103

137-
if ($line =~ m/^(?: DESCR| )/) {
138-
$details{descr} .= join '',
139-
map {s/\s+/ /g; $_}
140-
map {s/"//g; $_}
141-
map {s/^\s+DESCRIPTION\s+//; $_} ($line);
142-
next LINEOUT;
104+
my @report = read_lines($out, 'latin-1');
105+
my %details = ();
106+
107+
LINEOUT: foreach my $line (@report) {
108+
if ($line =~ m/^::=/) { # end of details
109+
$oidmap{$details{oid}} ||= join ',',
110+
($details{mib} .'::'. $details{leaf}),
111+
map {$details{$_} || ''} (qw/type mode index status syntax descr/);
112+
%details = ();
113+
next LINEOUT;
114+
}
115+
116+
if ($line =~ m/^\.\d/) { # oid
117+
$details{oid} = $line;
118+
next LINEOUT;
119+
}
120+
121+
if ($line =~ m/^(\S+)\s/) { # leaf name
122+
$details{leaf} = $1;
123+
next LINEOUT;
124+
}
125+
126+
if ($line =~ m/^\s+-- FROM\s+([-#\w]+)/) {
127+
$details{mib} = $1;
128+
next LINEOUT;
129+
}
130+
131+
if ($line =~ m/^\s+MAX-ACCESS\s+([-\w]+)/) {
132+
$details{mode} = $1;
133+
next LINEOUT;
134+
}
135+
136+
if ($line =~ m/^\s+INDEX\s+{([^}]+)}/) {
137+
$details{index} = join ':',
138+
map {s/\s+//g; $_}
139+
split m/,/, $1;
140+
next LINEOUT;
141+
}
142+
143+
if ($line =~ m/^\s+STATUS\s+([-\w]+)/) {
144+
$details{status} = $1;
145+
next LINEOUT;
146+
}
147+
148+
if ($line =~ m/^\s+SYNTAX\s+([-\w]+)(?:\s+(.+))?/) {
149+
$details{type} = $1;
150+
my $syntax = $2;
151+
next LINEOUT unless $syntax and $syntax =~ m/{/;
152+
$details{syntax} = join ':',
153+
map {s/\s+//g; $_}
154+
map {s/[{}]//g; $_}
155+
split m/,/, $syntax;
156+
next LINEOUT;
157+
}
158+
159+
if ($line =~ m/^(?: DESCR| )/) {
160+
$details{descr} .= join '',
161+
map {s/\s+/ /g; $_}
162+
map {s/"//g; $_}
163+
map {s/^\s+DESCRIPTION\s+//; $_} ($line);
164+
next LINEOUT;
165+
}
143166
}
144167
}
145-
}
146-
147-
delete $oidmap{'.1'};
148-
my $report = catfile($ENV{MIBHOME}, 'EXTRAS', 'reports', ($arg ? "${arg}_oids" : 'all_oids'));
149-
write_text($report, join '', map {"$_,$oidmap{$_}\n"}
150-
sort {lxoid($a) cmp lxoid($b)}
151-
grep {m/^\.1/} keys %oidmap);
152168

153-
blank();
154-
print sprintf "\N{BLACK FLAG} %d mibs translated into %d objects.\n", scalar @mibs, scalar keys %oidmap;
155-
exit(0);
169+
delete $oidmap{'.1'};
170+
return %oidmap;
171+
}
156172

157173
# take oid and make comparable
158174
sub lxoid {

0 commit comments

Comments
 (0)