Merged revision 304 (removal of all `extern time_t curtime' in all plugins) to the...
[collectd.git] / src / hddtemp.c
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  **/
23
24 #include "hddtemp.h"
25
26 #if COLLECT_HDDTEMP
27 #define MODULE_NAME "hddtemp"
28
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 */
39
40 #include "plugin.h"
41 #include "common.h"
42
43 /* LOCALHOST_ADDR
44    The ip address 127.0.0.1, as a 32 bit. */
45 #define LOCALHOST_ADDR 0x7F000001
46
47 /* HDDTEMP_PORT
48    The tcp port the hddtemp daemon is listening on. */
49 #define HDDTEMP_PORT 7634
50
51 /* BUFFER_SIZE
52    Size of the buffer we use to receive from the hddtemp daemon. */
53 #define BUFFER_SIZE 1024
54
55 static char *filename_format = "hddtemp-%s.rrd";
56
57 static char *ds_def[] =
58 {
59         "DS:value:GAUGE:25:U:U",
60         NULL
61 };
62 static int ds_num = 1;
63
64 typedef struct hddname
65 {
66         int major;
67         int minor;
68         char *name;
69         struct hddname *next;
70 } hddname_t;
71
72 static hddname_t *first_hddname = NULL;
73
74 /* hddtemp_query_daemon
75    Connect to the hddtemp daemon and receive data.
76
77    Parameters:
78      buffer:      the buffer where we put the received ascii string.
79      buffer_size: size of the buffer
80
81    Return value:
82      >= 0 if ok, < 0 otherwise.
83
84    Example of possible strings, as received from daemon:
85
86            |/dev/hda|ST340014A|36|C|
87            |/dev/hda|ST380011A|46|C||/dev/hdd|ST340016A|SLP|*|
88
89    FIXME: we need to create a new socket each time. Is there another way? */
90
91 static int hddtemp_query_daemon (char *buffer, int buffer_size)
92 {
93         int sock;
94         ssize_t size;
95         const struct sockaddr_in addr =
96         {
97                 AF_INET,                        /* sin_family */
98                 htons(HDDTEMP_PORT),            /* sin_port */
99                 {                               /* sin_addr */
100                         htonl(LOCALHOST_ADDR),  /* s_addr */
101                 }
102         };
103
104         /* create our socket descriptor */
105         if ((sock = socket (PF_INET, SOCK_STREAM, 0)) < 0)
106         {
107                 syslog (LOG_ERR, "hddtemp: could not create socket: %s", strerror (errno));
108                 return (-1);
109         }
110
111         /* connect to the hddtemp daemon */
112         if (connect (sock, (const struct sockaddr *) &addr, sizeof (addr)))
113         {
114                 syslog (LOG_ERR, "hddtemp: Could not connect to the hddtemp daemon: %s", strerror (errno));
115                 close (sock);
116                 return (-1);
117         }
118
119         /* receive data from the hddtemp daemon */
120         memset (buffer, '\0', buffer_size);
121         size = recv (sock, buffer, buffer_size, 0);
122
123         if (size >= buffer_size)
124         {
125                 syslog (LOG_WARNING, "hddtemp: Message from hddtemp has been truncated.");
126                 close (sock);
127                 return (-1);
128         }
129         /* FIXME: Since the server closes the connection this returns zero. At
130          * least my machine does. -octo */
131         /*
132         else if (size == 0)
133         {
134                 syslog (LOG_WARNING, "hddtemp: Peer has unexpectedly shut down the socket. Buffer: `%s'", buffer);
135                 close (sock);
136                 return (-1);
137         }
138         */
139         else if (size < 0)
140         {
141                 syslog (LOG_ERR, "hddtemp: Could not receive from the hddtemp daemon: %s", strerror (errno));
142                 close (sock);
143                 return (-1);
144         }
145
146         close (sock);
147         return (0);
148 }
149
150 static void hddtemp_init (void)
151 {
152         FILE *fh;
153         char buf[BUFFER_SIZE];
154         int buflen;
155
156         char *fields[16];
157         int num_fields;
158
159         int major;
160         int minor;
161         char *name;
162         hddname_t *next;
163         hddname_t *entry;
164
165         next = first_hddname;
166         while (next != NULL)
167         {
168                 entry = next;
169                 next = entry->next;
170
171                 free (entry->name);
172                 free (entry);
173         }
174         first_hddname = NULL;
175
176         if ((fh = fopen ("/proc/partitions", "r")) != NULL)
177         {
178                 while (fgets (buf, BUFFER_SIZE, fh) != NULL)
179                 {
180                         /* Delete trailing newlines */
181                         buflen = strlen (buf);
182                         while ((buflen > 0) && ((buf[buflen-1] == '\n') || (buf[buflen-1] == '\r')))
183                                 buf[--buflen] = '\0';
184                         if (buflen == 0)
185                                 continue;
186                         
187                         num_fields = strsplit (buf, fields, 16);
188
189                         if (num_fields != 4)
190                                 continue;
191
192                         major = atoi (fields[0]);
193                         minor = atoi (fields[1]);
194
195                         /* I know that this makes `minor' redundant, but I want
196                          * to be able to change this beavior in the future..
197                          * And 4 or 8 bytes won't hurt anybody.. -octo */
198                         if (major == 0)
199                                 continue;
200                         if (minor != 0)
201                                 continue;
202
203                         if ((name = strdup (fields[3])) == NULL)
204                                 continue;
205
206                         if ((entry = (hddname_t *) malloc (sizeof (hddname_t))) == NULL)
207                         {
208                                 free (name);
209                                 continue;
210                         }
211
212                         entry->major = major;
213                         entry->minor = minor;
214                         entry->name  = name;
215                         entry->next  = NULL;
216
217                         if (first_hddname == NULL)
218                         {
219                                 first_hddname = entry;
220                         }
221                         else
222                         {
223                                 entry->next = first_hddname;
224                                 first_hddname = entry;
225                         }
226                 }
227         }
228
229         return;
230 }
231
232 static void hddtemp_write (char *host, char *inst, char *val)
233 {
234         char filename[BUFFER_SIZE];
235         int status;
236
237         /* construct filename */
238         status = snprintf (filename, BUFFER_SIZE, filename_format, inst);
239         if (status < 1)
240                 return;
241         else if (status >= BUFFER_SIZE)
242                 return;
243
244         rrd_update_file (host, filename, val, ds_def, ds_num);
245 }
246
247 static char *hddtemp_get_name (char *drive)
248 {
249         hddname_t *list;
250         char *ret;
251
252         for (list = first_hddname; list != NULL; list = list->next)
253                 if (strcmp (drive, list->name) == 0)
254                         break;
255
256         if (list == NULL)
257                 return (strdup (drive));
258
259         if ((ret = (char *) malloc (128 * sizeof (char))) == NULL)
260                 return (NULL);
261
262         if (snprintf (ret, 128, "%i-%i", list->major, list->minor) >= 128)
263         {
264                 free (ret);
265                 return (NULL);
266         }
267
268         return (ret);
269 }
270
271 static void hddtemp_submit (char *inst, double temperature)
272 {
273         char buf[BUFFER_SIZE];
274
275         if (snprintf (buf, BUFFER_SIZE, "%u:%.3f", (unsigned int) curtime, temperature) >= BUFFER_SIZE)
276                 return;
277
278         plugin_submit (MODULE_NAME, inst, buf);
279 }
280
281 static void hddtemp_read (void)
282 {
283         char buf[BUFFER_SIZE];
284         char *fields[128];
285         char *ptr;
286         int num_fields;
287         int num_disks;
288         int i;
289
290         static int wait_time = 1;
291         static int wait_left = 0;
292
293         if (wait_left >= 10)
294         {
295                 wait_left -= 10;
296                 return;
297         }
298
299         /* get data from daemon */
300         if (hddtemp_query_daemon (buf, BUFFER_SIZE) < 0)
301         {
302                 /* This limit is reached in log2(86400) =~ 17 steps. Since
303                  * there is a 2^n seconds wait between each step it will need
304                  * roughly one day to reach this limit. -octo */
305                 
306                 wait_time *= 2;
307                 if (wait_time > 86400)
308                         wait_time = 86400;
309
310                 wait_left = wait_time;
311
312                 return;
313         }
314         else
315         {
316                 wait_time = 1;
317                 wait_left = 0;
318         }
319
320         /* NB: strtok will eat up "||" and leading "|"'s */
321         num_fields = 0;
322         ptr = buf;
323         while ((fields[num_fields] = strtok (ptr, "|")) != NULL)
324         {
325                 ptr = NULL;
326                 num_fields++;
327
328                 if (num_fields >= 128)
329                         break;
330         }
331
332         num_disks = num_fields / 4;
333
334         for (i = 0; i < num_disks; i++)
335         {
336                 char *name, *submit_name;
337                 double temperature;
338                 char *mode;
339
340                 mode = fields[4*i + 3];
341
342                 /* Skip non-temperature information */
343                 if (mode[0] != 'C' && mode[0] != 'F')
344                         continue;
345
346                 name = basename (fields[4*i + 0]);
347                 temperature = atof (fields[4*i + 2]);
348
349                 /* Convert farenheit to celsius */
350                 if (mode[0] == 'F')
351                         temperature = (temperature - 32.0) * 5.0 / 9.0;
352
353                 if ((submit_name = hddtemp_get_name (name)) != NULL)
354                 {
355                         hddtemp_submit (submit_name, temperature);
356                         free (submit_name);
357                 }
358                 else
359                 {
360                         hddtemp_submit (name, temperature);
361                 }
362         }
363 }
364
365 /* module_register
366    Register collectd plugin. */
367 void module_register (void)
368 {
369         plugin_register (MODULE_NAME, hddtemp_init, hddtemp_read, hddtemp_write);
370 }
371
372 #endif /* COLLECT_HDDTEMP */