openHAB + logwatch

If someone is interested in the ERRORs in his openhab.log and has logwatch running, he might be interested in this little enhancement. logwatch parses the openhab.log and reports the ERRORs and their counts.

Install logwatch using your repository installer, e.g.
apt-get install logwatch

All enhancements need to be done under /etc/logwatch.
cd /etc/logwatch

Create a file conf/services/openhab.conf with the content
LogFile=openhab Title = "openHAB LogWatch"

Create a file conf/logfiles/openhab.conf with the content
# LogFile Group for openhab service LogFile = /opt/usr/openhab/logs/openhab.log

At least create a little parser script, which sorts out and counts the ERROR lines; name it scripts/services/openhab

#!/usr/bin/perl
use strict; use warnings;
use Logwatch ':dates';

my $counter = 0;
my %unique_err = ();
my $line;
my $SearchDate = TimeFilter("%Y-%m-%d %H:%M:%S");
my $Date;

while (defined($line = <STDIN>)) {
  if ($line =~ /ERROR/) {

    # filter date
    ($Date) = ($line =~ /^(\d{4}-\d{2}-\d{2} ..:..:..).\d+/);
    if ($Date =~ $SearchDate) {

        # Regular expression to remove date/time field
        $line =~ s/^.*?\[ERROR\]\s+//;
        if (!exists $unique_err{$line}) {
          $unique_err{$line} = 0;
        }
        $unique_err{ $line } = $unique_err{$line}+1;
        $counter++;
    }
  }
}

while ( my ($key, $value) = each(%unique_err) ) {
  chomp $key;
  #print "Unique Error: $key\nCount: $value\n";
  print "Error ($value times):\t$key\n";
}

print "Total # of Errors: $counter\n"

The report could look like this and might be executed by cron on a daily basis:

 ################### Logwatch 7.4.0 (03/01/11) ####################
        Processing Initiated: Fri Sep  2 22:47:55 2016
        Date Range Processed: yesterday
                              ( 2016-Sep-01 )
                              Period is day.
        Detail Level of Output: 0
        Type of Output/Format: stdout / text
        Logfiles for Host: debian
 ##################################################################

 --------------------- openHAB LogWatch Begin ------------------------

 Unique Error: [.myopenhab.internal.MyOHClient] - Socket.IO error: io.socket.engineio.client.EngineIOException: websocket error
 Count: 7
 Unique Error: [o.o.b.o.i.c.OneWireConnection ] - reading from path 12.FD3CB6000000/sensed.B attempt 3 throws exception
 Count: 2
 Unique Error: [o.o.b.o.i.c.OneWireConnection ] - reading from path 26.76E356010000/humidity attempt 2 throws exception
 Count: 1
 Unique Error: [o.o.b.o.i.c.OneWireConnection ] - reading from path 26.76E356010000/humidity attempt 3 throws exception
 Count: 1
 Unique Error: [o.i.t.m.i.MqttBrokerConnection] - MQTT connection to broker was lost
 Count: 2
 Unique Error: [o.o.b.f.i.FritzboxTr064Binding] - Error retrieving SOAP response from FritzBox
 Count: 1
 Unique Error: [o.i.t.m.i.MqttBrokerConnection] - MQTT connection to 'mymqtt' was lost: Verbindung wurde getrennt : ReasonCode 32109 : Cause : null
 Count: 1
 Unique Error: [o.i.t.m.i.MqttBrokerConnection] - MQTT connection to 'mysensor' was lost: Verbindung wurde getrennt : ReasonCode 32109 : Cause : Connection reset
 Count: 1
 Unique Error: [o.o.b.o.i.c.OneWireConnection ] - reading from path 12.FD3CB6000000/sensed.B attempt 2 throws exception
 Count: 3
 Unique Error: [o.o.i.c.i.job.EventReloaderJob] - error while loading calendar entries: null
 Count: 4
 Unique Error: [o.o.b.f.i.FritzboxTr064Binding] - Releasing connection to FritzBox because of error!
 Count: 1
 Unique Error: [o.o.c.s.ScriptExecutionThread ] - Error during the execution of rule 'Netzwerk alarms': Index: 2, Size: 2
 Count: 2
 Unique Error: [o.o.b.o.i.c.OneWireConnection ] - reading from path 26.76E356010000/humidity attempt 1 throws exception
 Count: 2
 Unique Error: [o.o.b.f.i.FritzboxTr064Binding] - Cannot send/receive: Connect to 192.168.2.4:49443 [/192.168.2.4] failed: connect timed out
 Count: 1
 Unique Error: [o.o.b.o.i.c.OneWireConnection ] - reading from path 12.FD3CB6000000/sensed.B attempt 1 throws exception
 Count: 3
 Unique Error: [.o.b.o.internal.OneWireBinding] - Set Item for itemName=Rauchalarm to Undefined, because the readvalue is null
 Count: 2
 Unique Error: [o.o.b.o.i.c.OneWireConnection ] - reading from path 12.8136B6000000/sensed.A attempt 1 throws exception
 Count: 2
 Unique Error: [.o.b.o.internal.OneWireBinding] - Set Item for itemName=HumiSZ to Undefined, because the readvalue is null
 Count: 1
 Total # of Errors: 37

 ---------------------- openHAB LogWatch End -------------------------


 ###################### Logwatch End #########################

Of cource, this script just parses the lines containing the string ERROR, not the complete backtrace.

Hope this helps!

3 Likes

I have just setup something similar using https://github.com/spacemanspiff2007/GetOpenhabErrors. Very handy tools.

1 Like