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

Add support for string types #4087

Merged
merged 2 commits into from Jul 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions snmp/datadog_checks/snmp/snmp.py
Expand Up @@ -4,6 +4,7 @@
from collections import defaultdict

import pysnmp.proto.rfc1902 as snmp_type
from pyasn1.codec.ber import decoder
from pyasn1.type.univ import OctetString
from pysnmp import hlapi
from pysnmp.error import PySnmpError
Expand Down Expand Up @@ -588,4 +589,23 @@ def submit_metric(self, name, snmp_value, forced_type, tags=None):
self.gauge(metric_name, value, tags)
return

if snmp_class == 'Opaque':
# Try support for floats
try:
value = float(decoder.decode(bytes(snmp_value))[0])
except Exception:
pass
else:
self.gauge(metric_name, value, tags)
return

# Falls back to try to cast the value.
try:
value = float(snmp_value)
except ValueError:
pass
else:
self.gauge(metric_name, value, tags)
return

self.log.warning("Unsupported metric type %s for %s", snmp_class, metric_name)
5 changes: 5 additions & 0 deletions snmp/tests/common.py
Expand Up @@ -49,6 +49,11 @@

UNSUPPORTED_METRICS = [{'OID': "1.3.6.1.2.1.25.6.3.1.5.1", 'name': "IAmString"}] # String (not supported)

CAST_METRICS = [
{'OID': "1.3.6.1.4.1.2021.10.1.3.1", 'name': "cpuload1"}, # OctetString
{'OID': "1.3.6.1.4.1.2021.10.1.6.1", 'name': "cpuload2"}, # Opaque
]

CONSTRAINED_OID = [{"MIB": "RFC1213-MIB", "symbol": "tcpRtoAlgorithm"}]

DUMMY_MIB_OID = [{"MIB": "DUMMY-MIB", "symbol": "scalar"}]
Expand Down
11 changes: 11 additions & 0 deletions snmp/tests/test_check.py
Expand Up @@ -412,3 +412,14 @@ def test_network_failure(aggregator, check):
aggregator.assert_service_check("snmp.can_check", status=SnmpCheck.CRITICAL, tags=common.CHECK_TAGS, at_least=1)

aggregator.all_metrics_asserted()


def test_cast_metrics(aggregator, check):
metrics = common.CAST_METRICS
instance = common.generate_instance_config(metrics)

check.check(instance)
aggregator.assert_metric('snmp.cpuload1', value=0.06)
aggregator.assert_metric('snmp.cpuload2', value=0.06)

aggregator.all_metrics_asserted()
4 changes: 2 additions & 2 deletions snmp/tox.ini
Expand Up @@ -17,8 +17,8 @@ passenv =
COMPOSE*
commands =
pip install -r requirements.in
snmp: pytest -v -m"not unit"
unit: pytest -v -m"unit"
snmp: pytest -v -m"not unit" {posargs}
unit: pytest -v -m"unit" {posargs}

[testenv:bench]
commands =
Expand Down