X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Fapcups.c;h=29d58c3ad6e0a2d1b2038bf6433d078113fe0497;hb=70ea9457f76d6cd953f2cbd64eeda0ff265f47db;hp=66fe45e119cda0c396da3bbb46e2012de1401687;hpb=fb075e15e3400afdb691b88f1e1a4a38578aaba9;p=collectd.git diff --git a/src/apcups.c b/src/apcups.c index 66fe45e1..29d58c3a 100644 --- a/src/apcups.c +++ b/src/apcups.c @@ -32,9 +32,6 @@ #if HAVE_SYS_TYPES_H # include #endif -#if HAVE_SYS_SOCKET_H -# include -#endif #if HAVE_NETDB_H # include #endif @@ -74,12 +71,12 @@ static char *conf_node = NULL; static char *conf_service = NULL; /* Defaults to false for backwards compatibility. */ static _Bool conf_report_seconds = 0; +static _Bool conf_persistent_conn = 1; static int global_sockfd = -1; static int count_retries = 0; static int count_iterations = 0; -static _Bool close_socket = 0; static int net_shutdown (int *fd) { @@ -128,7 +125,7 @@ static int net_open (char const *node, char const *service) if (status != 0) { char errbuf[1024]; - INFO ("getaddrinfo failed: %s", + INFO ("apcups plugin: getaddrinfo failed: %s", (status == EAI_SYSTEM) ? sstrerror (errno, errbuf, sizeof (errbuf)) : gai_strerror (status)); @@ -147,7 +144,7 @@ static int net_open (char const *node, char const *service) if (sd < 0) { - DEBUG ("Unable to open a socket"); + DEBUG ("apcups plugin: Unable to open a socket"); freeaddrinfo (ai_return); return (-1); } @@ -159,13 +156,13 @@ static int net_open (char const *node, char const *service) if (status != 0) /* `connect(2)' failed */ { char errbuf[1024]; - INFO ("connect failed: %s", + INFO ("apcups plugin: connect failed: %s", sstrerror (errno, errbuf, sizeof (errbuf))); close (sd); return (-1); } - DEBUG ("Done opening a socket %i", sd); + DEBUG ("apcups plugin: Done opening a socket %i", sd); return (sd); } /* int net_open */ @@ -314,7 +311,7 @@ static int apc_query_server (char const *node, char const *service, "first %i iterations. Will close the socket " "in future iterations.", count_retries, count_iterations); - close_socket = 1; + conf_persistent_conn = 0; } while ((n = net_recv (&global_sockfd, recvline, sizeof (recvline) - 1)) > 0) @@ -367,7 +364,7 @@ static int apc_query_server (char const *node, char const *service, } status = errno; /* save errno, net_shutdown() may re-set it. */ - if (close_socket) + if (!conf_persistent_conn) net_shutdown (&global_sockfd); if (n < 0) @@ -395,6 +392,8 @@ static int apcups_config (oconfig_item_t *ci) cf_util_get_service (child, &conf_service); else if (strcasecmp (child->key, "ReportSeconds") == 0) cf_util_get_boolean (child, &conf_report_seconds); + else if (strcasecmp (child->key, "PersistentConnection") == 0) + cf_util_get_boolean (child, &conf_persistent_conn); else ERROR ("apcups plugin: Unknown config option \"%s\".", child->key); } @@ -456,7 +455,7 @@ static int apcups_read (void) */ if (status != 0) { - DEBUG ("apc_query_server (%s, %s) = %i", + DEBUG ("apcups plugin: apc_query_server (%s, %s) = %i", (conf_node == NULL) ? APCUPS_DEFAULT_NODE : conf_node, (conf_service == NULL) ? APCUPS_DEFAULT_SERVICE : conf_service, status);