7
7
from splunk_connect_for_snmp_mib_server .mongo import OidsRepository , MibsRepository
8
8
9
9
from pysmi import debug as pysmi_debug
10
- pysmi_debug .setLogger (pysmi_debug .Debug ('compiler' ))
10
+
11
+ pysmi_debug .setLogger (pysmi_debug .Debug ("compiler" ))
11
12
12
13
logger = logging .getLogger (__name__ )
13
14
@@ -17,18 +18,24 @@ def __init__(self, server_config):
17
18
self ._server_config = server_config
18
19
self ._custom_translation_table = self .get_custom_translation_table ()
19
20
self ._load_list = server_config ["snmp" ]["mibs" ]["load_list" ]
20
- self ._mib_builder , self ._mib_view_controller = self .build_mib_compiler (self ._load_list )
21
-
22
- self ._mongo_oids_coll = self .build_oids_collection (server_config ["mongo" ]["oid" ])
23
- self ._mongo_mibs_coll = self .build_mibs_collection (server_config ["mongo" ]["mib" ])
21
+ self ._mib_builder , self ._mib_view_controller = self .build_mib_compiler (
22
+ self ._load_list
23
+ )
24
+
25
+ self ._mongo_oids_coll = self .build_oids_collection (
26
+ server_config ["mongo" ]["oid" ]
27
+ )
28
+ self ._mongo_mibs_coll = self .build_mibs_collection (
29
+ server_config ["mongo" ]["mib" ]
30
+ )
24
31
# Execute the 1st translation to force mibs to be ready to use
25
32
self .first_mib_translation_trigger ()
26
-
33
+
27
34
# build a oids collection in mongo
28
35
def build_oids_collection (self , oid_mongo_config ):
29
36
oids_collection = OidsRepository (oid_mongo_config )
30
37
return oids_collection
31
-
38
+
32
39
# build a mibs collectoin in mongo
33
40
def build_mibs_collection (self , mib_mongo_config ):
34
41
mibs_collection = MibsRepository (mib_mongo_config )
@@ -39,13 +46,13 @@ def build_mib_compiler(self, load_list):
39
46
snmp_config = self ._server_config ["snmp" ]
40
47
mibBuilder = builder .MibBuilder ()
41
48
mibViewController = view .MibViewController (mibBuilder )
42
- compiler .addMibCompiler (mibBuilder , sources = [os .environ [' MIBS_FILES_URL' ]])
49
+ compiler .addMibCompiler (mibBuilder , sources = [os .environ [" MIBS_FILES_URL" ]])
43
50
44
51
# Optionally set an alternative path to compiled MIBs
45
- logger .debug (' Setting MIB sources...' )
52
+ logger .debug (" Setting MIB sources..." )
46
53
mibBuilder .addMibSources (builder .DirMibSource (snmp_config ["mibs" ]["dir" ]))
47
54
logger .debug (mibBuilder .getMibSources ())
48
- logger .debug (' done' )
55
+ logger .debug (" done" )
49
56
50
57
mib_file_path = os .path .join (os .getcwd (), self ._load_list )
51
58
logger .debug (f"mib_file_path { mib_file_path } " )
@@ -61,34 +68,32 @@ def build_mib_compiler(self, load_list):
61
68
except Exception as e :
62
69
logger .error (f"Error happened during load module: { e } " )
63
70
pass
64
-
71
+
65
72
logger .debug ("compiler is loaded" )
66
73
67
74
return mibBuilder , mibViewController
68
75
69
-
70
76
def first_mib_translation_trigger (self ):
71
77
# This is a test TRAP PDU
72
78
var_binds_list = [
73
79
{
74
- "oid" : ' 1.3.6.1.2.1.1.3.0' ,
80
+ "oid" : " 1.3.6.1.2.1.1.3.0" ,
75
81
"oid_type" : "ObjectName" ,
76
82
"val" : "123" ,
77
- "val_type" : "TimeTicks"
83
+ "val_type" : "TimeTicks" ,
78
84
},
79
85
{
80
- "oid" : ' 1.3.6.1.6.3.1.1.4.1.0' ,
86
+ "oid" : " 1.3.6.1.6.3.1.1.4.1.0" ,
81
87
"oid_type" : "ObjectName" ,
82
88
"val" : "1.3.6.1.6.3.1.1.5.2" ,
83
- "val_type" : "ObjectIdentifier"
84
- }
89
+ "val_type" : "ObjectIdentifier" ,
90
+ },
85
91
]
86
92
87
93
for var_bind in var_binds_list :
88
94
self .mib_translator (var_bind )
89
95
logger .debug ("mib_translator is ready to use!" )
90
96
91
-
92
97
# Check if the oid was translated properly
93
98
def is_not_translated (self , org_val , trans_val ):
94
99
# if translated value equals to original value, it was not translated at all
@@ -98,40 +103,48 @@ def is_not_translated(self, org_val, trans_val):
98
103
logger .debug (f"temp: { temp } " )
99
104
# if the second last number of the oid is numeric, it was not translated properly
100
105
return len (temp ) >= 2 and temp [- 2 ].isnumeric ()
101
-
106
+
102
107
def write_mib_to_load_list (self , mib_name ):
103
108
mib_file_path = os .path .join (os .getcwd (), self ._load_list )
104
109
logger .debug (f"mib_file_path: { mib_file_path } " )
105
110
try :
106
- with open (mib_file_path , 'a' ) as mib_list_file :
111
+ with open (mib_file_path , "a" ) as mib_list_file :
107
112
writer = csv .writer (mib_list_file )
108
113
writer .writerow ([mib_name ])
109
114
logger .debug (f"[-] Wrote { mib_name } to mib list" )
110
115
mib_list_file .close ()
111
116
except Exception as e :
112
- logger .error (f"Error happened during write the mib into mib load list file: { e } " )
117
+ logger .error (
118
+ f"Error happened during write the mib into mib load list file: { e } "
119
+ )
113
120
114
121
# Find mib module based on the oid
115
122
def find_mib_file (self , oid ):
116
123
snmp_config = self ._server_config ["snmp" ]
117
- value_tuple = str (oid ).replace ("." ,", " )
118
-
124
+ value_tuple = str (oid ).replace ("." , ", " )
125
+
119
126
try :
120
127
mib_name = self ._mongo_mibs_coll .search_oid (value_tuple )
121
128
except Exception as e :
122
- logger .error (f"Error happened during search the oid in mongo mibs collection: { e } " )
129
+ logger .error (
130
+ f"Error happened during search the oid in mongo mibs collection: { e } "
131
+ )
123
132
if not mib_name :
124
133
logger .warn (f"Can NOT find the mib file for the oid-{ oid } -- { value_tuple } " )
125
134
logger .debug (f"Writing the oid-{ oid } into mongo" )
126
135
try :
127
136
self ._mongo_oids_coll .add_oid (str (oid ))
128
- logger .debug (f"[-] The oid - { oid } was added into mongo oids collection" )
137
+ logger .debug (
138
+ f"[-] The oid - { oid } was added into mongo oids collection"
139
+ )
129
140
except Exception as e :
130
- logger .error (f"Error happened during add the oid - { oid } into mongo oids collection: { e } " )
141
+ logger .error (
142
+ f"Error happened during add the oid - { oid } into mongo oids collection: { e } "
143
+ )
131
144
return
132
145
mib_name = mib_name [:- 3 ]
133
146
logger .debug (f"mib_name: { mib_name } " )
134
- # load the mib module
147
+ # load the mib module
135
148
self .load_extra_mib (mib_name , oid )
136
149
137
150
# Load additional mib module
@@ -142,15 +155,21 @@ def load_extra_mib(self, mib_module, oid):
142
155
# add this mib module into mibs_list.csv if it was successfully loaded
143
156
self .write_mib_to_load_list (mib_module )
144
157
except Exception as e :
145
- logger .warn (f"Error happened during load mib module - { mib_module } for oid - { oid } : { e } " )
158
+ logger .warn (
159
+ f"Error happened during load mib module - { mib_module } for oid - { oid } : { e } "
160
+ )
146
161
logger .debug (f"Writing the oid-{ oid } into mongo oids collection" )
147
162
try :
148
163
self ._mongo_oids_coll .add_oid (str (oid ))
149
- logger .debug (f"[-] The oid - { oid } was added into mongo oids collection" )
164
+ logger .debug (
165
+ f"[-] The oid - { oid } was added into mongo oids collection"
166
+ )
150
167
except Exception as e :
151
- logger .error (f"Error happened during add the oid - { oid } into mongo oids collection: { e } " )
168
+ logger .error (
169
+ f"Error happened during add the oid - { oid } into mongo oids collection: { e } "
170
+ )
152
171
pass
153
-
172
+
154
173
# Check if the oid is already in the mongodb
155
174
def check_mongo (self , oid ):
156
175
# TODO remove #119-121 later
@@ -159,28 +178,29 @@ def check_mongo(self, oid):
159
178
# if the oid was found in mongo, then the oid is verified that there is not mapping mib module for it
160
179
no_mapping_mib = True if result != 0 else False
161
180
except Exception as e :
162
- logger .error (f"Error happend when finding oid in mongo oids collection: { e } " )
163
-
164
- return no_mapping_mib
181
+ logger .error (
182
+ f"Error happend when finding oid in mongo oids collection: { e } "
183
+ )
165
184
185
+ return no_mapping_mib
166
186
167
187
# Translate SNMP PDU varBinds into MIB objects using MIB
168
188
def mib_translator (self , var_bind ):
169
-
189
+
170
190
# Run var-binds through MIB resolver
171
191
try :
172
192
name = var_bind ["oid" ]
173
193
val = var_bind ["val" ]
174
194
varBind = rfc1902 .ObjectType (
175
195
rfc1902 .ObjectIdentity (name ), val
176
196
).resolveWithMib (self ._mib_view_controller )
177
-
197
+
178
198
logger .debug (f"* Translated PDU: { varBind .prettyPrint ()} " )
179
199
trans_string = varBind .prettyPrint ().replace (" = " , "=" )
180
200
trans_oid = trans_string .split ("=" )[0 ]
181
201
trans_val = trans_string .split ("=" )[1 ]
182
202
valType = var_bind ["val_type" ]
183
-
203
+
184
204
try :
185
205
# Check if oid exists in mongo oids collection and if oid was translated properly
186
206
no_mapping_mib_oid = self .check_mongo (name )
@@ -192,31 +212,31 @@ def mib_translator(self, var_bind):
192
212
if valType == "ObjectIdentifier" or valType == "ObjectIdentity" :
193
213
no_mapping_mib_val = self .check_mongo (val )
194
214
logger .debug (f"no_mapping_mib_val: { no_mapping_mib_val } " )
195
- if not no_mapping_mib_val and self .is_not_translated (val , trans_val ):
215
+ if not no_mapping_mib_val and self .is_not_translated (
216
+ val , trans_val
217
+ ):
196
218
self .find_mib_file (val )
197
219
198
- # Re-translated with the extra mibs
220
+ # Re-translated with the extra mibs
199
221
varBind = rfc1902 .ObjectType (
200
- rfc1902 .ObjectIdentity (name ), val
201
- ).resolveWithMib (self ._mib_view_controller )
222
+ rfc1902 .ObjectIdentity (name ), val
223
+ ).resolveWithMib (self ._mib_view_controller )
202
224
logger .debug (f"* Re-Translated PDU: { varBind .prettyPrint ()} " )
203
225
204
226
except Exception as e :
205
227
logger .debug (f"Error happended during translation checking: { e } " )
206
- pass
207
-
228
+ pass
229
+
208
230
return varBind .prettyPrint ().replace (" = " , "=" )
209
231
except Exception as e :
210
232
logger .error (f"Error happended in translation: { e } " )
211
233
finally :
212
234
pass
213
235
214
-
215
236
# Translate SNMP PDU varBinds into MIB objects using custom translation table
216
237
def custom_translator (self , oid ):
217
238
return self ._custom_translation_table .get (oid , None )
218
239
219
-
220
240
# Read the custom mib translation table into memory
221
241
def get_custom_translation_table (self ):
222
242
translation_table = {}
@@ -260,10 +280,13 @@ def format_trap_event(self, var_binds):
260
280
# translated_mib_string = self.mib_translator(name, val)
261
281
translated_mib_string = self .mib_translator (var_bind )
262
282
if translated_mib_string :
263
- translated_mib_string = '{translated_oid}="{translated_value}"' .format (translated_oid = translated_mib_string .split ("=" )[0 ], translated_value = translated_mib_string .split ("=" )[1 ])
283
+ translated_mib_string = '{translated_oid}="{translated_value}"' .format (
284
+ translated_oid = translated_mib_string .split ("=" )[0 ],
285
+ translated_value = translated_mib_string .split ("=" )[1 ],
286
+ )
264
287
else :
265
288
translated_mib_string = ""
266
-
289
+
267
290
if custom_translated_oid :
268
291
custom_translated_mib_string = (
269
292
'{custom_translated_oid}="{custom_translated_value}"' .format (
@@ -275,7 +298,9 @@ def format_trap_event(self, var_binds):
275
298
else :
276
299
custom_translated_mib_string = ""
277
300
278
- original_value = 'value{offset}="{value}"' .format (offset = offset , value = value )
301
+ original_value = 'value{offset}="{value}"' .format (
302
+ offset = offset , value = value
303
+ )
279
304
val_type_string = 'value{offset}-type="{val_type}"' .format (
280
305
offset = offset , val_type = valType
281
306
)
@@ -296,15 +321,15 @@ def format_trap_event(self, var_binds):
296
321
logger .debug (f"--- Trap Event String ---" )
297
322
logger .debug (trap_event_string )
298
323
return trap_event_string
299
-
324
+
300
325
# Format and translate the metric data
301
326
def format_metric_data (self , var_bind ):
302
327
"""
303
328
format one varBind object into metric format
304
329
@param var_bind: single varBind object
305
330
"""
306
331
metric_data = {}
307
-
332
+
308
333
# extract oid and value
309
334
oid = var_bind ["oid" ]
310
335
value = var_bind ["val" ]
@@ -323,9 +348,11 @@ def format_metric_data(self, var_bind):
323
348
324
349
# custom translation for oid
325
350
custom_translated_oid = self .custom_translator (oid )
326
-
351
+
327
352
# Construct metric data
328
- metric_data ["metric_name" ] = 'sc4snmp.{translated_oid.replace(".","_").replace("::", ".")}' ,
353
+ metric_data [
354
+ "metric_name"
355
+ ] = f'sc4snmp.{ translated_oid .replace ("." ,"_" ).replace ("::" , "." )} '
329
356
metric_data ["_value" ] = translated_val
330
357
metric_data ["metric_type" ] = valType
331
358
if custom_translated_oid :
0 commit comments