summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 4a7c22a)
raw | patch | inline | side by side (parent: 4a7c22a)
author | Fabien Wernli <cpan@faxm0dem.org> | |
Wed, 26 Oct 2011 09:22:46 +0000 (11:22 +0200) | ||
committer | Florian Forster <octo@collectd.org> | |
Sat, 25 Feb 2012 16:09:22 +0000 (17:09 +0100) |
Deduplicated some code from logfile and syslog
and added NotifyLevel option to plugin.
Change-Id: I364067189d628420333cb625c885a256399e076a
Signed-off-by: Florian Forster <octo@collectd.org>
and added NotifyLevel option to plugin.
Change-Id: I364067189d628420333cb625c885a256399e076a
Signed-off-by: Florian Forster <octo@collectd.org>
src/collectd.conf.pod | patch | blob | history | |
src/logfile.c | patch | blob | history | |
src/plugin.c | patch | blob | history | |
src/plugin.h | patch | blob | history | |
src/syslog.c | patch | blob | history |
diff --git a/src/collectd.conf.pod b/src/collectd.conf.pod
index d4f549056709621e6a4689b73035d430942479af..c5a75dc77fe9dd21af6182977cb69cdb65c7fd97 100644 (file)
--- a/src/collectd.conf.pod
+++ b/src/collectd.conf.pod
Please note that B<debug> is only available if collectd has been compiled with
debugging support.
+=item B<NotifyLevel> B<WARNING>|B<FAILURE>
+
+Controls which notifications should be sent to syslog. The default behaviour is
+not to send any. If either of C<WARNING> or C<FAILURE> is used, C<OKAY> notifications
+will also be sent to syslog.
+Notifications will be sent using severities based on their own levels. B<OKAY>
+and B<WARNING> will be sent using syslog B<WARNING> severity, whereas B<FAILURE>
+will yield a B<ERROR> syslog entry.
+
=back
=head2 Plugin C<table>
Please note that these placeholders are B<case sensitive>!
-=item B<Severity> B<"FATAL">|B<"WARNING">|B<"OKAY">
+=item B<Severity> 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 60fb5d9202ecfc5e5d9ddc2252820732e5586433..ded7732b00ce22fcc648cbb8ae2207c53f26db94 100644 (file)
--- a/src/logfile.c
+++ b/src/logfile.c
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 91c40b6bad115203cb021f95bd919583310c9460..fac4d779568c3d98deac69a354010109606a60a4 100644 (file)
--- a/src/plugin.c
+++ b/src/plugin.c
}
} /* 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 86d403400e88af2c04e987548aed48ad5d98fd57..56f927be4f083854ac32bbcb275a254f89225c61 100644 (file)
--- a/src/plugin.h
+++ b/src/plugin.h
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 ace9dc6f090e1a98a8bc3a0d4d08a25b39f6f21b..fc34e56c29cc6d00b4f953a2550139ca611df20f 100644 (file)
--- a/src/syslog.c
+++ b/src/syslog.c
#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);
{
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 */
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) */