Log-Dateien erleichtern einem das Leben ungemein, ja sie ermöglichen erst das Debuggen. Umso wichtiger ist es, diese Dateien auch regelmäßig auszuwerten. Aber mal Hand aufs Herz, bei einem kleinen IoT-Gerät wird das keiner tun. rSyslog bietet die Möglichkeit, Logeinträge entgegenzunehmen und weiter zu verarbeiten. Mit Telegraf können wir sie dann in einer InfluxDB speichern und in Grafana darstellen.
Dieses Tutorial baut auf diesem hier auf. Wir haben also schon Docker-Container mit u.a. Telegraf, InfluxDB und Grafana zu laufen. Diesmal werden wir uns unseren eigenes Docker-Image bauen, in dem dann rSyslog läuft und Log-Einträge entgegennimmt und an Telegraf weiterleitet. Aber Schritt für Schritt.
Als erstes aktivieren wir in Telegraf das Plugin, das die Log-Einträge von rSyslog entgegennimmt. Dazu ändern wir die Datei telegraf.conf (wir haben sie letztes mal hier abgelegt: /home/pi/docker/telegraf/telegraf.conf
). In Telegraf haben wir bereits ein Plugin dafür, wir müssen es nur aktivieren und ihm mitteilen, auf welchem Port es lauschen soll:
[[inputs.syslog]]
server = "udp://0.0.0.0:6514"
Als nächstes bauen wir unser Image. Dazu legen wir uns zunächst im Ordner docker
den Ordner rsyslog
an und darin die Datei dockerfile
(ohne Endung):
pi@raspberrypi:~/docker $ mkdir rsyslog
pi@raspberrypi:~/docker $ cd rsyslog
pi@raspberrypi:~/docker/rsyslog $ nano dockerfile
Das dockerfile
bekommt dann folgenden Inhalt: Wir starten mit dem Base-Image debian (also einer Linux-Distribution) und installieren darin rsyslog. Wenn wir den Container starten, starten wir rsyslog im Interactive-Mode:
FROM debian
RUN apt update
RUN apt install rsyslog -y
ENTRYPOINT ["rsyslogd", "-n"]
Das Image bauen wir dann mit:
pi@raspberrypi:~/docker/rsyslog $ sudo docker build . -t rsyslog
Das kann einen Moment dauern.
Anschließend legen wir uns eine Konfigurationsdatei für rsyslog an. Meiner Meinung nach ist das Format nicht einfach zu verstehen, deswegen mache ich hier auch nur eine Minimalkonfiguration. Wir legen uns die Datei rsyslog.conf
an:
pi@raspberrypi:~/docker/rsyslog $ nano rsyslog.conf
Diese bekommt dann den Inhalt:
module(load="imudp")
module(load="imtcp")
module(load="imuxsock")
input(type="imudp" port="514")
input(type="imtcp" port="514")
*.* @telegraf:6514;RSYSLOG_SyslogProtocol23Format
Ich versuche mal zu erklären, was passiert:
Anschließend müssen wir noch unser Docker-Compose-File anpassen:
pi@raspberrypi:~/docker/rsyslog $ cd ..
pi@raspberrypi:~/docker $ nano docker-compose.yml
Wir geben den Port 6514 des Telegraf-Containers frei und nehmen den rsyslog-Container neu auf:
...
telegraf:
...
ports:
- '6514:6514'
...
rsyslog:
image: 'rsyslog'
ports:
- '514:514'
- '514:514/udp'
volumes:
- '/home/pi/docker/rsyslog/rsyslog.conf:/etc/rsyslog.conf'
depends_on:
- 'telegraf'
container_name: 'rsyslog'
Jetzt können wir die Docker-Container neu starten:
pi@raspberrypi:~/docker $ sudo docker-compose down
pi@raspberrypi:~/docker $ sudo docker-compose up -d
Zum testen können wir einfach einmal eine Log-Nachricht erzeugen:
pi@raspberrypi:~/docker $ echo "81 <165>1 2003-08-24T05:14:15.00Z localhost myproc 0 - It's time to make the do-nuts" | nc 127.0.0.1 514&
Um sie zu sehen, erstellen wir in Grafana ein neues Dashboard:
Um die Log-Level jetzt noch farbig darzustellen, fügen wir Thresholds hinzu und um die Log-Level als Klartext auszugeben fügen wir Overrides hinzu:
Was welches Log-Level bedeutet, ist im ietf-syslog-protocol-23 definiert:
...
Numerical Severity
Code
0 Emergency: system is unusable
1 Alert: action must be taken immediately
2 Critical: critical conditions
3 Error: error conditions
4 Warning: warning conditions
5 Notice: normal but significant condition
6 Informational: informational messages
7 Debug: debug-level messages
...
Um unterschiedliche Log-Level zu erzeugen, können wir folgende Befehle ausführen:
pi@raspberrypi:~/docker $ echo "81 <160>1 2003-08-24T05:14:15.00Z localhost myproc 0 - It's time to make the do-nuts" | nc 127.0.0.1 514&
pi@raspberrypi:~/docker $ echo "81 <161>1 2003-08-24T05:14:15.00Z localhost myproc 0 - It's time to make the do-nuts" | nc 127.0.0.1 514&
pi@raspberrypi:~/docker $ echo "81 <162>1 2003-08-24T05:14:15.00Z localhost myproc 0 - It's time to make the do-nuts" | nc 127.0.0.1 514&
pi@raspberrypi:~/docker $ echo "81 <163>1 2003-08-24T05:14:15.00Z localhost myproc 0 - It's time to make the do-nuts" | nc 127.0.0.1 514&
pi@raspberrypi:~/docker $ echo "81 <164>1 2003-08-24T05:14:15.00Z localhost myproc 0 - It's time to make the do-nuts" | nc 127.0.0.1 514&
pi@raspberrypi:~/docker $ echo "81 <165>1 2003-08-24T05:14:15.00Z localhost myproc 0 - It's time to make the do-nuts" | nc 127.0.0.1 514&
pi@raspberrypi:~/docker $ echo "81 <166>1 2003-08-24T05:14:15.00Z localhost myproc 0 - It's time to make the do-nuts" | nc 127.0.0.1 514&
pi@raspberrypi:~/docker $ echo "81 <167>1 2003-08-24T05:14:15.00Z localhost myproc 0 - It's time to make the do-nuts" | nc 127.0.0.1 514&
Das Ergebnis sieht dann so aus:
Über die Farben kann man nochmal nachdenken. Gelb für Error ist mir eigentlich ein bisschen zu wenig, aber das kann man ja schnell anpassen.
Logs vom ESP32 zu senden ist auch einfach. Ich nutze hierzu MicroPython:
Später werde ich noch ein Modul schreiben, mit dem ich die Lognachrichten komfortabler erzeugen kann.
Eydam-Prototyping
Saccasner Straße 19
03096 Schmogrow-Fehrow
Germany