Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bit more robustness for legacy systems #31

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 23 additions & 10 deletions snmp_exporter/collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,43 @@
import time

import netsnmp

from prometheus_client import Metric, CollectorRegistry, generate_latest, Gauge

def walk_oids(host, port, oids, community):
def walk_oids(host, port, oids, community, do_bulkget):
session = netsnmp.Session(Version=2, DestHost=host, RemotePort=port,
Community=community, UseNumeric=True, Retries=3)
for oid in oids:
for v in walk_oid(session, oid):
for v in walk_oid(session, oid, do_bulkget):
yield v

def walk_oid(session, oid):
def walk_oid(session, oid, do_bulkget):
last_oid = oid
while True:
# getbulk starts from the last oid we saw.
vl = netsnmp.VarList(netsnmp.Varbind('.' + last_oid))
if not session.getbulk(0, 25, vl):
if do_bulkget:
res = session.getbulk(0, 25, vl)
else:
res = session.getnext(vl)

if not res:
return

for v in vl:
last_oid = v.tag[1:] + '.' + v.iid
if not (last_oid + '.').startswith(oid + '.'):
if v.iid == None or v.iid == '':
return

next_oid = v.tag[1:] + '.' + v.iid
if not (next_oid + '.').startswith(oid + '.'):
return
if next_oid == last_oid:
return

last_oid = next_oid
if v.iid == '0':
yield v.tag[1:], v.val
else:
yield last_oid, v.val
yield next_oid, v.val


def oid_to_tuple(oid):
"""Convert an OID to a tuple of numbers"""
Expand Down Expand Up @@ -80,7 +91,9 @@ def collect_snmp(config, host, port=161):
for metric in config['metrics']:
metrics[metric['name']] = Metric(metric['name'], 'SNMP OID {0}'.format(metric['oid']), 'untyped')

values = walk_oids(host, port, config['walk'], config.get('community', 'public'))
do_bulkget = 'bulkget' not in config or config['bulkget']
values = walk_oids(host, port, config['walk'], config.get('community', 'public'), do_bulkget)

oids = {}
for oid, value in values:
oids[oid_to_tuple(oid)] = value
Expand Down