4b749c1dfa040c7db9ba024f5a870a59fc16963f
1 /**
2 * collectd - src/hddtemp.c
3 * Copyright (C) 2005 Vincent Stehlé
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors:
20 * Vincent Stehlé <vincent.stehle at free.fr>
21 * Florian octo Forster <octo at verplant.org>
22 **/
24 #include "hddtemp.h"
26 #if COLLECT_HDDTEMP
27 #define MODULE_NAME "hddtemp"
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <netinet/tcp.h>
33 #include <string.h>
34 #include <errno.h>
35 #include <syslog.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <libgen.h> /* for basename */
40 #include "plugin.h"
41 #include "common.h"
43 /* LOCALHOST_ADDR
44 The ip address 127.0.0.1, as a 32 bit. */
45 #define LOCALHOST_ADDR 0x7F000001
47 /* HDDTEMP_PORT
48 The tcp port the hddtemp daemon is listening on. */
49 #define HDDTEMP_PORT 7634
51 /* BUFFER_SIZE
52 Size of the buffer we use to receive from the hddtemp daemon. */
53 #define BUFFER_SIZE 1024
55 static char *filename_format = "hddtemp-%s.rrd";
57 static char *ds_def[] =
58 {
59 "DS:value:GAUGE:25:U:U",
60 NULL
61 };
62 static int ds_num = 1;
64 extern time_t curtime;
66 typedef struct hddname
67 {
68 int major;
69 int minor;
70 char *name;
71 struct hddname *next;
72 } hddname_t;
74 static hddname_t *first_hddname = NULL;
76 /* hddtemp_query_daemon
77 Connect to the hddtemp daemon and receive data.
79 Parameters:
80 buffer: the buffer where we put the received ascii string.
81 buffer_size: size of the buffer
83 Return value:
84 >= 0 if ok, < 0 otherwise.
86 Example of possible strings, as received from daemon:
88 |/dev/hda|ST340014A|36|C|
89 |/dev/hda|ST380011A|46|C||/dev/hdd|ST340016A|SLP|*|
91 FIXME: we need to create a new socket each time. Is there another way? */
93 static int hddtemp_query_daemon (char *buffer, int buffer_size)
94 {
95 int sock;
96 ssize_t size;
97 const struct sockaddr_in addr =
98 {
99 AF_INET, /* sin_family */
100 htons(HDDTEMP_PORT), /* sin_port */
101 { /* sin_addr */
102 htonl(LOCALHOST_ADDR), /* s_addr */
103 }
104 };
106 /* create our socket descriptor */
107 if ((sock = socket (PF_INET, SOCK_STREAM, 0)) < 0)
108 {
109 syslog (LOG_ERR, "hddtemp: could not create socket: %s", strerror (errno));
110 return (-1);
111 }
113 /* connect to the hddtemp daemon */
114 if (connect (sock, (const struct sockaddr *) &addr, sizeof (addr)))
115 {
116 syslog (LOG_ERR, "hddtemp: Could not connect to the hddtemp daemon: %s", strerror (errno));
117 close (sock);
118 return (-1);
119 }
121 /* receive data from the hddtemp daemon */
122 memset (buffer, '\0', buffer_size);
123 size = recv (sock, buffer, buffer_size, 0);
125 if (size >= buffer_size)
126 {
127 syslog (LOG_WARNING, "hddtemp: Message from hddtemp has been truncated.");
128 close (sock);
129 return (-1);
130 }
131 /* FIXME: Since the server closes the connection this returns zero. At
132 * least my machine does. -octo */
133 /*
134 else if (size == 0)
135 {
136 syslog (LOG_WARNING, "hddtemp: Peer has unexpectedly shut down the socket. Buffer: `%s'", buffer);
137 close (sock);
138 return (-1);
139 }
140 */
141 else if (size < 0)
142 {
143 syslog (LOG_ERR, "hddtemp: Could not receive from the hddtemp daemon: %s", strerror (errno));
144 close (sock);
145 return (-1);
146 }
148 close (sock);
149 return (0);
150 }
152 static void hddtemp_init (void)
153 {
154 FILE *fh;
155 char buf[BUFFER_SIZE];
156 int buflen;
158 char *fields[16];
159 int num_fields;
161 int major;
162 int minor;
163 char *name;
164 hddname_t *next;
165 hddname_t *entry;
167 next = first_hddname;
168 while (next != NULL)
169 {
170 entry = next;
171 next = entry->next;
173 free (entry->name);
174 free (entry);
175 }
176 first_hddname = NULL;
178 if ((fh = fopen ("/proc/partitions", "r")) != NULL)
179 {
180 while (fgets (buf, BUFFER_SIZE, fh) != NULL)
181 {
182 /* Delete trailing newlines */
183 buflen = strlen (buf);
184 while ((buflen > 0) && ((buf[buflen-1] == '\n') || (buf[buflen-1] == '\r')))
185 buf[--buflen] = '\0';
186 if (buflen == 0)
187 continue;
189 num_fields = strsplit (buf, fields, 16);
191 if (num_fields != 4)
192 continue;
194 major = atoi (fields[0]);
195 minor = atoi (fields[1]);
197 /* I know that this makes `minor' redundant, but I want
198 * to be able to change this beavior in the future..
199 * And 4 or 8 bytes won't hurt anybody.. -octo */
200 if (major == 0)
201 continue;
202 if (minor != 0)
203 continue;
205 if ((name = strdup (fields[3])) == NULL)
206 continue;
208 if ((entry = (hddname_t *) malloc (sizeof (hddname_t))) == NULL)
209 {
210 free (name);
211 continue;
212 }
214 entry->major = major;
215 entry->minor = minor;
216 entry->name = name;
217 entry->next = NULL;
219 if (first_hddname == NULL)
220 {
221 first_hddname = entry;
222 }
223 else
224 {
225 entry->next = first_hddname;
226 first_hddname = entry;
227 }
228 }
229 }
231 return;
232 }
234 static void hddtemp_write (char *host, char *inst, char *val)
235 {
236 char filename[BUFFER_SIZE];
237 int status;
239 /* construct filename */
240 status = snprintf (filename, BUFFER_SIZE, filename_format, inst);
241 if (status < 1)
242 return;
243 else if (status >= BUFFER_SIZE)
244 return;
246 rrd_update_file (host, filename, val, ds_def, ds_num);
247 }
249 static char *hddtemp_get_name (char *drive)
250 {
251 hddname_t *list;
252 char *ret;
254 for (list = first_hddname; list != NULL; list = list->next)
255 if (strcmp (drive, list->name) == 0)
256 break;
258 if (list == NULL)
259 return (strdup (drive));
261 if ((ret = (char *) malloc (128 * sizeof (char))) == NULL)
262 return (NULL);
264 if (snprintf (ret, 128, "%i-%i", list->major, list->minor) >= 128)
265 {
266 free (ret);
267 return (NULL);
268 }
270 return (ret);
271 }
273 static void hddtemp_submit (char *inst, double temperature)
274 {
275 char buf[BUFFER_SIZE];
277 if (snprintf (buf, BUFFER_SIZE, "%u:%.3f", (unsigned int) curtime, temperature) >= BUFFER_SIZE)
278 return;
280 plugin_submit (MODULE_NAME, inst, buf);
281 }
283 static void hddtemp_read (void)
284 {
285 char buf[BUFFER_SIZE];
286 char *fields[128];
287 char *ptr;
288 int num_fields;
289 int num_disks;
290 int i;
292 static int wait_time = 1;
293 static int wait_left = 0;
295 if (wait_left >= 10)
296 {
297 wait_left -= 10;
298 return;
299 }
301 /* get data from daemon */
302 if (hddtemp_query_daemon (buf, BUFFER_SIZE) < 0)
303 {
304 /* This limit is reached in log2(86400) =~ 17 steps. Since
305 * there is a 2^n seconds wait between each step it will need
306 * roughly one day to reach this limit. -octo */
308 wait_time *= 2;
309 if (wait_time > 86400)
310 wait_time = 86400;
312 wait_left = wait_time;
314 return;
315 }
316 else
317 {
318 wait_time = 1;
319 wait_left = 0;
320 }
322 /* NB: strtok will eat up "||" and leading "|"'s */
323 num_fields = 0;
324 ptr = buf;
325 while ((fields[num_fields] = strtok (ptr, "|")) != NULL)
326 {
327 ptr = NULL;
328 num_fields++;
330 if (num_fields >= 128)
331 break;
332 }
334 num_disks = num_fields / 4;
336 for (i = 0; i < num_disks; i++)
337 {
338 char *name, *submit_name;
339 double temperature;
340 char *mode;
342 mode = fields[4*i + 3];
344 /* Skip non-temperature information */
345 if (mode[0] != 'C' && mode[0] != 'F')
346 continue;
348 name = basename (fields[4*i + 0]);
349 temperature = atof (fields[4*i + 2]);
351 /* Convert farenheit to celsius */
352 if (mode[0] == 'F')
353 temperature = (temperature - 32.0) * 5.0 / 9.0;
355 if ((submit_name = hddtemp_get_name (name)) != NULL)
356 {
357 hddtemp_submit (submit_name, temperature);
358 free (submit_name);
359 }
360 else
361 {
362 hddtemp_submit (name, temperature);
363 }
364 }
365 }
367 /* module_register
368 Register collectd plugin. */
369 void module_register (void)
370 {
371 plugin_register (MODULE_NAME, hddtemp_init, hddtemp_read, hddtemp_write);
372 }
374 #endif /* COLLECT_HDDTEMP */