Show part of the logfiles on your sitemap

Every log-nerd would want to display some contents of their logs in the sitemap right? :wink:
This will require the exec-binding to be installed…

In this example I will show a part of the events.log.
Start by making a shell-script that you store somewhere containing:

#!/bin/bash

echo '<META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=utf-8">' > /etc/openhab2/html/events.html

echo "<html>" >> /etc/openhab2/html/events.html
echo "<Body bgcolor="#303030">" >> /etc/openhab2/html/events.html
echo "<pre style='color:white; width:100%'>" >> /etc/openhab2/html/events.html
cat /var/log/openhab2/events.log | tail -n30 >> /etc/openhab2/html/events.html
echo "</pre>" >> /etc/openhab2/html/events.html
echo "</Body>" >> /etc/openhab2/html/events.html
echo "</html>" >> /etc/openhab2/html/events.html

I call my shell script ev.sh and its stored in /etc/openhab2/kommandon/ (this is a folder I made myself just to store shell scripts in…)

Next I make a thing-file containing:

Thing exec:command:ev [command="/etc/openhab2/kommandon/ev.sh", interval=60, timeout=5, autorun=false]

This will update the html-file once a minute…

Next add this to your sitemap:

 Webview url="/static/events.html" height=16

This will show the part of the logfile on your sitemap…

1 Like

Very cool, that was something on my list as well. I quick and dirty tried to imlement the logtail from @illnesse from this thread (Hacking BasicUI: My current Theme / OH2 Setup *Update with Repo*) and copied & modified his script and html, but unfortunately had some issues with the html accessing the data from the log. As I just had a few minutes time, I left it then.

I will copy your one and if I have the time try to make a structured html page. Well hopefully some time…:smiley:

hi =)
i tried your scrip but got this error

2019-10-18 20:07:58.492 [ERROR] [hab.binding.exec.handler.ExecHandler] - An exception occurred while executing '/etc/openhab2/kommandon/ev.sh' : 'Cannot run program "/etc/openhab2/kommandon/ev.sh": error=2, No such file or directory'

running by console results same
sudo: unable to execute /etc/openhab2/kommandon/ev.sh: No such file or directory

but file exist
[20:12:04] openhabian@openHABianPi:~$ dir /etc/openhab2/kommandon/
ev.sh

any ideas ?

Do on terminal:
cd /etc/openhab2/kommandon/

sudo chmod 774 ev.sh
Sudo ./ev.sh

still the same :frowning:

[20:53:04] openhabian@openHABianPi:~$ cd /etc/openhab2/kommandon
[20:53:30] openhabian@openHABianPi:/etc/openhab2/kommandon$ sudo chmod 774 ev.sh
[20:53:46] openhabian@openHABianPi:/etc/openhab2/kommandon$ sudo ./ev.sh
: No such file or directory

I would think its a problem with ownership/rights…openhab run by default as user/group openhab…

So the files you want to run needs to be owned by openhab (

chown openhab.openhab ev.sh

why does it not refresh every minute ?
If I call the script from the console the events.html gets updatet but not automatically by refresh time defined in the thing.
Br Peter

Ok… there was a ownership rights problem with my events.html
i fixed it here:
https://community.openhab.org/t/external-shell-script-wont-reload/92143/19

For this use case I use ansi2html

Debian: apt-get install colorized-logs
Ubuntu: apt-get install colorized-logs
Arch Linux: pacman -S python-ansi2html
Kali Linux: apt-get install colorized-logs
Fedora: dnf install python-ansi2html
Raspbian: apt-get install colorized-logs

Of course it also work if I would use a log file but you can do something like that:

/usr/bin/sshpass -p <pw> /usr/bin/ssh -t <usr>@<ip> pinout | /usr/bin/ansi2html > /etc/openhab2/html/pinout/pi.html

Maybe you run it within a rule by using executeCommandLine or use the Exec Binding with an item. The first has the advantage that you can use a cronjob which replaces every minute, every 10 minutes or every ten hours and so on the HTML file. But nearly the same as your example Thing. With items I mean you can create a Switch and when it goes to on the Exec Binding runs the command and every time you change the state to on the HTML file gets overwritten.

So in your case you have to make sure to run:

cat /var/log/openhab2/events.log | tail -n30 | /usr/bin/ansi2html > /etc/openhab2/html/events.html

As Webview I used:

Webview url="/static/pinout/pi.html" height=25

We can see that it`s completely the same as your solution. One advantage in my case is that I can use it multiple times for different logs or terminal outputs without thinking how should I create the HTML. An advantage from your solution is of course that you can use different styles or maybe stylesheets.

I’m just saying. In case of multiple use, a one-liner is certainly not the worst alternative. (And of course there are many other alternatives than ansi2html. And you could also use tools like pdf to html and so on. In some cloud solutions you can download a list as pdf.)

In my example the terminal output is coloured because I used the -t parameter. And ansi2html would create a black background because you know the terminal is configured black.

1 Like