Merge branch 'collectd-4.2'
[collectd.git] / src / logfile.c
1 /**
2  * collectd - src/logfile.c
3  * Copyright (C) 2007  Sebastian Harl
4  * Copyright (C) 2007  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
43 static const char *config_keys[] =
44 {
45         "LogLevel",
46         "File",
47         "Timestamp"
48 };
49 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
50
51 static int logfile_config (const char *key, const char *value)
52 {
53         if (0 == strcasecmp (key, "LogLevel")) {
54                 if ((0 == strcasecmp (value, "emerg"))
55                                 || (0 == strcasecmp (value, "alert"))
56                                 || (0 == strcasecmp (value, "crit"))
57                                 || (0 == strcasecmp (value, "err")))
58                         log_level = LOG_ERR;
59                 else if (0 == strcasecmp (value, "warning"))
60                         log_level = LOG_WARNING;
61                 else if (0 == strcasecmp (value, "notice"))
62                         log_level = LOG_NOTICE;
63                 else if (0 == strcasecmp (value, "info"))
64                         log_level = LOG_INFO;
65 #if COLLECT_DEBUG
66                 else if (0 == strcasecmp (value, "debug"))
67                         log_level = LOG_DEBUG;
68 #endif /* COLLECT_DEBUG */
69                 else
70                         return 1;
71         }
72         else if (0 == strcasecmp (key, "File")) {
73                 sfree (log_file);
74                 log_file = strdup (value);
75         }
76         else if (0 == strcasecmp (key, "Timestamp")) {
77                 if ((strcasecmp (value, "false") == 0)
78                                 || (strcasecmp (value, "no") == 0)
79                                 || (strcasecmp (value, "off") == 0))
80                         print_timestamp = 0;
81                 else
82                         print_timestamp = 1;
83         }
84         else {
85                 return -1;
86         }
87         return 0;
88 } /* int logfile_config (const char *, const char *) */
89
90 static void logfile_print (const char *msg, time_t timestamp_time)
91 {
92         FILE *fh;
93         int do_close = 0;
94         struct tm timestamp_tm;
95         char timestamp_str[64];
96
97         if (print_timestamp)
98         {
99                 localtime_r (&timestamp_time, &timestamp_tm);
100
101                 strftime (timestamp_str, sizeof (timestamp_str), "%Y-%m-%d %H:%M:%S",
102                                 &timestamp_tm);
103                 timestamp_str[sizeof (timestamp_str) - 1] = '\0';
104         }
105
106         pthread_mutex_lock (&file_lock);
107
108         if (log_file == NULL)
109         {
110                 fh = fopen (DEFAULT_LOGFILE, "a");
111                 do_close = 1;
112         }
113         else if (strcasecmp (log_file, "stderr") == 0)
114                 fh = stderr;
115         else if (strcasecmp (log_file, "stdout") == 0)
116                 fh = stdout;
117         else
118         {
119                 fh = fopen (log_file, "a");
120                 do_close = 1;
121         }
122
123         if (fh == NULL)
124         {
125                         char errbuf[1024];
126                         fprintf (stderr, "logfile plugin: fopen (%s) failed: %s\n",
127                                         (log_file == NULL) ? DEFAULT_LOGFILE : log_file,
128                                         sstrerror (errno, errbuf, sizeof (errbuf)));
129         }
130         else
131         {
132                 if (print_timestamp)
133                         fprintf (fh, "[%s] %s\n", timestamp_str, msg);
134                 else
135                         fprintf (fh, "%s\n", msg);
136
137                 if (do_close != 0)
138                         fclose (fh);
139         }
140
141         pthread_mutex_unlock (&file_lock);
142
143         return;
144 } /* void logfile_print */
145
146 static void logfile_log (int severity, const char *msg)
147 {
148         if (severity > log_level)
149                 return;
150
151         logfile_print (msg, time (NULL));
152 } /* void logfile_log (int, const char *) */
153
154 int logfile_notification (const notification_t *n)
155 {
156         char msg[1024] = "";
157         int status;
158
159         status = snprintf (msg, sizeof (msg), "Notification: %s: message = %s, "
160                         "host = %s",
161                         (n->severity == NOTIF_FAILURE) ? "FAILURE"
162                         : ((n->severity == NOTIF_WARNING) ? "WARNING"
163                                 : ((n->severity == NOTIF_OKAY) ? "OKAY" : "UNKNOWN")),
164                         n->message, n->host);
165         msg[sizeof (msg) - 1] = '\0';
166
167         logfile_print (msg, n->time);
168
169         return (0);
170 } /* int logfile_notification */
171
172 void module_register (void)
173 {
174         plugin_register_config ("logfile", logfile_config,
175                         config_keys, config_keys_num);
176         plugin_register_log ("logfile", logfile_log);
177         plugin_register_notification ("logfile", logfile_notification);
178 } /* void module_register (void) */
179
180 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */
181