2 * collectd - src/hddtemp.c
3 * Copyright (C) 2005,2006 Vincent Stehlé
4 * Copyright (C) 2006-2010 Florian octo Forster
5 * Copyright (C) 2008 Sebastian Harl
6 * Copyright (C) 2014 Carnegie Mellon University
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 * Vincent Stehlé <vincent.stehle at free.fr>
24 * Florian octo Forster <octo at collectd.org>
25 * Sebastian Harl <sh at tokkee.org>
26 * Benjamin Gilbert <bgilbert at backtick.net>
29 * Do a pass, some day, and spare some memory. We consume too much for now
30 * in string buffers and the like.
40 # include <netinet/in.h>
41 # include <netinet/tcp.h>
42 # include <libgen.h> /* for basename */
45 #if HAVE_LINUX_MAJOR_H
46 # include <linux/major.h>
49 #define HDDTEMP_DEF_HOST "127.0.0.1"
50 #define HDDTEMP_DEF_PORT "7634"
51 #define HDDTEMP_MAX_RECV_BUF (1 << 20)
53 static const char *config_keys[] =
58 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
60 static char *hddtemp_host = NULL;
61 static char hddtemp_port[16];
65 * hddtemp_query_daemon
68 * Connect to the hddtemp daemon and receive data.
71 * `buffer' The buffer where we put the received ascii string.
72 * `buffer_size' Size of the buffer
75 * >= 0 if ok, < 0 otherwise.
78 * Example of possible strings, as received from daemon:
79 * |/dev/hda|ST340014A|36|C|
80 * |/dev/hda|ST380011A|46|C||/dev/hdd|ST340016A|SLP|*|
83 * we need to create a new socket each time. Is there another way?
84 * Hm, maybe we can re-use the `sockaddr' structure? -octo
86 static char *hddtemp_query_daemon (void)
99 struct addrinfo *ai_list;
104 host = HDDTEMP_DEF_HOST;
107 if (strlen (port) == 0)
108 port = HDDTEMP_DEF_PORT;
110 struct addrinfo ai_hints = {
111 .ai_flags = AI_ADDRCONFIG,
112 .ai_family = AF_UNSPEC,
113 .ai_protocol = IPPROTO_TCP,
114 .ai_socktype = SOCK_STREAM
117 if ((ai_return = getaddrinfo (host, port, &ai_hints, &ai_list)) != 0)
120 ERROR ("hddtemp plugin: getaddrinfo (%s, %s): %s",
122 (ai_return == EAI_SYSTEM)
123 ? sstrerror (errno, errbuf, sizeof (errbuf))
124 : gai_strerror (ai_return));
129 for (struct addrinfo *ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
131 /* create our socket descriptor */
132 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
133 ai_ptr->ai_protocol);
137 ERROR ("hddtemp plugin: socket: %s",
138 sstrerror (errno, errbuf, sizeof (errbuf)));
142 /* connect to the hddtemp daemon */
143 if (connect (fd, (struct sockaddr *) ai_ptr->ai_addr,
147 INFO ("hddtemp plugin: connect (%s, %s) failed: %s",
149 sstrerror (errno, errbuf, sizeof (errbuf)));
155 /* A socket could be opened and connecting succeeded. We're
160 freeaddrinfo (ai_list);
164 ERROR ("hddtemp plugin: Could not connect to daemon.");
168 /* receive data from the hddtemp daemon */
174 if ((buffer_size == 0) || (buffer_fill >= buffer_size - 1))
176 if (buffer_size == 0)
180 if (buffer_size > HDDTEMP_MAX_RECV_BUF)
182 WARNING ("hddtemp plugin: Message from hddtemp has been "
186 new_buffer = realloc (buffer, buffer_size);
187 if (new_buffer == NULL) {
190 ERROR ("hddtemp plugin: Allocation failed.");
195 status = read (fd, buffer + buffer_fill, buffer_size - buffer_fill - 1);
199 else if (status == -1)
203 if ((errno == EAGAIN) || (errno == EINTR))
206 ERROR ("hddtemp plugin: Error reading from socket: %s",
207 sstrerror (errno, errbuf, sizeof (errbuf)));
212 buffer_fill += status;
215 if (buffer_fill == 0)
217 WARNING ("hddtemp plugin: Peer has unexpectedly shut down "
218 "the socket. Buffer: `%s'", buffer);
224 assert (buffer_fill < buffer_size);
225 buffer[buffer_fill] = '\0';
230 static int hddtemp_config (const char *key, const char *value)
232 if (strcasecmp (key, "Host") == 0)
234 if (hddtemp_host != NULL)
236 hddtemp_host = strdup (value);
238 else if (strcasecmp (key, "Port") == 0)
240 int port = (int) (atof (value));
241 if ((port > 0) && (port <= 65535))
242 ssnprintf (hddtemp_port, sizeof (hddtemp_port),
245 sstrncpy (hddtemp_port, value, sizeof (hddtemp_port));
255 static void hddtemp_submit (char *type_instance, double value)
257 value_list_t vl = VALUE_LIST_INIT;
259 vl.values = &(value_t) { .gauge = value };
261 sstrncpy (vl.plugin, "hddtemp", sizeof (vl.plugin));
262 sstrncpy (vl.type, "temperature", sizeof (vl.type));
263 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
265 plugin_dispatch_values (&vl);
268 static int hddtemp_read (void)
278 /* get data from daemon */
279 buf = hddtemp_query_daemon ();
283 /* NB: strtok_r will eat up "||" and leading "|"'s */
286 while ((name = strtok_r (ptr, "|", &saveptr)) != NULL &&
287 (model = strtok_r (NULL, "|", &saveptr)) != NULL &&
288 (temperature = strtok_r (NULL, "|", &saveptr)) != NULL &&
289 (mode = strtok_r (NULL, "|", &saveptr)) != NULL)
291 double temperature_value;
295 /* Skip non-temperature information */
296 if (mode[0] != 'C' && mode[0] != 'F')
299 name = basename (name);
300 temperature_value = atof (temperature);
302 /* Convert farenheit to celsius */
304 temperature_value = (temperature_value - 32.0) * 5.0 / 9.0;
306 hddtemp_submit (name, temperature_value);
311 } /* int hddtemp_read */
314 Register collectd plugin. */
315 void module_register (void)
317 plugin_register_config ("hddtemp", hddtemp_config,
318 config_keys, config_keys_num);
319 plugin_register_read ("hddtemp", hddtemp_read);