NagCollect is licensed under the terms of the GNU General Public License
[nagcollect.git] / server / bin / nagios-submit-service
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-service -- Submit passive service check result
13 #
14
15 NAME=`basename "$0"`
16 CREATE=
17
18 Usage() {
19         echo "$NAME [-c] <host> <service> <code> [<text>]"; echo
20         echo "  -c         create new service, if necessary"
21         echo "  <host>     Nagios host ID"
22         echo "  <service>  Nagios service ID"
23         echo "  <code>     Status code: 0=OK, 1=Warning, 2=Error, 3=Unknown"
24         echo "  <text>     Optional status message"
25         echo; exit 1
26 }
27
28 Error() {
29         echo "$NAME: $*"
30         exit 1
31 }
32
33 [ -r "/etc/nagios3/system.cfg" ] || Error "Can't read /etc/nagios3/system.cfg!"
34 . /etc/nagios3/system.cfg 
35
36 # Validate command line arguments
37 if [ "$1" = "-c" ]; then
38         CREATE=1; shift
39 fi
40 [ $# -ge 3 -a $# -le 4 ] || Usage
41 [ "$3" = 0 -o "$3" = "1" -o "$3" = "2" -o "$3" = "3" ] || Usage
42
43 # Validate configuration
44 [ -w "$CMDFILE" ] || Error "Can't write to \"$CMDFILE\"!"
45
46 time_t=`date +%s`
47 host="$1"
48 service="$2"
49 code="$3"
50 text="${4:-}"
51
52 cfg=`grep -lE "^[[:space:]]+host_name[[:space:]]+${host}\$" "$CFGDIR"/*.cfg`
53 [ -n "$cfg" ] || Error "Host \"$host\" not configured!?"
54
55 case "$service" in
56         *"_p")
57                 # Passive service check; does it exist already?
58                 grep -E "^[[:space:]]+service_description[[:space:]]+${service}\$" \
59                         "$cfg" >/dev/null 2>&1
60                 if [ $? -ne 0 ]; then
61                         [ -n "$CREATE" ] || \
62                                 Error "Service \"$service\" not configured on \"$host\"!?"
63                         echo "Creating new Nagios host configuration for \"$host\" ..."
64                         cat >>"$cfg" <<EOF
65
66 define service {
67         use                     ${SERVICE_TEMPLATE_PASSIVE}
68         service_description     $service
69         check_command           ${SERVICE_PASSIVE_CMD}
70         host                    $host
71 }
72 EOF
73                         chmod "$CFG_MODE" "$cfg" || Error "chmod(1) failed!"
74                         echo "Reloading Nagios configuration ..."
75                         "$RCFILE" reload || Error "Failed to reload Nagios configuration!"
76                         sleep 1
77                 fi
78                 ;;
79 esac
80
81 cmd="[$time_t] PROCESS_SERVICE_CHECK_RESULT;$host;$service;$code;$text"
82 echo "$cmd" >>"$CMDFILE"