1 /**
2 * collectd - src/notify_nagios.c
3 * Copyright (C) 2015 Florian octo Forster
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Florian octo Forster <octo at collectd.org>
25 */
27 #include "collectd.h"
29 #include "plugin.h"
30 #include "common.h"
31 #include "configfile.h"
33 #define NAGIOS_OK 0
34 #define NAGIOS_WARNING 1
35 #define NAGIOS_CRITICAL 2
36 #define NAGIOS_UNKNOWN 3
38 #ifndef NAGIOS_COMMAND_FILE
39 # define NAGIOS_COMMAND_FILE "/usr/local/nagios/var/rw/nagios.cmd"
40 #endif
42 static char *nagios_command_file;
44 static int nagios_config (oconfig_item_t *ci) /* {{{ */
45 {
46 int i;
48 for (i = 0; i < ci->children_num; i++)
49 {
50 oconfig_item_t *child = ci->children + i;
52 if (strcasecmp ("CommandFile", child->key) == 0)
53 cf_util_get_string (child, &nagios_command_file);
54 else
55 WARNING ("notify_nagios plugin: Ignoring unknown config option \"%s\".",
56 child->key);
57 }
59 return 0;
60 } /* }}} nagios_config */
62 static int nagios_print (char const *buffer) /* {{{ */
63 {
64 char const *file = NAGIOS_COMMAND_FILE;
65 int fd;
66 int status;
67 struct flock lock = { 0 };
69 if (nagios_command_file != NULL)
70 file = nagios_command_file;
72 fd = open (file, O_WRONLY | O_APPEND);
73 if (fd < 0)
74 {
75 char errbuf[1024];
76 status = errno;
77 ERROR ("notify_nagios plugin: Opening \"%s\" failed: %s",
78 file, sstrerror (status, errbuf, sizeof (errbuf)));
79 return status;
80 }
82 lock.l_type = F_WRLCK;
83 lock.l_whence = SEEK_END;
85 status = fcntl (fd, F_GETLK, &lock);
86 if (status != 0)
87 {
88 char errbuf[1024];
89 status = errno;
90 ERROR ("notify_nagios plugin: Failed to acquire write lock on \"%s\": %s",
91 file, sstrerror (status, errbuf, sizeof (errbuf)));
92 close (fd);
93 return status;
94 }
96 status = (int) lseek (fd, 0, SEEK_END);
97 if (status == -1)
98 {
99 char errbuf[1024];
100 status = errno;
101 ERROR ("notify_nagios plugin: Seeking to end of \"%s\" failed: %s",
102 file, sstrerror (status, errbuf, sizeof (errbuf)));
103 close (fd);
104 return status;
105 }
107 status = (int) swrite (fd, buffer, strlen (buffer));
108 if (status != 0)
109 {
110 char errbuf[1024];
111 status = errno;
112 ERROR ("notify_nagios plugin: Writing to \"%s\" failed: %s",
113 file, sstrerror (status, errbuf, sizeof (errbuf)));
114 close (fd);
115 return status;
116 }
118 close (fd);
119 return status;
120 } /* }}} int nagios_print */
122 static int nagios_notify (const notification_t *n, /* {{{ */
123 __attribute__((unused)) user_data_t *user_data)
124 {
125 char svc_description[4 * DATA_MAX_NAME_LEN];
126 char buffer[4096];
127 int code;
128 int status;
130 status = format_name (svc_description, (int) sizeof (svc_description),
131 /* host */ "", n->plugin, n->plugin_instance, n->type, n->type_instance);
132 if (status != 0)
133 {
134 ERROR ("notify_nagios plugin: Formatting service name failed.");
135 return status;
136 }
138 switch (n->severity)
139 {
140 case NOTIF_OKAY:
141 code = NAGIOS_OK;
142 break;
143 case NOTIF_WARNING:
144 code = NAGIOS_WARNING;
145 break;
146 case NOTIF_FAILURE:
147 code = NAGIOS_CRITICAL;
148 break;
149 default:
150 code = NAGIOS_UNKNOWN;
151 break;
152 }
154 ssnprintf (buffer, sizeof (buffer),
155 "[%.0f] PROCESS_SERVICE_CHECK_RESULT;%s;%s;%d;%s\n",
156 CDTIME_T_TO_DOUBLE (n->time), n->host, &svc_description[1], code,
157 n->message);
159 return nagios_print (buffer);
160 } /* }}} int nagios_notify */
162 void module_register (void)
163 {
164 plugin_register_complex_config ("notify_nagios", nagios_config);
165 plugin_register_notification ("notify_nagios", nagios_notify, NULL);
166 } /* void module_register (void) */
168 /* vim: set sw=2 sts=2 ts=8 et : */