Merge pull request #1875 from rubenk/remove-configfile-h-from-plugins
[collectd.git] / src / notify_nagios.c
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  */
26
27 #include "collectd.h"
28
29 #include "plugin.h"
30 #include "common.h"
31
32 #define NAGIOS_OK       0
33 #define NAGIOS_WARNING  1
34 #define NAGIOS_CRITICAL 2
35 #define NAGIOS_UNKNOWN  3
36
37 #ifndef NAGIOS_COMMAND_FILE
38 # define NAGIOS_COMMAND_FILE "/usr/local/nagios/var/rw/nagios.cmd"
39 #endif
40
41 static char *nagios_command_file;
42
43 static int nagios_config (oconfig_item_t *ci) /* {{{ */
44 {
45   for (int i = 0; i < ci->children_num; i++)
46   {
47     oconfig_item_t *child = ci->children + i;
48
49     if (strcasecmp ("CommandFile", child->key) == 0)
50       cf_util_get_string (child, &nagios_command_file);
51     else
52       WARNING ("notify_nagios plugin: Ignoring unknown config option \"%s\".",
53           child->key);
54   }
55
56   return 0;
57 } /* }}} nagios_config */
58
59 static int nagios_print (char const *buffer) /* {{{ */
60 {
61   char const *file = NAGIOS_COMMAND_FILE;
62   int fd;
63   int status;
64   struct flock lock = { 0 };
65
66   if (nagios_command_file != NULL)
67     file = nagios_command_file;
68
69   fd = open (file, O_WRONLY | O_APPEND);
70   if (fd < 0)
71   {
72     char errbuf[1024];
73     status = errno;
74     ERROR ("notify_nagios plugin: Opening \"%s\" failed: %s",
75         file, sstrerror (status, errbuf, sizeof (errbuf)));
76     return status;
77   }
78
79   lock.l_type = F_WRLCK;
80   lock.l_whence = SEEK_END;
81
82   status = fcntl (fd, F_GETLK, &lock);
83   if (status != 0)
84   {
85     char errbuf[1024];
86     status = errno;
87     ERROR ("notify_nagios plugin: Failed to acquire write lock on \"%s\": %s",
88         file, sstrerror (status, errbuf, sizeof (errbuf)));
89     close (fd);
90     return status;
91   }
92
93   status = (int) lseek (fd, 0, SEEK_END);
94   if (status == -1)
95   {
96     char errbuf[1024];
97     status = errno;
98     ERROR ("notify_nagios plugin: Seeking to end of \"%s\" failed: %s",
99         file, sstrerror (status, errbuf, sizeof (errbuf)));
100     close (fd);
101     return status;
102   }
103
104   status = (int) swrite (fd, buffer, strlen (buffer));
105   if (status != 0)
106   {
107     char errbuf[1024];
108     status = errno;
109     ERROR ("notify_nagios plugin: Writing to \"%s\" failed: %s",
110         file, sstrerror (status, errbuf, sizeof (errbuf)));
111     close (fd);
112     return status;
113   }
114
115   close (fd);
116   return status;
117 } /* }}} int nagios_print */
118
119 static int nagios_notify (const notification_t *n, /* {{{ */
120     __attribute__((unused)) user_data_t *user_data)
121 {
122   char svc_description[4 * DATA_MAX_NAME_LEN];
123   char buffer[4096];
124   int code;
125   int status;
126
127   status = format_name (svc_description, (int) sizeof (svc_description),
128       /* host */ "", n->plugin, n->plugin_instance, n->type, n->type_instance);
129   if (status != 0)
130   {
131     ERROR ("notify_nagios plugin: Formatting service name failed.");
132     return status;
133   }
134
135   switch (n->severity)
136   {
137     case NOTIF_OKAY:
138       code = NAGIOS_OK;
139       break;
140     case NOTIF_WARNING:
141       code = NAGIOS_WARNING;
142       break;
143     case NOTIF_FAILURE:
144       code = NAGIOS_CRITICAL;
145       break;
146     default:
147       code = NAGIOS_UNKNOWN;
148       break;
149   }
150
151   ssnprintf (buffer, sizeof (buffer),
152       "[%.0f] PROCESS_SERVICE_CHECK_RESULT;%s;%s;%d;%s\n",
153       CDTIME_T_TO_DOUBLE (n->time), n->host, &svc_description[1], code,
154       n->message);
155
156   return nagios_print (buffer);
157 } /* }}} int nagios_notify */
158
159 void module_register (void)
160 {
161   plugin_register_complex_config ("notify_nagios", nagios_config);
162   plugin_register_notification ("notify_nagios", nagios_notify, NULL);
163 } /* void module_register (void) */
164
165 /* vim: set sw=2 sts=2 ts=8 et : */