aboutsummaryrefslogtreecommitdiff
path: root/nagios-exists
blob: c9e61148950ad7f6287b3ac0fbfab897a0ac809a (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env bash

#
# nagios-exists(1) -- Return an exit status corresponding to whether HOST or
# HOST/SERVICE exists in Nagios. Mostly for use in scripts.
#
#    $ nex abc-example-mc-1
#    $ nex webhost/HTTP
#
# Author: Tom Ryder <tom@sanctum.geek.nz>
# Copyright: 2016
#

# Name self
self=nagios-exists

# Usage printing function
usage() {
    printf 'USAGE: %s HOST[/SERVICE]\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 exactly one argument remaining
if (($# != 1)) ; then
    usage >&2
    exit 1
fi

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

# Which query to run depends on what the argument looks like.
case $1 in

    # If there's a slash in the argument, it's a host/service tuplet.
    */*)
        host_name=${1%/*}
        service_description=${1#*/}
        while read -r _ ; do
            exit
        done < <(unixcat "$socket" <<EOF
GET services
Columns: host_name service_description
Filter: host_name = $host_name
Filter: service_description = $service_description
EOF
        )
        exit 1
        ;;

    # Otherwise, assume it's a host.
    *)
        host_name=$1
        while read -r _ ; do
            exit
        done < <(unixcat "$socket" <<EOF
GET hosts
Columns: host_name
Filter: host_name = $host_name
EOF
        )
        exit 1
        ;;
esac