From 45bd61ea2b32000fcfeafebf89527d99a6446be9 Mon Sep 17 00:00:00 2001 From: Fabien Wernli Date: Wed, 26 Oct 2011 11:22:46 +0200 Subject: [PATCH] Add notifications to syslog plugin Deduplicated some code from logfile and syslog and added NotifyLevel option to plugin. Change-Id: I364067189d628420333cb625c885a256399e076a Signed-off-by: Florian Forster --- src/collectd.conf.pod | 11 +++++- src/logfile.c | 19 +-------- src/plugin.c | 38 ++++++++++++++++++ src/plugin.h | 2 + src/syslog.c | 92 +++++++++++++++++++++++++++++++++---------- 5 files changed, 124 insertions(+), 38 deletions(-) diff --git a/src/collectd.conf.pod b/src/collectd.conf.pod index d4f54905..c5a75dc7 100644 --- a/src/collectd.conf.pod +++ b/src/collectd.conf.pod @@ -4232,6 +4232,15 @@ syslog-daemon. Please note that B is only available if collectd has been compiled with debugging support. +=item B B|B + +Controls which notifications should be sent to syslog. The default behaviour is +not to send any. If either of C or C is used, C notifications +will also be sent to syslog. +Notifications will be sent using severities based on their own levels. B +and B will be sent using syslog B severity, whereas B +will yield a B syslog entry. + =back =head2 Plugin C @@ -5537,7 +5546,7 @@ convert counter values to rates. Please note that these placeholders are B! -=item B B<"FATAL">|B<"WARNING">|B<"OKAY"> +=item B B<"FAILURE">|B<"WARNING">|B<"OKAY"> Sets the severity of the message. If omitted, the severity B<"WARNING"> is used. diff --git a/src/logfile.c b/src/logfile.c index 60fb5d92..ded7732b 100644 --- a/src/logfile.c +++ b/src/logfile.c @@ -53,23 +53,8 @@ static int config_keys_num = STATIC_ARRAY_SIZE (config_keys); static int logfile_config (const char *key, const char *value) { if (0 == strcasecmp (key, "LogLevel")) { - if ((0 == strcasecmp (value, "emerg")) - || (0 == strcasecmp (value, "alert")) - || (0 == strcasecmp (value, "crit")) - || (0 == strcasecmp (value, "err"))) - log_level = LOG_ERR; - else if (0 == strcasecmp (value, "warning")) - log_level = LOG_WARNING; - else if (0 == strcasecmp (value, "notice")) - log_level = LOG_NOTICE; - else if (0 == strcasecmp (value, "info")) - log_level = LOG_INFO; -#if COLLECT_DEBUG - else if (0 == strcasecmp (value, "debug")) - log_level = LOG_DEBUG; -#endif /* COLLECT_DEBUG */ - else - return 1; + log_level = parse_log_severity(value); + if (log_level == -1) return 1; /* to keep previous behaviour */ } else if (0 == strcasecmp (key, "File")) { sfree (log_file); diff --git a/src/plugin.c b/src/plugin.c index 91c40b6b..fac4d779 100644 --- a/src/plugin.c +++ b/src/plugin.c @@ -1706,6 +1706,44 @@ void plugin_log (int level, const char *format, ...) } } /* void plugin_log */ +int parse_log_severity (const char *severity) +{ + int log_level = -1; + + if ((0 == strcasecmp (severity, "emerg")) + || (0 == strcasecmp (severity, "alert")) + || (0 == strcasecmp (severity, "crit")) + || (0 == strcasecmp (severity, "err"))) + log_level = LOG_ERR; + else if (0 == strcasecmp (severity, "warning")) + log_level = LOG_WARNING; + else if (0 == strcasecmp (severity, "notice")) + log_level = LOG_NOTICE; + else if (0 == strcasecmp (severity, "info")) + log_level = LOG_INFO; +#if COLLECT_DEBUG + else if (0 == strcasecmp (severity, "debug")) + log_level = LOG_DEBUG; +#endif /* COLLECT_DEBUG */ + + return (log_level); +} /* int parse_log_severity */ + +int parse_notif_severity (const char *severity) +{ + int notif_severity = -1; + + if (strcasecmp (severity, "FAILURE")) + notif_severity = NOTIF_FAILURE; + else if (strcmp (severity, "OKAY")) + notif_severity = NOTIF_OKAY; + else if ((strcmp (severity, "WARNING")) + || (strcmp (severity, "WARN"))) + notif_severity = NOTIF_WARNING; + + return (notif_severity); +} /* int parse_notif_severity */ + const data_set_t *plugin_get_ds (const char *name) { data_set_t *ds; diff --git a/src/plugin.h b/src/plugin.h index 86d40340..56f927be 100644 --- a/src/plugin.h +++ b/src/plugin.h @@ -325,6 +325,8 @@ int plugin_dispatch_notification (const notification_t *notif); void plugin_log (int level, const char *format, ...) __attribute__ ((format(printf,2,3))); +int parse_log_severity (const char *severity); +int parse_notif_severity (const char *severity); #define ERROR(...) plugin_log (LOG_ERR, __VA_ARGS__) #define WARNING(...) plugin_log (LOG_WARNING, __VA_ARGS__) diff --git a/src/syslog.c b/src/syslog.c index ace9dc6f..fc34e56c 100644 --- a/src/syslog.c +++ b/src/syslog.c @@ -33,10 +33,12 @@ static int log_level = LOG_DEBUG; #else static int log_level = LOG_INFO; #endif /* COLLECT_DEBUG */ +static int notif_severity = -1; static const char *config_keys[] = { - "LogLevel" + "LogLevel", + "NotifyLevel", }; static int config_keys_num = STATIC_ARRAY_SIZE(config_keys); @@ -44,26 +46,13 @@ static int sl_config (const char *key, const char *value) { if (strcasecmp (key, "LogLevel") == 0) { - if ((strcasecmp (value, "emerg") == 0) - || (strcasecmp (value, "alert") == 0) - || (strcasecmp (value, "crit") == 0) - || (strcasecmp (value, "err") == 0)) - log_level = LOG_ERR; - else if (strcasecmp (value, "warning") == 0) - log_level = LOG_WARNING; - else if (strcasecmp (value, "notice") == 0) - log_level = LOG_NOTICE; - else if (strcasecmp (value, "info") == 0) - log_level = LOG_INFO; -#if COLLECT_DEBUG - else if (strcasecmp (value, "debug") == 0) - log_level = LOG_DEBUG; -#endif - else - return (1); + log_level = parse_log_severity (value); + if (log_level == -1) return (1); + } + else if (strcasecmp (key, "NotifyLevel") == 0) + { + notif_severity = parse_notif_severity(key); } - else - return (-1); return (0); } /* int sl_config */ @@ -84,11 +73,74 @@ static int sl_shutdown (void) return (0); } +static int sl_notification (const notification_t *n, + user_data_t __attribute__((unused)) *user_data) +{ + char buf[1024] = ""; + char *buf_ptr = buf; + int buf_len = sizeof (buf); + int status; + int severity; + + /* do nothing if parsing of NotifSeverity failed */ + if (notif_severity == -1) + return 0; + /* do nothing if NotifSeverity is higer than notification + * note that OKAY notifs will always be displayed */ + if ((notif_severity == NOTIF_FAILURE) && (n -> severity == NOTIF_WARNING)) + return 0; + + status = ssnprintf (buf_ptr, buf_len, "Notification: severity = %s", + (n->severity == NOTIF_FAILURE) ? "FAILURE" + : ((n->severity == NOTIF_WARNING) ? "WARNING" + : ((n->severity == NOTIF_OKAY) ? "OKAY" : "UNKNOWN"))); + if (status > 0) + { + buf_ptr += status; + buf_len -= status; + } + +#define APPEND(bufptr, buflen, key, value) \ + if ((buflen > 0) && (strlen (value) > 0)) { \ + int status = ssnprintf (bufptr, buflen, ", %s = %s", key, value); \ + if (status > 0) { \ + bufptr += status; \ + buflen -= status; \ + } \ + } + APPEND (buf_ptr, buf_len, "host", n->host); + APPEND (buf_ptr, buf_len, "plugin", n->plugin); + APPEND (buf_ptr, buf_len, "plugin_instance", n->plugin_instance); + APPEND (buf_ptr, buf_len, "type", n->type); + APPEND (buf_ptr, buf_len, "type_instance", n->type_instance); + APPEND (buf_ptr, buf_len, "message", n->message); + + buf[sizeof (buf) - 1] = '\0'; + + switch (n->severity) + { + case NOTIF_FAILURE: + severity = LOG_ERR; + break; + case NOTIF_WARNING: + severity = LOG_WARNING; + break; + case NOTIF_OKAY: + severity = LOG_WARNING; + break; + default: severity = LOG_INFO; + } + sl_log (severity, buf, NULL); + + return (0); +} /* int sl_notification */ + void module_register (void) { openlog ("collectd", LOG_CONS | LOG_PID, LOG_DAEMON); plugin_register_config ("syslog", sl_config, config_keys, config_keys_num); plugin_register_log ("syslog", sl_log, /* user_data = */ NULL); + plugin_register_notification ("syslog", sl_notification, NULL); plugin_register_shutdown ("syslog", sl_shutdown); } /* void module_register(void) */ -- 2.30.2