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 verplant.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"
33 #include "common.h"
34 #include "plugin.h"
35 #include "configfile.h"
37 # include <netdb.h>
38 # include <sys/socket.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;
93 struct addrinfo *ai_list, *ai_ptr;
94 int ai_return;
96 memset (&ai_hints, '\0', sizeof (ai_hints));
97 ai_hints.ai_flags = 0;
98 #ifdef AI_ADDRCONFIG
99 ai_hints.ai_flags |= AI_ADDRCONFIG;
100 #endif
101 ai_hints.ai_family = PF_UNSPEC;
102 ai_hints.ai_socktype = SOCK_STREAM;
103 ai_hints.ai_protocol = IPPROTO_TCP;
105 host = hddtemp_host;
106 if (host == NULL)
107 host = HDDTEMP_DEF_HOST;
109 port = hddtemp_port;
110 if (strlen (port) == 0)
111 port = HDDTEMP_DEF_PORT;
113 if ((ai_return = getaddrinfo (host, port, &ai_hints, &ai_list)) != 0)
114 {
115 char errbuf[1024];
116 ERROR ("hddtemp plugin: getaddrinfo (%s, %s): %s",
117 host, port,
118 (ai_return == EAI_SYSTEM)
119 ? sstrerror (errno, errbuf, sizeof (errbuf))
120 : gai_strerror (ai_return));
121 return (-1);
122 }
124 fd = -1;
125 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
126 {
127 /* create our socket descriptor */
128 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
129 ai_ptr->ai_protocol);
130 if (fd < 0)
131 {
132 char errbuf[1024];
133 ERROR ("hddtemp plugin: socket: %s",
134 sstrerror (errno, errbuf, sizeof (errbuf)));
135 continue;
136 }
138 /* connect to the hddtemp daemon */
139 if (connect (fd, (struct sockaddr *) ai_ptr->ai_addr,
140 ai_ptr->ai_addrlen))
141 {
142 char errbuf[1024];
143 INFO ("hddtemp plugin: connect (%s, %s) failed: %s",
144 host, port,
145 sstrerror (errno, errbuf, sizeof (errbuf)));
146 close (fd);
147 fd = -1;
148 continue;
149 }
151 /* A socket could be opened and connecting succeeded. We're
152 * done. */
153 break;
154 }
156 freeaddrinfo (ai_list);
158 if (fd < 0)
159 {
160 ERROR ("hddtemp plugin: Could not connect to daemon.");
161 return (-1);
162 }
164 /* receive data from the hddtemp daemon */
165 memset (buffer, '\0', buffer_size);
167 buffer_fill = 0;
168 while ((status = read (fd, buffer + buffer_fill, buffer_size - buffer_fill)) != 0)
169 {
170 if (status == -1)
171 {
172 char errbuf[1024];
174 if ((errno == EAGAIN) || (errno == EINTR))
175 continue;
177 ERROR ("hddtemp plugin: Error reading from socket: %s",
178 sstrerror (errno, errbuf, sizeof (errbuf)));
179 close (fd);
180 return (-1);
181 }
182 buffer_fill += status;
184 if (buffer_fill >= buffer_size)
185 break;
186 }
188 if (buffer_fill >= buffer_size)
189 {
190 buffer[buffer_size - 1] = '\0';
191 WARNING ("hddtemp plugin: Message from hddtemp has been "
192 "truncated.");
193 }
194 else if (buffer_fill == 0)
195 {
196 WARNING ("hddtemp plugin: Peer has unexpectedly shut down "
197 "the socket. Buffer: `%s'", buffer);
198 close (fd);
199 return (-1);
200 }
202 close (fd);
203 return (0);
204 }
206 static int hddtemp_config (const char *key, const char *value)
207 {
208 if (strcasecmp (key, "Host") == 0)
209 {
210 if (hddtemp_host != NULL)
211 free (hddtemp_host);
212 hddtemp_host = strdup (value);
213 }
214 else if (strcasecmp (key, "Port") == 0)
215 {
216 int port = (int) (atof (value));
217 if ((port > 0) && (port <= 65535))
218 ssnprintf (hddtemp_port, sizeof (hddtemp_port),
219 "%i", port);
220 else
221 sstrncpy (hddtemp_port, value, sizeof (hddtemp_port));
222 }
223 else
224 {
225 return (-1);
226 }
228 return (0);
229 }
231 static void hddtemp_submit (char *type_instance, double value)
232 {
233 value_t values[1];
234 value_list_t vl = VALUE_LIST_INIT;
236 values[0].gauge = value;
238 vl.values = values;
239 vl.values_len = 1;
240 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
241 sstrncpy (vl.plugin, "hddtemp", sizeof (vl.plugin));
242 sstrncpy (vl.type, "temperature", sizeof (vl.type));
243 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
245 plugin_dispatch_values (&vl);
246 }
248 static int hddtemp_read (void)
249 {
250 char buf[1024];
251 char *fields[128];
252 char *ptr;
253 char *saveptr;
254 int num_fields;
255 int num_disks;
256 int i;
258 /* get data from daemon */
259 if (hddtemp_query_daemon (buf, sizeof (buf)) < 0)
260 return (-1);
262 /* NB: strtok_r will eat up "||" and leading "|"'s */
263 num_fields = 0;
264 ptr = buf;
265 saveptr = NULL;
266 while ((fields[num_fields] = strtok_r (ptr, "|", &saveptr)) != NULL)
267 {
268 ptr = NULL;
269 num_fields++;
271 if (num_fields >= 128)
272 break;
273 }
275 num_disks = num_fields / 4;
277 for (i = 0; i < num_disks; i++)
278 {
279 char *name;
280 double temperature;
281 char *mode;
283 mode = fields[4*i + 3];
284 name = basename (fields[4*i + 0]);
286 /* Skip non-temperature information */
287 if (mode[0] != 'C' && mode[0] != 'F')
288 continue;
290 temperature = atof (fields[4*i + 2]);
292 /* Convert farenheit to celsius */
293 if (mode[0] == 'F')
294 temperature = (temperature - 32.0) * 5.0 / 9.0;
296 hddtemp_submit (name, temperature);
297 }
299 return (0);
300 } /* int hddtemp_read */
302 /* module_register
303 Register collectd plugin. */
304 void module_register (void)
305 {
306 plugin_register_config ("hddtemp", hddtemp_config,
307 config_keys, config_keys_num);
308 plugin_register_read ("hddtemp", hddtemp_read);
309 }