From a036dd8b917aa1a991817a45e387a119f0def868 Mon Sep 17 00:00:00 2001 From: Przemyslaw Szczerbik Date: Thu, 19 Jan 2017 11:30:54 +0000 Subject: [PATCH] mcelog: Dispatch notification on successful connection to mcelog server Change-Id: Ia5503972c665a2e951a2c5f14847be4c7580a9f3 Signed-off-by: Przemyslaw Szczerbik --- src/mcelog.c | 52 +++++++++++++++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 19 deletions(-) diff --git a/src/mcelog.c b/src/mcelog.c index 78888cfa..484464f9 100644 --- a/src/mcelog.c +++ b/src/mcelog.c @@ -152,6 +152,17 @@ static int socket_write(socket_adapter_t *self, const char *msg, return ret; } +static void mcelog_dispatch_notification(notification_t *n) { + if (!n) { + ERROR(MCELOG_PLUGIN ": %s: NULL pointer", __FUNCTION__); + return; + } + + sstrncpy(n->host, hostname_g, sizeof(n->host)); + sstrncpy(n->type, "gauge", sizeof(n->type)); + plugin_dispatch_notification(n); +} + static int socket_reinit(socket_adapter_t *self) { char errbuff[MCELOG_BUFF_SIZE]; int ret = -1; @@ -183,19 +194,19 @@ static int socket_reinit(socket_adapter_t *self) { sstrerror(errno, errbuff, sizeof(errbuff))); self->close(self); ret = -1; - } else + } else { ret = 0; - + mcelog_dispatch_notification( + &(notification_t){.severity = NOTIF_OKAY, + .time = cdtime(), + .message = "Connected to mcelog server", + .plugin = MCELOG_PLUGIN, + .type_instance = "mcelog_status"}); + } pthread_rwlock_unlock(&self->lock); return ret; } -static void mcelog_dispatch_notification(notification_t n) { - sstrncpy(n.host, hostname_g, sizeof(n.host)); - sstrncpy(n.type, "gauge", sizeof(n.type)); - plugin_dispatch_notification(&n); -} - static int mcelog_prepare_notification(notification_t *n, mcelog_memory_rec_t mr) { if (n == NULL) @@ -379,12 +390,12 @@ static int socket_receive(socket_adapter_t *self, FILE **pp_file) { /* connection is broken */ ERROR("%s: Connection to socket is broken", MCELOG_PLUGIN); if (poll_fd.revents & (POLLERR | POLLHUP)) { - notification_t n = { - NOTIF_FAILURE, cdtime(), "", "", MCELOG_PLUGIN, "", "", "", NULL}; - ssnprintf(n.message, sizeof(n.message), - "Connection to mcelog socket is broken."); - sstrncpy(n.type_instance, "mcelog_status", sizeof(n.type_instance)); - mcelog_dispatch_notification(n); + mcelog_dispatch_notification( + &(notification_t){.severity = NOTIF_FAILURE, + .time = cdtime(), + .message = "Connection to mcelog socket is broken.", + .plugin = MCELOG_PLUGIN, + .type_instance = "mcelog_status"}); } pthread_rwlock_unlock(&self->lock); return -1; @@ -440,12 +451,15 @@ static void *poll_worker(__attribute__((unused)) void *arg) { memset(&memory_record, 0, sizeof(memory_record)); continue; } - notification_t n = {NOTIF_OKAY, cdtime(), "", "", MCELOG_PLUGIN, - "", "", "", NULL}; - ssnprintf(n.message, sizeof(n.message), "Got memory errors info."); - sstrncpy(n.type_instance, "memory_erros", sizeof(n.type_instance)); + + notification_t n = {.severity = NOTIF_OKAY, + .time = cdtime(), + .message = "Got memory errors info.", + .plugin = MCELOG_PLUGIN, + .type_instance = "memory_erros"}; + if (mcelog_prepare_notification(&n, memory_record) == 0) - mcelog_dispatch_notification(n); + mcelog_dispatch_notification(&n); if (mcelog_submit(memory_record) != 0) ERROR("%s: Failed to submit memory errors", MCELOG_PLUGIN); memset(&memory_record, 0, sizeof(memory_record)); -- 2.11.0