summaryrefslogtreecommitdiffstats
path: root/check-hosts
blob: 8cbb28fb148c9b73ad0546cd842610fc9322b6f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/bin/bash

STALE_DIR="${HOME}/var/stale/"

get_names() {
    /usr/sbin/bcfg2-reports "$@" --fields=name | sed 's/ *$//'
}

get_stale() {
    get_names --stale | sed 's/ *$//'
}

get_all() {
    get_names -a | sed 's/ *$//'
}

get_bad() {
    get_names -d | sed 's/ *$//'
}

get_clean() {
    get_names -c | sed 's/ *$//'
}

get_count() {
    count=$(/usr/sbin/bcfg2-reports "$@" | wc -l)

    if [ "$count" = "0" ]; then
        echo 0
    else
        echo $((count - 1))
    fi
}

get_nagios() {
    if [ -z "$1" ]; then
        clean=$(get_clean | wc -l)
        bad=$(get_bad | wc -l)
        stale=$(get_stale | wc -l)

        status="OK"
        exit=0
        if [ "$bad" != "0" -o "$stale" != "0" ]; then
            status="CRITICAL"
            exit=2
        fi

        echo "$status | clean=$clean; bad=$bad; stale=$stale;"
        exit $exit
    else
        if ! get_names -a | grep -q "^$1$"; then
            echo "CRITICAL $1 not known to bcfg2"
            exit 2
        fi

        if get_names --stale | grep -q "^$1$"; then
            echo "CRITICAL $1 is stale"
            exit 2
        fi

        bad=$(get_count -b "$1")
        if [ "$bad" != "0" ]; then
            echo "CRITICAL $bad bad entries"
            exit 2
        fi

        extra=$(get_count -e "$1")
        total=$(bcfg2-reports -t "$1" | sed "s/good: [0-9]*)/extra: $extra)/")
        echo "OK $total"
    fi
}

get_stale_once() {
    get_stale | while read host; do
	STALE_FILE="${STALE_DIR}/${host}"
        test -f "${STALE_FILE}" || (
            touch "${STALE_FILE}"
            echo $host
        )
    done

    comm -13 <(get_stale | sort) <(get_all | sort) | while read host; do
        STALE_FILE="${STALE_DIR}/${host}"
        test -f "${STALE_FILE}" && rm -f "${STALE_FILE}"
    done
}

cron() {
    stale=$(get_stale_once)
    if [ -n "$stale" ]; then
        /usr/sbin/sendmail -t <<EOM
To: bcfg2-admins@spline.inf.fu-berlin.de
From: cron@bcfg2.spline.inf.fu-berlin.de
Subject: New stale hosts

Hallo,

die folgenden Hosts haben sich seit 24h nicht mehr bei bcfg2 gemeldet:

$(echo "$stale" | sed 's/^/    /')

Gruß,
bcfg2
EOM
    fi
}

show_usage() {
     cat <<EOU
Usage:
	$0 [mode]

mode should be one of the following:
  -a|--all	display all hosts
  -b|--bad	display bad hosts
  -s|--stale	display hosts, that haven't run in the last 24 hours
  -e|--extra	display hosts with extra entries

  -c|--cron	run in cron mode (report new stale hosts)
  -n|--nagios	run in nagios mode (report count hosts in groups)
EOU
}

ARGS="$(getopt -o 'absecn::' --long 'all,bad,stale,extra,cron,nagios::' -- "$@")"

if [ $? -ne 0 ]; then
    show_usage
    exit 1
fi

eval set -- "$ARGS"

while true; do
    case "$1" in
        -a|--all)
            get_all
            exit 0
            ;;
        -b|--bad)
            get_bad
            exit 0
            ;;
        -s|--stale)
            get_stale
            exit 0
            ;;
        -e|--extra)
            get_extra
            exit 0
            ;;

        -c|--cron)
            cron
            exit 0
            ;;
        -n|--nagios)
            get_nagios "$2"
            exit 0
            ;;
        *)
            show_usage
            exit 1
            ;;
    esac
    shift
done

exit 0