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