Add a simple script that checks some VPS parameters
authorStefan Huber <shuber@sthu.org>
Tue, 9 Apr 2013 05:44:41 +0000 (07:44 +0200)
committerStefan Huber <shuber@sthu.org>
Tue, 9 Apr 2013 05:44:41 +0000 (07:44 +0200)
checkserver.sh [new file with mode: 0755]

diff --git a/checkserver.sh b/checkserver.sh
new file mode 100755 (executable)
index 0000000..937b336
--- /dev/null
@@ -0,0 +1,98 @@
+#!/bin/sh
+
+failed() {
+       echo "$@" >&2
+       exit 1
+}
+
+log() {
+       [ "$VERBOSE" = "1" ] && echo "$@"
+}
+
+checkDNS() {
+       log "Check DNS lookup for '$1'."
+       if ! host "$1" 8.8.8.8 > /dev/null; then
+               failed "DNS lookup at 8.8.8.8 for '$1' failed."
+       fi
+}
+
+checkWebserver() {
+       log "Check web server at '$1'."
+       if ! curl -s "$1" > /dev/null; then
+               failed "Failed to retrieve website '$1'."
+       fi
+}
+
+checkMailserver() {
+       log "Check mail server at '$1'."
+       if ! echo "quit" | nc "$1" 587 | grep -m1 "^220 $1" > /dev/null; then
+               failed "Checking mail server '$1' failed."
+       fi
+}
+
+checkDiskfilling() {
+       log "Check disk filling at '$1'."
+       DF=$(ssh aristoteles.sthu.org "df -h | grep /$ | grep -o '[0-9]*%' | head -c -2")
+       if [ -n "$DF" ] && ! [ $DF -lt 80 ]; then
+               failed "Checking disk filling at '$1' failed."
+       fi
+}
+
+checkInternet() {
+       sleep 10
+       if ! host 8.8.8.8 8.8.8.8 > /dev/null; then
+               log "We do not appear to be connected to the internet."
+               exit 0
+       fi
+}
+
+
+usage() {
+       cat << EOF
+Usage:
+ $0 [OPTIONS]
+ $0 -h
+
+OPTIONS:
+  -h            Show this text.
+  -v            Print verbose output.
+  -a ADDRESS    Check DNS (reverse) lookup.
+  -w ADDRESS    Check webserver.
+  -m ADDRESS    Check mailserver.
+  -d ADDRESS    Check disk filling.
+EOF
+}
+
+source $HOME/.profile-sshagent
+VERBOSE=1
+checkInternet
+
+VERBOSE=0
+while getopts "vha:w:m:d:" OPTION; do
+
+       case "$OPTION" in
+               h)
+                       usage
+                       exit
+                       ;;
+               v)
+                       VERBOSE=1
+                       ;;
+               a)
+                       checkDNS $OPTARG
+                       ;;
+               w)
+                       checkWebserver $OPTARG
+                       ;;
+               m)
+                       checkMailserver $OPTARG
+                       ;;
+               d)
+                       checkDiskfilling $OPTARG
+                       ;;
+       esac
+done
+
+
+
+