From 65487c14eca1f942975ebb0bdaebef2519bf45fc Mon Sep 17 00:00:00 2001 From: Stefan Huber Date: Tue, 9 Apr 2013 07:44:41 +0200 Subject: [PATCH] Add a simple script that checks some VPS parameters --- checkserver.sh | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100755 checkserver.sh diff --git a/checkserver.sh b/checkserver.sh new file mode 100755 index 0000000..937b336 --- /dev/null +++ b/checkserver.sh @@ -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 + + + + -- 2.39.5