Last active
January 15, 2023 11:38
-
-
Save mdeweerd/198bcb3d0a8a00e5421c9427c54f9a53 to your computer and use it in GitHub Desktop.
setup `monit` on home assistant
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
setupMonit.conf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# See https://community.home-assistant.io/t/monit-for-monitoring-ha-including-restart-and-more/353252 | |
# SCRIPT PATHS | |
MYDIR=$(realpath $(dirname 0)) | |
CONFFILE="${MYDIR}/setupMonit.conf" | |
# CONFIGURATIONS DEFINITIONS, OVERRIDE BY ADDING LOCAL setupMonit.conf | |
# Host and port for HA, | |
# `curl http://homeassistant:8123` gives home page | |
# `curl http://homeassistant:8123/api/` gives '401: Unauthorized' (not ... Forbidden): | |
# | |
# or set to other working host, sometimes pubic host works `curl https://publichaurl` | |
# update protocol http to https if needed | |
# (Testing https, also tests the nginx proxy) | |
HA_HTTP_HOST=homeassistant | |
HA_HTTP_PORT=8123 | |
PROTOCOL=http | |
# Otherwise, define HA_HTTP_HOST to empty value | |
EMAILLOGIN="YOUREMAILLOGIN" | |
EMAILPASSWORD="YOUREMAILPASSWORD" | |
MAILSERVER=YOURMAILSERVER | |
# Read user settings overriding examples above | |
[ -r "$CONFFILE" ] && . $CONFFILE | |
# Script (adjust some variables to correspond to your setup): | |
mkdir -p /etc/monit.d | |
# change line with uncomment include of monit.d | |
sed -i 's@#\(\s*include\s*/etc/monit.d\)@\1@' /etc/monitrc | |
sed -i 's@set log syslog@set log /var/log/monit.log@' /etc/monitrc | |
if [ "$HA_HTTP_HOST" != "" ] ; then | |
cat > /etc/monit.d/ha <<EOHA | |
check host ha with address $HA_HTTP_HOST | |
# with pidfile PATHTOPIDFILE | |
if failed | |
port $HA_HTTP_PORT protocol $PROTOCOL | |
# Expect unauthorized, shows that the server is up | |
# Could check status if API key provided. | |
#and request /api/ with content = "401: Unauthorized" with timeout 20 seconds | |
and request / with timeout 20 seconds | |
then | |
exec "/usr/bin/ha core restart" | |
group ha | |
EOHA | |
fi | |
# RESTART HA when CPU LOAD is high for 10 minutes | |
cat > /etc/monit.d/checkload <<EOCPUHIGH | |
check system homeassistant.local | |
if loadavg (1min) per core > 1.5 for 10 cycles then alert | |
if loadavg (1min) per core > 1.5 for 15 cycles then exec "/usr/bin/ha core restart" | |
EOCPUHIGH | |
# Its possible to set up mail alerts. | |
# Example for server using TLS on port 587: | |
cat > /etc/monit.d/mailserver <<EOMS | |
set mailserver | |
$MAILSERVER port 587 username "$EMAILLOGIN" password "$EMAILPASSWD" using tlsv1 | |
EOMS | |
monit | |
monit reload | |
sleep 1 | |
monit summary |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment