| 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 "plugin.h" |
| 32 |
#include "utils/common/common.h" |
| 33 |
|
| 34 |
#if COLLECT_DEBUG |
| 35 |
static int log_level = LOG_DEBUG; |
| 36 |
#else |
| 37 |
static int log_level = LOG_INFO; |
| 38 |
#endif /* COLLECT_DEBUG */ |
| 39 |
|
| 40 |
static pthread_mutex_t file_lock = PTHREAD_MUTEX_INITIALIZER; |
| 41 |
|
| 42 |
static char *log_file; |
| 43 |
static int print_timestamp = 1; |
| 44 |
static int print_severity; |
| 45 |
|
| 46 |
static const char *config_keys[] = {"LogLevel", "File", "Timestamp", |
| 47 |
"PrintSeverity"}; |
| 48 |
static int config_keys_num = STATIC_ARRAY_SIZE(config_keys); |
| 49 |
|
| 50 |
static int logfile_config(const char *key, const char *value) { |
| 51 |
if (0 == strcasecmp(key, "LogLevel")) { |
| 52 |
log_level = parse_log_severity(value); |
| 53 |
if (log_level < 0) { |
| 54 |
log_level = LOG_INFO; |
| 55 |
WARNING("logfile: invalid loglevel [%s] defaulting to 'info'", value); |
| 56 |
return 0; |
| 57 |
} |
| 58 |
} else if (0 == strcasecmp(key, "File")) { |
| 59 |
sfree(log_file); |
| 60 |
log_file = strdup(value); |
| 61 |
} else if (0 == strcasecmp(key, "Timestamp")) { |
| 62 |
if (IS_FALSE(value)) |
| 63 |
print_timestamp = 0; |
| 64 |
else |
| 65 |
print_timestamp = 1; |
| 66 |
} else if (0 == strcasecmp(key, "PrintSeverity")) { |
| 67 |
if (IS_FALSE(value)) |
| 68 |
print_severity = 0; |
| 69 |
else |
| 70 |
print_severity = 1; |
| 71 |
} else { |
| 72 |
return -1; |
| 73 |
} |
| 74 |
return 0; |
| 75 |
} /* int logfile_config (const char *, const char *) */ |
| 76 |
|
| 77 |
static void logfile_print(const char *msg, int severity, |
| 78 |
cdtime_t timestamp_time) { |
| 79 |
FILE *fh; |
| 80 |
bool do_close = false; |
| 81 |
char timestamp_str[64]; |
| 82 |
char level_str[16] = ""; |
| 83 |
|
| 84 |
if (print_severity) { |
| 85 |
switch (severity) { |
| 86 |
case LOG_ERR: |
| 87 |
snprintf(level_str, sizeof(level_str), "[error] "); |
| 88 |
break; |
| 89 |
case LOG_WARNING: |
| 90 |
snprintf(level_str, sizeof(level_str), "[warning] "); |
| 91 |
break; |
| 92 |
case LOG_NOTICE: |
| 93 |
snprintf(level_str, sizeof(level_str), "[notice] "); |
| 94 |
break; |
| 95 |
case LOG_INFO: |
| 96 |
snprintf(level_str, sizeof(level_str), "[info] "); |
| 97 |
break; |
| 98 |
case LOG_DEBUG: |
| 99 |
snprintf(level_str, sizeof(level_str), "[debug] "); |
| 100 |
break; |
| 101 |
default: |
| 102 |
break; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
if (print_timestamp) { |
| 107 |
struct tm timestamp_tm; |
| 108 |
localtime_r(&CDTIME_T_TO_TIME_T(timestamp_time), ×tamp_tm); |
| 109 |
|
| 110 |
strftime(timestamp_str, sizeof(timestamp_str), "%Y-%m-%d %H:%M:%S", |
| 111 |
×tamp_tm); |
| 112 |
timestamp_str[sizeof(timestamp_str) - 1] = '\0'; |
| 113 |
} |
| 114 |
|
| 115 |
pthread_mutex_lock(&file_lock); |
| 116 |
|
| 117 |
if (log_file == NULL) { |
| 118 |
fh = stderr; |
| 119 |
} else if (strcasecmp(log_file, "stderr") == 0) |
| 120 |
fh = stderr; |
| 121 |
else if (strcasecmp(log_file, "stdout") == 0) |
| 122 |
fh = stdout; |
| 123 |
else { |
| 124 |
fh = fopen(log_file, "a"); |
| 125 |
do_close = true; |
| 126 |
} |
| 127 |
|
| 128 |
if (fh == NULL) { |
| 129 |
fprintf(stderr, "logfile plugin: fopen (%s) failed: %s\n", log_file, |
| 130 |
STRERRNO); |
| 131 |
} else { |
| 132 |
if (print_timestamp) |
| 133 |
fprintf(fh, "[%s] %s%s\n", timestamp_str, level_str, msg); |
| 134 |
else |
| 135 |
fprintf(fh, "%s%s\n", level_str, msg); |
| 136 |
|
| 137 |
if (do_close) { |
| 138 |
fclose(fh); |
| 139 |
} else { |
| 140 |
fflush(fh); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
pthread_mutex_unlock(&file_lock); |
| 145 |
|
| 146 |
return; |
| 147 |
} /* void logfile_print */ |
| 148 |
|
| 149 |
static void logfile_log(int severity, const char *msg, |
| 150 |
user_data_t __attribute__((unused)) * user_data) { |
| 151 |
if (severity > log_level) |
| 152 |
return; |
| 153 |
|
| 154 |
logfile_print(msg, severity, cdtime()); |
| 155 |
} /* void logfile_log (int, const char *) */ |
| 156 |
|
| 157 |
static int logfile_notification(const notification_t *n, |
| 158 |
user_data_t __attribute__((unused)) * |
| 159 |
user_data) { |
| 160 |
char buf[1024] = ""; |
| 161 |
char *buf_ptr = buf; |
| 162 |
int buf_len = sizeof(buf); |
| 163 |
int status; |
| 164 |
|
| 165 |
status = snprintf( |
| 166 |
buf_ptr, buf_len, "Notification: severity = %s", |
| 167 |
(n->severity == NOTIF_FAILURE) |
| 168 |
? "FAILURE" |
| 169 |
: ((n->severity == NOTIF_WARNING) |
| 170 |
? "WARNING" |
| 171 |
: ((n->severity == NOTIF_OKAY) ? "OKAY" : "UNKNOWN"))); |
| 172 |
if (status > 0) { |
| 173 |
buf_ptr += status; |
| 174 |
buf_len -= status; |
| 175 |
} |
| 176 |
|
| 177 |
#define APPEND(bufptr, buflen, key, value) \ |
| 178 |
if ((buflen > 0) && (strlen(value) > 0)) { \ |
| 179 |
status = snprintf(bufptr, buflen, ", %s = %s", key, value); \ |
| 180 |
if (status > 0) { \ |
| 181 |
bufptr += status; \ |
| 182 |
buflen -= status; \ |
| 183 |
} \ |
| 184 |
} |
| 185 |
APPEND(buf_ptr, buf_len, "host", n->host); |
| 186 |
APPEND(buf_ptr, buf_len, "plugin", n->plugin); |
| 187 |
APPEND(buf_ptr, buf_len, "plugin_instance", n->plugin_instance); |
| 188 |
APPEND(buf_ptr, buf_len, "type", n->type); |
| 189 |
APPEND(buf_ptr, buf_len, "type_instance", n->type_instance); |
| 190 |
APPEND(buf_ptr, buf_len, "message", n->message); |
| 191 |
|
| 192 |
buf[sizeof(buf) - 1] = '\0'; |
| 193 |
|
| 194 |
logfile_print(buf, LOG_INFO, (n->time != 0) ? n->time : cdtime()); |
| 195 |
|
| 196 |
return 0; |
| 197 |
} /* int logfile_notification */ |
| 198 |
|
| 199 |
void module_register(void) { |
| 200 |
plugin_register_config("logfile", logfile_config, config_keys, |
| 201 |
config_keys_num); |
| 202 |
plugin_register_log("logfile", logfile_log, /* user_data = */ NULL); |
| 203 |
plugin_register_notification("logfile", logfile_notification, |
| 204 |
/* user_data = */ NULL); |
| 205 |
} /* void module_register (void) */ |