* Florian octo Forster <octo at verplant.org>
**/
-#include "hddtemp.h"
+#include "collectd.h"
+#include "common.h"
+#include "plugin.h"
+#include "configfile.h"
-#if COLLECT_HDDTEMP
#define MODULE_NAME "hddtemp"
-#include <sys/types.h>
+#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
-#include <string.h>
-#include <errno.h>
-#include <syslog.h>
-#include <stdlib.h>
-#include <string.h>
#include <libgen.h> /* for basename */
-#include "plugin.h"
-#include "common.h"
-
-/* 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. */
static char *ds_def[] =
{
- "DS:value:GAUGE:25:U:U",
+ "DS:value:GAUGE:"COLLECTD_HEARTBEAT":U:U",
NULL
};
static int ds_num = 1;
-extern time_t curtime;
+static char *config_keys[] =
+{
+ "Host",
+ "Port",
+ NULL
+};
+static int config_keys_num = 2;
typedef struct hddname
{
} hddname_t;
static hddname_t *first_hddname = NULL;
+static char *hddtemp_host = NULL;
+static char *hddtemp_port = NULL;
-/* hddtemp_query_daemon
- Connect to the hddtemp daemon and receive data.
-
- Parameters:
- buffer: the buffer where we put the received ascii string.
- buffer_size: size of the buffer
+/*
+ * NAME
+ * hddtemp_query_daemon
+ *
+ * DESCRIPTION
+ * Connect to the hddtemp daemon and receive data.
+ *
+ * ARGUMENTS:
+ * `buffer' The buffer where we put the received ascii string.
+ * `buffer_size' Size of the buffer
+ *
+ * RETURN VALUE:
+ * >= 0 if ok, < 0 otherwise.
+ *
+ * NOTES:
+ * Example of possible strings, as received from daemon:
+ * |/dev/hda|ST340014A|36|C|
+ * |/dev/hda|ST380011A|46|C||/dev/hdd|ST340016A|SLP|*|
+ *
+ * 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 fd;
+ ssize_t status;
+ int buffer_fill;
- Return value:
- >= 0 if ok, < 0 otherwise.
+ char *host;
+ char *port;
- Example of possible strings, as received from daemon:
+ struct addrinfo ai_hints;
+ struct addrinfo *ai_list, *ai_ptr;
+ int ai_return;
- |/dev/hda|ST340014A|36|C|
- |/dev/hda|ST380011A|46|C||/dev/hdd|ST340016A|SLP|*|
+ 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;
- FIXME: we need to create a new socket each time. Is there another way? */
+ host = hddtemp_host;
+ if (host == NULL)
+ host = HDDTEMP_DEF_HOST;
-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 */
- }
- };
+ port = hddtemp_port;
+ if (port == NULL)
+ port = HDDTEMP_DEF_PORT;
- /* create our socket descriptor */
- if ((sock = socket (PF_INET, SOCK_STREAM, 0)) < 0)
+ 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);
}
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);
}
-#endif /* COLLECT_HDDTEMP */
+#undef MODULE_NAME