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
|
SNMP MIB parser
---------------
[](https://pypi.org/project/pysmi/)
[](https://secure.travis-ci.org/etingof/pysmi)
[](https://codecov.io/github/etingof/pysmi)
[](https://raw.githubusercontent.com/etingof/pysmi/master/LICENSE.rst)
PySMI is a pure-Python implementation of
[SNMP SMI](https://en.wikipedia.org/wiki/Management_information_base) MIB parser.
This tool is designed to turn ASN.1 MIBs into various formats. As of this moment,
JSON and [pysnmp](https://github.com/etingof/pysnmp) modules can be generated
from ASN.1 MIBs.
Features
--------
* Understands SMIv1, SMIv2 and de-facto SMI dialects
* Turns MIBs into pysnmp classes and JSON documents
* Maintains an index of MIB objects over many MIB modules
* Automatically pulls ASN.1 MIBs from local directories, ZIP archives,
HTTP and FTP servers
* 100% Python, works with Python 2.4 up to Python 3.7
Rendered PySMI documentation can be found at [pysmi site](http://snmplabs.com/pysmi).
How to use PySMI
----------------
If you are using pysnmp, you might never notice pysmi presence - pysnmp
calls pysmi for MIB download and compilation behind the scenes (you can
still can do that manually by invoking *mibdump.py* tool).
To turn ASN.1 MIB into a JSON document, call *mibdump.py* tool like this:
```
$ mibdump.py --generate-mib-texts --destination-format json IF-MIB
Source MIB repositories: file:///usr/share/snmp/mibs, http://mibs.snmplabs.com/asn1/@mib@
Borrow missing/failed MIBs from: http://mibs.snmplabs.com/json/fulltexts/@mib@
Existing/compiled MIB locations:
Compiled MIBs destination directory: .
MIBs excluded from code generation: RFC-1212, RFC-1215, RFC1065-SMI, RFC1155-SMI,
RFC1158-MIB, RFC1213-MIB, SNMPv2-CONF, SNMPv2-SMI, SNMPv2-TC, SNMPv2-TM
MIBs to compile: IF-MIB
Destination format: json
Parser grammar cache directory: not used
Also compile all relevant MIBs: yes
Rebuild MIBs regardless of age: yes
Do not create/update MIBs: no
Byte-compile Python modules: no (optimization level no)
Ignore compilation errors: no
Generate OID->MIB index: no
Generate texts in MIBs: yes
Keep original texts layout: no
Try various filenames while searching for MIB module: yes
Created/updated MIBs: IANAifType-MIB, IF-MIB, SNMPv2-MIB
Pre-compiled MIBs borrowed:
Up to date MIBs: SNMPv2-CONF, SNMPv2-SMI, SNMPv2-TC
Missing source MIBs:
Ignored MIBs:
Failed MIBs:
```
JSON document build from
[IF-MIB module](http://mibs.snmplabs.com/asn1/IF-MIB)
would hold information such as:
```
{
"ifMIB": {
"name": "ifMIB",
"oid": "1.3.6.1.2.1.31",
"class": "moduleidentity",
"revisions": [
"2007-02-15 00:00",
"1996-02-28 21:55",
"1993-11-08 21:55"
]
},
...
"ifTestTable": {
"name": "ifTestTable",
"oid": "1.3.6.1.2.1.31.1.3",
"nodetype": "table",
"class": "objecttype",
"maxaccess": "not-accessible"
},
"ifTestEntry": {
"name": "ifTestEntry",
"oid": "1.3.6.1.2.1.31.1.3.1",
"nodetype": "row",
"class": "objecttype",
"maxaccess": "not-accessible",
"augmention": {
"name": "ifTestEntry",
"module": "IF-MIB",
"object": "ifEntry"
}
},
"ifTestId": {
"name": "ifTestId",
"oid": "1.3.6.1.2.1.31.1.3.1.1",
"nodetype": "column",
"class": "objecttype",
"syntax": {
"type": "TestAndIncr",
"class": "type"
},
"maxaccess": "read-write"
},
...
}
```
In general, converted MIBs capture all aspects of original (ASN.1) MIB contents
and layout. The snippet above is just a partial example, but here is the
complete [IF-MIB.json](http://mibs.snmplabs.com/json/fulltexts/IF-MIB.json)
file.
Besides one-to-one MIB conversion, PySMI library can produce JSON index to
facilitate fast MIB information lookup across large collection of MIB files.
For example, JSON index for
[IP-MIB.json](http://mibs.snmplabs.com/json/asn1/IP-MIB),
[TCP-MIB.json](http://mibs.snmplabs.com/json/asn1/TCP-MIB) and
[UDP-MIB.json](http://mibs.snmplabs.com/json/asn1/UDP-MIB)
modules would keep information like this:
```
{
"compliance": {
"1.3.6.1.2.1.48.2.1.1": [
"IP-MIB"
],
"1.3.6.1.2.1.49.2.1.1": [
"TCP-MIB"
],
"1.3.6.1.2.1.50.2.1.1": [
"UDP-MIB"
]
},
"identity": {
"1.3.6.1.2.1.48": [
"IP-MIB"
],
"1.3.6.1.2.1.49": [
"TCP-MIB"
],
"1.3.6.1.2.1.50": [
"UDP-MIB"
]
},
"oids": {
"1.3.6.1.2.1.4": [
"IP-MIB"
],
"1.3.6.1.2.1.5": [
"IP-MIB"
],
"1.3.6.1.2.1.6": [
"TCP-MIB"
],
"1.3.6.1.2.1.7": [
"UDP-MIB"
],
"1.3.6.1.2.1.49": [
"TCP-MIB"
],
"1.3.6.1.2.1.50": [
"UDP-MIB"
]
}
}
```
With this example, *compliance* and *identity* keys point to
*MODULE-COMPLIANCE* and *MODULE-IDENTITY* MIB objects, *oids*
list top-level OIDs branches defined in MIB modules. Full index
build over thousands of MIBs could be seen
[here](http://mibs.snmplabs.com/json/index.json).
The PySMI library can automatically fetch required MIBs from HTTP, FTP sites
or local directories. You could configure any MIB source available to you (including
[http://mibs.snmplabs.com/asn1/](http://mibs.snmplabs.com/asn1/)) for that purpose.
How to get PySMI
----------------
The pysmi package is distributed under terms and conditions of 2-clause
BSD [license](http://snmplabs.com/pysmi/license.html). Source code is freely
available as a GitHub [repo](https://github.com/etingof/pysmi).
You could `pip install pysmi` or download it from [PyPI](https://pypi.org/project/pysmi/).
If something does not work as expected,
[open an issue](https://github.com/etingof/pysmi/issues) at GitHub or
post your question [on Stack Overflow](http://stackoverflow.com/questions/ask).
Copyright (c) 2015-2019, [Ilya Etingof](mailto:etingof@gmail.com).
All rights reserved.
|