Added copyright/GPL header to all .c and .h files in trunk
[collectd.git] / src / sensors.c
1 /**
2  * collectd - src/sensors.c
3  * Copyright (C) 2005  Florian octo Forster
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  *   Florian octo Forster <octo at verplant.org>
21  **/
22
23 #include "sensors.h"
24
25 #if COLLECT_SENSORS
26 #define MODULE_NAME "sensors"
27
28 #include <sensors/sensors.h>
29
30 #include "plugin.h"
31 #include "common.h"
32
33 typedef struct featurelist
34 {
35         const sensors_chip_name    *chip;
36         const sensors_feature_data *data;
37         struct featurelist         *next;
38 } featurelist_t;
39
40 featurelist_t *first_feature = NULL;
41
42 static char *filename_format = "sensors-%s.rrd";
43
44 static char *ds_def[] =
45 {
46         "DS:value:GAUGE:25:U:U",
47         NULL
48 };
49 static int ds_num = 1;
50
51 extern time_t curtime;
52
53 void collectd_sensors_init (void)
54 {
55         FILE *fh;
56         featurelist_t *last_feature = NULL;
57         featurelist_t *new_feature;
58         
59         const sensors_chip_name *chip;
60         int chip_num;
61
62         const sensors_feature_data *data;
63         int data_num0, data_num1;
64         
65         new_feature = first_feature;
66         while (new_feature != NULL)
67         {
68                 last_feature = new_feature->next;
69                 free (new_feature);
70                 new_feature = last_feature;
71         }
72
73 #ifdef assert
74         assert (new_feature == NULL);
75         assert (last_feature == NULL);
76 #endif
77
78         if ((fh = fopen ("/etc/sensors.conf", "r")) == NULL)
79                 return;
80
81         if (sensors_init (fh))
82         {
83                 fclose (fh);
84                 syslog (LOG_ERR, "sensors: Cannot initialize sensors. Data will not be collected.");
85                 return;
86         }
87
88         fclose (fh);
89
90         chip_num = 0;
91         while ((chip = sensors_get_detected_chips (&chip_num)) != NULL)
92         {
93                 data = NULL;
94                 data_num0 = data_num1 = 0;
95
96                 while ((data = sensors_get_all_features (*chip, &data_num0, &data_num1)) != NULL)
97                 {
98                         /* "master features" only */
99                         if (data->mapping != SENSORS_NO_MAPPING)
100                                 continue;
101
102                         /* Only temperature for now.. */
103                         if (strncmp (data->name, "temp", 4)
104                                         && strncmp (data->name, "fan", 3))
105                                 continue;
106
107                         if ((new_feature = (featurelist_t *) malloc (sizeof (featurelist_t))) == NULL)
108                         {
109                                 perror ("malloc");
110                                 continue;
111                         }
112
113                         /*
114                         syslog (LOG_INFO, "sensors: Adding feature: %s/%s", chip->prefix, data->name);
115                         */
116
117                         new_feature->chip = chip;
118                         new_feature->data = data;
119                         new_feature->next = NULL;
120
121                         if (first_feature == NULL)
122                         {
123                                 first_feature = new_feature;
124                                 last_feature  = new_feature;
125                         }
126                         else
127                         {
128                                 last_feature->next = new_feature;
129                                 last_feature = new_feature;
130                         }
131                 }
132         }
133
134         if (first_feature == NULL)
135                 sensors_cleanup ();
136 }
137
138 void sensors_write (char *host, char *inst, char *val)
139 {
140         char file[512];
141         int status;
142
143         status = snprintf (file, 512, filename_format, inst);
144         if (status < 1)
145                 return;
146         else if (status >= 512)
147                 return;
148
149         rrd_update_file (host, file, val, ds_def, ds_num);
150 }
151
152 #define BUFSIZE 512
153 void sensors_submit (const char *feat_name, const char *chip_prefix, double value)
154 {
155         char buf[BUFSIZE];
156         char inst[BUFSIZE];
157
158         if (snprintf (buf, BUFSIZE, "%u:%.3f", (unsigned int) curtime, value) >= BUFSIZE)
159                 return;
160
161         if (snprintf (inst, BUFSIZE, "%s-%s", chip_prefix, feat_name) >= BUFSIZE)
162                 return;
163
164         plugin_submit (MODULE_NAME, inst, buf);
165 }
166 #undef BUFSIZE
167
168 void sensors_read (void)
169 {
170         featurelist_t *feature;
171         double value;
172
173         for (feature = first_feature; feature != NULL; feature = feature->next)
174         {
175                 if (sensors_get_feature (*feature->chip, feature->data->number, &value) < 0)
176                         continue;
177
178                 sensors_submit (feature->data->name, feature->chip->prefix, value);
179         }
180 }
181
182 void module_register (void)
183 {
184         plugin_register (MODULE_NAME, collectd_sensors_init, sensors_read,
185                         sensors_write);
186 }
187
188 #undef MODULE_NAME
189 #endif /* COLLECT_SENSORS */