]> git.sthu.org Git - shutils.git/blob - checkserver.sh
zsh: Add starship support
[shutils.git] / checkserver.sh
1 #!/bin/sh
2
3 failed() {
4 echo "$@" >&2
5 exit 1
6 }
7
8 log() {
9 [ "$VERBOSE" = "1" ] && echo "$@"
10 }
11
12 checkDNS() {
13 log "Check DNS lookup for '$1'."
14 if ! host "$1" 8.8.8.8 > /dev/null; then
15 failed "DNS lookup at 8.8.8.8 for '$1' failed."
16 fi
17 }
18
19 checkWebserver() {
20 log "Check web server at '$1'."
21 if ! curl -s "$1" > /dev/null; then
22 failed "Failed to retrieve website '$1'."
23 fi
24 }
25
26 checkMailserver() {
27 log "Check mail server at '$1'."
28 if ! echo "quit" | nc "$1" 587 | grep -m1 "^220 $1" > /dev/null; then
29 failed "Checking mail server '$1' failed."
30 fi
31 }
32
33 checkDiskfilling() {
34 log "Check disk filling at '$1'."
35 DF=$(ssh aristoteles.sthu.org "df -h | grep /$ | grep -o '[0-9]*%' | head -c -2")
36 if [ -n "$DF" ] && ! [ $DF -lt 80 ]; then
37 failed "Checking disk filling at '$1' failed."
38 fi
39 }
40
41 checkInternet() {
42 sleep 10
43 if ! host 8.8.8.8 8.8.8.8 > /dev/null; then
44 log "We do not appear to be connected to the internet."
45 exit 0
46 fi
47 }
48
49
50 usage() {
51 cat << EOF
52 Usage:
53 $0 [OPTIONS]
54 $0 -h
55
56 OPTIONS:
57 -h Show this text.
58 -v Print verbose output.
59 -a ADDRESS Check DNS (reverse) lookup.
60 -w ADDRESS Check webserver.
61 -m ADDRESS Check mailserver.
62 -d ADDRESS Check disk filling.
63 EOF
64 }
65
66 source $HOME/.profile-sshagent
67 VERBOSE=1
68 checkInternet
69
70 VERBOSE=0
71 while getopts "vha:w:m:d:" OPTION; do
72
73 case "$OPTION" in
74 h)
75 usage
76 exit
77 ;;
78 v)
79 VERBOSE=1
80 ;;
81 a)
82 checkDNS $OPTARG
83 ;;
84 w)
85 checkWebserver $OPTARG
86 ;;
87 m)
88 checkMailserver $OPTARG
89 ;;
90 d)
91 checkDiskfilling $OPTARG
92 ;;
93 esac
94 done
95
96
97
98