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