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 <libgen.h> /* for basename */
42 #include <netinet/in.h>
43 #include <netinet/tcp.h>
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[] = {"Host", "Port"};
54 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
56 static char *hddtemp_host;
57 static char hddtemp_port[16];
61 * hddtemp_query_daemon
64 * Connect to the hddtemp daemon and receive data.
67 * `buffer' The buffer where we put the received ascii string.
68 * `buffer_size' Size of the buffer
71 * >= 0 if ok, < 0 otherwise.
74 * Example of possible strings, as received from daemon:
75 * |/dev/hda|ST340014A|36|C|
76 * |/dev/hda|ST380011A|46|C||/dev/hdd|ST340016A|SLP|*|
79 * we need to create a new socket each time. Is there another way?
80 * Hm, maybe we can re-use the `sockaddr' structure? -octo
82 static char *hddtemp_query_daemon(void) {
94 struct addrinfo *ai_list;
99 host = HDDTEMP_DEF_HOST;
102 if (strlen(port) == 0)
103 port = HDDTEMP_DEF_PORT;
105 struct addrinfo ai_hints = {.ai_flags = AI_ADDRCONFIG,
106 .ai_family = AF_UNSPEC,
107 .ai_protocol = IPPROTO_TCP,
108 .ai_socktype = SOCK_STREAM};
110 if ((ai_return = getaddrinfo(host, port, &ai_hints, &ai_list)) != 0) {
111 ERROR("hddtemp plugin: getaddrinfo (%s, %s): %s", host, port,
112 (ai_return == EAI_SYSTEM) ? STRERRNO : gai_strerror(ai_return));
117 for (struct addrinfo *ai_ptr = ai_list; ai_ptr != NULL;
118 ai_ptr = ai_ptr->ai_next) {
119 /* create our socket descriptor */
120 fd = socket(ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
122 ERROR("hddtemp plugin: socket: %s", STRERRNO);
126 /* connect to the hddtemp daemon */
127 if (connect(fd, (struct sockaddr *)ai_ptr->ai_addr, ai_ptr->ai_addrlen)) {
128 INFO("hddtemp plugin: connect (%s, %s) failed: %s", host, port, STRERRNO);
134 /* A socket could be opened and connecting succeeded. We're
139 freeaddrinfo(ai_list);
142 ERROR("hddtemp plugin: Could not connect to daemon.");
146 /* receive data from the hddtemp daemon */
151 if ((buffer_size == 0) || (buffer_fill >= buffer_size - 1)) {
152 if (buffer_size == 0)
156 if (buffer_size > HDDTEMP_MAX_RECV_BUF) {
157 WARNING("hddtemp plugin: Message from hddtemp has been "
161 new_buffer = realloc(buffer, buffer_size);
162 if (new_buffer == NULL) {
165 ERROR("hddtemp plugin: Allocation failed.");
170 status = read(fd, buffer + buffer_fill, buffer_size - buffer_fill - 1);
173 } else if (status == -1) {
175 if ((errno == EAGAIN) || (errno == EINTR))
178 ERROR("hddtemp plugin: Error reading from socket: %s", STRERRNO);
183 buffer_fill += status;
186 if (buffer_fill == 0) {
187 WARNING("hddtemp plugin: Peer has unexpectedly shut down "
188 "the socket. Buffer: `%s'",
195 assert(buffer_fill < buffer_size);
196 buffer[buffer_fill] = '\0';
201 static int hddtemp_config(const char *key, const char *value) {
202 if (strcasecmp(key, "Host") == 0) {
203 if (hddtemp_host != NULL)
205 hddtemp_host = strdup(value);
206 } else if (strcasecmp(key, "Port") == 0) {
207 int port = (int)(atof(value));
208 if ((port > 0) && (port <= 65535))
209 snprintf(hddtemp_port, sizeof(hddtemp_port), "%i", port);
211 sstrncpy(hddtemp_port, value, sizeof(hddtemp_port));
219 static void hddtemp_submit(char *type_instance, double value) {
220 value_list_t vl = VALUE_LIST_INIT;
222 vl.values = &(value_t){.gauge = value};
224 sstrncpy(vl.plugin, "hddtemp", sizeof(vl.plugin));
225 sstrncpy(vl.type, "temperature", sizeof(vl.type));
226 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
228 plugin_dispatch_values(&vl);
231 static int hddtemp_read(void) {
240 /* get data from daemon */
241 buf = hddtemp_query_daemon();
245 /* NB: strtok_r will eat up "||" and leading "|"'s */
248 while ((name = strtok_r(ptr, "|", &saveptr)) != NULL &&
249 (model = strtok_r(NULL, "|", &saveptr)) != NULL &&
250 (temperature = strtok_r(NULL, "|", &saveptr)) != NULL &&
251 (mode = strtok_r(NULL, "|", &saveptr)) != NULL) {
252 double temperature_value;
256 /* Skip non-temperature information */
257 if (mode[0] != 'C' && mode[0] != 'F')
260 name = basename(name);
261 temperature_value = atof(temperature);
263 /* Convert farenheit to celsius */
265 temperature_value = (temperature_value - 32.0) * 5.0 / 9.0;
267 hddtemp_submit(name, temperature_value);
272 } /* int hddtemp_read */
275 Register collectd plugin. */
276 void module_register(void) {
277 plugin_register_config("hddtemp", hddtemp_config, config_keys,
279 plugin_register_read("hddtemp", hddtemp_read);