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