2 * rd_sensors.c: Read sensors statistics
3 * (C) 1999-2022 by Sebastien GODARD (sysstat <at> orange.fr)
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. *
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 *
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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA *
19 ***************************************************************************
27 #include "rd_sensors.h"
32 #define _(string) gettext(string)
34 #define _(string) (string)
37 #if (defined(HAVE_SENSORS) && !defined(ARCH32)) || (defined(ARCH32) && defined(HAVE_SENSORS32))
38 #include "sensors/sensors.h"
42 ***************************************************************************
43 * Read fan statistics.
46 * @st_pwr_fan Structure where stats will be saved.
47 * @nr_alloc Total number of structures allocated. Value is >= 1.
50 * @st_pwr_fan Structure with statistics.
53 * Number of fans read, or -1 if the buffer was too small and needs to be
55 ***************************************************************************
57 __nr_t read_fan(struct stats_pwr_fan *st_pwr_fan, __nr_t nr_alloc)
59 #if (defined(HAVE_SENSORS) && !defined(ARCH32)) || (defined(ARCH32) && defined(HAVE_SENSORS32))
61 const sensors_chip_name *chip;
62 const sensors_feature *feature;
63 const sensors_subfeature *sub;
64 struct stats_pwr_fan *st_pwr_fan_i;
68 memset(st_pwr_fan, 0, STATS_PWR_FAN_SIZE);
70 while ((chip = sensors_get_detected_chips(NULL, &chip_nr))) {
73 while ((feature = sensors_get_features(chip, &i))) {
75 if (feature->type == SENSORS_FEATURE_FAN) {
77 if (fan_read + 1 > nr_alloc)
79 st_pwr_fan_i = st_pwr_fan + fan_read++;
80 sensors_snprintf_chip_name(st_pwr_fan_i->device, MAX_SENSORS_DEV_LEN, chip);
82 while ((sub = sensors_get_all_subfeatures(chip, feature, &j))) {
84 if ((sub->type == SENSORS_SUBFEATURE_FAN_INPUT) &&
85 (sub->flags & SENSORS_MODE_R)) {
86 if (sensors_get_value(chip, sub->number, &st_pwr_fan_i->rpm)) {
87 st_pwr_fan_i->rpm = 0;
90 else if ((sub->type == SENSORS_SUBFEATURE_FAN_MIN)) {
91 if (sensors_get_value(chip, sub->number, &st_pwr_fan_i->rpm_min)) {
92 st_pwr_fan_i->rpm_min = 0;
103 #endif /* HAVE_SENSORS */
107 ***************************************************************************
108 * Read device temperature statistics.
111 * @st_pwr_temp Structure where stats will be saved.
112 * @nr_alloc Total number of structures allocated. Value is >= 1.
115 * @st_pwr_temp Structure with statistics.
118 * Number of devices read, or -1 if the buffer was too small and needs to be
120 ***************************************************************************
122 __nr_t read_temp(struct stats_pwr_temp *st_pwr_temp, __nr_t nr_alloc)
124 #if (defined(HAVE_SENSORS) && !defined(ARCH32)) || (defined(ARCH32) && defined(HAVE_SENSORS32))
125 __nr_t temp_read = 0;
126 const sensors_chip_name *chip;
127 const sensors_feature *feature;
128 const sensors_subfeature *sub;
129 struct stats_pwr_temp *st_pwr_temp_i;
133 memset(st_pwr_temp, 0, STATS_PWR_TEMP_SIZE);
135 while ((chip = sensors_get_detected_chips(NULL, &chip_nr))) {
138 while ((feature = sensors_get_features(chip, &i))) {
140 if (feature->type == SENSORS_FEATURE_TEMP) {
142 if (temp_read + 1 > nr_alloc)
144 st_pwr_temp_i = st_pwr_temp + temp_read++;
145 sensors_snprintf_chip_name(st_pwr_temp_i->device, MAX_SENSORS_DEV_LEN, chip);
147 while ((sub = sensors_get_all_subfeatures(chip, feature, &j))) {
149 if ((sub->type == SENSORS_SUBFEATURE_TEMP_INPUT) &&
150 (sub->flags & SENSORS_MODE_R)) {
151 if (sensors_get_value(chip, sub->number, &st_pwr_temp_i->temp)) {
152 st_pwr_temp_i->temp = 0;
155 else if ((sub->type == SENSORS_SUBFEATURE_TEMP_MIN)) {
156 if (sensors_get_value(chip, sub->number, &st_pwr_temp_i->temp_min)) {
157 st_pwr_temp_i->temp_min = 0;
160 else if ((sub->type == SENSORS_SUBFEATURE_TEMP_MAX)) {
161 if (sensors_get_value(chip, sub->number, &st_pwr_temp_i->temp_max)) {
162 st_pwr_temp_i->temp_max = 0;
173 #endif /* HAVE_SENSORS */
177 ***************************************************************************
178 * Read voltage inputs statistics.
181 * @st_pwr_in Structure where stats will be saved.
182 * @nr_alloc Total number of structures allocated. Value is >= 1.
185 * @st_pwr_in Structure with statistics.
188 * Number of devices read, or -1 if the buffer was too small and needs to be
190 ***************************************************************************
192 __nr_t read_in(struct stats_pwr_in *st_pwr_in, __nr_t nr_alloc)
194 #if (defined(HAVE_SENSORS) && !defined(ARCH32)) || (defined(ARCH32) && defined(HAVE_SENSORS32))
196 const sensors_chip_name *chip;
197 const sensors_feature *feature;
198 const sensors_subfeature *sub;
199 struct stats_pwr_in *st_pwr_in_i;
203 memset(st_pwr_in, 0, STATS_PWR_IN_SIZE);
205 while ((chip = sensors_get_detected_chips(NULL, &chip_nr))) {
208 while ((feature = sensors_get_features(chip, &i))) {
210 if (feature->type == SENSORS_FEATURE_IN) {
212 if (in_read + 1 > nr_alloc)
214 st_pwr_in_i = st_pwr_in + in_read++;
215 sensors_snprintf_chip_name(st_pwr_in_i->device, MAX_SENSORS_DEV_LEN, chip);
217 while ((sub = sensors_get_all_subfeatures(chip, feature, &j))) {
219 if ((sub->type == SENSORS_SUBFEATURE_IN_INPUT) &&
220 (sub->flags & SENSORS_MODE_R)) {
221 if (sensors_get_value(chip, sub->number, &st_pwr_in_i->in)) {
225 else if ((sub->type == SENSORS_SUBFEATURE_IN_MIN)) {
226 if (sensors_get_value(chip, sub->number, &st_pwr_in_i->in_min)) {
227 st_pwr_in_i->in_min = 0;
230 else if ((sub->type == SENSORS_SUBFEATURE_IN_MAX)) {
231 if (sensors_get_value(chip, sub->number, &st_pwr_in_i->in_max)) {
232 st_pwr_in_i->in_max = 0;
243 #endif /* HAVE_SENSORS */
246 #if (defined(HAVE_SENSORS) && !defined(ARCH32)) || (defined(ARCH32) && defined(HAVE_SENSORS32))
248 ***************************************************************************
249 * Count the number of sensors of given type on the machine.
252 * @type Type of sensors.
256 ***************************************************************************
258 __nr_t get_sensors_nr(sensors_feature_type type) {
260 const sensors_chip_name *chip;
261 const sensors_feature *feature;
265 while ((chip = sensors_get_detected_chips(NULL, &chip_nr))) {
268 while ((feature = sensors_get_features(chip, &i))) {
270 if (feature->type == type) {
278 #endif /* HAVE_SENSORS */
281 ***************************************************************************
282 * Count the number of fans on the machine.
286 ***************************************************************************
288 __nr_t get_fan_nr(void)
290 #if (defined(HAVE_SENSORS) && !defined(ARCH32)) || (defined(ARCH32) && defined(HAVE_SENSORS32))
291 return get_sensors_nr(SENSORS_FEATURE_FAN);
294 #endif /* HAVE_SENSORS */
298 ***************************************************************************
299 * Count the number of temperature sensors on the machine.
302 * Number of temperature sensors.
303 ***************************************************************************
305 __nr_t get_temp_nr(void)
307 #if (defined(HAVE_SENSORS) && !defined(ARCH32)) || (defined(ARCH32) && defined(HAVE_SENSORS32))
308 return get_sensors_nr(SENSORS_FEATURE_TEMP);
311 #endif /* HAVE_SENSORS */
315 ***************************************************************************
316 * Count the number of voltage inputs on the machine.
319 * Number of voltage inputs.
320 ***************************************************************************
322 __nr_t get_in_nr(void)
324 #if (defined(HAVE_SENSORS) && !defined(ARCH32)) || (defined(ARCH32) && defined(HAVE_SENSORS32))
325 return get_sensors_nr(SENSORS_FEATURE_IN);
328 #endif /* HAVE_SENSORS */