]> arthur.barton.de Git - nagcollect.git/blob - server/bin/nagios-submit-host
40c991c60a01758e1c46ac303ade8253937d9e71
[nagcollect.git] / server / bin / nagios-submit-host
1 #!/bin/sh
2 # nagios-submit-host -- Submit passive host check result
3 # 2009-12-11, alex@barton.de
4
5 NAME=`basename "$0"`
6 CREATE=
7
8 Usage() {
9         echo "$NAME [-c] <host> <code> [<text>]"; echo
10         echo "  -c      create new host, if necessary"
11         echo "  <host>  Nagios host ID"
12         echo "  <code>  Status code: 0=OK, 1=Warning, 2=Error, 3=Unknown"
13         echo "  <text>  Optional status message"
14         echo; exit 1
15 }
16
17 Error() {
18         echo "$NAME: $*"
19         exit 1
20 }
21
22 [ -r "/etc/nagios3/system.cfg" ] || Error "Can't read /etc/nagios3/system.cfg!"
23 . /etc/nagios3/system.cfg 
24
25 # Validate command line arguments
26 if [ "$1" = "-c" ]; then
27         CREATE=1; shift
28 fi
29 [ $# -ge 2 -a $# -le 3 ] || Usage
30 [ "$2" = 0 -o "$2" = "1" -o "$2" = "2" -o "$2" = "3" ] || Usage
31
32 # Validate configuration
33 [ -w "$CMDFILE" ] || Error "Can't write to \"$CMDFILE\"!"
34
35 time_t=`date +%s`
36 host="$1"
37 code="$2"
38 text="${3:-}"
39
40 grep -E "^[[:space:]]+host_name[[:space:]]+${host}\$" "$CFGDIR"/*.cfg >/dev/null 2>&1
41 if [ $? -ne 0 ]; then
42         # Host not configured!
43         [ -n "$CREATE" ] || Error "Host \"$host\" not configured!?"
44         echo "Creating new Nagios host configuration for \"$host\" ..."
45         cfg="$CFGDIR/_host_$host.cfg"
46         cat >"$cfg" <<EOF
47 define host {
48         use             ${HOST_TEMPLATE_PASSIVE}
49         host_name       $host
50 }
51 EOF
52         chmod "$CFG_MODE" "$cfg" || Error "chmod(1) failed!"
53         echo "Reloading Nagios configuration ..."
54         "$RCFILE" reload || Error "Failed to reload Nagios configuration!"
55         sleep 1
56 fi
57
58 cmd="[$time_t] PROCESS_HOST_CHECK_RESULT;$host;$code;$text"
59 echo "$cmd" >>"$CMDFILE"