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 = NULL;
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) {
112 ERROR("hddtemp plugin: getaddrinfo (%s, %s): %s", host, port,
113 (ai_return == EAI_SYSTEM) ? sstrerror(errno, errbuf, sizeof(errbuf))
114 : gai_strerror(ai_return));
119 for (struct addrinfo *ai_ptr = ai_list; ai_ptr != NULL;
120 ai_ptr = ai_ptr->ai_next) {
121 /* create our socket descriptor */
122 fd = socket(ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
125 ERROR("hddtemp plugin: socket: %s",
126 sstrerror(errno, errbuf, sizeof(errbuf)));
130 /* connect to the hddtemp daemon */
131 if (connect(fd, (struct sockaddr *)ai_ptr->ai_addr, ai_ptr->ai_addrlen)) {
133 INFO("hddtemp plugin: connect (%s, %s) failed: %s", host, port,
134 sstrerror(errno, errbuf, sizeof(errbuf)));
140 /* A socket could be opened and connecting succeeded. We're
145 freeaddrinfo(ai_list);
148 ERROR("hddtemp plugin: Could not connect to daemon.");
152 /* receive data from the hddtemp daemon */
157 if ((buffer_size == 0) || (buffer_fill >= buffer_size - 1)) {
158 if (buffer_size == 0)
162 if (buffer_size > HDDTEMP_MAX_RECV_BUF) {
163 WARNING("hddtemp plugin: Message from hddtemp has been "
167 new_buffer = realloc(buffer, buffer_size);
168 if (new_buffer == NULL) {
171 ERROR("hddtemp plugin: Allocation failed.");
176 status = read(fd, buffer + buffer_fill, buffer_size - buffer_fill - 1);
179 } else if (status == -1) {
182 if ((errno == EAGAIN) || (errno == EINTR))
185 ERROR("hddtemp plugin: Error reading from socket: %s",
186 sstrerror(errno, errbuf, sizeof(errbuf)));
191 buffer_fill += status;
194 if (buffer_fill == 0) {
195 WARNING("hddtemp plugin: Peer has unexpectedly shut down "
196 "the socket. Buffer: `%s'",
203 assert(buffer_fill < buffer_size);
204 buffer[buffer_fill] = '\0';
209 static int hddtemp_config(const char *key, const char *value) {
210 if (strcasecmp(key, "Host") == 0) {
211 if (hddtemp_host != NULL)
213 hddtemp_host = strdup(value);
214 } else if (strcasecmp(key, "Port") == 0) {
215 int port = (int)(atof(value));
216 if ((port > 0) && (port <= 65535))
217 ssnprintf(hddtemp_port, sizeof(hddtemp_port), "%i", port);
219 sstrncpy(hddtemp_port, value, sizeof(hddtemp_port));
227 static void hddtemp_submit(char *type_instance, double value) {
228 value_list_t vl = VALUE_LIST_INIT;
230 vl.values = &(value_t){.gauge = value};
232 sstrncpy(vl.plugin, "hddtemp", sizeof(vl.plugin));
233 sstrncpy(vl.type, "temperature", sizeof(vl.type));
234 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
236 plugin_dispatch_values(&vl);
239 static int hddtemp_read(void) {
248 /* get data from daemon */
249 buf = hddtemp_query_daemon();
253 /* NB: strtok_r will eat up "||" and leading "|"'s */
256 while ((name = strtok_r(ptr, "|", &saveptr)) != NULL &&
257 (model = strtok_r(NULL, "|", &saveptr)) != NULL &&
258 (temperature = strtok_r(NULL, "|", &saveptr)) != NULL &&
259 (mode = strtok_r(NULL, "|", &saveptr)) != NULL) {
260 double temperature_value;
264 /* Skip non-temperature information */
265 if (mode[0] != 'C' && mode[0] != 'F')
268 name = basename(name);
269 temperature_value = atof(temperature);
271 /* Convert farenheit to celsius */
273 temperature_value = (temperature_value - 32.0) * 5.0 / 9.0;
275 hddtemp_submit(name, temperature_value);
280 } /* int hddtemp_read */
283 Register collectd plugin. */
284 void module_register(void) {
285 plugin_register_config("hddtemp", hddtemp_config, config_keys,
287 plugin_register_read("hddtemp", hddtemp_read);