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