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…
- Install msmtp and configure (there are lots of tutorials)
- 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:
- 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/)
- 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.