2 * collectd - src/syslog.c
3 * Copyright (C) 2007 Florian Forster
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
24 * Florian Forster <octo at collectd.org>
30 #include "utils/common/common.h"
37 static int log_level = LOG_DEBUG;
39 static int log_level = LOG_INFO;
40 #endif /* COLLECT_DEBUG */
41 static int notif_severity;
43 static const char *config_keys[] = {
47 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
49 static int sl_config(const char *key, const char *value) {
50 if (strcasecmp(key, "LogLevel") == 0) {
51 log_level = parse_log_severity(value);
54 WARNING("syslog: invalid loglevel [%s] defaulting to 'info'", value);
56 } else if (strcasecmp(key, "NotifyLevel") == 0) {
57 notif_severity = parse_notif_severity(value);
58 if (notif_severity < 0) {
59 ERROR("syslog: invalid notification severity [%s]", value);
67 static void sl_log(int severity, const char *msg,
68 user_data_t __attribute__((unused)) * user_data) {
69 if (severity > log_level)
72 syslog(severity, "%s", msg);
75 static int sl_shutdown(void) {
81 static int sl_notification(const notification_t *n,
82 user_data_t __attribute__((unused)) * user_data) {
86 const char *severity_string;
89 if (n->severity > notif_severity)
92 switch (n->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(...) \
112 status = snprintf(&buf[offset], sizeof(buf) - offset, __VA_ARGS__); \
115 else if (((size_t)status) >= (sizeof(buf) - offset)) \
118 offset += ((size_t)status); \
121 #define BUFFER_ADD_FIELD(field) \
124 BUFFER_ADD(", " #field " = %s", n->field); \
127 BUFFER_ADD("Notification: severity = %s", severity_string);
128 BUFFER_ADD_FIELD(host);
129 BUFFER_ADD_FIELD(plugin);
130 BUFFER_ADD_FIELD(plugin_instance);
131 BUFFER_ADD_FIELD(type);
132 BUFFER_ADD_FIELD(type_instance);
133 BUFFER_ADD_FIELD(message);
135 #undef BUFFER_ADD_FIELD
138 buf[sizeof(buf) - 1] = '\0';
140 sl_log(log_severity, buf, NULL);
143 } /* int sl_notification */
145 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) */