2 * collectd - src/syslog.c
3 * Copyright (C) 2007 Florian Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Florian Forster <octo at verplant.org>
32 static int log_level = LOG_DEBUG;
34 static int log_level = LOG_INFO;
35 #endif /* COLLECT_DEBUG */
36 static int notif_severity = 0;
38 static const char *config_keys[] =
43 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
45 static int sl_config (const char *key, const char *value)
47 if (strcasecmp (key, "LogLevel") == 0)
49 log_level = parse_log_severity (value);
53 else if (strcasecmp (key, "NotifyLevel") == 0)
55 notif_severity = parse_notif_severity (value);
56 if (notif_severity < 0)
63 static void sl_log (int severity, const char *msg,
64 user_data_t __attribute__((unused)) *user_data)
66 if (severity > log_level)
69 syslog (severity, "%s", msg);
72 static int sl_shutdown (void)
79 static int sl_notification (const notification_t *n,
80 user_data_t __attribute__((unused)) *user_data)
85 char *severity_string;
88 if (n->severity > notif_severity)
94 severity_string = "FAILURE";
95 log_severity = LOG_ERR;
98 severity_string = "WARNING";
99 log_severity = LOG_WARNING;
102 severity_string = "OKAY";
103 log_severity = LOG_NOTICE;
106 severity_string = "UNKNOWN";
107 log_severity = LOG_ERR;
110 #define BUFFER_ADD(...) do { \
111 status = ssnprintf (&buf[offset], sizeof (buf) - offset, \
115 else if (((size_t) status) >= (sizeof (buf) - offset)) \
118 offset += ((size_t) status); \
121 #define BUFFER_ADD_FIELD(field) do { \
123 BUFFER_ADD (", " #field " = %s", n->field); \
126 BUFFER_ADD ("Notification: severity = %s", severity_string);
127 BUFFER_ADD_FIELD (host);
128 BUFFER_ADD_FIELD (plugin);
129 BUFFER_ADD_FIELD (plugin_instance);
130 BUFFER_ADD_FIELD (type);
131 BUFFER_ADD_FIELD (type_instance);
132 BUFFER_ADD_FIELD (message);
134 #undef BUFFER_ADD_FIELD
137 buf[sizeof (buf) - 1] = '\0';
139 sl_log (log_severity, buf, NULL);
142 } /* int sl_notification */
144 void module_register (void)
146 openlog ("collectd", LOG_CONS | LOG_PID, LOG_DAEMON);
148 plugin_register_config ("syslog", sl_config, config_keys, config_keys_num);
149 plugin_register_log ("syslog", sl_log, /* user_data = */ NULL);
150 plugin_register_notification ("syslog", sl_notification, NULL);
151 plugin_register_shutdown ("syslog", sl_shutdown);
152 } /* void module_register(void) */