Code Monkey home page Code Monkey logo

hnmp's Introduction

HNMP is a high-level Python library to ease the pain of retrieving and processing data from SNMP-capable devices such as network switches, routers, and printers. It's not meant to provide everything SNMP has to offer, but to get rid of most of the suck inherent to writing Munin or Icinga plugins that process SNMP data.

HNMP is meant to be used like this:

  1. acquire MIB files (optional, some luck required)
  2. use a MIB browser application like this freeish and cross-platform one to figure out the numeric OIDs you're interested in
  3. use HNMP to retrieve your data and do some light processing
  4. put man on moon ๐Ÿš€

Usage

>>> from hnmp import SNMP
>>>
>>> snmp = SNMP("example.com", community="public")

# get a single value
>>> uptime = snmp.get("1.3.6.1.2.1.1.3.0")
>>> uptime
datetime.timedelta(412, 29152)

# build a table
>>> wifi_clients = snmp.table(
>>>     "1.3.6.1.4.1.14179.2.1.4.1",
>>>     columns={
>>>         3: "username",
>>>         25: "protocol",
>>>     },
>>>     column_value_mapping={
>>>         "protocol": {
>>>             3: "802.11g",
>>>             6: "802.11n",
>>>         },
>>>     },
>>>     fetch_all_columns=False,  # fetch only named columns (useful with large tables)
>>> )
>>>
>>> wifi_clients.columns["username"]
("jdoe", "rms", "bwayne")
>>> wifi_clients.columns["protocol"]
("802.11g", "802.11n", "802.11n")
>>> wifi_clients.rows[0]["username"]
"jdoe"

# conveniently count column values
>>> wifi_clients.columns["protocol"]
("802.11g", "802.11n", "802.11n")
>>> wifi_clients.columns["protocol"].value_count
{"802.11g": 1, "802.11n": 2}

# helpers for converting MAC and IP addresses
>>> from hnmp import ipv4_address, mac_address
>>> raw_string = snmp.get("1.3.6.1.4.1.9.9.513.1.1.1.1.2.[...]")
>>> raw_string
't&\xac\x1b\xe7\xa1'
>>> mac_address(raw_string)
'74:26:ac:1b:e7:a1'
>>> ipv4_address(snmp.get("1.3.6.1.4.1.9.9.513.1.1.1.1.11.[...]"))
'10.1.2.3'

Install

pip install hnmp

FAQ

How do the table OIDs work?

Quite often you will find that you need to query a whole table instead of a single value (e.g. a list of network interfaces). This is how the OIDs are organized in a table:

1.3.6.1.4.1.9.9.513.1.1.1.1.11.1.2.3.4.5
1.3.6.1.4.1.9.9.513.1.1.1.1.11.1.2.3.4.6
1.3.6.1.4.1.9.9.513.1.1.1.1.11.1.2.3.4.7
1.3.6.1.4.1.9.9.513.1.1.1.1.12.1.2.3.4.5
1.3.6.1.4.1.9.9.513.1.1.1.1.12.1.2.3.4.6
1.3.6.1.4.1.9.9.513.1.1.1.1.12.1.2.3.4.7
1.3.6.1.4.1.9.9.513.1.1.1.1.12.1.2.3.4.7
\_____________ ____________/|  \___ ___/
              โ”‚             โ”‚      |
              โ”‚             โ”‚    Row ID
              โ”‚             โ”‚
              โ”‚          Column ID
              โ”‚
           Base OID

Starting from the base OID (1.3.6.1.4.1.9.9.513.1.1.1.1 in this example), you will see the different column IDs (11 and 12 in this example) repeated for every row. Row IDs consist of everything after the column ID. Sometimes these are only sequential single digits, but they can also be made up of multiple numbers. In this example, there are three rows with IDs 1.2.3.4.5, 1.2.3.4.6, and 1.2.3.4.7.

Why doesn't HNMP support loading MIB files?

Depending on MIB files would make the calling piece of code harder to distribute (since you need to include the MIBs, which may have some nasty non-free license attached to them). I consider MIB files a means to manually discover OIDs, nothing more. HNMP is biased towards use in scripts rather than full-blown applications. Having to use a library is bad enough for scripts, MIBs would just make your script even more unwieldy.

Why doesn't HNMP support SNMPv3?

The need just hasn't arisen yet. Version 2c works pretty well for my purposes.


PyPI downloads ย  PyPI version ย  Python 2.7 ย  Python 3.x ย  PyPI license

hnmp's People

Contributors

trehn avatar eill avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.