commit 9951c1ced5ebde2b7164cf305387bdc7d07c2baa Author: Wolfgang Hottgenroth Date: Mon Feb 10 12:37:46 2025 +0100 initial diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..dad78d1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +build +.DS_Store +tsm +src/tsm/tsm diff --git a/.woodpecker.yml b/.woodpecker.yml new file mode 100644 index 0000000..1f2c6c3 --- /dev/null +++ b/.woodpecker.yml @@ -0,0 +1,80 @@ +steps: + build: + image: golang:1.22.5-alpine3.20 + commands: + - GOPATH=/woodpecker/go + - cd src/tsm + - go mod tidy + - go build -a -installsuffix nocgo -o tsm tsm.go + - cp tsm ../.. + when: + - event: [push, tag] + + scan: + image: quay.io/wollud1969/woodpecker-helper:0.5.1 + environment: + TRIVY_TOKEN: + from_secret: trivy_token + TRIVY_URL: + from_secret: trivy_url + DTRACK_API_KEY: + from_secret: dtrack_api_key + DTRACK_API_URL: + from_secret: dtrack_api_url + commands: + - export GOPATH=/woodpecker/go # the export is required, otherwise trivy will not consider the variable + - HOME=/home/`id -nu` + - TAG="${CI_COMMIT_TAG:-$CI_COMMIT_SHA}" + - | + trivy fs \ + --server $TRIVY_URL \ + --token $TRIVY_TOKEN \ + --format cyclonedx \ + --scanners license \ + --output /tmp/sbom.xml \ + . + - cat /tmp/sbom.xml + - | + curl -X "POST" \ + -H "Content-Type: multipart/form-data" \ + -H "X-Api-Key: $DTRACK_API_KEY" \ + -F "autoCreate=true" \ + -F "projectName=$CI_REPO" \ + -F "projectVersion=$TAG" \ + -F "bom=@/tmp/sbom.xml"\ + "$DTRACK_API_URL/api/v1/bom" + when: + - event: [push, tag] + + dockerize: + image: plugins/kaniko + settings: + repo: ${FORGE_NAME}/${CI_REPO} + registry: + from_secret: container_registry + tags: latest,${CI_COMMIT_SHA},${CI_COMMIT_TAG} + username: + from_secret: container_registry_username + password: + from_secret: container_registry_password + dockerfile: Dockerfile + when: + - event: [push, tag] + + deploy: + image: quay.io/wollud1969/woodpecker-helper:0.5.1 + environment: + KUBE_CONFIG_CONTENT: + from_secret: kube_config + ENCRYPTION_KEY: + from_secret: encryption_key + MD5_CHECKSUM: + from_secret: secrets_checksum + commands: + - export IMAGE_TAG=$CI_COMMIT_TAG + - printf "$KUBE_CONFIG_CONTENT" > /tmp/kubeconfig + - export KUBECONFIG=/tmp/kubeconfig + - ./deployment/deploy.sh + when: + - event: tag + diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..633e4e0 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,9 @@ +FROM scratch + +ENV TSM_MQTT_CONF "" + +COPY tsm ./ +ENTRYPOINT ["./tsm"] + + + diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..f3563f4 --- /dev/null +++ b/LICENSE @@ -0,0 +1,23 @@ +MIT License + +Copyright (c) 2019 Daniel Chote + +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. + + diff --git a/README.md b/README.md new file mode 100644 index 0000000..95c20c5 --- /dev/null +++ b/README.md @@ -0,0 +1,64 @@ +# snmp-mqtt + +This project is directly derived from https://github.com/dchote/snmp-mqtt + +A simple go app that reads SNMP values and publishes it to the specified MQTT endpoint at the specified interval. + +In constrast to the original version, all configuration must be provided via an environment variable containing +a JSON string. + +The MQTT configuration is part of this JSON string. + +The topic has been moved to the MQTT configuration and there is now only one topic for all endpoints and variables. + +The published message on MQTT looks like this: + +``` +{"device":"172.16.3.1","label":"router","variables":{"lan-in":{"label":"lan-in","variable":".1.3.6.1.2.1.31.1.1.1.6.2","value":"979673705579"},"lan-out":{"label":"lan-out","variable":".1.3.6.1.2.1.31.1.1.1.10.2","value":"1813410276168"},"wan-in":{"label":"wan-in","variable":".1.3.6.1.2.1.31.1.1.1.6.4","value":"83591215399"},"wan-out":{"label":"wan-out","variable":".1.3.6.1.2.1.31.1.1.1.10.4","value":"83741895468"}}} +``` + +``` +export SNMP_MQTT_CONF=$(cat config.json) +./smq +``` + +An example config.json file: +``` +{ + "mqtt": { + "broker": "mqtt://172.23.1.102:1883", + "tlsEnable": "false", + "topic": "snmp" + }, + "interval": 10, + "snmpEndpoints": [ + { + "endpoint": "172.16.3.1", + "label": "router", + "community": "public", + "oidTopics": [ + { + "oid": ".1.3.6.1.2.1.31.1.1.1.6.4", + "label": "wan-in", + "diff": "true" + }, + { + "oid": ".1.3.6.1.2.1.31.1.1.1.10.4", + "label": "wan-out", + "diff": "true" + }, + { + "oid": ".1.3.6.1.2.1.31.1.1.1.6.2", + "label": "lan-in", + "diff": "true" + }, + { + "oid": ".1.3.6.1.2.1.31.1.1.1.10.2", + "label": "lan-out", + "diff": "true" + } + ] + } + ] +} +``` diff --git a/deployment/config.json b/deployment/config.json new file mode 100644 index 0000000..bad8350 --- /dev/null +++ b/deployment/config.json @@ -0,0 +1,14 @@ +{ + "mqtt": { + "broker": "mqtt://emqx01-anonymous-cluster-internal.broker.svc.cluster.local:1883", + "tlsEnable": "false", + "topic": "tsm" + }, + "interval": 10, + "servers": [ + { + "name": "172.16.13.10", + "label": "david" + } + ] +} diff --git a/deployment/deploy-yml.tmpl b/deployment/deploy-yml.tmpl new file mode 100644 index 0000000..0f429ff --- /dev/null +++ b/deployment/deploy-yml.tmpl @@ -0,0 +1,27 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: tsm-mqtt + namespace: homea + labels: + app: tsm-mqtt + annotations: + secret.reloader.stakater.com/reload: tsmsnmp-mqtt-conf +spec: + replicas: 1 + selector: + matchLabels: + app: tsm-nmqtt + template: + metadata: + labels: + app: tsm-nmqtt + spec: + containers: + - name: tsm-nmqtt + image: %IMAGE% + envFrom: + - configMapRef: + name: tsm-mqtt-conf + + diff --git a/deployment/deploy.sh b/deployment/deploy.sh new file mode 100755 index 0000000..bd6ab74 --- /dev/null +++ b/deployment/deploy.sh @@ -0,0 +1,34 @@ +#!/bin/bash + +if [ "$IMAGE_TAG" == "" ]; then + echo "Make sure IMAGE_TAG is set" + exit 1 +fi + + +IMAGE_NAME=gitea.hottis.de/wn/timeserver-monitoring +NAMESPACE=homea +DEPLOYMENT_DIR=$PWD/deployment +CONFIG_FILE=config.json + +pushd $DEPLOYMENT_DIR > /dev/null + +kubectl create namespace $NAMESPACE \ + --dry-run=client \ + -o yaml | \ + kubectl -f - apply + +kubectl create configmap tsm-mqtt-conf \ + --from-literal=TSM_MQTT_CONF="`cat $CONFIG_FILE`" \ + --dry-run=client \ + -o yaml \ + --save-config | \ + kubectl apply -f - -n $NAMESPACE + + +cat $DEPLOYMENT_DIR/deploy-yml.tmpl | \ + sed -e 's,%IMAGE%,'$IMAGE_NAME':'$IMAGE_TAG','g | \ + kubectl apply -f - -n $NAMESPACE + +popd > /dev/null + diff --git a/deployment/pushconfig.sh b/deployment/pushconfig.sh new file mode 100755 index 0000000..9885a27 --- /dev/null +++ b/deployment/pushconfig.sh @@ -0,0 +1,4 @@ +#!/bin/bash + + +kubectl create configmap tsm-mqtt-conf --from-literal=SNMP_MQTT_CONF="`cat config.json`" --dry-run=client -o yaml --save-config | kubectl apply -f - -n homea