static int wg_flush_nolock (int timeout, struct wg_callback *cb) { int status; DEBUG ("write_graphite plugin: wg_flush_nolock: timeout = %.3f; " "send_buf_fill = %zu;", (double)timeout, cb->send_buf_fill); /* timeout == 0 => flush unconditionally */ if (timeout > 0) { time_t now; now = time (NULL); if ((cb->send_buf_init_time + timeout) > now) return (0); } if (cb->send_buf_fill <= 0) { cb->send_buf_init_time = time (NULL); return (0); } status = wg_send_buffer (cb); wg_reset_buffer (cb); return (status); }
static int wg_callback_init (struct wg_callback *cb) { int status; struct sockaddr_in serv_addr; if (cb->sock_fd > 0) return (0); cb->sock_fd = socket (AF_INET, SOCK_STREAM, 0); if (cb->sock_fd < 0) { ERROR ("write_graphite plugin: socket failed: %s", strerror (errno)); return (-1); } cb->server = gethostbyname(cb->host); if (cb->server == NULL) { ERROR ("write_graphite plugin: no such host"); return (-1); } memset (&serv_addr, 0, sizeof (serv_addr)); serv_addr.sin_family = AF_INET; memcpy (&serv_addr.sin_addr.s_addr, cb->server->h_addr, cb->server->h_length); serv_addr.sin_port = htons(cb->port); status = connect(cb->sock_fd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)); if (status < 0) { char errbuf[1024]; sstrerror (errno, errbuf, sizeof (errbuf)); ERROR ("write_graphite plugin: connect failed: %s", errbuf); close (cb->sock_fd); cb->sock_fd = -1; return (-1); } wg_reset_buffer (cb); return (0); }
static int wg_callback_init (struct wg_callback *cb) { struct addrinfo ai_hints; struct addrinfo *ai_list; struct addrinfo *ai_ptr; cdtime_t now; int status; char connerr[1024] = ""; if (cb->sock_fd > 0) return (0); /* Don't try to reconnect too often. By default, one reconnection attempt * is made per second. */ now = cdtime (); if ((now - cb->last_connect_time) < WG_MIN_RECONNECT_INTERVAL) return (EAGAIN); cb->last_connect_time = now; memset (&ai_hints, 0, sizeof (ai_hints)); #ifdef AI_ADDRCONFIG ai_hints.ai_flags |= AI_ADDRCONFIG; #endif ai_hints.ai_family = AF_UNSPEC; if (0 == strcasecmp ("tcp", cb->protocol)) ai_hints.ai_socktype = SOCK_STREAM; else ai_hints.ai_socktype = SOCK_DGRAM; ai_list = NULL; status = getaddrinfo (cb->node, cb->service, &ai_hints, &ai_list); if (status != 0) { ERROR ("write_graphite plugin: getaddrinfo (%s, %s, %s) failed: %s", cb->node, cb->service, cb->protocol, gai_strerror (status)); return (-1); } assert (ai_list != NULL); for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next) { cb->sock_fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol); if (cb->sock_fd < 0) { char errbuf[1024]; snprintf (connerr, sizeof (connerr), "failed to open socket: %s", sstrerror (errno, errbuf, sizeof (errbuf))); continue; } status = connect (cb->sock_fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen); if (status != 0) { char errbuf[1024]; snprintf (connerr, sizeof (connerr), "failed to connect to remote " "host: %s", sstrerror (errno, errbuf, sizeof (errbuf))); close (cb->sock_fd); cb->sock_fd = -1; continue; } break; } freeaddrinfo (ai_list); if (cb->sock_fd < 0) { if (connerr[0] == '\0') /* this should not happen but try to get a message anyway */ sstrerror (errno, connerr, sizeof (connerr)); c_complain (LOG_ERR, &cb->init_complaint, "write_graphite plugin: Connecting to %s:%s via %s failed. " "The last error was: %s", cb->node, cb->service, cb->protocol, connerr); return (-1); } else { c_release (LOG_INFO, &cb->init_complaint, "write_graphite plugin: Successfully connected to %s:%s via %s.", cb->node, cb->service, cb->protocol); } /* wg_force_reconnect_check does not flush the buffer before closing a * sending socket, so only call wg_reset_buffer() if the socket was closed * for a different reason (tracked in cb->reconnect_interval_reached). */ if (!cb->reconnect_interval_reached || (cb->send_buf_free == 0)) wg_reset_buffer (cb); else cb->reconnect_interval_reached = 0; return (0); }
static int wg_callback_init (struct wg_callback *cb) { struct addrinfo ai_hints; struct addrinfo *ai_list; struct addrinfo *ai_ptr; int status; const char *node = cb->node ? cb->node : WG_DEFAULT_NODE; const char *service = cb->service ? cb->service : WG_DEFAULT_SERVICE; if (cb->sock_fd > 0) return (0); memset (&ai_hints, 0, sizeof (ai_hints)); #ifdef AI_ADDRCONFIG ai_hints.ai_flags |= AI_ADDRCONFIG; #endif ai_hints.ai_family = AF_UNSPEC; ai_hints.ai_socktype = SOCK_STREAM; ai_list = NULL; status = getaddrinfo (node, service, &ai_hints, &ai_list); if (status != 0) { ERROR ("write_graphite plugin: getaddrinfo (%s, %s) failed: %s", node, service, gai_strerror (status)); return (-1); } assert (ai_list != NULL); for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next) { cb->sock_fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol); if (cb->sock_fd < 0) continue; status = connect (cb->sock_fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen); if (status != 0) { close (cb->sock_fd); cb->sock_fd = -1; continue; } break; } freeaddrinfo (ai_list); if (cb->sock_fd < 0) { char errbuf[1024]; ERROR ("write_graphite plugin: Connecting to %s:%s failed. " "The last error was: %s", node, service, sstrerror (errno, errbuf, sizeof (errbuf))); close (cb->sock_fd); return (-1); } wg_reset_buffer (cb); return (0); }