diff --git a/README.md b/README.md new file mode 100644 index 0000000..4aa6465 --- /dev/null +++ b/README.md @@ -0,0 +1,43 @@ +# signal-monitoring + +This program is for simple server monitoring and notifications with signal-cli. +It's goal is to be able to run on supersimple servers, like NAS, raspberry pi, +home routers, etc. + +## Setup + +First, install and configure [signal-cli](https://github.com/AsamK/signal-cli) +(or replace script in notify function for different kind of notification). + +Then modify script and configure your sending number, recipient number and +optionally path to java. I add PATH to signal-cli and java commands as well. + +Then at the bottom of the script, configure ping checks and URL string match +checks. + +check_ping takes one argument, which is the name of the server + +check_url takes three arguments: "check identificator" (can be anything recognizable that can be a part of filename, such as hostname), URL and string to look for on the web page. + +The last signal-cli command just downloads all messages for this instance and +drops them. Use this if this script is the only user using this server to +ease up storage requirements for signal servers and make sure that it does not +store too much (encrypted) messages for you. + +## Cron + +Run it from cron or task scheduler of your OS. Please refer to the documentation +of your OS. + +## Why this project + +I wanted to be able to perform a simple monitoring for my hosted server from +my home NAS. Signal is what I read, so e-mail notifications won't do it, +I don't read e-mail that often. + +The script sends one notification per hour, if the service is consistently down. +When it goes up again, it sends up notification. + +## If you liked this script, donate + +If you like this script, [support me by sending a small donation](https://juraj.bednar.io/en/support-me/) \ No newline at end of file diff --git a/signal-monitoring.sh b/signal-monitoring.sh new file mode 100755 index 0000000..4599211 --- /dev/null +++ b/signal-monitoring.sh @@ -0,0 +1,95 @@ +#!/bin/bash +# if you need to add something to path: +#PATH=$PATH:/path/to/java:/path/to/signal-cli +# for some cases you should explicitly specify JAVA_HOME in +# order for signal-cli to work +#export JAVA_HOME=/var/packages/Java8/target/j2sdk-image/jre + +# username of the sending entity (phone number in international format) +SIGNAL_USER="+1123123123123" + +# phone number of the user that receives the notification +NOTIFY_NUMBER="+132132132132" + +# now go to the bottom of the script and specify the checks + +mkdir -p ~/.signal-monitoring && cd ~/.signal-monitoring + +function log { + echo "$1" >> ~/.signal-monitoring/log +} + +# arguments: notify_text +function notify { + echo $1 | signal-cli -u ${SIGNAL_USER} send $NOTIFY_NUMBER + log "Sending notification ${1}" +} + +# arguments: check_name description +function check_passed { + check_name=$1 + description=$2 + check_filename="${check_name}-error" + + log "check_passed ${check_name} ${description}" + + if [ -f ${check_filename} ] + then + rm -f $check_filename + notify "${description}" + fi +} + +# arguments: check_name description +function check_failed { + check_name=$1 + description=$2 + check_filename="${check_name}-error" + + log "check_failed ${check_name} ${description}" + + FOUND=`find ~/.signal-monitoring -mmin -60 -name ${check_name}-error -not -empty -print` + if [ -z "$FOUND" ] + then # we don't have recent notification (60 minutes) + echo "${description}" > "${check_filename}" + notify "${description}" + fi +} + + +# argument: hostname +function check_ping { + server=$1 + if ping -c 5 -q $server > /dev/null 2>&1 + then + check_passed ${server}-ping "${server} ping is up" + else + check_failed ${server}-ping "${server} ping is not responding" + fi +} + +# arguments: check_name url content_to_look_for +function check_url { + check_name=$1 + url=$2 + content_to_look_for=$3 + if wget -q -O - "$url" | grep "$content_to_look_for" > /dev/null 2>&1 + then + check_passed ${check_name}-url "${check_name} UP: ${url} now contains ${content_to_look_for}" + else + check_failed ${check_name}-url "${check_name} DOWN: ${url} does not contain ${content_to_look_for}" + fi +} + +# here are the checks + +# check pings +check_ping my-first.server.com +check_ping my-second.server.com + +check_url my-first.server.com "https://my-first.server.com/url/index.html" "Welcome to My First Server" +check_url my-third.server.com "https://my-third.server.com/index.html" "Welcome to My Third Server" + +# Leave this if you don't use signal-cli outside of this script, +# otherwise comment out, see readme +signal-cli -u $SIGNAL_USER receive > /dev/null 2>&1