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 for (int i = 0; i < ci->children_num; i++)
47 {
48 oconfig_item_t *child = ci->children + i;
50 if (strcasecmp ("CommandFile", child->key) == 0)
51 cf_util_get_string (child, &nagios_command_file);
52 else
53 WARNING ("notify_nagios plugin: Ignoring unknown config option \"%s\".",
54 child->key);
55 }
57 return 0;
58 } /* }}} nagios_config */
60 static int nagios_print (char const *buffer) /* {{{ */
61 {
62 char const *file = NAGIOS_COMMAND_FILE;
63 int fd;
64 int status;
65 struct flock lock = { 0 };
67 if (nagios_command_file != NULL)
68 file = nagios_command_file;
70 fd = open (file, O_WRONLY | O_APPEND);
71 if (fd < 0)
72 {
73 char errbuf[1024];
74 status = errno;
75 ERROR ("notify_nagios plugin: Opening \"%s\" failed: %s",
76 file, sstrerror (status, errbuf, sizeof (errbuf)));
77 return status;
78 }
80 lock.l_type = F_WRLCK;
81 lock.l_whence = SEEK_END;
83 status = fcntl (fd, F_GETLK, &lock);
84 if (status != 0)
85 {
86 char errbuf[1024];
87 status = errno;
88 ERROR ("notify_nagios plugin: Failed to acquire write lock on \"%s\": %s",
89 file, sstrerror (status, errbuf, sizeof (errbuf)));
90 close (fd);
91 return status;
92 }
94 status = (int) lseek (fd, 0, SEEK_END);
95 if (status == -1)
96 {
97 char errbuf[1024];
98 status = errno;
99 ERROR ("notify_nagios plugin: Seeking to end of \"%s\" failed: %s",
100 file, sstrerror (status, errbuf, sizeof (errbuf)));
101 close (fd);
102 return status;
103 }
105 status = (int) swrite (fd, buffer, strlen (buffer));
106 if (status != 0)
107 {
108 char errbuf[1024];
109 status = errno;
110 ERROR ("notify_nagios plugin: Writing to \"%s\" failed: %s",
111 file, sstrerror (status, errbuf, sizeof (errbuf)));
112 close (fd);
113 return status;
114 }
116 close (fd);
117 return status;
118 } /* }}} int nagios_print */
120 static int nagios_notify (const notification_t *n, /* {{{ */
121 __attribute__((unused)) user_data_t *user_data)
122 {
123 char svc_description[4 * DATA_MAX_NAME_LEN];
124 char buffer[4096];
125 int code;
126 int status;
128 status = format_name (svc_description, (int) sizeof (svc_description),
129 /* host */ "", n->plugin, n->plugin_instance, n->type, n->type_instance);
130 if (status != 0)
131 {
132 ERROR ("notify_nagios plugin: Formatting service name failed.");
133 return status;
134 }
136 switch (n->severity)
137 {
138 case NOTIF_OKAY:
139 code = NAGIOS_OK;
140 break;
141 case NOTIF_WARNING:
142 code = NAGIOS_WARNING;
143 break;
144 case NOTIF_FAILURE:
145 code = NAGIOS_CRITICAL;
146 break;
147 default:
148 code = NAGIOS_UNKNOWN;
149 break;
150 }
152 ssnprintf (buffer, sizeof (buffer),
153 "[%.0f] PROCESS_SERVICE_CHECK_RESULT;%s;%s;%d;%s\n",
154 CDTIME_T_TO_DOUBLE (n->time), n->host, &svc_description[1], code,
155 n->message);
157 return nagios_print (buffer);
158 } /* }}} int nagios_notify */
160 void module_register (void)
161 {
162 plugin_register_complex_config ("notify_nagios", nagios_config);
163 plugin_register_notification ("notify_nagios", nagios_notify, NULL);
164 } /* void module_register (void) */
166 /* vim: set sw=2 sts=2 ts=8 et : */