Skip to content

Telegraf container setup with syslog receiver input plugin

Introduction

I discovered that a smart power strip could communicate with a syslog server, but couldn’t find documentation for this feature. This provided an excellent opportunity to set up a Telegraf container as a syslog server to see what the device would send. This setup could potentially be used for the Unifi Network Application as well.

Telegraf from InfluxData is a server agent for collecting metrics from sensors and systems, and writing them to InfluxDB or other outputs.

The plan was as follows:

  1. Adjust the firewall to allow the IoT device to communicate from the IoT VLAN to the Telegraf Syslog Plugin via UDP port 6514 in the Server VLAN.
  2. Set up Telegraf with the syslog plugin as input and a file as output in influx format (InfluxDB can be set as output later).
  3. Test the syslog server via the command line with tcpdump and logger, as the IoT device initially did not write anything to the syslog server.
  4. View the Telegraf output in influx data format.

I won’t describe adding the firewall rule, but you can read more about the firewall setup and the Unifi Network Application here.

Setup

  1. Create the folders needed by the container

    Run the following commands inside your home folder:

    # Open your terminal application
    cd ~
    mkdir -p telegraf/config
    cd telegraf
  2. Create the configuration

    Save the following script as config/telegraf.conf:

    config/telegraf.conf
    # To create this script use your text editor application, for example Nano
    # The syslog plugin listens for syslog messages transmitted over a Unix Domain socket
    [[inputs.syslog]]
    ## Protocol, address and port to host the syslog receiver.
    server = "udp://:6514"
    # Send telegraf metrics to file(s)
    [[outputs.file]]
    ## Files to write to, "stdout" is a specially handled file.
    files = ["stdout", "/tmp/metrics.out"]
    ## Data format to output.
    data_format = "influx"
  3. Create the script needed to run the container

    Save the following script as telegraf_run.sh:

    telegraf_run.sh
    # To create this script use your text editor application, for example Nano
    docker run -d \
    --name=telegraf \
    --hostname=telegraf \
    -p 6514:6514/udp \
    -v $PWD/config/telegraf.conf:/etc/telegraf/telegraf.conf:ro \
    -e TZ=Europe/Amsterdam \
    --restart unless-stopped \
    telegraf
    # IMPORTANT: Please read the instructions below
    Instructions:
    • Optional Replace docker with podman if needed
    • Required Replace port number 6514 (on the left side of :) with a port number that is available if needed
    • Optional Replace $PWD/config/telegraf.conf with the location of your Telegraf configuration if needed
    • Required Replace Europe/Amsterdam with your own timezone
  4. Run the script to create the container

    Run the following command:

    # Open your terminal application
    sudo sh telegraf_run.sh

    The image telegraf is automatically pulled and the container is created.

  5. Check the results

    If needed you can check if the container is running properly.

    Then I configured the IoT device with the syslog server:

    • IP address of Telegraf (the docker host)
    • Port 6514 (udp)

    Make sure the container is started and run the following commands:

    # Open your terminal application
    sudo docker exec -it telegraf /bin/bash
    # Now you are in the container, and you can view the output with:
    cat /tmp/metrics.out

    For me the file was empty and this was because the IoT device had not sent anything. So I tested the Telegraf syslog server with the logger command with which you can send syslog messages to the server yourself. The output of metrics.out then looks like this:

    /tmp/metrics.out
    syslog,appname=admin,facility=user,host=telegraf,hostname=vm,severity=notice timestamp=1657970416034287000i,message="test",version=1i,timeQuality_tzKnown="1",timeQuality_isSynced="1",timeQuality_syncAccuracy="539010",facility_code=1i,severity_code=5i 1657970416034809769

    Now everything works the output can be modified to InfluxDB.

Favorites

Comments

    No comments found for this note.

    Join the discussion for this note on Github. Comments appear on this page instantly.

    Copyright 2021- Fiction Becomes Fact