How to build up the IoT data visualization server on Ubuntu with MQTT+TLS, Telegraf, InfluxDB, and Grafana

Jun OKAMURA
4 min readJan 14, 2023

How to install the MQTT+TLS

Base Ubuntu is following;

DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=22.04
DISTRIB_CODENAME=jammy
DISTRIB_DESCRIPTION="Ubuntu 22.04.1 LTS"

Install the following PKGs;

$ sudo apt update
$ sudo apt upgrade

$ sudo apt install openssl
$ sudo apt install openssh-server
$ sudo apt install mosquitto
$ sudo apt install mosquitto-clients

Generate certification files by following the steps;

$ cd /etc/mosquitto
$ sudo chown mosquitto.mosquitto certs
$ sudo chmod 700 certs
$ sudo su
$ cd certs

# Generate CA
$ sudo -u mosquitto openssl genrsa -des3 -out ca.key 2048
$ sudo -u mosquitto openssl req -new -x509 -days 3650 -key ca.key -out ca.crt -subj /emailAddress=admin@localhost/C=US/ST=Tokyo/L=Meguro/O=CAmaster/OU=test/CN=localhost

# Generate Server Key & CSR
$ sudo -u mosquitto openssl genrsa -out server.key 2048
$ sudo -u mosquitto openssl req -new -out server.csr -key server.key -subj /emailAddress=admin@localhost/C=JP/ST=Nagoya/L=Innovation/O=GIServer/OU=test/CN=localhost

# Certified by CA
$ sudo -u mosquitto openssl x509 -req -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out server.crt -days 3650

$ ls -al
-rw-r--r-- 1 root root 130 Feb 7 2022 README
-rw-rw-r-- 1 mosquitto mosquitto 1472 Jan 13 13:17 ca.crt
-rw------- 1 mosquitto mosquitto 1854 Jan 13 13:15 ca.key
-rw-rw-r-- 1 mosquitto mosquitto 1359 Jan 13 13:18 server.crt
-rw-rw-r-- 1 mosquitto mosquitto 1078 Jan 13 13:17 server.csr
-rw------- 1 mosquitto mosquitto 1704 Jan 13 13:17 server.key

Configured /etc/mosquitto/conf.d

$ sudo vi /etc/mosquitto/mosquitto.conf

# Put following at the tail.
# -----
listener 8883
certfile /etc/mosquitto/certs/server.crt
keyfile /etc/mosquitto/certs/server.key
cafile /etc/mosquitto/certs/ca.crt
allow_anonymous true
#-----

Start mosquitto service

$ sudo systemctl enable mosquitto
$ sudo systemctl start mosquitto
$ sudo systemctl status mosquitto
● mosquitto.service - Mosquitto MQTT Broker
Loaded: loaded (/lib/systemd/system/mosquitto.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2023-01-13 15:02:10 JST; 20h ago
Docs: man:mosquitto.conf(5)
man:mosquitto(8)
Main PID: 1484 (mosquitto)
Tasks: 1 (limit: 9236)
Memory: 3.0M
CPU: 59.694s
CGroup: /system.slice/mosquitto.service
└─1484 /usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf

Test

$ cd /etc/mosquitto/certs
$ mosquitto_pub -d -h localhost-t "topic/test" -m "hello world" --cafile ./ca.crt -p 8883
Client (null) sending CONNECT
Client (null) received CONNACK (0)
Client (null) sending PUBLISH (d0, q0, r0, m1, 'topic/test', ... (11 bytes))
Client (null) sending DISCONNECT

# Recieve MQTT message "hellow world" by another terminal then it's OK

$ mosquitto_sub -h db.greeninnov.org -t "topic/test" --cafile ./ca.crt -p 8883
hello world

How to install the InfluxDB

Install the following PKGs;

$ sudo apt install influxdb influxdb-client 
$ cd /etc/influxdb

# Edit influxdb.conf

$ diff influxdb.conf influxdb.conf_orig
#-----
220c220
< enabled = true
---
> # enabled = true
226c226
< auth-enabled = false
---
> # auth-enabled = false
#-----

Start InfluxDB service

$ sudo systemctl enable influxdb
$ sudo systemctl start influxdb
$ sudo systemctl status influxdb
● influxdb.service - InfluxDB is an open-source, distributed, time series database
Loaded: loaded (/lib/systemd/system/influxdb.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2023-01-13 15:18:20 JST; 5min ago
Docs: man:influxd(1)
Main PID: 1863 (influxd)
Tasks: 10 (limit: 9236)
Memory: 7.8M
CPU: 5.547s
CGroup: /system.slice/influxdb.service
└─1863 /usr/bin/influxd -config /etc/influxdb/influxdb.conf

How to install the Telegraf

Install the following PKGs;

$ sudo apt install telegraf

$ cd /etc/telegraf
$ sudo cp ../mosquitto/certs/ca.crt .

# Edit telegraf.conf

$ diff telegraf.conf telegraf.conf_orig

#-----
108c108
< [[outputs.influxdb]]
---
> #[[outputs.influxdb]]
115c115
< urls = ["http://127.0.0.1:8086"]
---
> # urls = ["http://127.0.0.1:8086"]
119c119
< database = "telegraf"
---
> # database = "telegraf"
8194c8194
< [[inputs.mqtt_consumer]]
---
> # [[inputs.mqtt_consumer]]
8200c8200
< servers = ["ssl://localhost:8883"]
---
> # servers = ["tcp://127.0.0.1:1883"]
8207d8206
< topics = ["#",]
---
8243c8242
< tls_ca = "/etc/telegraf/ca.crt"
---
> # # tls_ca = "/etc/telegraf/ca.pem"
8247c8246
< insecure_skip_verify = true
---
> # # insecure_skip_verify = false
8252c8251
< data_format = "json"
---
> # data_format = "influx"

Start Telegraf service

$ sudo systemctl enable telegraf
$ sudo systemctl start telegraf
$ $ sudo systemctl status telegraf.service
● telegraf.service - The plugin-driven server agent for reporting metrics into InfluxDB
Loaded: loaded (/lib/systemd/system/telegraf.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2023-01-13 16:02:50 JST; 1min 42s ago
Docs: https://github.com/influxdata/telegraf
Main PID: 3512 (telegraf)
Tasks: 10 (limit: 9236)
Memory: 61.5M
CPU: 2.323s
CGroup: /system.slice/telegraf.service
└─3512 /usr/bin/telegraf -config /etc/telegraf/telegraf.conf -config-directory /etc/telegraf/telegraf.d

How to install the Grafana

Install the following PKGs;

$ sudo apt-get install -y apt-transport-https
$ sudo apt-get install -y software-properties-common wget
$ sudo wget -q -O /usr/share/keyrings/grafana.key https://packages.grafana.com/gpg.key

$ echo "deb [signed-by=/usr/share/keyrings/grafana.key] https://apt.grafana.com stable main" | sudo tee -a /etc/apt/sources.list.d/grafana.list
$ sudo apt update
$ sudo apt install grafana
$ sudo systemctl daemon-reload

Start Grafana service

$ sudo systemctl enable grafana-server.service 
$ sudo systemctl start grafana-server.service
$ sudo systemctl status grafana-server.service
● grafana-server.service - Grafana instance
Loaded: loaded (/lib/systemd/system/grafana-server.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2023-01-13 16:11:06 JST; 7s ago
Docs: http://docs.grafana.org
Main PID: 4905 (grafana-server)
Tasks: 16 (limit: 9236)
Memory: 50.8M
CPU: 4.887s
CGroup: /system.slice/grafana-server.service
└─4905 /usr/sbin/grafana-server --config=/etc/grafana/grafana.ini --pidfile=/run/grafana/grafana-server.pid --packaging=deb cfg:default.paths.logs=/var/log/grafana cfg:default.paths.data=/>

DONE and Enjoy!

--

--