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"
37 # include <netdb.h>
38 # include <netinet/in.h>
39 # include <netinet/tcp.h>
40 # include <libgen.h> /* for basename */
42 #if HAVE_LINUX_MAJOR_H
43 # include <linux/major.h>
44 #endif
46 #define HDDTEMP_DEF_HOST "127.0.0.1"
47 #define HDDTEMP_DEF_PORT "7634"
49 static const char *config_keys[] =
50 {
51 "Host",
52 "Port"
53 };
54 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
56 static char *hddtemp_host = NULL;
57 static char hddtemp_port[16];
59 /*
60 * NAME
61 * hddtemp_query_daemon
62 *
63 * DESCRIPTION
64 * Connect to the hddtemp daemon and receive data.
65 *
66 * ARGUMENTS:
67 * `buffer' The buffer where we put the received ascii string.
68 * `buffer_size' Size of the buffer
69 *
70 * RETURN VALUE:
71 * >= 0 if ok, < 0 otherwise.
72 *
73 * NOTES:
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|*|
77 *
78 * FIXME:
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
81 */
82 static int hddtemp_query_daemon (char *buffer, int buffer_size)
83 {
84 int fd;
85 ssize_t status;
86 int buffer_fill;
88 const char *host;
89 const char *port;
91 struct addrinfo *ai_list;
92 int ai_return;
94 host = hddtemp_host;
95 if (host == NULL)
96 host = HDDTEMP_DEF_HOST;
98 port = hddtemp_port;
99 if (strlen (port) == 0)
100 port = HDDTEMP_DEF_PORT;
102 struct addrinfo ai_hints = {
103 .ai_flags = AI_ADDRCONFIG,
104 .ai_family = AF_UNSPEC,
105 .ai_protocol = IPPROTO_TCP,
106 .ai_socktype = SOCK_STREAM
107 };
109 if ((ai_return = getaddrinfo (host, port, &ai_hints, &ai_list)) != 0)
110 {
111 char errbuf[1024];
112 ERROR ("hddtemp plugin: getaddrinfo (%s, %s): %s",
113 host, port,
114 (ai_return == EAI_SYSTEM)
115 ? sstrerror (errno, errbuf, sizeof (errbuf))
116 : gai_strerror (ai_return));
117 return (-1);
118 }
120 fd = -1;
121 for (struct addrinfo *ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
122 {
123 /* create our socket descriptor */
124 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
125 ai_ptr->ai_protocol);
126 if (fd < 0)
127 {
128 char errbuf[1024];
129 ERROR ("hddtemp plugin: socket: %s",
130 sstrerror (errno, errbuf, sizeof (errbuf)));
131 continue;
132 }
134 /* connect to the hddtemp daemon */
135 if (connect (fd, (struct sockaddr *) ai_ptr->ai_addr,
136 ai_ptr->ai_addrlen))
137 {
138 char errbuf[1024];
139 INFO ("hddtemp plugin: connect (%s, %s) failed: %s",
140 host, port,
141 sstrerror (errno, errbuf, sizeof (errbuf)));
142 close (fd);
143 fd = -1;
144 continue;
145 }
147 /* A socket could be opened and connecting succeeded. We're
148 * done. */
149 break;
150 }
152 freeaddrinfo (ai_list);
154 if (fd < 0)
155 {
156 ERROR ("hddtemp plugin: Could not connect to daemon.");
157 return (-1);
158 }
160 /* receive data from the hddtemp daemon */
161 memset (buffer, '\0', buffer_size);
163 buffer_fill = 0;
164 while ((status = read (fd, buffer + buffer_fill, buffer_size - buffer_fill)) != 0)
165 {
166 if (status == -1)
167 {
168 char errbuf[1024];
170 if ((errno == EAGAIN) || (errno == EINTR))
171 continue;
173 ERROR ("hddtemp plugin: Error reading from socket: %s",
174 sstrerror (errno, errbuf, sizeof (errbuf)));
175 close (fd);
176 return (-1);
177 }
178 buffer_fill += status;
180 if (buffer_fill >= buffer_size)
181 break;
182 }
184 if (buffer_fill >= buffer_size)
185 {
186 buffer[buffer_size - 1] = '\0';
187 WARNING ("hddtemp plugin: Message from hddtemp has been "
188 "truncated.");
189 }
190 else if (buffer_fill == 0)
191 {
192 WARNING ("hddtemp plugin: Peer has unexpectedly shut down "
193 "the socket. Buffer: `%s'", buffer);
194 close (fd);
195 return (-1);
196 }
198 close (fd);
199 return (0);
200 }
202 static int hddtemp_config (const char *key, const char *value)
203 {
204 if (strcasecmp (key, "Host") == 0)
205 {
206 if (hddtemp_host != NULL)
207 free (hddtemp_host);
208 hddtemp_host = strdup (value);
209 }
210 else if (strcasecmp (key, "Port") == 0)
211 {
212 int port = (int) (atof (value));
213 if ((port > 0) && (port <= 65535))
214 ssnprintf (hddtemp_port, sizeof (hddtemp_port),
215 "%i", port);
216 else
217 sstrncpy (hddtemp_port, value, sizeof (hddtemp_port));
218 }
219 else
220 {
221 return (-1);
222 }
224 return (0);
225 }
227 static void hddtemp_submit (char *type_instance, double value)
228 {
229 value_t values[1];
230 value_list_t vl = VALUE_LIST_INIT;
232 values[0].gauge = value;
234 vl.values = values;
235 vl.values_len = 1;
236 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
237 sstrncpy (vl.plugin, "hddtemp", sizeof (vl.plugin));
238 sstrncpy (vl.type, "temperature", sizeof (vl.type));
239 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
241 plugin_dispatch_values (&vl);
242 }
244 static int hddtemp_read (void)
245 {
246 char buf[1024];
247 char *fields[128];
248 char *ptr;
249 char *saveptr;
250 int num_fields;
251 int num_disks;
253 /* get data from daemon */
254 if (hddtemp_query_daemon (buf, sizeof (buf)) < 0)
255 return (-1);
257 /* NB: strtok_r will eat up "||" and leading "|"'s */
258 num_fields = 0;
259 ptr = buf;
260 saveptr = NULL;
261 while ((fields[num_fields] = strtok_r (ptr, "|", &saveptr)) != NULL)
262 {
263 ptr = NULL;
264 num_fields++;
266 if (num_fields >= 128)
267 break;
268 }
270 num_disks = num_fields / 4;
272 for (int i = 0; i < num_disks; i++)
273 {
274 char *name;
275 double temperature;
276 char *mode;
278 mode = fields[4*i + 3];
279 name = basename (fields[4*i + 0]);
281 /* Skip non-temperature information */
282 if (mode[0] != 'C' && mode[0] != 'F')
283 continue;
285 temperature = atof (fields[4*i + 2]);
287 /* Convert farenheit to celsius */
288 if (mode[0] == 'F')
289 temperature = (temperature - 32.0) * 5.0 / 9.0;
291 hddtemp_submit (name, temperature);
292 }
294 return (0);
295 } /* int hddtemp_read */
297 /* module_register
298 Register collectd plugin. */
299 void module_register (void)
300 {
301 plugin_register_config ("hddtemp", hddtemp_config,
302 config_keys, config_keys_num);
303 plugin_register_read ("hddtemp", hddtemp_read);
304 }