From 3e14c965e9c8036cb85fcc1898c9acc82f6f0b15 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Thu, 5 Nov 2015 14:42:29 +0100 Subject: [PATCH] notify_nagios plugin: Add plugin for writing Nagios passive check results. --- configure.ac | 2 + src/Makefile.am | 6 ++ src/collectd.conf.in | 5 ++ src/collectd.conf.pod | 15 +++++ src/notify_nagios.c | 170 ++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 198 insertions(+) create mode 100644 src/notify_nagios.c diff --git a/configure.ac b/configure.ac index 3f5f2d8b..e648f648 100644 --- a/configure.ac +++ b/configure.ac @@ -5703,6 +5703,7 @@ AC_PLUGIN([nfs], [$plugin_nfs], [NFS statistics]) AC_PLUGIN([nginx], [$with_libcurl], [nginx statistics]) AC_PLUGIN([notify_desktop], [$with_libnotify], [Desktop notifications]) AC_PLUGIN([notify_email], [$with_libesmtp], [Email notifier]) +AC_PLUGIN([notify_nagios], [yes], [Nagios notification plugin]) AC_PLUGIN([ntpd], [yes], [NTPd statistics]) AC_PLUGIN([numa], [$plugin_numa], [NUMA virtual memory statistics]) AC_PLUGIN([nut], [$with_libupsclient], [Network UPS tools statistics]) @@ -6102,6 +6103,7 @@ Configuration: nginx . . . . . . . . $enable_nginx notify_desktop . . . $enable_notify_desktop notify_email . . . . $enable_notify_email + notify_nagios . . . . $enable_notify_nagios ntpd . . . . . . . . $enable_ntpd numa . . . . . . . . $enable_numa nut . . . . . . . . . $enable_nut diff --git a/src/Makefile.am b/src/Makefile.am index d0bf5463..5218054c 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -748,6 +748,12 @@ notify_email_la_LDFLAGS = $(PLUGIN_LDFLAGS) notify_email_la_LIBADD = -lesmtp -lssl -lcrypto -lpthread endif +if BUILD_PLUGIN_NOTIFY_NAGIOS +pkglib_LTLIBRARIES += notify_nagios.la +notify_nagios_la_SOURCES = notify_nagios.c +notify_nagios_la_LDFLAGS = $(PLUGIN_LDFLAGS) +endif + if BUILD_PLUGIN_NTPD pkglib_LTLIBRARIES += ntpd.la ntpd_la_SOURCES = ntpd.c diff --git a/src/collectd.conf.in b/src/collectd.conf.in index 1f4ccf81..649b2f3c 100644 --- a/src/collectd.conf.in +++ b/src/collectd.conf.in @@ -150,6 +150,7 @@ #@BUILD_PLUGIN_NGINX_TRUE@LoadPlugin nginx #@BUILD_PLUGIN_NOTIFY_DESKTOP_TRUE@LoadPlugin notify_desktop #@BUILD_PLUGIN_NOTIFY_EMAIL_TRUE@LoadPlugin notify_email +#@BUILD_PLUGIN_NOTIFY_NAGIOS_TRUE@LoadPlugin notify_nagios #@BUILD_PLUGIN_NTPD_TRUE@LoadPlugin ntpd #@BUILD_PLUGIN_NUMA_TRUE@LoadPlugin numa #@BUILD_PLUGIN_NUT_TRUE@LoadPlugin nut @@ -833,6 +834,10 @@ # Recipient "email2@domain2.com" # +# +# CommandFile "/usr/local/nagios/var/rw/nagios.cmd" +# + # # Host "localhost" # Port 123 diff --git a/src/collectd.conf.pod b/src/collectd.conf.pod index 8a508089..8a034cd9 100644 --- a/src/collectd.conf.pod +++ b/src/collectd.conf.pod @@ -4404,6 +4404,21 @@ Default: C =back +=head2 Plugin C + +The I plugin writes notifications to Nagios' I as +a I. + +Available configuration options: + +=over 4 + +=item B I + +Sets the I to write to. Defaults to F. + +=back + =head2 Plugin C =over 4 diff --git a/src/notify_nagios.c b/src/notify_nagios.c new file mode 100644 index 00000000..1f4182bb --- /dev/null +++ b/src/notify_nagios.c @@ -0,0 +1,170 @@ +/** + * collectd - src/notify_nagios.c + * Copyright (C) 2015 Florian octo Forster + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + * Authors: + * Florian octo Forster + */ + +#include "collectd.h" +#include "plugin.h" +#include "common.h" +#include "configfile.h" + +#define NAGIOS_OK 0 +#define NAGIOS_WARNING 1 +#define NAGIOS_CRITICAL 2 +#define NAGIOS_UNKNOWN 3 + +#ifndef NAGIOS_COMMAND_FILE +# define NAGIOS_COMMAND_FILE "/usr/local/nagios/var/rw/nagios.cmd" +#endif + +static char *nagios_command_file; + +static int nagios_config (oconfig_item_t *ci) /* {{{ */ +{ + int i; + + for (i = 0; i < ci->children_num; i++) + { + oconfig_item_t *child = ci->children + i; + + if (strcasecmp ("CommandFile", child->key) == 0) + cf_util_get_string (child, &nagios_command_file); + else + WARNING ("notify_nagios plugin: Ignoring unknown config option \"%s\".", + child->key); + } + + return 0; +} /* }}} nagios_config */ + +static int nagios_print (char const *buffer) /* {{{ */ +{ + char const *file = NAGIOS_COMMAND_FILE; + int fd; + int status; + struct flock lock; + + if (nagios_command_file != NULL) + file = nagios_command_file; + + fd = open (file, O_WRONLY | O_APPEND); + if (fd < 0) + { + char errbuf[1024]; + status = errno; + ERROR ("notify_nagios plugin: Opening \"%s\" failed: %s", + file, sstrerror (status, errbuf, sizeof (errbuf))); + return status; + } + + memset (&lock, 0, sizeof (lock)); + lock.l_type = F_WRLCK; + lock.l_whence = SEEK_END; + lock.l_start = 0; + lock.l_len = 0; /* to end of file */ + + status = fcntl (fd, F_GETLK, &lock); + if (status != 0) + { + char errbuf[1024]; + status = errno; + ERROR ("notify_nagios plugin: Failed to acquire write lock on \"%s\": %s", + file, sstrerror (status, errbuf, sizeof (errbuf))); + close (fd); + return status; + } + + status = (int) lseek (fd, 0, SEEK_END); + if (status == -1) + { + char errbuf[1024]; + status = errno; + ERROR ("notify_nagios plugin: Seeking to end of \"%s\" failed: %s", + file, sstrerror (status, errbuf, sizeof (errbuf))); + close (fd); + return status; + } + + status = (int) swrite (fd, buffer, strlen (buffer)); + if (status != 0) + { + char errbuf[1024]; + status = errno; + ERROR ("notify_nagios plugin: Writing to \"%s\" failed: %s", + file, sstrerror (status, errbuf, sizeof (errbuf))); + close (fd); + return status; + } + + close (fd); + return status; +} /* }}} int nagios_print */ + +static int nagios_notify (const notification_t *n, /* {{{ */ + __attribute__((unused)) user_data_t *user_data) +{ + char svc_description[4 * DATA_MAX_NAME_LEN]; + char buffer[4096]; + int code; + int status; + + status = format_name (svc_description, (int) sizeof (svc_description), + /* host */ "", n->plugin, n->plugin_instance, n->type, n->type_instance); + if (status != 0) + { + ERROR ("notify_nagios plugin: Formatting service name failed."); + return status; + } + + switch (n->severity) + { + case NOTIF_OKAY: + code = NAGIOS_OK; + break; + case NOTIF_WARNING: + code = NAGIOS_WARNING; + break; + case NOTIF_FAILURE: + code = NAGIOS_CRITICAL; + break; + default: + code = NAGIOS_UNKNOWN; + break; + } + + ssnprintf (buffer, sizeof (buffer), + "[%.0f] PROCESS_SERVICE_CHECK_RESULT;%s;%s;%d;%s\n", + CDTIME_T_TO_DOUBLE (n->time), n->host, &svc_description[1], code, + n->message); + + return nagios_print (buffer); +} /* }}} int nagios_notify */ + +void module_register (void) +{ + plugin_register_complex_config ("notify_nagios", nagios_config); + plugin_register_notification ("notify_nagios", nagios_notify, NULL); +} /* void module_register (void) */ + +/* vim: set sw=2 sts=2 ts=8 et : */ -- 2.11.0