]> granicus.if.org Git - sysstat/blob - rd_sensors.c
Fixes from static analysis
[sysstat] / rd_sensors.c
1 /*
2  * rd_sensors.c: Read sensors statistics
3  * (C) 1999-2014 by Sebastien GODARD (sysstat <at> orange.fr)
4  *
5  ***************************************************************************
6  * This program is free software; you can redistribute it and/or modify it *
7  * under the terms of the GNU General Public License as published  by  the *
8  * Free Software Foundation; either version 2 of the License, or (at  your *
9  * option) any later version.                                              *
10  *                                                                         *
11  * This program is distributed in the hope that it  will  be  useful,  but *
12  * WITHOUT ANY WARRANTY; without the implied warranty  of  MERCHANTABILITY *
13  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
14  * for more details.                                                       *
15  *                                                                         *
16  * You should have received a copy of the GNU General Public License along *
17  * with this program; if not, write to the Free Software Foundation, Inc., *
18  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA                   *
19  ***************************************************************************
20  */
21
22 #include <stdio.h>
23 #include <string.h>
24
25 #include "common.h"
26 #include "rd_sensors.h"
27
28 #ifdef USE_NLS
29 #include <locale.h>
30 #include <libintl.h>
31 #define _(string) gettext(string)
32 #else
33 #define _(string) (string)
34 #endif
35
36 #ifdef HAVE_SENSORS
37 #include "sensors/sensors.h"
38 #endif
39
40 /*
41  ***************************************************************************
42  * Read fan statistics.
43  *
44  * IN:
45  * @st_pwr_fan  Structure where stats will be saved.
46  * @nbr         Total number of fans.
47  *
48  * OUT:
49  * @st_pwr_fan Structure with statistics.
50  ***************************************************************************
51  */
52 void read_fan(struct stats_pwr_fan *st_pwr_fan, int nbr)
53 {
54 #ifdef HAVE_SENSORS
55         int count = 0;
56         const sensors_chip_name *chip;
57         const sensors_feature *feature;
58         const sensors_subfeature *sub;
59         struct stats_pwr_fan *st_pwr_fan_i;
60         int chip_nr = 0;
61         int i, j;
62
63         memset(st_pwr_fan, 0, STATS_PWR_FAN_SIZE);
64
65         while ((chip = sensors_get_detected_chips(NULL, &chip_nr))) {
66                 i = 0;
67                 while ((feature = sensors_get_features(chip, &i))) {
68                         if ((feature->type == SENSORS_FEATURE_FAN) && (count < nbr)) {
69                                 j = 0;
70                                 st_pwr_fan_i = st_pwr_fan + count;
71                                 sensors_snprintf_chip_name(st_pwr_fan_i->device, MAX_SENSORS_DEV_LEN, chip);
72
73                                 while ((sub = sensors_get_all_subfeatures(chip, feature, &j))) {
74                                         if ((sub->type == SENSORS_SUBFEATURE_FAN_INPUT) &&
75                                             (sub->flags & SENSORS_MODE_R)) {
76                                                 if (sensors_get_value(chip, sub->number, &st_pwr_fan_i->rpm)) {
77                                                         st_pwr_fan_i->rpm = 0;
78                                                 }
79                                         }
80                                         else if ((sub->type == SENSORS_SUBFEATURE_FAN_MIN)) {
81                                                 if (sensors_get_value(chip, sub->number, &st_pwr_fan_i->rpm_min)) {
82                                                         st_pwr_fan_i->rpm_min = 0;
83                                                 }
84                                         }
85                                 }
86                                 count++;
87                         }
88                 }
89         }
90 #endif /* HAVE_SENSORS */
91 }
92
93 /*
94  ***************************************************************************
95  * Read device temperature statistics.
96  *
97  * IN:
98  * @st_pwr_temp Structure where stats will be saved.
99  * @nbr         Total number of fans.
100  *
101  * OUT:
102  * @st_pwr_temp Structure with statistics.
103  ***************************************************************************
104  */
105 void read_temp(struct stats_pwr_temp *st_pwr_temp, int nbr)
106 {
107 #ifdef HAVE_SENSORS
108         int count = 0;
109         const sensors_chip_name *chip;
110         const sensors_feature *feature;
111         const sensors_subfeature *sub;
112         struct stats_pwr_temp *st_pwr_temp_i;
113         int chip_nr = 0;
114         int i, j;
115
116         memset(st_pwr_temp, 0, STATS_PWR_TEMP_SIZE);
117
118         while ((chip = sensors_get_detected_chips(NULL, &chip_nr))) {
119                 i = 0;
120                 while ((feature = sensors_get_features(chip, &i))) {
121                         if ((feature->type == SENSORS_FEATURE_TEMP) && (count < nbr)) {
122                                 j = 0;
123                                 st_pwr_temp_i = st_pwr_temp + count;
124                                 sensors_snprintf_chip_name(st_pwr_temp_i->device, MAX_SENSORS_DEV_LEN, chip);
125
126                                 while ((sub = sensors_get_all_subfeatures(chip, feature, &j))) {
127                                         if ((sub->type == SENSORS_SUBFEATURE_TEMP_INPUT) &&
128                                                 (sub->flags & SENSORS_MODE_R)) {
129                                                 if (sensors_get_value(chip, sub->number, &st_pwr_temp_i->temp)) {
130                                                         st_pwr_temp_i->temp = 0;
131                                                 }
132                                         }
133                                         else if ((sub->type == SENSORS_SUBFEATURE_TEMP_MIN)) {
134                                                 if (sensors_get_value(chip, sub->number, &st_pwr_temp_i->temp_min)) {
135                                                         st_pwr_temp_i->temp_min = 0;
136                                                 }
137                                         }
138                                         else if ((sub->type == SENSORS_SUBFEATURE_TEMP_MAX)) {
139                                                 if (sensors_get_value(chip, sub->number, &st_pwr_temp_i->temp_max)) {
140                                                         st_pwr_temp_i->temp_max = 0;
141                                                 }
142                                         }
143                                 }
144                                 count++;
145                         }
146                 }
147         }
148 #endif /* HAVE_SENSORS */
149 }
150
151 /*
152  ***************************************************************************
153  * Read voltage inputs statistics.
154  *
155  * IN:
156  * @st_pwr_in   Structure where stats will be saved.
157  * @nbr         Total number of voltage inputs.
158  *
159  * OUT:
160  * @st_pwr_in   Structure with statistics.
161  ***************************************************************************
162  */
163 void read_in(struct stats_pwr_in *st_pwr_in, int nbr)
164 {
165 #ifdef HAVE_SENSORS
166         int count = 0;
167         const sensors_chip_name *chip;
168         const sensors_feature *feature;
169         const sensors_subfeature *sub;
170         struct stats_pwr_in *st_pwr_in_i;
171         int chip_nr = 0;
172         int i, j;
173
174         memset(st_pwr_in, 0, STATS_PWR_IN_SIZE);
175
176         while ((chip = sensors_get_detected_chips(NULL, &chip_nr))) {
177                 i = 0;
178                 while ((feature = sensors_get_features(chip, &i))) {
179                         if ((feature->type == SENSORS_FEATURE_IN) && (count < nbr)) {
180                                 j = 0;
181                                 st_pwr_in_i = st_pwr_in + count;
182                                 sensors_snprintf_chip_name(st_pwr_in_i->device, MAX_SENSORS_DEV_LEN, chip);
183
184                                 while ((sub = sensors_get_all_subfeatures(chip, feature, &j))) {
185                                         if ((sub->type == SENSORS_SUBFEATURE_IN_INPUT) &&
186                                                 (sub->flags & SENSORS_MODE_R)) {
187                                                 if (sensors_get_value(chip, sub->number, &st_pwr_in_i->in)) {
188                                                         st_pwr_in_i->in = 0;
189                                                 }
190                                         }
191                                         else if ((sub->type == SENSORS_SUBFEATURE_IN_MIN)) {
192                                                 if (sensors_get_value(chip, sub->number, &st_pwr_in_i->in_min)) {
193                                                         st_pwr_in_i->in_min = 0;
194                                                 }
195                                         }
196                                         else if ((sub->type == SENSORS_SUBFEATURE_IN_MAX)) {
197                                                 if (sensors_get_value(chip, sub->number, &st_pwr_in_i->in_max)) {
198                                                         st_pwr_in_i->in_max = 0;
199                                                 }
200                                         }
201                                 }
202                                 count++;
203                         }
204                 }
205         }
206 #endif /* HAVE_SENSORS */
207 }
208
209 #ifdef HAVE_SENSORS
210 /*
211  ***************************************************************************
212  * Count the number of sensors of given type on the machine.
213  *
214  * IN:
215  * @type        Type of sensors.
216  *
217  * RETURNS:
218  * Number of sensors.
219  ***************************************************************************
220  */
221 int get_sensors_nr(sensors_feature_type type) {
222         int count = 0;
223         const sensors_chip_name *chip;
224         const sensors_feature *feature;
225         int chip_nr = 0;
226         int i;
227
228         while ((chip = sensors_get_detected_chips(NULL, &chip_nr))) {
229                 i = 0;
230                 while ((feature = sensors_get_features(chip, &i))) {
231                         if (feature->type == type) {
232                                 count++;
233                         }
234                 }
235         }
236
237         return count;
238 }
239 #endif /* HAVE_SENSORS */
240
241 /*
242  ***************************************************************************
243  * Count the number of fans on the machine.
244  *
245  * RETURNS:
246  * Number of fans.
247  ***************************************************************************
248  */
249 int get_fan_nr(void)
250 {
251 #ifdef HAVE_SENSORS
252         return get_sensors_nr(SENSORS_FEATURE_FAN);
253 #else
254         return 0;
255 #endif /* HAVE_SENSORS */
256 }
257
258 /*
259  ***************************************************************************
260  * Count the number of temperature sensors on the machine.
261  *
262  * RETURNS:
263  * Number of temperature sensors.
264  ***************************************************************************
265  */
266 int get_temp_nr(void)
267 {
268 #ifdef HAVE_SENSORS
269         return get_sensors_nr(SENSORS_FEATURE_TEMP);
270 #else
271         return 0;
272 #endif /* HAVE_SENSORS */
273
274 }
275
276 /*
277  ***************************************************************************
278  * Count the number of voltage inputs on the machine.
279  *
280  * RETURNS:
281  * Number of voltage inputs.
282  ***************************************************************************
283  */
284 int get_in_nr(void)
285 {
286 #ifdef HAVE_SENSORS
287         return get_sensors_nr(SENSORS_FEATURE_IN);
288 #else
289         return 0;
290 #endif /* HAVE_SENSORS */
291
292 }