** Update2 **
Check post #144. This binding is now part of newest SNAPSHOT builds.
** Update **
Author and developer of this binding has changed. @pauli_anttila took the lead. Please check post #110 for updated info and new download links.
** Old post **
Hi all,
I’ve been developing a small binding for OH2 to automatically read openhab.log
. Originally the idea came from this topic: Skript to read openhab logs. I don’t want to sit on my desktop all the time and watch logs so I created this binding to do it for me. If errors are found, Telegram action is used to notify me on my mobile phone (Example .rules file later in this post).
Now, few details about the binding. It reads through the log file and updates 7 different channels:
1. DateTime logRotated - Last time your log rotated.
2. DateTime lastRead - Last time when log was read by this binding
3. DateTime lastLine - Last log lines time stamp
4. Number warningLines - How many [WARN ] lines was found since last read
5. Number errorLines - How many [ERROR ] lines was found since last read
6. String lastWarningLine - Contents of last warning line
7. String lastErrorLine - Contents of last error line
It has one configuration options
1. Resfresh rate - How often to read. Defaults to 60. (At the time this is in seconds but I think would be better in minutes. What do you think?)
Binding picks log files path using system properties ${OPENHAB_LOGDIR}/openhab.log so it depends on default logging settings.
Attention! I’ve only tested on my OH2 server running version 2.2.0-SNAPSHOT (#1080) and installed through apt-get. If you are testing on some other platform and find it working or failing, please let me know.
You can configure everything through PaperUI. But I’ll provide my .items file if somebody likes to define items manually like I do .
Example logreader.items
DateTime logreaderLogRotated "Last Log Rotation [%1$tY.%1$tm.%1$te %1$tR]" <time> { channel="logreader:reader:reader1:logRotated" }
DateTime logreaderLastRead "Last Read [%1$tY.%1$tm.%1$te %1$tR]" <time> { channel="logreader:reader:reader1:lastRead" }
DateTime logreaderLastLine "Last Line [%1$tY.%1$tm.%1$te %1$tR]" <time> { channel="logreader:reader:reader1:lastLine" }
Number logreaderWarnings "Warning lines [%d]" <alarm> { channel="logreader:reader:reader1:warningLines" }
Number logreaderErrors "Error lines [%d]" <alarm> { channel="logreader:reader:reader1:errorLines" }
String logreaderLastWarningLine "LastWarningLine [%s]" { channel="logreader:reader:reader1:lastWarningLine" }
String logreaderLastErrorLine "LastErrorLine [%s]" { channel="logreader:reader:reader1:lastErrorLine" }
Example logreader.rules
rule "LogReader"
when
Item logreaderLastRead changed
then
if (logreaderErrors.state > 0) {
sendTelegram("YourBot", "LogReader alarm!\n\n" + logreaderErrors.state.toString + " Errors in log! Heres the last one:\n\n" + logreaderLastErrorLine.state.toString)
}
end
Example logreader.things
logreader:reader:reader1[ refreshRate=60 ]
If you find this somehow useful and would like to test, download here and drop in your addons folder. Feedback and improvement ideas are always welcome!
/Miika
Edit #1
Fixed example rule to use logreaderLastRead
as trigger instead of logreaderErrors
. Now it triggers after every read and then checks if there are errors. With logreaderErrors
there was a possibility to have same amount of errors between multiple reads --> channel would not update and the state stays the same --> rule doesn’t trigger.
Edit #2
New channels and configuration. Added more informative message to logreader.rules
using new channel lastErrorLine
.