From b2d4ddb2c1e3675db8b242daea726bcb5e27ef82 Mon Sep 17 00:00:00 2001 From: Anton Lundin Date: Sun, 1 Aug 2021 07:26:12 +0200 Subject: [PATCH] Create and enable a snmp trap handling container (#180) --- Dockerfile | 2 +- README.md | 1 + doc/docker/environment-variables.md | 6 ++++ doc/notes/snmptrapd.md | 12 ++++++++ examples/compose/docker-compose.yml | 32 ++++++++++++++++++++ examples/rrdcached-server/docker-compose.yml | 32 ++++++++++++++++++++ examples/traefik/docker-compose.yml | 29 ++++++++++++++++++ rootfs/etc/cont-init.d/04-svc-main.sh | 3 +- rootfs/etc/cont-init.d/07-svc-cron.sh | 3 +- rootfs/etc/cont-init.d/08-svc-snmptrapd.sh | 27 +++++++++++++++++ rootfs/etc/snmp/snmptrapd.conf | 3 ++ 11 files changed, 147 insertions(+), 3 deletions(-) create mode 100644 doc/notes/snmptrapd.md create mode 100644 rootfs/etc/cont-init.d/08-svc-snmptrapd.sh create mode 100644 rootfs/etc/snmp/snmptrapd.conf diff --git a/Dockerfile b/Dockerfile index 2cba619..844ec7d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -131,7 +131,7 @@ RUN apk --update --no-cache add -t build-dependencies \ COPY rootfs / -EXPOSE 8000 514 514/udp +EXPOSE 8000 514 514/udp 162 162/udp VOLUME [ "/data" ] ENTRYPOINT [ "/init" ] diff --git a/README.md b/README.md index 168f5d6..f73c9b6 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,7 @@ If you are interested, [check out](https://hub.docker.com/r/crazymax/) my other * Multi-platform image * [Dispatcher service](doc/docker/environment-variables.md#dispatcher-service) as "sidecar" container * Syslog-ng support through a ["sidecar" container](doc/docker/environment-variables.md#syslog-ng) +* Snmp-trap support through a ["sidecar" container](doc/docker/environment-variables.md#snmptrapd) * Built-in LibreNMS [Weathermap plugin](https://docs.librenms.org/Extensions/Weathermap/) * Ability to add custom Monitoring plugins (Nagios) * Ability to add custom alert templates diff --git a/doc/docker/environment-variables.md b/doc/docker/environment-variables.md index 78a15fe..fc9a051 100644 --- a/doc/docker/environment-variables.md +++ b/doc/docker/environment-variables.md @@ -35,6 +35,12 @@ * `SIDECAR_SYSLOGNG`: Set to `1` to enable sidecar syslog-ng mode for this container (default `0`) +### Snmptrapd + +> :warning: Only used if you enable and run a [sidecar snmptrapd container](../notes/snmptrapd.md) + +* `SIDECAR_SNMPTRAPD`: Set to `1` to enable sidecar snmptrapd mode for this container (default `0`) + ### Database * `DB_HOST`: MySQL database hostname / IP address diff --git a/doc/notes/snmptrapd.md b/doc/notes/snmptrapd.md new file mode 100644 index 0000000..3a3b67d --- /dev/null +++ b/doc/notes/snmptrapd.md @@ -0,0 +1,12 @@ +## Snmptrapd + +If you want to enable snmptrapd, you have to run a "sidecar" container (see snmptrapd service in [docker-compose.yml](../../examples/compose/docker-compose.yml) example) or run a simple container like this : + +```bash +docker run -d --name librenms_snmptrapd \ + --env-file $(pwd)/librenms.env \ + -e SIDECAR_SNMPTRAPD=1 \ + -p 162 -p 162/udp \ + -v librenms:/data \ + librenms/librenms:latest +``` diff --git a/examples/compose/docker-compose.yml b/examples/compose/docker-compose.yml index 5789dd5..cde6eae 100644 --- a/examples/compose/docker-compose.yml +++ b/examples/compose/docker-compose.yml @@ -138,3 +138,35 @@ services: - "REDIS_DB=0" - "SIDECAR_SYSLOGNG=1" restart: always + + snmptrapd: + image: librenms/librenms:latest + container_name: librenms_snmptrapd + hostname: librenms-snmptrapd + cap_add: + - NET_ADMIN + - NET_RAW + depends_on: + - librenms + ports: + - target: 162 + published: 162 + protocol: tcp + - target: 162 + published: 162 + protocol: udp + volumes: + - "./librenms:/data" + env_file: + - "./librenms.env" + environment: + - "TZ=${TZ}" + - "PUID=${PUID}" + - "PGID=${PGID}" + - "DB_HOST=db" + - "DB_NAME=${MYSQL_DATABASE}" + - "DB_USER=${MYSQL_USER}" + - "DB_PASSWORD=${MYSQL_PASSWORD}" + - "DB_TIMEOUT=60" + - "SIDECAR_SNNMPTRAPD=1" + restart: always diff --git a/examples/rrdcached-server/docker-compose.yml b/examples/rrdcached-server/docker-compose.yml index cc52f48..04ecbbe 100644 --- a/examples/rrdcached-server/docker-compose.yml +++ b/examples/rrdcached-server/docker-compose.yml @@ -156,3 +156,35 @@ services: - "REDIS_DB=0" - "SIDECAR_SYSLOGNG=1" restart: always + + snmptrapd: + image: librenms/librenms:latest + container_name: librenms_snmptrapd + hostname: librenms-snmptrapd + cap_add: + - NET_ADMIN + - NET_RAW + depends_on: + - librenms + ports: + - target: 162 + published: 162 + protocol: tcp + - target: 162 + published: 162 + protocol: udp + volumes: + - "./librenms:/data" + env_file: + - "./librenms.env" + environment: + - "TZ=${TZ}" + - "PUID=${PUID}" + - "PGID=${PGID}" + - "DB_HOST=db" + - "DB_NAME=${MYSQL_DATABASE}" + - "DB_USER=${MYSQL_USER}" + - "DB_PASSWORD=${MYSQL_PASSWORD}" + - "DB_TIMEOUT=60" + - "SIDECAR_SNNMPTRAPD=1" + restart: always diff --git a/examples/traefik/docker-compose.yml b/examples/traefik/docker-compose.yml index d2c86c5..4d77cd6 100644 --- a/examples/traefik/docker-compose.yml +++ b/examples/traefik/docker-compose.yml @@ -167,3 +167,32 @@ services: - "REDIS_DB=0" - "SIDECAR_SYSLOGNG=1" restart: always + + snmptrapd: + image: librenms/librenms:latest + container_name: librenms_snmptrapd + hostname: librenms-snmptrapd + depends_on: + - librenms + ports: + - target: 162 + published: 162 + protocol: tcp + - target: 162 + published: 162 + protocol: udp + volumes: + - "./librenms:/data" + env_file: + - "./librenms.env" + environment: + - "TZ=${TZ}" + - "PUID=${PUID}" + - "PGID=${PGID}" + - "DB_HOST=db" + - "DB_NAME=${MYSQL_DATABASE}" + - "DB_USER=${MYSQL_USER}" + - "DB_PASSWORD=${MYSQL_PASSWORD}" + - "DB_TIMEOUT=60" + - "SIDECAR_SNNMPTRAPD=1" + restart: always diff --git a/rootfs/etc/cont-init.d/04-svc-main.sh b/rootfs/etc/cont-init.d/04-svc-main.sh index 829f85f..8271cb5 100644 --- a/rootfs/etc/cont-init.d/04-svc-main.sh +++ b/rootfs/etc/cont-init.d/04-svc-main.sh @@ -30,8 +30,9 @@ DB_TIMEOUT=${DB_TIMEOUT:-60} SIDECAR_DISPATCHER=${SIDECAR_DISPATCHER:-0} SIDECAR_SYSLOGNG=${SIDECAR_SYSLOGNG:-0} +SIDECAR_SNNMPTRAPD=${SIDECAR_SNNMPTRAPD:-0} -if [ "$SIDECAR_DISPATCHER" = "1" ] || [ "$SIDECAR_SYSLOGNG" = "1" ]; then +if [ "$SIDECAR_DISPATCHER" = "1" ] || [ "$SIDECAR_SYSLOGNG" = "1" ] || [ "$SIDECAR_SNNMPTRAPD" = "1" ]; then exit 0 fi diff --git a/rootfs/etc/cont-init.d/07-svc-cron.sh b/rootfs/etc/cont-init.d/07-svc-cron.sh index a05fbe4..508d80b 100644 --- a/rootfs/etc/cont-init.d/07-svc-cron.sh +++ b/rootfs/etc/cont-init.d/07-svc-cron.sh @@ -8,8 +8,9 @@ LIBRENMS_DAILY_SCHEDULE="15 0 * * *" SIDECAR_DISPATCHER=${SIDECAR_DISPATCHER:-0} SIDECAR_SYSLOGNG=${SIDECAR_SYSLOGNG:-0} +SIDECAR_SNNMPTRAPD=${SIDECAR_SNNMPTRAPD:-0} -if [ "$SIDECAR_DISPATCHER" = "1" ] || [ "$SIDECAR_SYSLOGNG" = "1" ]; then +if [ "$SIDECAR_DISPATCHER" = "1" ] || [ "$SIDECAR_SYSLOGNG" = "1" ] || [ "$SIDECAR_SNNMPTRAPD" = "1" ]; then exit 0 fi diff --git a/rootfs/etc/cont-init.d/08-svc-snmptrapd.sh b/rootfs/etc/cont-init.d/08-svc-snmptrapd.sh new file mode 100644 index 0000000..eb6d282 --- /dev/null +++ b/rootfs/etc/cont-init.d/08-svc-snmptrapd.sh @@ -0,0 +1,27 @@ +#!/usr/bin/with-contenv sh + +SIDECAR_SNNMPTRAPD=${SIDECAR_SNNMPTRAPD:-0} +LIBRENMS_SNMP_COMMUNITY=${LIBRENMS_SNMP_COMMUNITY:-librenmsdocker} + +# Continue only if sidecar snmptrapd container +if [ "$SIDECAR_SNNMPTRAPD" != "1" ]; then + exit 0 +fi + +echo ">>" +echo ">> Sidecar snmptrapd container detected" +echo ">>" + +mkdir -p /run/snmptrapd +chown -R librenms. /run/snmptrapd + +sed -ie "s/@LIBRENMS_SNMP_COMMUNITY@/${LIBRENMS_SNMP_COMMUNITY}/" /etc/snmp/snmptrapd.conf + +# Create service +mkdir -p /etc/services.d/snmptrapd +cat > /etc/services.d/snmptrapd/run <