Sending logs via mail binding

Hi Everyone.
i have a question. might have been asked in a different way. but here it is again.

I want to be able to send my log files via email to me.
The Openhab server is remote installed and i want it to send me logs say once a week?
can it be done ?

You should be able to do this.
Areas you will need to look at -

File handling from rules or scripts. There are at least two logs of interest, openhab.log and events. Each of those may have multiple archive versions, do you want those? Depends on your logging strategy I expect.

Email handling of multiple file attachments. There may be limits on number or filesize set by your mail provider.

Also the logreader binding in combination with the mail or telegram action might be of interest to send yourself information about warnings / errors.

1 Like

Here is one of those cases where it’s probably going to be easiest to do this not by using openHAB.

If you want the logs as the body of the email…

  1. Install msmtp and configure (there are lots of tutorials)
  2. Put the following script into /etc/cron.weekly

NOTE: do not add an extension to the file, e.g. use email_logs instead of email_logs.sh for the filename.

#!/bin/bash -e

email=youremail@email.com
to='To: '$email'\n'
from='From: '$email'\n'
subject='Subject: openHAB Logs\n\n'
body='openHAB.log ----------\n'
body=${body}"$(< /var/log/openhab2/openhab.log)" # loads the contents of openhab.log into the body of the email
body=${body}'\n\nevents.log ---------\n'
body=${body}"$(< /var/log/openhab2/events.log)" # loads the contents of events.log into the body of the email
msg=${to}${from}${subject}${body}

echo -e "$msg" | /usr/sbin/sendmail $email

Another alternative that might work:

#!/bin/bash -e

echo "openhab.log"
cat /var/log/openhab2/openhab.log
echo "\n\nevents.log"
cat /var/log/openhab2/events.log

This is far simpler and takes advantage of the fact that once you have msmtp installed and configured cron will automatically send an email with the results generated by cron jobs. But you can’t control the subject and customize the email in other ways you might want. If you don’t care, this will be the easiest to implement.

To send the email with the log files as attachments:

  1. Install and configure msmtp and mutt. Configure mutt for the root user (see https://hostpresto.com/community/tutorials/how-to-send-email-from-the-command-line-with-msmtp-and-mutt/)
  2. Put the following script into /etc/cron.weekly
#!/bin/bash -e

email=youremail@email.com
to='To: '$email'\n'
from='From: '$email'\n'
subject='Subject: openHAB Logs\n\n'
body='openHAB.logs\n\n'
msg=${to}${from}${subject}${body}

echo -e "$msg" | mutt -a /var/log/openhab2/openhab.log -a /var/log/openhab2/events.log -s "openHAB log file" -- ${email} ${email}

NOTE: I just typed in the above scripts based on similar scripts I have on my system to email tripwire reports to me every day.

Of course, all of the concerns mentioned by rossko57 still apply as well. These scripts can be easily modified to

If you want something more timely, the logreader binding mentioned by Felix can watch your log files and send an email or alert only when it sees something of interest (e.g. an error).

The thing I like about using cron for something like this is that if openHAB crashes, you will still get the logs and be able to see not only that openHAB crashed but why without needing to log into the machine to pull the logs manually. It’s also not any more complicated nor does it require you to learn anything more than it would required to get this working with openHAB itself. The hardest part is learning about the existence of msmtp and mutt.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.