Jaroslav Ištvan
ČeštinaEnglish
← Back to projects

ISA — snmp2otel

C++17SNMPv2cBEROTLP/HTTP

What it does

snmp2otel periodically sends SNMPv2c GET requests to a given agent and forwards the values it gets back as OpenTelemetry metrics - a bridge between the old world of network monitoring (SNMP) and a modern observability stack (OTEL).

It only cares about scalar OIDs (the ones ending in .0) - typically counters and gauges from the agent’s MIB, not whole subtrees.

What SNMP is

The Simple Network Management Protocol is a standard from the early nineties that is still used to read data from routers, switches, printers or UPS units. If there is a chart of a company link’s load hanging somewhere, chances are the data behind it came over SNMP.

  • An agent runs on the managed device. It keeps a tree database of values (the MIB) and addresses each value with a numeric OID - for example 1.3.6.1.2.1.1.3.0 is the device’s uptime.
  • The manager (in our case this program) sends a GetRequest with a list of OIDs over UDP and the agent answers with the same structure filled in with the values.
  • Version 2c authenticates with a so-called community string, which is essentially a password sent in plain text. That is not suitable for a public network, but inside managed infrastructure it is still used today.
  • The packets are neither text nor JSON but binary ASN.1/BER - which is why the project carries its own encoder and decoder.

The .0 suffix of an OID means a scalar value, that is one specific number, not a table. The program deliberately handles only these - tables would require walking a subtree, which is a different task.

What OpenTelemetry is

OpenTelemetry is an open, vendor-neutral standard for collecting telemetry - metrics, traces and logs. It solves the situation where every monitoring tool had its own format and its own agent, and moving elsewhere meant redoing the instrumentation of the whole application.

  • Data is sent with the OTLP protocol; the program uses its HTTP variant with a JSON body.
  • On the other side there is usually an OTEL Collector, which receives the data and forwards it to any backend - Prometheus, Grafana, anything. The sender does not need to know where the data ends up, and that is the whole point.
  • A gauge is a metric that simply has some value at a given moment - a temperature, a number of connections. That makes it different from a counter, which only grows monotonically.

So why build a bridge: devices speaking SNMP are everywhere and will not disappear any time soon, but a modern observability stack speaks OTLP. The exporter is a translator between those two worlds.

Input and output

The input is the agent’s address and a file with a list of OIDs (one per line, comments with #); the output is the JSON request body that the program sends to the collector’s OTLP/HTTP endpoint:

Command + oids.txt
./snmp2otel -t 127.0.0.1 -o ./oids.txt \
  -e http://127.0.0.1:4318/v1/metrics -p 1161 -v

# oids.txt:
1.3.6.1.2.1.1.3.0
1.3.6.1.4.1.9999.1.1.0
1.3.6.1.4.1.9999.1.2.0
1.3.6.1.4.1.9999.1.3.0
OTLP metrics sent (shortened)
{
  "resourceMetrics": [{
    "resource": { "attributes": [
      { "key": "service.name",
        "value": { "stringValue": "snmp2otel" } }
    ]},
    "scopeMetrics": [{
      "scope": { "name": "snmp2otel" },
      "metrics": [{
        "name": "snmp.1.3.6.1.2.1.1.3.0",
        "unit": "",
        "gauge": { "dataPoints": [{
          "timeUnixNano": "1732820000000000000",
          "asDouble": 483920
        }]}
      }]
    }]
  }]
}

How it works inside

  1. The CLI parser (cli.hpp) reads the target, community string, port, timeout and interval.
  2. The OID file is read once, skipping empty lines, comment lines and non-scalar OIDs.
  3. A hand-written BER encoder (ber.cpp) builds the SNMPv2c GetRequest packet - no external SNMP library.
  4. A UDP round trip to the agent, with a configurable number of retries and a timeout.
  5. The BER decoder parses the response into typed VarBind values (Integer, OctetString, OID, Counter32, Gauge32, TimeTicks, Counter64…).
  6. Numeric types are converted to OTLP gauge metrics and sent in a POST request via cpp-httplib.
  7. The whole thing repeats every -i seconds for as long as the program runs.

Modules

The project keeps headers in include/ and the implementation in src/. Each pair corresponds to one layer on the way from the command line to the HTTP request sent:

ModuleResponsibility
berASN.1/BER encoding and decoding — lengths, integers, octet strings, OIDs, reading TLV elements
cliProcessing options into the Options structure, printing usage
snmpBuilding GetRequest, parsing GetResponse, UDP round trip with retries
otelParsing the endpoint URL, building OTLP JSON, sending it over HTTP
vlogVerbose logging through a single global switch
mainReading the OID file and the main polling loop
tests/Tests for ber, snmp and otel (GoogleTest)
third_party/cpp-httplib and googletest

The order is not random: ber does not know SNMP, snmp does not know OTEL and otel does not know where the values came from. Dependencies point one way, so each layer can be tested on its own.

Data structures

StructureRole
ber::BytesAlias for a byte vector — the raw content of a packet
ber::TLVOne element read as [tag][length][value], including the range of the value in the buffer
ber::ber_errorException for corrupted or unexpected encoding
snmp::VarBindValueTyped value — Integer, OctetString, OID, Counter32, Gauge32, TimeTicks, Counter64
snmp::VarBindA pair of an OID and its value
snmp::PDUThe response body: request-id, error-status, error-index and a list of VarBinds
OptionsConfiguration assembled from the command-line arguments
ParsedUrlThe parsed endpoint — scheme, host, port and path

It is worth noting that you will find almost no classes here - just plain structures and free functions in namespaces. That is intentional: the program has no multiple implementations of the same interface to choose between at run time. It is a straight pipe from one end to the other, and for that a class hierarchy is dead weight. In IPK, where four protocols really do take turns behind a common interface, it came out exactly the other way round.

Design notes

  • Custom BER (de)coding — SNMP packets are ASN.1/BER, so parsing is manual work with bytes, not just a call to a JSON library.
  • Deliberately simple scope — SNMPv2c GET, scalar OIDs, one export format.
  • Fault tolerance — a missing response, an invalid OID or an unreachable collector does not bring the program down; it just logs it and carries on with the next cycle. An exporter that crashes at the first network outage is useless.

Testing

The tests (GoogleTest, the tests/ folder) cover BER encoding and decoding, building and parsing SNMP PDUs and parsing the export endpoint URL.

GitHub