63448cb6c4e9f61cc611c71ed2c37840d160e17e
[collectd.git] / src / logfile.c
1 /**
2  * collectd - src/logfile.c
3  * Copyright (C) 2007  Sebastian Harl
4  * Copyright (C) 2007,2008  Florian Forster
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the
8  * Free Software Foundation; only version 2 of the License is applicable.
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  *   Sebastian Harl <sh at tokkee.org>
21  *   Florian Forster <octo at verplant.org>
22  **/
23
24 #include "collectd.h"
25 #include "common.h"
26 #include "plugin.h"
27
28 #include <pthread.h>
29
30 #define DEFAULT_LOGFILE LOCALSTATEDIR"/log/collectd.log"
31
32 #if COLLECT_DEBUG
33 static int log_level = LOG_DEBUG;
34 #else
35 static int log_level = LOG_INFO;
36 #endif /* COLLECT_DEBUG */
37
38 static pthread_mutex_t file_lock = PTHREAD_MUTEX_INITIALIZER;
39
40 static char *log_file = NULL;
41 static int print_timestamp = 1;
42 static int print_severity = 0;
43
44 static const char *config_keys[] =
45 {
46         "LogLevel",
47         "File",
48         "Timestamp",
49         "PrintSeverity"
50 };
51 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
52
53 static int logfile_config (const char *key, const char *value)
54 {
55         if (0 == strcasecmp (key, "LogLevel")) {
56                 log_level = parse_log_severity(value);
57                 if (log_level < 0) {
58                         log_level = LOG_INFO;
59                         ERROR ("logfile: invalid loglevel [%s] defaulting to 'info'", value);
60                         return (1);
61                 }
62         }
63         else if (0 == strcasecmp (key, "File")) {
64                 sfree (log_file);
65                 log_file = strdup (value);
66         }
67         else if (0 == strcasecmp (key, "Timestamp")) {
68                 if (IS_FALSE (value))
69                         print_timestamp = 0;
70                 else
71                         print_timestamp = 1;
72         } else if (0 == strcasecmp(key, "PrintSeverity")) {
73                 if (IS_FALSE (value))
74                         print_severity = 0;
75                 else
76                         print_severity = 1;
77         }
78         else {
79                 return -1;
80         }
81         return 0;
82 } /* int logfile_config (const char *, const char *) */
83
84 static void logfile_print (const char *msg, int severity,
85                 cdtime_t timestamp_time)
86 {
87         FILE *fh;
88         _Bool do_close = 0;
89         struct tm timestamp_tm;
90         char timestamp_str[64];
91         char level_str[16] = "";
92
93         if (print_severity)
94         {
95                 switch (severity)
96                 {
97                 case LOG_ERR:
98                         snprintf(level_str, sizeof (level_str), "[error] ");
99                         break;  
100                 case LOG_WARNING:
101                         snprintf(level_str, sizeof (level_str), "[warning] ");
102                         break;
103                 case LOG_NOTICE:
104                         snprintf(level_str, sizeof (level_str), "[notice] ");
105                         break;  
106                 case LOG_INFO:
107                         snprintf(level_str, sizeof (level_str), "[info] ");
108                         break;  
109                 case LOG_DEBUG:
110                         snprintf(level_str, sizeof (level_str), "[debug] ");
111                         break;  
112                 default:
113                         break;
114                 }
115         }
116
117         if (print_timestamp)
118         {
119                 time_t tt = CDTIME_T_TO_TIME_T (timestamp_time);
120                 localtime_r (&tt, &timestamp_tm);
121
122                 strftime (timestamp_str, sizeof (timestamp_str), "%Y-%m-%d %H:%M:%S",
123                                 &timestamp_tm);
124                 timestamp_str[sizeof (timestamp_str) - 1] = '\0';
125         }
126
127         pthread_mutex_lock (&file_lock);
128
129         if (log_file == NULL)
130         {
131                 fh = fopen (DEFAULT_LOGFILE, "a");
132                 do_close = 1;
133         }
134         else if (strcasecmp (log_file, "stderr") == 0)
135                 fh = stderr;
136         else if (strcasecmp (log_file, "stdout") == 0)
137                 fh = stdout;
138         else
139         {
140                 fh = fopen (log_file, "a");
141                 do_close = 1;
142         }
143
144         if (fh == NULL)
145         {
146                         char errbuf[1024];
147                         fprintf (stderr, "logfile plugin: fopen (%s) failed: %s\n",
148                                         (log_file == NULL) ? DEFAULT_LOGFILE : log_file,
149                                         sstrerror (errno, errbuf, sizeof (errbuf)));
150         }
151         else
152         {
153                 if (print_timestamp)
154                         fprintf (fh, "[%s] %s%s\n", timestamp_str, level_str, msg);
155                 else
156                         fprintf (fh, "%s%s\n", level_str, msg);
157
158                 if (do_close) {
159                         fclose (fh);
160                 } else {
161                         fflush(fh);
162                 }
163         }
164
165         pthread_mutex_unlock (&file_lock);
166
167         return;
168 } /* void logfile_print */
169
170 static void logfile_log (int severity, const char *msg,
171                 user_data_t __attribute__((unused)) *user_data)
172 {
173         if (severity > log_level)
174                 return;
175
176         logfile_print (msg, severity, cdtime ());
177 } /* void logfile_log (int, const char *) */
178
179 static int logfile_notification (const notification_t *n,
180                 user_data_t __attribute__((unused)) *user_data)
181 {
182         char  buf[1024] = "";
183         char *buf_ptr = buf;
184         int   buf_len = sizeof (buf);
185         int status;
186
187         status = ssnprintf (buf_ptr, buf_len, "Notification: severity = %s",
188                         (n->severity == NOTIF_FAILURE) ? "FAILURE"
189                         : ((n->severity == NOTIF_WARNING) ? "WARNING"
190                                 : ((n->severity == NOTIF_OKAY) ? "OKAY" : "UNKNOWN")));
191         if (status > 0)
192         {
193                 buf_ptr += status;
194                 buf_len -= status;
195         }
196
197 #define APPEND(bufptr, buflen, key, value) \
198         if ((buflen > 0) && (strlen (value) > 0)) { \
199                 int status = ssnprintf (bufptr, buflen, ", %s = %s", key, value); \
200                 if (status > 0) { \
201                         bufptr += status; \
202                         buflen -= status; \
203                 } \
204         }
205         APPEND (buf_ptr, buf_len, "host", n->host);
206         APPEND (buf_ptr, buf_len, "plugin", n->plugin);
207         APPEND (buf_ptr, buf_len, "plugin_instance", n->plugin_instance);
208         APPEND (buf_ptr, buf_len, "type", n->type);
209         APPEND (buf_ptr, buf_len, "type_instance", n->type_instance);
210         APPEND (buf_ptr, buf_len, "message", n->message);
211
212         buf[sizeof (buf) - 1] = '\0';
213
214         logfile_print (buf, LOG_INFO,
215                         (n->time != 0) ? n->time : cdtime ());
216
217         return (0);
218 } /* int logfile_notification */
219
220 void module_register (void)
221 {
222         plugin_register_config ("logfile", logfile_config,
223                         config_keys, config_keys_num);
224         plugin_register_log ("logfile", logfile_log, /* user_data = */ NULL);
225         plugin_register_notification ("logfile", logfile_notification,
226                         /* user_data = */ NULL);
227 } /* void module_register (void) */
228
229 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */
230