aboutsummaryrefslogtreecommitdiff
path: root/nagios-downstream-list
blob: 6485e89a8bb8ff5248a9ff749159c947de4b02a2 (plain) (blame)
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
#!/usr/bin/env bash

#
# nagios-downstream-list(1) -- List all the descendents of at least one given
# host, unique and sorted.
#
#   $ ndl abc-example-mc-1
#
# Author: Tom Ryder <tom@sanctum.geek.nz>
# Copyright: 2016
#

# Name self
self=nagios-downstream-list

# Usage printing function
usage() {
    printf 'USAGE: %s HOST1 [HOST2 ... ]\n' "$self"
}

# Handle options, just -h help at the moment
OPTIND=1
while getopts 'h' opt ; do
    case "$opt" in
        h)
            usage
            exit 0
            ;;
        '?')
            usage >&2
            exit 1
            ;;
    esac
done
shift "$((OPTIND-1))"

# We need at least one argument remaining
if ! (($#)) ; then
    usage >&2
    exit 1
fi

# Define the path to the Livestatus socket
socket=${MK_LIVESTATUS_SOCKET:-/usr/local/nagios/var/rw/live}

# Recursive function to print all the descendents of the arguments
descendents() {
    for ancestor ; do
        while read -r child ; do
            printf '%s\n' "$child"
            descendents "$child"
        done < <(unixcat "$socket" <<EOF
GET hosts
Columns: host_name
Filter: parents >= $ancestor
EOF
        )
    done | sort -uV
}

# Run the recursive function on all the arguments given to this script
descendents "$@"