amqp, write_graphite and write_kafka plugins: Implement the "[Graphite]PreserveSepara...
[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  * 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  **/
28
29 #include "collectd.h"
30
31 #include "common.h"
32 #include "plugin.h"
33
34 #define DEFAULT_LOGFILE LOCALSTATEDIR"/log/collectd.log"
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
42 static pthread_mutex_t file_lock = PTHREAD_MUTEX_INITIALIZER;
43
44 static char *log_file = NULL;
45 static int print_timestamp = 1;
46 static int print_severity = 0;
47
48 static const char *config_keys[] =
49 {
50         "LogLevel",
51         "File",
52         "Timestamp",
53         "PrintSeverity"
54 };
55 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
56
57 static int logfile_config (const char *key, const char *value)
58 {
59         if (0 == strcasecmp (key, "LogLevel")) {
60                 log_level = parse_log_severity(value);
61                 if (log_level < 0) {
62                         log_level = LOG_INFO;
63                         ERROR ("logfile: invalid loglevel [%s] defaulting to 'info'", value);
64                         return (1);
65                 }
66         }
67         else if (0 == strcasecmp (key, "File")) {
68                 sfree (log_file);
69                 log_file = strdup (value);
70         }
71         else if (0 == strcasecmp (key, "Timestamp")) {
72                 if (IS_FALSE (value))
73                         print_timestamp = 0;
74                 else
75                         print_timestamp = 1;
76         } else if (0 == strcasecmp(key, "PrintSeverity")) {
77                 if (IS_FALSE (value))
78                         print_severity = 0;
79                 else
80                         print_severity = 1;
81         }
82         else {
83                 return -1;
84         }
85         return 0;
86 } /* int logfile_config (const char *, const char *) */
87
88 static void logfile_print (const char *msg, int severity,
89                 cdtime_t timestamp_time)
90 {
91         FILE *fh;
92         _Bool do_close = 0;
93         char timestamp_str[64];
94         char level_str[16] = "";
95
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         }
119
120         if (print_timestamp)
121         {
122                 struct tm timestamp_tm;
123                 localtime_r (&CDTIME_T_TO_TIME_T (timestamp_time), &timestamp_tm);
124
125                 strftime (timestamp_str, sizeof (timestamp_str), "%Y-%m-%d %H:%M:%S",
126                                 &timestamp_tm);
127                 timestamp_str[sizeof (timestamp_str) - 1] = '\0';
128         }
129
130         pthread_mutex_lock (&file_lock);
131
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         }
146
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);
160
161                 if (do_close) {
162                         fclose (fh);
163                 } else {
164                         fflush(fh);
165                 }
166         }
167
168         pthread_mutex_unlock (&file_lock);
169
170         return;
171 } /* void logfile_print */
172
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;
178
179         logfile_print (msg, severity, cdtime ());
180 } /* void logfile_log (int, const char *) */
181
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;
189
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         }
199
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);
214
215         buf[sizeof (buf) - 1] = '\0';
216
217         logfile_print (buf, LOG_INFO,
218                         (n->time != 0) ? n->time : cdtime ());
219
220         return (0);
221 } /* int logfile_notification */
222
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) */
231
232 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */
233