/** * Function to send messages (Msg) to riemann. * * Acquires the host lock, disconnects on errors. */ static int riemann_send(struct riemann_host *host, Msg const *msg) /* {{{ */ { int status = 0; pthread_mutex_lock (&host->lock); status = riemann_send_msg(host, msg); if (status != 0) { riemann_disconnect (host); pthread_mutex_unlock (&host->lock); return status; } /* * For TCP we need to receive message acknowledgemenent. */ if (host->use_tcp) { status = riemann_recv_ack(host); if (status != 0) { riemann_disconnect (host); pthread_mutex_unlock (&host->lock); return status; } } pthread_mutex_unlock (&host->lock); return 0; } /* }}} int riemann_send */
static int riemann_send(struct riemann_host *host, Msg const *msg) { u_char *buffer; size_t buffer_len; int status; pthread_mutex_lock (&host->lock); status = riemann_connect (host); if (status != 0) { pthread_mutex_unlock (&host->lock); return status; } buffer_len = msg__get_packed_size(msg); if (host->use_tcp) buffer_len += 4; buffer = malloc (buffer_len); if (buffer == NULL) { pthread_mutex_unlock (&host->lock); ERROR ("write_riemann plugin: malloc failed."); return ENOMEM; } memset (buffer, 0, buffer_len); if (host->use_tcp) { uint32_t length = htonl ((uint32_t) (buffer_len - 4)); memcpy (buffer, &length, 4); msg__pack(msg, buffer + 4); } else { msg__pack(msg, buffer); } status = (int) swrite (host->s, buffer, buffer_len); if (status != 0) { char errbuf[1024]; riemann_disconnect (host); pthread_mutex_unlock (&host->lock); ERROR ("write_riemann plugin: Sending to Riemann at %s:%s failed: %s", (host->node != NULL) ? host->node : RIEMANN_HOST, (host->service != NULL) ? host->service : RIEMANN_PORT, sstrerror (errno, errbuf, sizeof (errbuf))); sfree (buffer); return -1; } pthread_mutex_unlock (&host->lock); sfree (buffer); return 0; }
/* * Always call while holding host->lock ! */ static int riemann_batch_flush_nolock (cdtime_t timeout, struct riemann_host *host) { cdtime_t now; int status = 0; if (timeout > 0) { now = cdtime (); if ((host->batch_init + timeout) > now) return status; } riemann_send_msg(host, host->batch_msg); riemann_msg_protobuf_free(host->batch_msg); if (host->use_tcp && ((status = riemann_recv_ack(host)) != 0)) riemann_disconnect (host); host->batch_init = cdtime(); host->batch_msg = NULL; return status; }
static void riemann_free(void *p) /* {{{ */ { struct riemann_host *host = p; if (host == NULL) return; pthread_mutex_lock (&host->lock); host->reference_count--; if (host->reference_count > 0) { pthread_mutex_unlock (&host->lock); return; } riemann_disconnect (host); sfree(host->service); pthread_mutex_destroy (&host->lock); sfree(host); } /* }}} void riemann_free */