]> arthur.barton.de Git - nagcollect.git/blob - server/bin/nagios-submit-host
Remove OS X packaging scripts
[nagcollect.git] / server / bin / nagios-submit-host
1 #!/bin/sh
2 #
3 # NagCollect -- Nagios Data Collector for Passive Checks
4 # Copyright (c)2009-2010 Alexander Barton, alex@barton.de
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 # Please read the file COPYING, README and AUTHORS for more information.
11 #
12 # nagios-submit-host -- Submit passive host check result
13 #
14
15 NAME=`basename "$0"`
16 CREATE=
17
18 Usage() {
19         echo "$NAME [-c] <host> <code> [<text>]"; echo
20         echo "  -c      create new host, if necessary"
21         echo "  <host>  Nagios host ID"
22         echo "  <code>  Status code: 0=OK, 1=Warning, 2=Error, 3=Unknown"
23         echo "  <text>  Optional status message"
24         echo; exit 1
25 }
26
27 Error() {
28         echo "$NAME: $*"
29         exit 1
30 }
31
32 [ -r "/etc/nagios3/system.cfg" ] || Error "Can't read /etc/nagios3/system.cfg!"
33 . /etc/nagios3/system.cfg 
34
35 # Validate command line arguments
36 if [ "$1" = "-c" ]; then
37         CREATE=1; shift
38 fi
39 [ $# -ge 2 -a $# -le 3 ] || Usage
40 [ "$2" = 0 -o "$2" = "1" -o "$2" = "2" -o "$2" = "3" ] || Usage
41
42 # Validate configuration
43 [ -w "$CMDFILE" ] || Error "Can't write to \"$CMDFILE\"!"
44
45 time_t=`date +%s`
46 host="$1"
47 code="$2"
48 text="${3:-}"
49
50 grep -E "^[[:space:]]+host_name[[:space:]]+${host}\$" "$CFGDIR"/*.cfg >/dev/null 2>&1
51 if [ $? -ne 0 ]; then
52         # Host not configured!
53         [ -n "$CREATE" ] || Error "Host \"$host\" not configured!?"
54         echo "Creating new Nagios host configuration for \"$host\" ..."
55         cfg="$CFGDIR/_host_$host.cfg"
56         cat >"$cfg" <<EOF
57 define host {
58         use             ${HOST_TEMPLATE_PASSIVE}
59         host_name       $host
60 }
61 EOF
62         chmod "$CFG_MODE" "$cfg" || Error "chmod(1) failed!"
63         echo "Reloading Nagios configuration ..."
64         "$RCFILE" reload || Error "Failed to reload Nagios configuration!"
65         sleep 1
66 fi
67
68 cmd="[$time_t] PROCESS_HOST_CHECK_RESULT;$host;$code;$text"
69 echo "$cmd" >>"$CMDFILE"