1 /**
2 * collectd - src/syslog.c
3 * Copyright (C) 2007 Florian Forster
4 *
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.
9 *
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.
14 *
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
18 *
19 * Authors:
20 * Florian Forster <octo at verplant.org>
21 **/
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
27 #if HAVE_SYSLOG_H
28 # include <syslog.h>
29 #endif
31 #if COLLECT_DEBUG
32 static int log_level = LOG_DEBUG;
33 #else
34 static int log_level = LOG_INFO;
35 #endif /* COLLECT_DEBUG */
36 static int notif_severity = 0;
38 static const char *config_keys[] =
39 {
40 "LogLevel",
41 "NotifyLevel",
42 };
43 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
45 static int sl_config (const char *key, const char *value)
46 {
47 if (strcasecmp (key, "LogLevel") == 0)
48 {
49 log_level = parse_log_severity (value);
50 if (log_level < 0)
51 {
52 log_level = LOG_INFO;
53 ERROR ("syslog: invalid loglevel [%s] defaulting to 'info'", value);
54 return (1);
55 }
56 }
57 else if (strcasecmp (key, "NotifyLevel") == 0)
58 {
59 notif_severity = parse_notif_severity (value);
60 if (notif_severity < 0)
61 return (1);
62 }
64 return (0);
65 } /* int sl_config */
67 static void sl_log (int severity, const char *msg,
68 user_data_t __attribute__((unused)) *user_data)
69 {
70 if (severity > log_level)
71 return;
73 syslog (severity, "%s", msg);
74 } /* void sl_log */
76 static int sl_shutdown (void)
77 {
78 closelog ();
80 return (0);
81 }
83 static int sl_notification (const notification_t *n,
84 user_data_t __attribute__((unused)) *user_data)
85 {
86 char buf[1024] = "";
87 size_t offset = 0;
88 int log_severity;
89 char *severity_string;
90 int status;
92 if (n->severity > notif_severity)
93 return (0);
95 switch (n->severity)
96 {
97 case NOTIF_FAILURE:
98 severity_string = "FAILURE";
99 log_severity = LOG_ERR;
100 break;
101 case NOTIF_WARNING:
102 severity_string = "WARNING";
103 log_severity = LOG_WARNING;
104 break;
105 case NOTIF_OKAY:
106 severity_string = "OKAY";
107 log_severity = LOG_NOTICE;
108 break;
109 default:
110 severity_string = "UNKNOWN";
111 log_severity = LOG_ERR;
112 }
114 #define BUFFER_ADD(...) do { \
115 status = ssnprintf (&buf[offset], sizeof (buf) - offset, \
116 __VA_ARGS__); \
117 if (status < 1) \
118 return (-1); \
119 else if (((size_t) status) >= (sizeof (buf) - offset)) \
120 return (-ENOMEM); \
121 else \
122 offset += ((size_t) status); \
123 } while (0)
125 #define BUFFER_ADD_FIELD(field) do { \
126 if (n->field[0]) \
127 BUFFER_ADD (", " #field " = %s", n->field); \
128 } while (0)
130 BUFFER_ADD ("Notification: severity = %s", severity_string);
131 BUFFER_ADD_FIELD (host);
132 BUFFER_ADD_FIELD (plugin);
133 BUFFER_ADD_FIELD (plugin_instance);
134 BUFFER_ADD_FIELD (type);
135 BUFFER_ADD_FIELD (type_instance);
136 BUFFER_ADD_FIELD (message);
138 #undef BUFFER_ADD_FIELD
139 #undef BUFFER_ADD
141 buf[sizeof (buf) - 1] = '\0';
143 sl_log (log_severity, buf, NULL);
145 return (0);
146 } /* int sl_notification */
148 void module_register (void)
149 {
150 openlog ("collectd", LOG_CONS | LOG_PID, LOG_DAEMON);
152 plugin_register_config ("syslog", sl_config, config_keys, config_keys_num);
153 plugin_register_log ("syslog", sl_log, /* user_data = */ NULL);
154 plugin_register_notification ("syslog", sl_notification, NULL);
155 plugin_register_shutdown ("syslog", sl_shutdown);
156 } /* void module_register(void) */