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