Telegraf

Telegraf is developed by InfluxData and is a plugin-based server for collecting and reporting metrics.
Telegraf has built-in capabilities to capture a variety of metrics, events, and logs directly from the systems it runs on, pulls values from third-party APIs, or even listens to metrics through StatsD and Kafka services.
It also has output plugins to send metrics to a multitude of other services and message queues, including InfluxDB, Graphite, OpenTSDB, Datadog, Librato, Kafka, MQTT, NSQ, and many others.

Telegraf Installation - Debian 9

To install the service/server, we run:
apt install telegraf

Before editing an existing configuration file, it is advisable to make a backup of it:
cp /etc/telegraf/telegraf.conf /etc/telegraf/telegraf.conf.bak
With your preferred editor, we edit the configuration file:
nano -w /etc/telegraf/telegraf.conf

After which we edit the file by removing comments and writing the necessary information for connecting and collecting metrics from the mosquitto server, as follows:

...
# # Read metrics from MQTT topic(s)
[[inputs.mqtt_consumer]]
#   ## MQTT broker URLs to be used. The format should be scheme://host:port,
#   ## scheme can be tcp, ssl, or ws.
servers = ["tcp://localhost:1883"]
#
#   ## QoS policy for messages
#   ##   0 = at most once
#   ##   1 = at least once
#   ##   2 = exactly once
#   ##
#   ## When using a QoS of 1 or 2, you should enable persistent_session to allow
#   ## resuming unacknowledged messages.
qos = 0
#
#   ## Connection timeout for initial connection in seconds
#   connection_timeout = "30s"
#
#   ## Maximum messages to read from the broker that have not been written by an
#   ## output.  For best throughput set based on the number of metrics within
#   ## each message and the size of the output's metric_batch_size.
#   ##
#   ## For example, if each message from the queue contains 10 metrics and the
#   ## output metric_batch_size is 1000, setting this to 100 will ensure that a
#   ## full batch is collected and the write is triggered immediately without
#   ## waiting until the next flush_interval.
#   # max_undelivered_messages = 1000
#
#   ## Topics to subscribe to
   topics = [
     "telegraf/host01/cpu",
     "telegraf/+/mem",
     "sensors/#",
   ]
#
#   # if true, messages that can't be delivered while the subscriber is offline
#   # will be delivered when it comes back (such as on service restart).
#   # NOTE: if true, client_id MUST be set
#   persistent_session = false
#   # If empty, a random client ID will be generated.
   client_id = "telegraf"
#
#   ## username and password to connect to the MQTT server.
 username = "telegraf"
 password = "parola"
#
#   ## Optional TLS Config
#   # tls_ca = "/etc/telegraf/ca.pem"
#   # tls_cert = "/etc/telegraf/cert.pem"
#   # tls_key = "/etc/telegraf/key.pem"
#   ## Use TLS but skip chain & host verification
#   # insecure_skip_verify = false
#
#   ## Data format to consume.
#   ## Each data format has its own unique set of configuration options, read
#   ## more about them here:
#   ## https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_INPUT.md
   data_format = "influx"
...

The user telegraf must have access to the MQTT server, for this we run the command:
mosquitto_passwd /etc/mosquitto/passwd telegraf
after which we will enter the password above, and for the changes to take effect we will restart the mosquitto service/server:
systemctl reload mosquitto

We will continue with editing the configuration file:
nano -w /etc/telegraf/telegraf.conf

After which we edit the file by removing comments and writing the necessary information to connect to the influxdb server, as follows:

  ...
  # Configuration for sending metrics to InfluxDB
[[outputs.influxdb]]
  ## The full HTTP or UDP URL for your InfluxDB instance.
  ##
  ## Multiple URLs can be specified for a single cluster, only ONE of the
  ## urls will be written to each interval.
  # urls = ["unix:///var/run/influxdb.sock"]
  # urls = ["udp://127.0.0.1:8089"]
   urls = ["http://127.0.0.1:8086"]

  ## The target database for metrics; will be created as needed.
   database = "espduino"

  ## If true, no CREATE DATABASE queries will be sent.  Set to true when using
  ## Telegraf with a user without permissions to create databases or when the
  ## database already exists.
  # skip_database_creation = false

  ## Name of existing retention policy to write to.  Empty string writes to
  ## The default retention policy.  Only takes effect when using HTTP.
  # retention_policy = ""

  ## Write consistency (clusters only), can be: "any", "one", "quorum", "all".
  ## Only takes effect when using HTTP.
   write_consistency = "any"

  ## Timeout for HTTP messages.
   timeout = "5s"

  ## HTTP Basic Auth
   username = "telegraf"
   password = "parola"

  ## HTTP User-Agent
  # user_agent = "telegraf"

  ## UDP payload size is the maximum packet size to send.
  # udp_payload = "512B"

  ## Optional TLS Config for use on HTTP connections.
  # tls_ca = "/etc/telegraf/ca.pem"
  # tls_cert = "/etc/telegraf/cert.pem"
  # tls_key = "/etc/telegraf/key.pem"
  ## Use TLS but skip chain & host verification
  # insecure_skip_verify = false

  ## HTTP Proxy override, if unset values the standard proxy environment
  ## Variables are consulted to determine which proxy, if any, should be used.
  # http_proxy = "http://corporate.proxy:3128"

  ## Additional HTTP headers
  # http_headers = {"X-Special-Header" = "Special-Value"}

  ## HTTP Content-Encoding for write request body can be set to "gzip" to
  ## compress the body or "identity" to apply no encoding.
  # content_encoding = "identity"

  ## When true, Telegraf will output unsigned integers as unsigned values,
  ## i.e.: "42u".  You will need a version of InfluxDB supporting unsigned
  ## integer values.  Enabling this option will result in field type errors if
  ## existing data has been written.
  # influx_uint_support = false
  ...

Finally, restart the Telegraf service:
systemctl reload telegraf