2 * collectd - src/mcelog.c
5 * Copyright(c) 2016 Intel Corporation. All rights reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
26 * Maryam Tahhan <maryam.tahhan@intel.com>
27 * Volodymyr Mytnyk <volodymyrx.mytnyk@intel.com>
28 * Taras Chornyi <tarasx.chornyi@intel.com>
29 * Krzysztof Matczak <krzysztofx.matczak@intel.com>
36 #include <sys/socket.h>
40 #define MCELOG_PLUGIN "mcelog"
41 #define MCELOG_BUFF_SIZE 1024
42 #define MCELOG_POLL_TIMEOUT 1000 /* ms */
43 #define MCELOG_SOCKET_STR "SOCKET"
44 #define MCELOG_DIMM_NAME "DMI_NAME"
45 #define MCELOG_CORRECTED_ERR "corrected memory errors:"
46 #define MCELOG_UNCORRECTED_ERR "uncorrected memory errors:"
48 typedef struct mcelog_config_s {
49 char logfile[PATH_MAX]; /* mcelog logfile */
50 pthread_t tid; /* poll thread id */
53 typedef struct socket_adapter_s socket_adapter_t;
55 struct socket_adapter_s {
56 int sock_fd; /* mcelog server socket fd */
57 struct sockaddr_un unix_sock; /* mcelog client socket */
58 pthread_rwlock_t lock;
59 /* function pointers for socket operations */
60 int (*write)(socket_adapter_t *self, const char *msg, const size_t len);
61 int (*reinit)(socket_adapter_t *self);
62 int (*receive)(socket_adapter_t *self, FILE **p_file);
63 int (*close)(socket_adapter_t *self);
66 typedef struct mcelog_memory_rec_s {
67 char location[DATA_MAX_NAME_LEN]; /* SOCKET x CHANNEL x DIMM x*/
68 char dimm_name[DATA_MAX_NAME_LEN]; /* DMI_NAME "DIMM_F1" */
69 int corrected_err_total; /* x total*/
70 int corrected_err_timed; /* x in 24h*/
71 char corrected_err_timed_period[DATA_MAX_NAME_LEN];
72 int uncorrected_err_total; /* x total*/
73 int uncorrected_err_timed; /* x in 24h*/
74 char uncorrected_err_timed_period[DATA_MAX_NAME_LEN];
75 } mcelog_memory_rec_t;
77 static int socket_close(socket_adapter_t *self);
78 static int socket_write(socket_adapter_t *self, const char *msg,
80 static int socket_reinit(socket_adapter_t *self);
81 static int socket_receive(socket_adapter_t *self, FILE **p_file);
83 static mcelog_config_t g_mcelog_config = {
84 .logfile = "/var/log/mcelog", .tid = 0,
87 static socket_adapter_t socket_adapter = {
91 .sun_family = AF_UNIX, .sun_path = "/var/run/mcelog-client",
93 .lock = PTHREAD_RWLOCK_INITIALIZER,
94 .close = socket_close,
95 .write = socket_write,
96 .reinit = socket_reinit,
97 .receive = socket_receive,
100 static _Bool mcelog_thread_running = 0;
102 static int mcelog_config(oconfig_item_t *ci) {
103 for (int i = 0; i < ci->children_num; i++) {
104 oconfig_item_t *child = ci->children + i;
105 if (strcasecmp("McelogClientSocket", child->key) == 0) {
106 if (cf_util_get_string_buffer(child, socket_adapter.unix_sock.sun_path,
107 sizeof(socket_adapter.unix_sock.sun_path)) <
109 ERROR("%s: Invalid configuration option: \"%s\".", MCELOG_PLUGIN,
113 } else if (strcasecmp("McelogLogfile", child->key) == 0) {
114 if (cf_util_get_string_buffer(child, g_mcelog_config.logfile,
115 sizeof(g_mcelog_config.logfile)) < 0) {
116 ERROR("%s: Invalid configuration option: \"%s\".", MCELOG_PLUGIN,
121 ERROR("%s: Invalid configuration option: \"%s\".", MCELOG_PLUGIN,
129 static int socket_close(socket_adapter_t *self) {
131 pthread_rwlock_rdlock(&self->lock);
132 if (fcntl(self->sock_fd, F_GETFL) != -1) {
133 if (shutdown(self->sock_fd, SHUT_RDWR) != 0) {
134 char errbuf[MCELOG_BUFF_SIZE];
135 ERROR("%s: Socket shutdown failed: %s", MCELOG_PLUGIN,
136 sstrerror(errno, errbuf, sizeof(errbuf)));
139 close(self->sock_fd);
141 pthread_rwlock_unlock(&self->lock);
145 static int socket_write(socket_adapter_t *self, const char *msg,
148 pthread_rwlock_rdlock(&self->lock);
149 if (swrite(self->sock_fd, msg, len) < 0)
151 pthread_rwlock_unlock(&self->lock);
155 static int socket_reinit(socket_adapter_t *self) {
156 char errbuff[MCELOG_BUFF_SIZE];
159 cdtime_t interval = plugin_get_interval();
160 struct timeval socket_timeout = CDTIME_T_TO_TIMEVAL(interval);
162 /* synchronization via write lock since sock_fd may be changed here */
163 pthread_rwlock_wrlock(&self->lock);
164 self->sock_fd = socket(PF_UNIX, SOCK_STREAM, 0);
165 if (self->sock_fd < 0) {
166 ERROR("%s: Could not create a socket. %s", MCELOG_PLUGIN,
167 sstrerror(errno, errbuff, sizeof(errbuff)));
168 pthread_rwlock_unlock(&self->lock);
172 flags = fcntl(self->sock_fd, F_GETFL, 0);
174 fcntl(self->sock_fd, F_SETFL, flags);
176 /* Set socket timeout option */
177 if (setsockopt(self->sock_fd, SOL_SOCKET, SO_SNDTIMEO,
178 (char *)&socket_timeout, sizeof(socket_timeout)) < 0)
179 ERROR("%s: Failed to set the socket timeout option.", MCELOG_PLUGIN);
181 /* downgrading to read lock due to possible recursive read locks
182 * in self->close(self) call */
183 pthread_rwlock_unlock(&self->lock);
184 pthread_rwlock_rdlock(&self->lock);
185 if (connect(self->sock_fd, (struct sockaddr *)&(self->unix_sock),
186 sizeof(self->unix_sock)) < 0) {
187 ERROR("%s: Failed to connect to mcelog server. %s", MCELOG_PLUGIN,
188 sstrerror(errno, errbuff, sizeof(errbuff)));
194 pthread_rwlock_unlock(&self->lock);
198 static void mcelog_dispatch_notification(notification_t n) {
199 sstrncpy(n.host, hostname_g, sizeof(n.host));
200 sstrncpy(n.type, "gauge", sizeof(n.type));
201 plugin_dispatch_notification(&n);
204 static int mcelog_prepare_notification(notification_t *n,
205 mcelog_memory_rec_t mr) {
209 if (plugin_notification_meta_add_string(n, MCELOG_SOCKET_STR, mr.location) <
211 ERROR("%s: add memory location meta data failed", MCELOG_PLUGIN);
214 if (strlen(mr.dimm_name) > 0)
215 if (plugin_notification_meta_add_string(n, MCELOG_DIMM_NAME, mr.dimm_name) <
217 ERROR("%s: add DIMM name meta data failed", MCELOG_PLUGIN);
220 if (plugin_notification_meta_add_signed_int(n, MCELOG_CORRECTED_ERR,
221 mr.corrected_err_total) < 0) {
222 ERROR("%s: add corrected errors meta data failed", MCELOG_PLUGIN);
225 if (plugin_notification_meta_add_signed_int(
226 n, "corrected memory timed errors", mr.corrected_err_timed) < 0) {
227 ERROR("%s: add corrected timed errors meta data failed", MCELOG_PLUGIN);
230 if (plugin_notification_meta_add_string(n, "corrected errors time period",
231 mr.corrected_err_timed_period) < 0) {
232 ERROR("%s: add corrected errors period meta data failed", MCELOG_PLUGIN);
235 if (plugin_notification_meta_add_signed_int(n, MCELOG_UNCORRECTED_ERR,
236 mr.uncorrected_err_total) < 0) {
237 ERROR("%s: add corrected errors meta data failed", MCELOG_PLUGIN);
240 if (plugin_notification_meta_add_signed_int(
241 n, "uncorrected memory timed errors", mr.uncorrected_err_timed) < 0) {
242 ERROR("%s: add corrected timed errors meta data failed", MCELOG_PLUGIN);
245 if (plugin_notification_meta_add_string(n, "uncorrected errors time period",
246 mr.uncorrected_err_timed_period) <
248 ERROR("%s: add corrected errors period meta data failed", MCELOG_PLUGIN);
255 static int mcelog_submit(mcelog_memory_rec_t mr) {
257 value_list_t vl = VALUE_LIST_INIT;
261 sstrncpy(vl.plugin, MCELOG_PLUGIN, sizeof(vl.plugin));
262 sstrncpy(vl.type, "errors", sizeof(vl.type));
263 if (strlen(mr.dimm_name) > 0) {
264 ssnprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%s_%s",
265 mr.location, mr.dimm_name);
267 sstrncpy(vl.plugin_instance, mr.location, sizeof(vl.plugin_instance));
269 sstrncpy(vl.type_instance, "corrected_memory_errors",
270 sizeof(vl.type_instance));
271 vl.values = &(value_t){.derive = (derive_t)mr.corrected_err_total};
272 plugin_dispatch_values(&vl);
274 ssnprintf(vl.type_instance, sizeof(vl.type_instance),
275 "corrected_memory_errors_in_%s", mr.corrected_err_timed_period);
276 vl.values = &(value_t){.derive = (derive_t)mr.corrected_err_timed};
277 plugin_dispatch_values(&vl);
279 sstrncpy(vl.type_instance, "uncorrected_memory_errors",
280 sizeof(vl.type_instance));
281 vl.values = &(value_t){.derive = (derive_t)mr.uncorrected_err_total};
282 plugin_dispatch_values(&vl);
284 ssnprintf(vl.type_instance, sizeof(vl.type_instance),
285 "uncorrected_memory_errors_in_%s", mr.uncorrected_err_timed_period);
286 vl.values = &(value_t){.derive = (derive_t)mr.uncorrected_err_timed};
287 plugin_dispatch_values(&vl);
292 static int parse_memory_info(FILE *p_file, mcelog_memory_rec_t *memory_record) {
293 char buf[DATA_MAX_NAME_LEN] = {0};
294 while (fgets(buf, sizeof(buf), p_file)) {
295 /* Got empty line or "done" */
296 if ((!strncmp("\n", buf, strlen(buf))) ||
297 (!strncmp(buf, "done\n", strlen(buf))))
301 if (!strncmp(buf, MCELOG_SOCKET_STR, strlen(MCELOG_SOCKET_STR))) {
302 sstrncpy(memory_record->location, buf, strlen(buf));
303 /* replace spaces with '_' */
304 for (size_t i = 0; i < strlen(memory_record->location); i++)
305 if (memory_record->location[i] == ' ')
306 memory_record->location[i] = '_';
307 DEBUG("%s: Got SOCKET INFO %s", MCELOG_PLUGIN, memory_record->location);
309 if (!strncmp(buf, MCELOG_DIMM_NAME, strlen(MCELOG_DIMM_NAME))) {
311 char *saveptr = NULL;
312 name = strtok_r(buf, "\"", &saveptr);
313 if (name != NULL && saveptr != NULL) {
314 name = strtok_r(NULL, "\"", &saveptr);
316 sstrncpy(memory_record->dimm_name, name,
317 sizeof(memory_record->dimm_name));
318 DEBUG("%s: Got DIMM NAME %s", MCELOG_PLUGIN,
319 memory_record->dimm_name);
323 if (!strncmp(buf, MCELOG_CORRECTED_ERR, strlen(MCELOG_CORRECTED_ERR))) {
325 if (fgets(buf, sizeof(buf), p_file) != NULL) {
326 sscanf(buf, "\t%d total", &(memory_record->corrected_err_total));
327 DEBUG("%s: Got corrected error total %d", MCELOG_PLUGIN,
328 memory_record->corrected_err_total);
330 if (fgets(buf, sizeof(buf), p_file) != NULL) {
331 sscanf(buf, "\t%d in %s", &(memory_record->corrected_err_timed),
332 memory_record->corrected_err_timed_period);
333 DEBUG("%s: Got timed corrected errors %d in %s", MCELOG_PLUGIN,
334 memory_record->corrected_err_total,
335 memory_record->corrected_err_timed_period);
338 if (!strncmp(buf, MCELOG_UNCORRECTED_ERR, strlen(MCELOG_UNCORRECTED_ERR))) {
339 if (fgets(buf, sizeof(buf), p_file) != NULL) {
340 sscanf(buf, "\t%d total", &(memory_record->uncorrected_err_total));
341 DEBUG("%s: Got uncorrected error total %d", MCELOG_PLUGIN,
342 memory_record->uncorrected_err_total);
344 if (fgets(buf, sizeof(buf), p_file) != NULL) {
345 sscanf(buf, "\t%d in %s", &(memory_record->uncorrected_err_timed),
346 memory_record->uncorrected_err_timed_period);
347 DEBUG("%s: Got timed uncorrected errors %d in %s", MCELOG_PLUGIN,
348 memory_record->uncorrected_err_total,
349 memory_record->uncorrected_err_timed_period);
352 memset(buf, 0, sizeof(buf));
354 /* parsing definitely finished */
358 static void poll_worker_cleanup(void *arg) {
359 mcelog_thread_running = 0;
360 FILE *p_file = *((FILE **)arg);
366 static int socket_receive(socket_adapter_t *self, FILE **pp_file) {
368 pthread_rwlock_rdlock(&self->lock);
369 struct pollfd poll_fd = {
370 .fd = self->sock_fd, .events = POLLIN | POLLPRI,
373 if ((res = poll(&poll_fd, 1, MCELOG_POLL_TIMEOUT)) <= 0) {
374 if (res != 0 && errno != EINTR) {
375 char errbuf[MCELOG_BUFF_SIZE];
376 ERROR("mcelog: poll failed: %s",
377 sstrerror(errno, errbuf, sizeof(errbuf)));
379 pthread_rwlock_unlock(&self->lock);
383 if (poll_fd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
384 /* connection is broken */
385 ERROR("%s: Connection to socket is broken", MCELOG_PLUGIN);
386 if (poll_fd.revents & (POLLERR | POLLHUP)) {
388 NOTIF_FAILURE, cdtime(), "", "", MCELOG_PLUGIN, "", "", "", NULL};
389 ssnprintf(n.message, sizeof(n.message),
390 "Connection to mcelog socket is broken.");
391 sstrncpy(n.type_instance, "mcelog_status", sizeof(n.type_instance));
392 mcelog_dispatch_notification(n);
394 pthread_rwlock_unlock(&self->lock);
398 if (!(poll_fd.revents & (POLLIN | POLLPRI))) {
399 INFO("%s: No data to read", MCELOG_PLUGIN);
400 pthread_rwlock_unlock(&self->lock);
404 if ((*pp_file = fdopen(dup(self->sock_fd), "r")) == NULL)
407 pthread_rwlock_unlock(&self->lock);
411 static void *poll_worker(__attribute__((unused)) void *arg) {
412 char errbuf[MCELOG_BUFF_SIZE];
413 mcelog_thread_running = 1;
414 FILE **pp_file = calloc(1, sizeof(FILE *));
415 if (pp_file == NULL) {
416 ERROR("mcelog: memory allocation failed: %s",
417 sstrerror(errno, errbuf, sizeof(errbuf)));
418 pthread_exit((void *)1);
421 pthread_cleanup_push(poll_worker_cleanup, pp_file);
426 res = socket_adapter.receive(&socket_adapter, pp_file);
428 socket_adapter.close(&socket_adapter);
429 if (socket_adapter.reinit(&socket_adapter) != 0) {
430 socket_adapter.close(&socket_adapter);
431 usleep(MCELOG_POLL_TIMEOUT);
435 /* timeout or no data to read */
439 if (*pp_file == NULL)
442 mcelog_memory_rec_t memory_record;
443 memset(&memory_record, 0, sizeof(memory_record));
444 while (parse_memory_info(*pp_file, &memory_record)) {
445 notification_t n = {NOTIF_OKAY, cdtime(), "", "", MCELOG_PLUGIN,
447 ssnprintf(n.message, sizeof(n.message), "Got memory errors info.");
448 sstrncpy(n.type_instance, "memory_erros", sizeof(n.type_instance));
449 if (mcelog_prepare_notification(&n, memory_record) == 0)
450 mcelog_dispatch_notification(n);
451 if (mcelog_submit(memory_record) != 0)
452 ERROR("%s: Failed to submit memory errors", MCELOG_PLUGIN);
453 memset(&memory_record, 0, sizeof(memory_record));
460 mcelog_thread_running = 0;
461 pthread_cleanup_pop(1);
465 static int mcelog_init(void) {
466 if (socket_adapter.reinit(&socket_adapter) != 0) {
467 ERROR("%s: Cannot connect to client socket", MCELOG_PLUGIN);
471 if (plugin_thread_create(&g_mcelog_config.tid, NULL, poll_worker, NULL,
473 ERROR("%s: Error creating poll thread.", MCELOG_PLUGIN);
479 static int get_memory_machine_checks(void) {
480 static const char dump[] = "dump all bios\n";
481 int ret = socket_adapter.write(&socket_adapter, dump, sizeof(dump));
483 ERROR("%s: SENT DUMP REQUEST FAILED", MCELOG_PLUGIN);
485 DEBUG("%s: SENT DUMP REQUEST OK", MCELOG_PLUGIN);
489 static int mcelog_read(__attribute__((unused)) user_data_t *ud) {
490 DEBUG("%s: %s", MCELOG_PLUGIN, __FUNCTION__);
492 if (get_memory_machine_checks() != 0)
493 ERROR("%s: MACHINE CHECK INFO NOT AVAILABLE", MCELOG_PLUGIN);
498 static int mcelog_shutdown(void) {
500 if (mcelog_thread_running) {
501 pthread_cancel(g_mcelog_config.tid);
502 if (pthread_join(g_mcelog_config.tid, NULL) != 0) {
503 ERROR("%s: Stopping thread failed.", MCELOG_PLUGIN);
508 ret = socket_adapter.close(&socket_adapter) || ret;
509 pthread_rwlock_destroy(&(socket_adapter.lock));
513 void module_register(void) {
514 plugin_register_complex_config(MCELOG_PLUGIN, mcelog_config);
515 plugin_register_init(MCELOG_PLUGIN, mcelog_init);
516 plugin_register_complex_read(NULL, MCELOG_PLUGIN, mcelog_read, 0, NULL);
517 plugin_register_shutdown(MCELOG_PLUGIN, mcelog_shutdown);