summaryrefslogtreecommitdiffstats
path: root/check-hosts
blob: 6c7dcfd674b1d55deb3d5e71396b8f3338df9afb (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
#!/bin/bash

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

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

get_stale() {
    get_names --stale
}

get_all() {
    get_names
}

get_bad() {
    get_names -d
}

get_clean() {
    get_names -c
}

get_extra() {
    /usr/sbin/bcfg2-reports clients --fields=extra,name | sed '1d;s/ *$//' | \
    while read extra name ; do
        if [ "$extra" != "0" ]; then
            echo $name
        fi
    done
}

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 -23 <(ls -1 ${STALE_DIR}/ | sort) <(get_all | sort) | while read file; do
        STALE_FILE="${STALE_DIR}/${file}"
        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)
EOU
}

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

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
            ;;
        *)
            show_usage
            exit 1
            ;;
    esac
    shift
done

exit 0