While Snimpy is targeted at being used interactively or through simple scripts, you can also use it from your Python program. It provides a high-level interface as well as lower-level ones.
This module is the high-level interface to Snimpy. It exposes Manager class to instantiate a new manager (which is an SNMP client). This is the preferred interface for Snimpy.
Here is a simple example of use of this module:
>>> load("IF-MIB")
>>> m = Manager("localhost")
>>> m.ifDescr[1]
<String: lo>
SNMP manager. An instance of this class will represent an SNMP manager (client).
When a MIB is loaded with load(), scalars and row names from it will be made available as an instance attribute. For a scalar, reading the corresponding attribute will get its value while setting it will allow to modify it:
>>> load("SNMPv2-MIB")
>>> m = Manager("localhost", "private")
>>> m.sysContact
<String: root>
>>> m.sysContact = "Brian Jones"
>>> m.sysContact
<String: Brian Jones>
For a row name, the provided interface is like a Python dictionary. Requesting an item using its index will retrieve the value from the agent (the server):
>>> load("IF-MIB")
>>> m = Manager("localhost", "private")
>>> m.ifDescr[1]
<String: lo>
>>> m.ifName[1] = "Loopback interface"
Also, it is possible to iterate on a row name to get all available values for index:
>>> load("IF-MIB")
>>> m = Manager("localhost", "private")
>>> for idx in m.ifDescr:
... print(m.ifDescr[idx])
A context manager is also provided. Any modification issued inside the context will be delayed until the end of the context and then grouped into a single SNMP PDU to be executed atomically:
>>> load("IF-MIB")
>>> m = Manager("localhost", "private")
>>> with m:
... m.ifName[1] = "Loopback interface"
... m.ifName[2] = "First interface"
Any error will be turned into an exception:
>>> load("IF-MIB")
>>> m = Manager("localhost", "private")
>>> m.ifDescr[999]
Traceback (most recent call last):
...
SNMPNoSuchName: There is no such variable name in this MIB.
This module is a low-level interface to manipulate and extract information from MIB files. It is a CFFI wrapper around libsmi. You may find convenient to use it in other projects but the wrapper is merely here to serve Snimpy use and is therefore incomplete.
MIB node. An instance of this class represents a MIB node. It can be specialized by other classes, like Scalar, Table, Column, Node.
Get possible enum values. When the node can only take a discrete number of values, those values are defined in the MIB and can be retrieved through this property.
Returns: | The dictionary of possible values keyed by the integer value. |
---|
Get node format. The node format is a string to use to display a user-friendly version of the node. This is can be used for both octet strings or integers (to make them appear as decimal numbers).
Returns: | The node format as a string or None if there is no format available. |
---|
Get OID for the current node. The OID can then be used to request the node from an SNMP agent.
Returns: | OID as a tuple |
---|
Get node ranges. An node can be restricted by a set of ranges. For example, an integer can only be provided between two values. For strings, the restriction is on the length of the string.
The returned value can be None if no restriction on range exists for the current node, a single value if the range is fixed or a list of tuples or fixed values otherwise.
Returns: | The valid range for this node. |
---|
SMI related exception. Any exception thrown in this module is inherited from this one.
MIB scalar node. This class represents a scalar value in the MIB. A scalar value is a value not contained in a table.
MIB table node. This class represents a table. A table is an ordered collection of objects consisting of zero or more rows. Each object in the table is identified using an index. An index can be a single value or a list of values.
Get table columns. The columns are the different kind of objects that can be retrieved in a table.
Returns: | list of table columns (Column instances) |
---|
Get a node by its name.
Parameters: |
|
---|---|
Returns: | the requested MIB node (Node) |
Get a node by its OID.
Parameters: | oid – The OID as a tuple |
---|---|
Returns: | The requested MIB node (Node) |
Return all columns from a givem MIB.
Parameters: | mib – The MIB name |
---|---|
Returns: | The list of all columns for the MIB |
Return type: | list of Column instances |
Return all nodes from a given MIB.
Parameters: | mib – The MIB name |
---|---|
Returns: | The list of all MIB nodes for the MIB |
Return type: | list of Node instances |
Return all scalars from a given MIB.
Parameters: | mib – The MIB name |
---|---|
Returns: | The list of all scalars for the MIB |
Return type: | list of Scalar instances |
Return all tables from a given MIB.
Parameters: | mib – The MIB name |
---|---|
Returns: | The list of all tables for the MIB |
Return type: | list of Table instances |
Load a MIB into the library.
Parameters: | mib – The MIB to load, either a filename or a MIB name. |
---|---|
Returns: | The MIB name that has been loaded. |
Raises SMIException: | |
The requested MIB cannot be loaded. |
Generates the list of loaded MIB names.
Yield: | The names of all currently loaded MIBs. |
---|
This module is a low-level interface to build SNMP requests, send them and receive answers. It is built on top of pysnmp but the exposed interface is far simpler. It is also far less complete and there is an important dependency to the basictypes module for type coercing.
SNMP related base exception. All SNMP exceptions are inherited from this one. The inherited exceptions are named after the name of the corresponding SNMP error.
SNMP session. An instance of this object will represent an SNMP session. From such an instance, one can get information from the associated agent.
Get bulk settings.
Returns: | False if bulk is disabled or a non-negative integer for the number of repetitions. |
---|
Retrieve an OID value using GET.
Parameters: | oids – a list of OID to retrieve. An OID is a tuple. |
---|---|
Returns: | a list of tuples with the retrieved OID and the raw value. |
Get number of times a request is retried.
Returns: | Number of retries for each request. |
---|
Set an OID value using SET. This function takes an odd number of arguments. They are working by pair. The first member is an OID and the second one is basictypes.Type instace whose pack() method will be used to transform into the appropriate form.
Returns: | a list of tuples with the retrieved OID and the raw value. |
---|
Retrieve OIDs values using GETBULK or GETNEXT. The method is called “walk” but this is either a GETBULK or a GETNEXT. The later is only used for SNMPv1 or if bulk has been disabled using bulk() property.
Parameters: | oids – a list of OID to retrieve. An OID is a tuple. |
---|---|
Returns: | a list of tuples with the retrieved OID and the raw value. |
This module is aimed at providing Pythonic representation of various SNMP types. Each SMIv2 type is mapped to a corresponding class which tries to mimic a basic type from Python. For example, display strings are like Python string while SMIv2 integers are just like Python integers. This module is some kind of a hack and its use outside of Snimpy seems convoluted.
Class for an enumeration. An enumaration is an integer but labels are attached to some values for a more user-friendly display.
Class for a generic octet string. This class should be compared to String which is used to represent a display string. This class is usually used to store raw bytes, like a bitmask of VLANs.
Class for a display string. Such a string is an unicode string and it is therefore expected that only printable characters are used. This is usually the case if the corresponding MIB node comes with a format string.
With such an instance, the user is expected to be able to provide a formatted. For example, a MAC address could be written 00:11:22:33:44:55.
Base class for all types.
Create instance from an OID.
This is the sister function of toOid().
Parameters: |
|
---|---|
Returns: | A couple (l, v) with l the number of suboids needed to create the instance and v the instance created from the OID |
Convert to an OID.
If this function is implemented, then class function fromOid() should also be implemented as the “invert” function of this one.
This function only works if the entity is used as an index! Otherwise, it should raises NotImplementedError.
Returns: | An OID that can be used as index |
---|