oitc/snmp2mqtt

By oitc

Updated 2 months ago

Read device information via SNMP and publish them in a MQTT topic as json formatted string.

Image
Monitoring & Observability

98

Quick reference

Maintained by: Michael Oberdorf IT-Consulting

Source code: GitHub

Container image: DockerHub

Supported tags and respective Dockerfile links

Summary

The container image is based on Alpine Linux with python3 interpreter. The tool is written in python and connects to a MQTT server. After that, it loops over the configured SNMP endpoints and graps the configured OIDs. The collected SNMP data will be transformed as a json object and published to the given MQTT topic.

Configuration

Container configuration

The container reads some configuration via environment variables.

Environment variable nameDescriptionRequiredDefault value
CONFIG_FILEThe configuration file that contains the connection parameters to MQTT and the SNMP endpoints.OPTIONAL/app/etc/snmp2mqtt.json

Configuration file

The path and filename to the general configuration file can be set via environment variable CONFIG_FILE. By default, the script will use /app/etc/snmp2mqtt.json.

Inside this file we need to configure the MQTT server connection parameters and the SNMP endpoint connection parameters.

Example
{
  "DEBUG": true,
  "devices": [
    "EXAMPLE"
  ],
  "mqtt": {
    "client_id": "snmp2mqtt",
    "user": "snmp2mqtt",
    "password": "myMQTTPassword",
    "server": "test.mosquitto.org",
    "port": 1883,
    "tls": false,
    "retain": true,
    "hostname_validation": false,
    "protocol_version": 3
    },
  "EXAMPLE": {
    "server": "127.0.0.1",
    "port": 161,
    "snmpCredentials": {
      "user": "snmpUser",
      "password": "snmpPassword",
      "algorithm": "MD5"
    },
    "snmpEncryption": {
      "password": "snmpEncryptionKey",
      "algorithm": "DES"
    },
    "snmpOID2Attribute": {
      "1.3.6.1.4.1.2021.4.3.0": "memTotalSwap"
    },
    "mqttTopic": "de/oberdorf-itc/devices/EXAMPLE"
  }
}
Field description
FieldTypeDescription
DEBUGBooleanEnable debug output on stdout
devicesArrayA List of names that represent SNMP endpoints. These keywords are later used inside the configuation file.
mqttObjectContains MQTT specific configuration parameters.
mqtt.client_idStringThe MQTT client identifier.
mqtt.userStringThe username to authenticate to the MQTT server.
mqtt.passwordStringThe password to authenticate to the MQTT server.
mqtt.serverStringIP address or FQDN of the MQTT server.
mqtt.portStringThe TCP port number of the MQTT server.
mqtt.tlsBooleanIf a TLS encrpted communication should be established or not.
mqtt.hostname_validationBooleanValidate the hostname from the servercertificate or not.
mqtt.protocol_versionIntegerThe MQTT protocol version. Can be 3 (for MQTTv311) or 5 (for MQTTv5).
<device>ObjectContains the SNMP endpoint specific configuration parametes.
<device>.serverStringIP address or FQDN of the SNMP endpoint.
<device>.portIntegerThe TCP port number of the SNMP endpoint.
<device>.snmpCredentialsObjectThe SNMP endpoint credentials for authentication.
<device>.snmpCredentials.userStringThe username to authenticate to the SNMP endpoint.
<device>.snmpCredentials.passwordStringThe password to authenticate to the SNMP endpoint.
<device>.snmpCredentials.algorithmStringThe algorithm to use when authenticating to the SNMP endpoint (supported: MD5 or SHA).
<device>.snmpEncryptionObjectThe SNMP endpoint data encrption parameters.
<device>.snmpEncryption.passwordStringThe SNMP data encrption password.
<device>.snmpEncryption.algorithmStringThe SNMP data encrption algorithm (supported: DES).
<device>.snmpOID2AttributeObjectList of SNMP OIDs and a human readable name.
<device>.snmpOID2Attribute.<OID>StringA human readable translation of the given OID.
<device>.mqttTopicStringthe MQTT topic to publish the colected device data.

Running the container image

docker run --rm -v ./myConfig.json:/app/etc/snmp2mqtt.json:ro oitc/snmp2mqtt:latest

Docker compose configuration

version: '3.8'

services:
  snmp2mqtt:
    container_name: snmp2mqtt
    image: oitc/snmp2mqtt:1.0.0
    restart: "no"
    user: 3917:3917
    volumes:
      - /srv/docker/snmp2mqtt/etc/snmp2mqtt.json:/app/etc/snmp2mqtt.json:ro

Donate

I would appreciate a small donation to support the further development of my open source projects.

Donate with PayPal

License

Copyright (c) 2023-2024 Michael Oberdorf IT-Consulting

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Docker Pull Command

docker pull oitc/snmp2mqtt