1 /**
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 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 *
21 * Authors:
22 * Vincent Stehlé <vincent.stehle at free.fr>
23 * Florian octo Forster <octo at collectd.org>
24 * Sebastian Harl <sh at tokkee.org>
25 *
26 * TODO:
27 * Do a pass, some day, and spare some memory. We consume too much for now
28 * in string buffers and the like.
29 *
30 **/
32 #include "collectd.h"
34 #include "common.h"
35 #include "plugin.h"
36 #include "configfile.h"
38 # include <netdb.h>
39 # include <netinet/in.h>
40 # include <netinet/tcp.h>
41 # include <libgen.h> /* for basename */
43 #if HAVE_LINUX_MAJOR_H
44 # include <linux/major.h>
45 #endif
47 #define HDDTEMP_DEF_HOST "127.0.0.1"
48 #define HDDTEMP_DEF_PORT "7634"
50 static const char *config_keys[] =
51 {
52 "Host",
53 "Port"
54 };
55 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
57 static char *hddtemp_host = NULL;
58 static char hddtemp_port[16];
60 /*
61 * NAME
62 * hddtemp_query_daemon
63 *
64 * DESCRIPTION
65 * Connect to the hddtemp daemon and receive data.
66 *
67 * ARGUMENTS:
68 * `buffer' The buffer where we put the received ascii string.
69 * `buffer_size' Size of the buffer
70 *
71 * RETURN VALUE:
72 * >= 0 if ok, < 0 otherwise.
73 *
74 * NOTES:
75 * Example of possible strings, as received from daemon:
76 * |/dev/hda|ST340014A|36|C|
77 * |/dev/hda|ST380011A|46|C||/dev/hdd|ST340016A|SLP|*|
78 *
79 * FIXME:
80 * we need to create a new socket each time. Is there another way?
81 * Hm, maybe we can re-use the `sockaddr' structure? -octo
82 */
83 static int hddtemp_query_daemon (char *buffer, int buffer_size)
84 {
85 int fd;
86 ssize_t status;
87 int buffer_fill;
89 const char *host;
90 const char *port;
92 struct addrinfo ai_hints = { 0 };
93 struct addrinfo *ai_list, *ai_ptr;
94 int ai_return;
96 #ifdef AI_ADDRCONFIG
97 ai_hints.ai_flags |= AI_ADDRCONFIG;
98 #endif
99 ai_hints.ai_family = PF_UNSPEC;
100 ai_hints.ai_socktype = SOCK_STREAM;
101 ai_hints.ai_protocol = IPPROTO_TCP;
103 host = hddtemp_host;
104 if (host == NULL)
105 host = HDDTEMP_DEF_HOST;
107 port = hddtemp_port;
108 if (strlen (port) == 0)
109 port = HDDTEMP_DEF_PORT;
111 if ((ai_return = getaddrinfo (host, port, &ai_hints, &ai_list)) != 0)
112 {
113 char errbuf[1024];
114 ERROR ("hddtemp plugin: getaddrinfo (%s, %s): %s",
115 host, port,
116 (ai_return == EAI_SYSTEM)
117 ? sstrerror (errno, errbuf, sizeof (errbuf))
118 : gai_strerror (ai_return));
119 return (-1);
120 }
122 fd = -1;
123 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
124 {
125 /* create our socket descriptor */
126 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
127 ai_ptr->ai_protocol);
128 if (fd < 0)
129 {
130 char errbuf[1024];
131 ERROR ("hddtemp plugin: socket: %s",
132 sstrerror (errno, errbuf, sizeof (errbuf)));
133 continue;
134 }
136 /* connect to the hddtemp daemon */
137 if (connect (fd, (struct sockaddr *) ai_ptr->ai_addr,
138 ai_ptr->ai_addrlen))
139 {
140 char errbuf[1024];
141 INFO ("hddtemp plugin: connect (%s, %s) failed: %s",
142 host, port,
143 sstrerror (errno, errbuf, sizeof (errbuf)));
144 close (fd);
145 fd = -1;
146 continue;
147 }
149 /* A socket could be opened and connecting succeeded. We're
150 * done. */
151 break;
152 }
154 freeaddrinfo (ai_list);
156 if (fd < 0)
157 {
158 ERROR ("hddtemp plugin: Could not connect to daemon.");
159 return (-1);
160 }
162 /* receive data from the hddtemp daemon */
163 memset (buffer, '\0', buffer_size);
165 buffer_fill = 0;
166 while ((status = read (fd, buffer + buffer_fill, buffer_size - buffer_fill)) != 0)
167 {
168 if (status == -1)
169 {
170 char errbuf[1024];
172 if ((errno == EAGAIN) || (errno == EINTR))
173 continue;
175 ERROR ("hddtemp plugin: Error reading from socket: %s",
176 sstrerror (errno, errbuf, sizeof (errbuf)));
177 close (fd);
178 return (-1);
179 }
180 buffer_fill += status;
182 if (buffer_fill >= buffer_size)
183 break;
184 }
186 if (buffer_fill >= buffer_size)
187 {
188 buffer[buffer_size - 1] = '\0';
189 WARNING ("hddtemp plugin: Message from hddtemp has been "
190 "truncated.");
191 }
192 else if (buffer_fill == 0)
193 {
194 WARNING ("hddtemp plugin: Peer has unexpectedly shut down "
195 "the socket. Buffer: `%s'", buffer);
196 close (fd);
197 return (-1);
198 }
200 close (fd);
201 return (0);
202 }
204 static int hddtemp_config (const char *key, const char *value)
205 {
206 if (strcasecmp (key, "Host") == 0)
207 {
208 if (hddtemp_host != NULL)
209 free (hddtemp_host);
210 hddtemp_host = strdup (value);
211 }
212 else if (strcasecmp (key, "Port") == 0)
213 {
214 int port = (int) (atof (value));
215 if ((port > 0) && (port <= 65535))
216 ssnprintf (hddtemp_port, sizeof (hddtemp_port),
217 "%i", port);
218 else
219 sstrncpy (hddtemp_port, value, sizeof (hddtemp_port));
220 }
221 else
222 {
223 return (-1);
224 }
226 return (0);
227 }
229 static void hddtemp_submit (char *type_instance, double value)
230 {
231 value_t values[1];
232 value_list_t vl = VALUE_LIST_INIT;
234 values[0].gauge = value;
236 vl.values = values;
237 vl.values_len = 1;
238 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
239 sstrncpy (vl.plugin, "hddtemp", sizeof (vl.plugin));
240 sstrncpy (vl.type, "temperature", sizeof (vl.type));
241 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
243 plugin_dispatch_values (&vl);
244 }
246 static int hddtemp_read (void)
247 {
248 char buf[1024];
249 char *fields[128];
250 char *ptr;
251 char *saveptr;
252 int num_fields;
253 int num_disks;
254 int i;
256 /* get data from daemon */
257 if (hddtemp_query_daemon (buf, sizeof (buf)) < 0)
258 return (-1);
260 /* NB: strtok_r will eat up "||" and leading "|"'s */
261 num_fields = 0;
262 ptr = buf;
263 saveptr = NULL;
264 while ((fields[num_fields] = strtok_r (ptr, "|", &saveptr)) != NULL)
265 {
266 ptr = NULL;
267 num_fields++;
269 if (num_fields >= 128)
270 break;
271 }
273 num_disks = num_fields / 4;
275 for (i = 0; i < num_disks; i++)
276 {
277 char *name;
278 double temperature;
279 char *mode;
281 mode = fields[4*i + 3];
282 name = basename (fields[4*i + 0]);
284 /* Skip non-temperature information */
285 if (mode[0] != 'C' && mode[0] != 'F')
286 continue;
288 temperature = atof (fields[4*i + 2]);
290 /* Convert farenheit to celsius */
291 if (mode[0] == 'F')
292 temperature = (temperature - 32.0) * 5.0 / 9.0;
294 hddtemp_submit (name, temperature);
295 }
297 return (0);
298 } /* int hddtemp_read */
300 /* module_register
301 Register collectd plugin. */
302 void module_register (void)
303 {
304 plugin_register_config ("hddtemp", hddtemp_config,
305 config_keys, config_keys_num);
306 plugin_register_read ("hddtemp", hddtemp_read);
307 }