X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Fhddtemp.c;h=9d272f5435d9d816beff17c35afb228baa3feaf7;hb=a2a5b1bd944b822b6acab068393cb695a6a01b27;hp=cfa7b8ff8fec7f02bcdc1e6f98bc79147b763df2;hpb=9ac8dcf06e686456ed893acedf5f0ba68e3e3c97;p=collectd.git diff --git a/src/hddtemp.c b/src/hddtemp.c index cfa7b8ff..9d272f54 100644 --- a/src/hddtemp.c +++ b/src/hddtemp.c @@ -24,21 +24,18 @@ #include "collectd.h" #include "common.h" #include "plugin.h" +#include "configfile.h" #define MODULE_NAME "hddtemp" +#include #include #include #include #include /* for basename */ -/* LOCALHOST_ADDR - The ip address 127.0.0.1, as a 32 bit. */ -#define LOCALHOST_ADDR 0x7F000001 - -/* HDDTEMP_PORT - The tcp port the hddtemp daemon is listening on. */ -#define HDDTEMP_PORT 7634 +#define HDDTEMP_DEF_HOST "127.0.0.1" +#define HDDTEMP_DEF_PORT "7634" /* BUFFER_SIZE Size of the buffer we use to receive from the hddtemp daemon. */ @@ -53,6 +50,14 @@ static char *ds_def[] = }; static int ds_num = 1; +static char *config_keys[] = +{ + "Host", + "Port", + NULL +}; +static int config_keys_num = 2; + typedef struct hddname { int major; @@ -62,6 +67,8 @@ typedef struct hddname } hddname_t; static hddname_t *first_hddname = NULL; +static char *hddtemp_host = NULL; +static char *hddtemp_port = NULL; /* * NAME @@ -84,63 +91,127 @@ static hddname_t *first_hddname = NULL; * * FIXME: * we need to create a new socket each time. Is there another way? + * Hm, maybe we can re-use the `sockaddr' structure? -octo */ static int hddtemp_query_daemon (char *buffer, int buffer_size) { - int sock; - ssize_t size; - const struct sockaddr_in addr = - { - AF_INET, /* sin_family */ - htons(HDDTEMP_PORT), /* sin_port */ - { /* sin_addr */ - htonl(LOCALHOST_ADDR), /* s_addr */ - } - }; + int fd; + ssize_t status; + int buffer_fill; + + char *host; + char *port; + + struct addrinfo ai_hints; + struct addrinfo *ai_list, *ai_ptr; + int ai_return; + + memset (&ai_hints, '\0', sizeof (ai_hints)); + ai_hints.ai_flags = AI_ADDRCONFIG; + ai_hints.ai_family = PF_UNSPEC; + ai_hints.ai_socktype = SOCK_STREAM; + ai_hints.ai_protocol = IPPROTO_TCP; + + host = hddtemp_host; + if (host == NULL) + host = HDDTEMP_DEF_HOST; - /* create our socket descriptor */ - if ((sock = socket (PF_INET, SOCK_STREAM, 0)) < 0) + port = hddtemp_port; + if (port == NULL) + port = HDDTEMP_DEF_PORT; + + if ((ai_return = getaddrinfo (host, port, &ai_hints, &ai_list)) != 0) { - syslog (LOG_ERR, "hddtemp: could not create socket: %s", strerror (errno)); + syslog (LOG_ERR, "hddtemp: getaddrinfo (%s, %s): %s", + host, port, + ai_return == EAI_SYSTEM ? strerror (errno) : gai_strerror (ai_return)); return (-1); } - /* connect to the hddtemp daemon */ - if (connect (sock, (const struct sockaddr *) &addr, sizeof (addr))) + fd = -1; + for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next) { - syslog (LOG_ERR, "hddtemp: Could not connect to the hddtemp daemon: %s", strerror (errno)); - close (sock); - return (-1); + /* create our socket descriptor */ + if ((fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol)) < 0) + { + syslog (LOG_ERR, "hddtemp: socket: %s", + strerror (errno)); + continue; + } + + /* connect to the hddtemp daemon */ + if (connect (fd, (struct sockaddr *) ai_ptr->ai_addr, ai_ptr->ai_addrlen)) + { + syslog (LOG_ERR, "hddtemp: connect (%s, %s): %s", + host, port, strerror (errno)); + close (fd); + fd = -1; + continue; + } } + freeaddrinfo (ai_list); + + if (fd < 0) + return (-1); + /* receive data from the hddtemp daemon */ memset (buffer, '\0', buffer_size); - size = recv (sock, buffer, buffer_size, 0); - if (size >= buffer_size) + buffer_fill = 0; + while ((status = read (fd, buffer + buffer_fill, buffer_size - buffer_fill)) != 0) { + if (status == -1) + { + if ((errno == EAGAIN) || (errno == EINTR)) + continue; + + syslog (LOG_ERR, "hddtemp: Error reading from socket: %s", + strerror (errno)); + return (-1); + } + buffer_fill += status; + + if (buffer_fill >= buffer_size) + break; + } + + if (buffer_fill >= buffer_size) + { + buffer[buffer_size - 1] = '\0'; syslog (LOG_WARNING, "hddtemp: Message from hddtemp has been truncated."); - close (sock); - return (-1); } - /* FIXME: Since the server closes the connection this returns zero. At - * least my machine does. -octo */ - /* - else if (size == 0) + else if (buffer_fill == 0) { - syslog (LOG_WARNING, "hddtemp: Peer has unexpectedly shut down the socket. Buffer: `%s'", buffer); - close (sock); + syslog (LOG_WARNING, "hddtemp: Peer has unexpectedly shut down the socket. " + "Buffer: `%s'", buffer); + close (fd); return (-1); } - */ - else if (size < 0) + + close (fd); + return (0); +} + +static int hddtemp_config (char *key, char *value) +{ + if (strcasecmp (key, "host") == 0) + { + if (hddtemp_host != NULL) + free (hddtemp_host); + hddtemp_host = strdup (value); + } + else if (strcasecmp (key, "port") == 0) + { + if (hddtemp_port != NULL) + free (hddtemp_port); + hddtemp_port = strdup (value); + } + else { - syslog (LOG_ERR, "hddtemp: Could not receive from the hddtemp daemon: %s", strerror (errno)); - close (sock); return (-1); } - close (sock); return (0); } @@ -364,6 +435,7 @@ static void hddtemp_read (void) void module_register (void) { plugin_register (MODULE_NAME, hddtemp_init, hddtemp_read, hddtemp_write); + cf_register (MODULE_NAME, hddtemp_config, config_keys, config_keys_num); } #undef MODULE_NAME