Code

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