-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgenerate_oid_dictionary.sh
More file actions
executable file
路187 lines (159 loc) 路 5.73 KB
/
Copy pathgenerate_oid_dictionary.sh
File metadata and controls
executable file
路187 lines (159 loc) 路 5.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/bin/bash
#
# generate_oid_dictionary.sh
#
# Generates a static OID dictionary (oid_dictionary.json) from net-snmp's snmptranslate.
# Run this on a server where snmptranslate and python3 are installed.
#
# Usage: ./generate_oid_dictionary.sh
# Output: oid_dictionary.json in the current directory
set -euo pipefail
exec python3 - "$@" << 'PYEOF'
import subprocess
import json
import re
import sys
import os
from concurrent.futures import ThreadPoolExecutor, as_completed
OUTFILE = "oid_dictionary.json"
PARALLEL = 50
LIMIT = int(sys.argv[1]) if len(sys.argv) > 1 and sys.argv[1].isdigit() else 0
def get_all_oids():
result = subprocess.run(
['snmptranslate', '-mall', '-To'],
capture_output=True, text=True
)
oids = sorted(set(line.strip() for line in result.stdout.strip().split('\n')
if line.strip() and line.strip().startswith('.')))
return oids
def translate_one(label):
"""Run snmptranslate -Td on one OID label and return (label, raw_output)."""
try:
result = subprocess.run(
['snmptranslate', '-mall', '-Td', label],
capture_output=True, text=True, timeout=10
)
return (label, result.stdout.strip())
except Exception:
return (label, '')
def get_numeric(label):
"""Run snmptranslate -On to get the numeric OID."""
try:
result = subprocess.run(
['snmptranslate', '-mall', '-On', label],
capture_output=True, text=True, timeout=10
)
return result.stdout.strip()
except Exception:
return ''
def parse_td_output(text):
if not text:
return None
lines = text.split('\n')
if not lines:
return None
info = {}
info['oid'] = lines[0].strip()
for line in lines:
if ' -- FROM' in line:
info['mib'] = re.sub(r'.*--\s*FROM\s*', '', line).strip()
elif ' -- TEXTUAL CONVENTION' in line:
info['conv'] = re.sub(r'.*--\s*TEXTUAL CONVENTION\s*', '', line).strip()
elif re.match(r'\s+SYNTAX\s', line) and '::=' not in line:
info['syntax'] = re.sub(r'^\s*SYNTAX\s+', '', line).strip()
elif ' DISPLAY-HINT' in line:
info['hint'] = re.sub(r'.*DISPLAY-HINT\s*', '', line).strip()
elif ' MAX-ACCESS' in line:
info['access'] = re.sub(r'.*MAX-ACCESS\s*', '', line).strip()
elif re.match(r'\s+STATUS\s', line):
info['status'] = re.sub(r'^\s*STATUS\s+', '', line).strip()
elif '::= ' in line:
info['line'] = re.sub(r'::=\s*', '', line).strip()
desc_started = False
desc_lines = []
for line in lines:
if desc_started:
if '::= ' in line:
break
desc_lines.append(line)
elif 'DESCRIPTION' in line:
desc_started = True
remainder = re.sub(r'.*DESCRIPTION\s*', '', line).strip()
if remainder.startswith('"'):
remainder = remainder[1:]
if remainder:
desc_lines.append(remainder)
desc = ' '.join(l.strip() for l in desc_lines).strip()
if desc.endswith('"'):
desc = desc[:-1]
info['description'] = desc
for key in ('mib', 'conv', 'syntax', 'hint', 'access', 'status', 'line', 'description'):
if key not in info:
info[key] = ''
return info
# --- Main ---
print("Step 1: Enumerating all OIDs from MIBs...")
all_oids = get_all_oids()
if LIMIT:
all_oids = all_oids[:LIMIT]
print(f" Found {len(all_oids)} OIDs (limited to {LIMIT}).")
else:
print(f" Found {len(all_oids)} OIDs.")
print("Step 2: Translating each OID (parallel)...")
raw_results = {}
done = 0
with ThreadPoolExecutor(max_workers=PARALLEL) as pool:
futures = {pool.submit(translate_one, label): label for label in all_oids}
for future in as_completed(futures):
label, raw = future.result()
if raw:
raw_results[label] = raw
done += 1
if done % 1000 == 0:
print(f" {done} / {len(all_oids)}...")
print(f" Got {len(raw_results)} non-empty translations.")
print("Step 3: Parsing translations...")
data = {}
aliases = {}
for label, raw in raw_results.items():
info = parse_td_output(raw)
if info is None or not info.get('oid'):
continue
oid_full = info['oid']
if not oid_full:
continue
# Remove empty fields to save space
info = {k: v for k, v in info.items() if v}
# Primary key: MODULE::name
data[oid_full] = info
# Alias: short name -> MODULE::name
if '::' in oid_full:
short_name = oid_full.split('::')[1]
if short_name and short_name not in data:
aliases[short_name] = oid_full
print(f" {len(data)} primary entries, {len(aliases)} short-name aliases.")
print("Step 4: Adding numeric OID aliases (parallel)...")
primary_keys = list(data.keys())
done = 0
with ThreadPoolExecutor(max_workers=PARALLEL) as pool:
futures = {pool.submit(get_numeric, sym): sym for sym in primary_keys}
for future in as_completed(futures):
sym = futures[future]
numeric = future.result()
if numeric and numeric.startswith('.') and numeric not in data:
aliases[numeric] = sym
done += 1
if done % 1000 == 0:
print(f" {done} / {len(primary_keys)}...")
print(f" {len(aliases)} total aliases.")
print(f"Step 5: Writing {OUTFILE}...")
output = {"data": data, "aliases": aliases}
with open(OUTFILE, 'w') as f:
json.dump(output, f, ensure_ascii=False, separators=(',', ':'))
size = os.path.getsize(OUTFILE)
if size > 1024 * 1024:
print(f" {len(data)} entries + {len(aliases)} aliases, {size / 1024 / 1024:.1f} MB")
else:
print(f" {len(data)} entries + {len(aliases)} aliases, {size / 1024:.0f} KB")
print("Done.")
PYEOF