[Linux/Openhabian] watch disk space and send alert mail if less than 10% available

EDIT: In Openhabian you can also use the integrated MTU. you can activate it in the Openhabian config. Open the config with:

openhabian-config

this will open the config menu.

but I don’t know how configure this integrated MTU… so no support for that in this thread!

Original Post:
I created a little bash script to remind me when the filesystem runs out of space…

Here is the script

#!/bin/bash
EXCLUDE_LIST="<define excluded directories here>|<seperated with | >"
df -H | grep -vE "^Filesystem|tmpfs|cdrom|${EXCLUDE_LIST}" | awk '{print $5 " " $6}' | while read output;
do
  echo $output
  usage=$(echo $output | awk '{ print $1}' | cut -d'%' -f1  )
  partition=$(echo $output | awk '{ print $2 }' )
  if [ $usage -ge 90 ]; then
    printf "Subject: <your subject here>\n\n$(hostname) is running out of space!\n\nThe actual disk-usage on <your partition name> partition is $usage%%\n\nPlease consider freeing up some diskspace!\n\n$(date)" |
    msmtp -d <me@mymail.com>
  fi
done

open /etc/cron.daily/ and save a new file with the name memwatchdog.sh:

cd /etc/cron.daily/ && sudo nano memwatchdog.sh

This will open an empty file, paste the script there and edit it!

You can easily customize the script to your needs (edit subject and body)
note:

if [ $usage -ge 90 ]; then

The “90” is the percent of diskusage to start sending alert mails…

After that you will have to setup linux to send mails over smtp, you need your smtp server credentials…

Install mailservice and editor from linux console:

sudo apt install msmtp msmtp-mta mailutils nano

setup msmtp mail:

sudo nano /etc/msmtprc

This will open an empty file (if not confiured)
paste the following and change the settings with your smtp credentials:

# Set default values for all following accounts.
defaults
# Use the mail submission port 587 instead of the SMTP port 25.
port 587
# Always use TLS.
tls on
# Set a list of trusted CAs for TLS. The default is to use system settings, but
# you can select your own file.
tls_trust_file /etc/ssl/certs/ca-certificates.crt
# The SMTP server of your ISP
account <your isp name>
host <your smtp server address e.g. mail.myisp.com>
from <the mailaddress to send mails from e.g. me@mymail.com>
auth on
user <your username>
password <your password>
# Set default account to your isp
account default: <your isp name (must be the same as account name above)>
# Map local users to mail addresses
aliases /etc/aliases

In order to be able to use the mail command you need to install mailx additionally:

sudo apt install bsd-mailx

After that configure mailx to use msmtp

sudo nano /etc/mail.rc

scroll down to the last line and append this:

set mta=/usr/bin/msmtp

now you should be able to send mail from your headless linux system.

Test your configuration with something like:

echo "Subject:Hello World/nThis is my first mail send from my server" | msmtp -d <your email address e.g. me@mymail.com>

or with mail instead of msmtp

echo "This is my first mail send from my server" | mail -s "Hello World" <your email address e.g. me@mymail.com> 

Now open crontab

sudo crontab -e

to automatically start the script add this line at the end:

0 8 * * * /etc/cron.daily/memwatchdog.sh

this will start the script at 8 a.m. every day…

have fun!

I’m curious to know if you looked at the SystemInfo binding to get the disk space available? I realized I need something similar but I’m considering using that binding with a rule triggered daily to check the value. It may not be as flexible as what you have.

No I just used my script yet…
I use a debian server to host multiple lxc-containers for my homenet and the script runs directly on my server not inside my openhab container. but thanks I will have a look…

btw, docker is also based on lxc but I don’t need any fancy docker stuff to run containers on linux :wink:

also I think the downside of using a binding for this is that openhab has to be running to send the alerts. Not sure if I like that behavior :wink:

Your thread title suggests to use this on openHABian too,
But on openHABian you should not be installing your own MTA but use the provided one you can install that from the openhabian-config menu.
Please remove that “openHABian” from the title. Thanks.

Hi Markus,

sorry, wasn’t aware of that… but Markus what stops one from using this solution??
Only because I can use the same option in openhabian? Seems to me a bit like “but I want you to use our stuff” isn’t that a little “dictatorial” in my opinion one should use the solution he likes to achive his/her goal…

Edit: I added this option (without support) to my initial post, but I will not remove Openhabian from the title, because it’s a working solution for debian systems, thanks!

Well as you explained your solution was built on non-openHABian without awareness on what that already provides. Nothing wrong with using it whereever you like.
You’re ‘promoting’ it for general application and writing in the ‘Solutions’ category though.
That people will pay extra attention to when your post shows up in response to a search.
But if a user is on openHABian and there exists a solution already (as is the case here) that he is not aware of he might apply yours.
To all of us as a community it’s better not to reinvent the wheel every time so we should use and promote working solutions whereever they exist. That’s my intention here.
And the openHABian solution is shrink-wrapped in menus so really anybody can put it to use quickly without any Linux knowledge. And no need for any manual installation handling whatsoever.
I’m fine with your EDIT.

Thanks

Maybe you give the openHABian-provided MTA a try yourself and recommend it to openHABian users in your post to make it a universally applicable one ?
Your MTA install instructions are valuable as the would still be needed on non-openHABian, and the rest of the post as well.

Sorry Markus, but I’m not using a Pi actually… so there’s no openhabian install to test your solution.

I got some Raspberries lying around here, but I don’t know if I’ll find some time to test this integrated MTA stuff.

But feel free to post that solution here by yourself :wink: I would link your solution to my initial post than