Skip to content

Instantly share code, notes, and snippets.

@baudneo
Forked from roelle/systemmonitor.sh
Created September 24, 2021 08:07
Show Gist options
  • Save baudneo/249a8e46619d13b356bff473872a37d4 to your computer and use it in GitHub Desktop.
Save baudneo/249a8e46619d13b356bff473872a37d4 to your computer and use it in GitHub Desktop.
This is a simple bash script that will monitor a few things on a server and notify you over pushover is there are any problems. Specifically, the script checks for reboots, ZFS pool health, dropbox status, disk (over)utilization, and that a specified process is running.
#!/bin/bash
# Copy into /etc/cron.hourly
pushover_app_token="your--app--token"
pushover_user_guid="your--user--or--group--id"
dropbox_username="user--with--dropbox--installed"
process_to_check="your--process--name"
# Notification function
notify () {
# Send pushover notification
push=$(curl -s \
--form-string "token=$pushover_app_token" \
--form-string "user=$pushover_user_guid" \
--form-string "message=IT alert, $HOSTNAME: $1" \
https://api.pushover.net/1/messages.json)
log "Pushover attempted: $push"
# Log the message
log "$1"
}
# Logging function
log () {
echo "$(date --iso-8601=s) $1" >> /var/log/systemmonitor.log
}
# Check for reboot - adjust the time per frequency of cron
if (( $(cat /proc/uptime | sed 's/\..*/\ /') < 6000 )); then
notify "Reboot.
$(uptime)"
fi
# Check zpool
if [[ "$(zpool status -x)" != "all pools are healthy" ]]; then
notify "zpool health.
$(zpool status)"
fi
# Check dropbox
dropbox_status=$(su -c dropbox.py\ status $dropbox_username)
if [[ "$(echo "$dropbox_status" | grep Syncing | awk '{print $1}')" != "Syncing" ]] &&
[[ "$dropbox_status" != "Up to date" ]]; then
notify "Dropbox status.
$dropbox_status"
fi
# Check disk usage
disk_fractions=$(df | sed 's/ \+/ /g' | awk -F " |%" '/dev/{print $5}')
for disk_fraction in $disk_fractions
do
if (( $disk_fraction > 85 )); then
notify "Disk usage.
$(df -h)"
break
fi
done
# Check process
if ! pgrep $process_to_check > /dev/null; then
notify "$process_to_check is not running.
$(ps aux | grep 'node\|ruby')"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment