1 /**
2 * collectd - src/logfile.c
3 * Copyright (C) 2007 Sebastian Harl
4 * Copyright (C) 2007,2008 Florian Forster
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Sebastian Harl <sh at tokkee.org>
26 * Florian Forster <octo at collectd.org>
27 **/
29 #include "collectd.h"
30 #include "common.h"
31 #include "plugin.h"
33 #define DEFAULT_LOGFILE LOCALSTATEDIR"/log/collectd.log"
35 #if COLLECT_DEBUG
36 static int log_level = LOG_DEBUG;
37 #else
38 static int log_level = LOG_INFO;
39 #endif /* COLLECT_DEBUG */
41 static pthread_mutex_t file_lock = PTHREAD_MUTEX_INITIALIZER;
43 static char *log_file = NULL;
44 static int print_timestamp = 1;
45 static int print_severity = 0;
47 static const char *config_keys[] =
48 {
49 "LogLevel",
50 "File",
51 "Timestamp",
52 "PrintSeverity"
53 };
54 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
56 static int logfile_config (const char *key, const char *value)
57 {
58 if (0 == strcasecmp (key, "LogLevel")) {
59 log_level = parse_log_severity(value);
60 if (log_level < 0) {
61 log_level = LOG_INFO;
62 ERROR ("logfile: invalid loglevel [%s] defaulting to 'info'", value);
63 return (1);
64 }
65 }
66 else if (0 == strcasecmp (key, "File")) {
67 sfree (log_file);
68 log_file = strdup (value);
69 }
70 else if (0 == strcasecmp (key, "Timestamp")) {
71 if (IS_FALSE (value))
72 print_timestamp = 0;
73 else
74 print_timestamp = 1;
75 } else if (0 == strcasecmp(key, "PrintSeverity")) {
76 if (IS_FALSE (value))
77 print_severity = 0;
78 else
79 print_severity = 1;
80 }
81 else {
82 return -1;
83 }
84 return 0;
85 } /* int logfile_config (const char *, const char *) */
87 static void logfile_print (const char *msg, int severity,
88 cdtime_t timestamp_time)
89 {
90 FILE *fh;
91 _Bool do_close = 0;
92 struct tm timestamp_tm;
93 char timestamp_str[64];
94 char level_str[16] = "";
96 if (print_severity)
97 {
98 switch (severity)
99 {
100 case LOG_ERR:
101 snprintf(level_str, sizeof (level_str), "[error] ");
102 break;
103 case LOG_WARNING:
104 snprintf(level_str, sizeof (level_str), "[warning] ");
105 break;
106 case LOG_NOTICE:
107 snprintf(level_str, sizeof (level_str), "[notice] ");
108 break;
109 case LOG_INFO:
110 snprintf(level_str, sizeof (level_str), "[info] ");
111 break;
112 case LOG_DEBUG:
113 snprintf(level_str, sizeof (level_str), "[debug] ");
114 break;
115 default:
116 break;
117 }
118 }
120 if (print_timestamp)
121 {
122 time_t tt = CDTIME_T_TO_TIME_T (timestamp_time);
123 localtime_r (&tt, ×tamp_tm);
125 strftime (timestamp_str, sizeof (timestamp_str), "%Y-%m-%d %H:%M:%S",
126 ×tamp_tm);
127 timestamp_str[sizeof (timestamp_str) - 1] = '\0';
128 }
130 pthread_mutex_lock (&file_lock);
132 if (log_file == NULL)
133 {
134 fh = fopen (DEFAULT_LOGFILE, "a");
135 do_close = 1;
136 }
137 else if (strcasecmp (log_file, "stderr") == 0)
138 fh = stderr;
139 else if (strcasecmp (log_file, "stdout") == 0)
140 fh = stdout;
141 else
142 {
143 fh = fopen (log_file, "a");
144 do_close = 1;
145 }
147 if (fh == NULL)
148 {
149 char errbuf[1024];
150 fprintf (stderr, "logfile plugin: fopen (%s) failed: %s\n",
151 (log_file == NULL) ? DEFAULT_LOGFILE : log_file,
152 sstrerror (errno, errbuf, sizeof (errbuf)));
153 }
154 else
155 {
156 if (print_timestamp)
157 fprintf (fh, "[%s] %s%s\n", timestamp_str, level_str, msg);
158 else
159 fprintf (fh, "%s%s\n", level_str, msg);
161 if (do_close) {
162 fclose (fh);
163 } else {
164 fflush(fh);
165 }
166 }
168 pthread_mutex_unlock (&file_lock);
170 return;
171 } /* void logfile_print */
173 static void logfile_log (int severity, const char *msg,
174 user_data_t __attribute__((unused)) *user_data)
175 {
176 if (severity > log_level)
177 return;
179 logfile_print (msg, severity, cdtime ());
180 } /* void logfile_log (int, const char *) */
182 static int logfile_notification (const notification_t *n,
183 user_data_t __attribute__((unused)) *user_data)
184 {
185 char buf[1024] = "";
186 char *buf_ptr = buf;
187 int buf_len = sizeof (buf);
188 int status;
190 status = ssnprintf (buf_ptr, buf_len, "Notification: severity = %s",
191 (n->severity == NOTIF_FAILURE) ? "FAILURE"
192 : ((n->severity == NOTIF_WARNING) ? "WARNING"
193 : ((n->severity == NOTIF_OKAY) ? "OKAY" : "UNKNOWN")));
194 if (status > 0)
195 {
196 buf_ptr += status;
197 buf_len -= status;
198 }
200 #define APPEND(bufptr, buflen, key, value) \
201 if ((buflen > 0) && (strlen (value) > 0)) { \
202 status = ssnprintf (bufptr, buflen, ", %s = %s", key, value); \
203 if (status > 0) { \
204 bufptr += status; \
205 buflen -= status; \
206 } \
207 }
208 APPEND (buf_ptr, buf_len, "host", n->host);
209 APPEND (buf_ptr, buf_len, "plugin", n->plugin);
210 APPEND (buf_ptr, buf_len, "plugin_instance", n->plugin_instance);
211 APPEND (buf_ptr, buf_len, "type", n->type);
212 APPEND (buf_ptr, buf_len, "type_instance", n->type_instance);
213 APPEND (buf_ptr, buf_len, "message", n->message);
215 buf[sizeof (buf) - 1] = '\0';
217 logfile_print (buf, LOG_INFO,
218 (n->time != 0) ? n->time : cdtime ());
220 return (0);
221 } /* int logfile_notification */
223 void module_register (void)
224 {
225 plugin_register_config ("logfile", logfile_config,
226 config_keys, config_keys_num);
227 plugin_register_log ("logfile", logfile_log, /* user_data = */ NULL);
228 plugin_register_notification ("logfile", logfile_notification,
229 /* user_data = */ NULL);
230 } /* void module_register (void) */
232 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */