Skip to content

Instantly share code, notes, and snippets.

@m000
Created March 29, 2019 15:50
Show Gist options
  • Save m000/ea16f7fe62e81a0f38d444fc43d73da2 to your computer and use it in GitHub Desktop.
Save m000/ea16f7fe62e81a0f38d444fc43d73da2 to your computer and use it in GitHub Desktop.
QNAP scripts
#!/bin/bash
#####################################################################
# Monitoring script for use with QNAP boxes and cronitor.io.
#####################################################################
DATA_DIR="/share/MD0_DATA"
RUN_DIR="/share/MD0_DATA/homes/admin/run"
uptime_save="$RUN_DIR"/uptime_cronitor
monitorid="QbBI8O"
cronitor_run_url="https://cronitor.link/$monitorid/run"
cronitor_complete_url="https://cronitor.link/$monitorid/complete"
cronitor_fail_url="https://cronitor.link/$monitorid/fail"
AWK=/bin/awk
CAT=/bin/cat
CP=/bin/cp
CUT=/bin/cut
DF=/bin/df
WGET=/usr/bin/wget
#####################################################################
function humanize_uptime() {
echo "$1" | "$AWK" '{
fmt="%dd%02dh%02dm\n";
d=int($1/(3600*24));
h=int($1%(3600*24)/3600);
m=int($1%3600/60);
printf(fmt, d, h, m)
}'
}
function uptime_diff() {
echo "$1 $2" | "$AWK" '{
diff = $1 - $2;
print diff;
if (diff < 0) { exit 1; }
else { exit 0; }
}'
}
#####################################################################
# first run = initialize
if ! [ -f "$uptime_save" ]; then
"$CAT" /proc/uptime > "$uptime_save"
fi
# get and update uptimes
uptime_before=$("$CUT" -d\ -f1 < "$uptime_save")
"$CAT" /proc/uptime > "$uptime_save"
uptime_now=$("$CUT" -d\ -f1 < "$uptime_save")
# get info
storage=$("$DF" -h "$DATA_DIR" | "$AWK" 'END{print "capacity:"$2 " used:"$3 " available:"$4}')
uptime="uptime:$(humanize_uptime "$uptime_now")"
# initial ping
"$WGET" -q -O /dev/null -T 10 "$cronitor_run_url"?msg="$uptime $storage" || true
sleep 5
# check diff and do actions
uptime_diff "$uptime_now" "$uptime_before" > /dev/null
if [ "$?" -eq 0 ]; then
"$WGET" -q -O /dev/null -T 10 "$cronitor_complete_url"?msg="ok" || true
else
uptime_before_fail=$(humanize_uptime "$uptime_before")
"$WGET" -q -O /dev/null -T 10 "$cronitor_fail_url"?msg="failed after $uptime_before_fail" || true
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment