File: mibview.py

package info (click to toggle)
python-pysnmp4-apps 0.4.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 284 kB
  • sloc: python: 2,397; makefile: 3
file content (411 lines) | stat: -rw-r--r-- 14,942 bytes parent folder | download
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#
# This file is part of pysnmp-apps software.
#
# Copyright (c) 2005-2016, Ilya Etingof <ilya@glas.net>
# License: http://pysnmp.sf.net/license.html
#
# C/L interface to MIB variables. Mimics Net-SNMP CLI.
#
import os
from pyasn1.type import univ
from pysnmp_apps.cli import base
from pysnmp.proto import rfc1902
from pysnmp.smi import builder, compiler
from pysnmp import error

defaultMibSourceUrl = 'http://mibs.snmplabs.com/asn1/@mib@'
defaultMibBorrowerUrl = 'http://mibs.snmplabs.com/pysnmp/fulltexts/@mib@'

# Usage

def getUsage():
    return "\
MIB options:\n\
   -m MIB[:...]   load given list of MIBs (ALL loads all compiled MIBs)\n\
   -M DIR[:...]   look in given list of directories for MIBs\n\
   -P MIBOPTS     Toggle various defaults controlling MIB parsing:\n\
              XS: search for ASN.1 MIBs in remote directories specified\n\
                  in URL form. The @mib@ token in the URL is substituted\n\
                  with actual MIB to be downloaded. Default repository\n\
                  address is %s\n\
              XB: search for pysnmp MIBs in remote directories specified\n\
                  in URL form. The @mib@ token in the URL is substituted\n\
                  with actual MIB to be downloaded. Default repository\n\
                  address is %s\n\
   -O OUTOPTS     Toggle various defaults controlling output display:\n\
              q:  removes the equal sign and type information\n\
              Q:  removes the type information\n\
              f:  print full OIDs on output\n\
              s:  print only last symbolic element of OID\n\
              S:  print MIB module-id plus last element\n\
              u:  print OIDs using UCD-style prefix suppression\n\
              n:  print OIDs numerically\n\
              e:  print enums numerically\n\
              b:  do not break OID indexes down\n\
              E:  include a \" to escape the quotes in indices\n\
              X:  place square brackets around each index\n\
              T:  print value in hex\n\
              v:  print values only (not OID = value)\n\
              U:  don't print units\n\
              t:  output timeticks values as raw numbers\n\
   -I INOPTS      Toggle various defaults controlling input parsing:\n\
              h:  don't apply DISPLAY-HINTs\n\
              u:  top-level OIDs must have '.' prefix (UCD-style)\n\
" % (defaultMibSourceUrl, defaultMibBorrowerUrl)

# Scanner

class MibViewScannerMixIn:
    def t_mibfiles(self, s):
        r' -m '
        self.rv.append(base.ConfigToken('mibfiles'))

    def t_mibdirs(self, s):
        r' -M '
        self.rv.append(base.ConfigToken('mibdirs'))

    def t_parseropts(self, s):
        r' -P '
        self.rv.append(base.ConfigToken('parseropts'))

    def t_outputopts(self, s):
        r' -O '
        self.rv.append(base.ConfigToken('outputopts'))

    def t_inputopts(self, s):
        r' -I '
        self.rv.append(base.ConfigToken('inputopts'))

# Parser

class MibViewParserMixIn:
    def p_mibView(self, args):
        '''
        Option ::= GeneralOption
        Option ::= ParserOption
        Option ::= OutputOption
        Option ::= InputOption

        GeneralOption ::= MibDirList
        MibDirList ::= mibdirs MibDirs
        MibDirList ::= mibdirs whitespace MibDirs
        MibDirs ::= MibDir semicolon MibDirs
        MibDirs ::= MibDir
        MibDir ::= string
        GeneralOption ::= MibFileList
        MibFileList ::= mibfiles MibFiles
        MibFileList ::= mibfiles whitespace MibFiles
        MibFiles ::= MibFile semicolon MibFiles
        MibFiles ::= MibFile
        MibFile ::= string

        ParserOption ::= parseropts string
        ParserOption ::= parseropts whitespace string
        ParserOption ::= parseropts string whitespace Url
        ParserOption ::= parseropts whitespace string whitespace Url
        Url ::= string semicolon string

        OutputOption ::= outputopts string
        OutputOption ::= outputopts whitespace string

        InputOption ::= inputopts string
        InputOption ::= inputopts whitespace string
        '''

# Generator

class __MibViewGenerator(base.GeneratorTemplate):
    # Load MIB modules
    def n_MibFile(self, cbCtx, node):
        snmpEngine, ctx = cbCtx
        mibBuilder = snmpEngine.getMibBuilder()
        if node[0].attr.lower() == 'all':
            mibBuilder.loadModules()
        else:
            mibBuilder.loadModules(node[0].attr)
            
    def n_MibDir(self, cbCtx, node):
        snmpEngine, ctx = cbCtx
        if 'MibDir' not in ctx:
            ctx['MibDir'] = []
        ctx['MibDir'].append(node[0].attr)

    def n_Url(self, cbCtx, node):
        snmpEngine, ctx = cbCtx
        ctx['Url'] = node[0].attr+':'+node[2].attr

    def n_ParserOption_exit(self, cbCtx, node):
        snmpEngine, ctx = cbCtx
        opt = node[1].attr or node[2].attr
        for c in opt:
            if c == 'XS':
                if 'MibDir' not in ctx:
                    ctx['MibDir'] = []
                if 'Url' not in ctx:
                    raise error.PySnmpError('Missing URL for option')
                ctx['MibDir'].append(ctx['Url'])
                del ctx['Url']
            elif c == 'XB':
                if 'MibBorrowers' not in ctx:
                    ctx['MibBorrowers'] = []
                if 'Url' not in ctx:
                    raise error.PySnmpError('Missing URL for option')
                ctx['MibBorrowers'].append(ctx['Url'])
                del ctx['Url']

    def n_OutputOption(self, cbCtx, node):
        snmpEngine, ctx = cbCtx
        mibViewProxy = ctx['mibViewProxy']
        opt = node[1].attr or node[2].attr
        for c in opt:
            if c == 'q':
                mibViewProxy.buildEqualSign = 0
                mibViewProxy.buildTypeInfo = 0
            elif c == 'Q':
                mibViewProxy.buildTypeInfo = 0
            elif c == 'f':
                mibViewProxy.buildModInfo = 0
                mibViewProxy.buildObjectDesc = 0
                mibViewProxy.buildAbsoluteName = 1
            elif c == 's':
                mibViewProxy.buildModInfo = 0
                mibViewProxy.buildObjectDesc = 1
            elif c == 'S':
                mibViewProxy.buildObjectDesc = 1
            elif c == 'u':
                pass
            elif c == 'n':
                mibViewProxy.buildObjectDesc = 0
                mibViewProxy.buildModInfo = 0
                mibViewProxy.buildNumericName = 1
                mibViewProxy.buildNumericIndices = 1
                mibViewProxy.buildAbsoluteName = 1
            elif c == 'e':
                raise error.PySnmpError('Option not implemented')
            elif c == 'b':
                mibViewProxy.buildNumericIndices = 1
            elif c == 'E':
                mibViewProxy.buildEscQuotes = 1
            elif c == 'X':
                mibViewProxy.buildSquareBrackets = 1
            elif c == 'T':
                mibViewProxy.buildHexVals = 1
            elif c == 'v':
                mibViewProxy.buildObjectName = 0
            elif c == 'U':
                mibViewProxy.buildUnits = 0
            elif c == 't':
                mibViewProxy.buildRawTimeTicks = 1
                pass
            elif c == 'R':
                mibViewProxy.buildRawVals = 1
            else:
                raise error.PySnmpError(
                    'Unknown output option %s at %s' % (c, self)
                    )

    def n_InputOption(self, cbCtx, node):
        snmpEngine, ctx = cbCtx
        mibViewProxy = ctx['mibViewProxy']
        opt = node[1].attr or node[2].attr
        for c in opt:
            if c == 'R':
                pass
            elif c == 'b':
                pass
            elif c == 'u':
                mibViewProxy.defaultOidPrefix = (
                    'iso', 'org', 'dod', 'internet', 'mgmt', 'mib-2'
                    )
            elif c == 'r':
                pass
            elif c == 'h':
                pass
            else:
                raise error.PySnmpError(
                    'Unknown input option %s at %s' % (c, self)
                    )

def generator(cbCtx, ast):
    snmpEngine, ctx = cbCtx
    if 'mibViewProxy' not in ctx:
        ctx['mibViewProxy'] = MibViewProxy(ctx['mibViewController'])

    compiler.addMibCompiler(snmpEngine.getMibBuilder())

    snmpEngine, ctx = __MibViewGenerator().preorder((snmpEngine, ctx), ast)

    if 'MibDir' not in ctx:
        ctx['MibDir'] = [ defaultMibSourceUrl ]
    if 'MibBorrowers' not in ctx:
        ctx['MibBorrowers'] = [ defaultMibBorrowerUrl ]

    compiler.addMibCompiler(snmpEngine.getMibBuilder(),
                            sources=ctx['MibDir'],
                            borrowers=ctx['MibBorrowers'])
    return snmpEngine, ctx

class UnknownSyntax:
    def prettyOut(self, val):
        return str(val)
unknownSyntax = UnknownSyntax()
    
#  Proxy MIB view

class MibViewProxy:
    # Defaults
    defaultOidPrefix = (
        'iso', 'org', 'dod', 'internet', 'mgmt', 'mib-2', 'system'
        )
    defaultMibs = ('SNMPv2-MIB',)
    defaultMibDirs = ()

    # MIB parsing options
    # currently N/A
    
    # MIB output options
    buildObjectName = 1
    buildValue = 1
    buildModInfo = 1
    buildObjectDesc = 1
    buildNumericName = 0
    buildAbsoluteName = 0
    buildNumericIndices = 0
    buildEqualSign = 1
    buildTypeInfo = 1
    buildEscQuotes = 0
    buildSquareBrackets = 0
    buildHexVals = 0
    buildRawVals = 0
    buildRawTimeTicks = 0
    buildGuessedStringVals = 1
    buildUnits = 1
    
    # MIB input options
    parseAsRandomAccessMib = 1
    parseAsRegExp = 0
    parseAsRelativeOid = 1
    parseAndCheckIndices = 1
    parseAsDisplayHint = 1
    
    def __init__(self, mibViewController):
        if 'PYSNMPOIDPREFIX' in os.environ:
            self.defaultOidPrefix = os.environ['PYSNMPOIDPREFIX']
        if 'PYSNMPMIBS' in os.environ:
            self.defaultMibs = os.environ['PYSNMPMIBS'].split(':')
        if 'PYSNMPMIBDIRS' in os.environ:
            self.defaultMibDirs = os.environ['PYSNMPMIBDIRS'].split(':')
        if self.defaultMibDirs:
            mibViewController.mibBuilder.setMibSources(
                *mibViewController.mibBuilder.getMibSources() + tuple(
                    [ builder.ZipMibSource(m).init() for m in self.defaultMibDirs ]
                    )
                 )
        if self.defaultMibs:
            mibViewController.mibBuilder.loadModules(*self.defaultMibs)
        self.__oidValue = univ.ObjectIdentifier()
        self.__intValue = univ.Integer()
        self.__timeValue = rfc1902.TimeTicks()
        
    def getPrettyOidVal(self, mibViewController, oid, val):
        prefix, label, suffix = mibViewController.getNodeName(oid)
        modName, nodeDesc, _suffix = mibViewController.getNodeLocation(prefix)
        out = ''
        # object name
        if self.buildObjectName:        
            if self.buildModInfo:
                out = '%s::' % modName
            if self.buildObjectDesc:
                out = out + nodeDesc
            else:
                if self.buildNumericName:
                    name = prefix
                else:
                    name = label
                if not self.buildAbsoluteName:
                    name = name[len(self.defaultOidPrefix):]
                out = out + '.'.join([ str(x) for x in name ])
            
            if suffix:
                if suffix == (0,):
                    out = out + '.0'
                else:
                    m, n, s = mibViewController.getNodeLocation(prefix[:-1])
                    rowNode, = mibViewController.mibBuilder.importSymbols(
                        m, n
                        )
                    if self.buildNumericIndices:
                        out = out + '.' + '.'.join([ str(x) for x in suffix ])
                    else:
                        try:
                            for i in rowNode.getIndicesFromInstId(suffix):
                                if self.buildEscQuotes:
                                    out = out + '.\\\"%s\\\"' % i.prettyOut(i)
                                elif self.buildSquareBrackets:
                                    out = out + '.[%s]' % i.prettyOut(i)
                                else:
                                    out = out + '.\"%s\"' % i.prettyOut(i)
                        except AttributeError:
                            out = out + '.' + '.'.join(
                                [ str(x) for x in suffix ]
                                )

        if self.buildObjectName and self.buildValue:
            if self.buildEqualSign:
                out = out + ' = '
            else:
                out = out + ' '

        # Value
        if self.buildValue:
            if isinstance(val, univ.Null):
                return out + val.prettyPrint()
            mibNode, = mibViewController.mibBuilder.importSymbols(
                modName, nodeDesc
            )
            if hasattr(mibNode, 'syntax'):
                syntax = mibNode.syntax
            else:
                syntax = val
            if syntax is None: # lame Agent may return a non-instance OID
                syntax = unknownSyntax
            if self.buildTypeInfo:
                out = out + '%s: ' % syntax.__class__.__name__
            if self.buildRawVals:
                out = out + str(val)
            elif self.buildHexVals: # XXX make it always in hex?
                if self.__intValue.isSuperTypeOf(val):
                    out = out + '%x' % int(val)
                elif self.__oidValue.isSuperTypeOf(val):
                    out = out + ' '.join([ '%x' % x for x in tuple(val) ])
                else:
                    out = out + ' '.join([ '%.2x' % ord(x) for x in str(val) ])
            elif self.__timeValue.isSameTypeWith(val):
                if self.buildRawTimeTicks:
                    out = out + str(int(val))
                else: # TimeTicks is not a TC
                    val = int(val)
                    d, m = divmod(val, 8640000)
                    out = out + '%d days ' % d
                    d, m = divmod(m, 360000)
                    out = out + '%d:' % d
                    d, m = divmod(m, 6000)
                    out = out + '%d:' % d
                    d, m = divmod(m, 100)
                    out = out + '%d.%d' % (d, m)
            elif self.__oidValue.isSuperTypeOf(val):
                oid, label, suffix = mibViewController.getNodeName(val)
                out = out + '.'.join(
                    label + tuple([ str(x) for x in suffix ])
                )
            else:
                out = out + syntax.prettyOut(val)

            if self.buildUnits:
                if hasattr(mibNode, 'getUnits'):
                    out = out + ' %s' % mibNode.getUnits()

        return out
    
    def setPrettyOidValue(self, oid, val, t):
        return oid, val