2 * rd_sensors.c: Read sensors statistics
3 * (C) 1999-2021 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))) {
72 while ((feature = sensors_get_features(chip, &i))) {
73 if (feature->type == SENSORS_FEATURE_FAN) {
75 if (fan_read + 1 > nr_alloc)
77 st_pwr_fan_i = st_pwr_fan + fan_read++;
78 sensors_snprintf_chip_name(st_pwr_fan_i->device, MAX_SENSORS_DEV_LEN, chip);
80 while ((sub = sensors_get_all_subfeatures(chip, feature, &j))) {
81 if ((sub->type == SENSORS_SUBFEATURE_FAN_INPUT) &&
82 (sub->flags & SENSORS_MODE_R)) {
83 if (sensors_get_value(chip, sub->number, &st_pwr_fan_i->rpm)) {
84 st_pwr_fan_i->rpm = 0;
87 else if ((sub->type == SENSORS_SUBFEATURE_FAN_MIN)) {
88 if (sensors_get_value(chip, sub->number, &st_pwr_fan_i->rpm_min)) {
89 st_pwr_fan_i->rpm_min = 0;
100 #endif /* HAVE_SENSORS */
104 ***************************************************************************
105 * Read device temperature statistics.
108 * @st_pwr_temp Structure where stats will be saved.
109 * @nr_alloc Total number of structures allocated. Value is >= 1.
112 * @st_pwr_temp Structure with statistics.
115 * Number of devices read, or -1 if the buffer was too small and needs to be
117 ***************************************************************************
119 __nr_t read_temp(struct stats_pwr_temp *st_pwr_temp, __nr_t nr_alloc)
121 #if (defined(HAVE_SENSORS) && !defined(ARCH32)) || (defined(ARCH32) && defined(HAVE_SENSORS32))
122 __nr_t temp_read = 0;
123 const sensors_chip_name *chip;
124 const sensors_feature *feature;
125 const sensors_subfeature *sub;
126 struct stats_pwr_temp *st_pwr_temp_i;
130 memset(st_pwr_temp, 0, STATS_PWR_TEMP_SIZE);
132 while ((chip = sensors_get_detected_chips(NULL, &chip_nr))) {
134 while ((feature = sensors_get_features(chip, &i))) {
135 if (feature->type == SENSORS_FEATURE_TEMP) {
137 if (temp_read + 1 > nr_alloc)
139 st_pwr_temp_i = st_pwr_temp + temp_read++;
140 sensors_snprintf_chip_name(st_pwr_temp_i->device, MAX_SENSORS_DEV_LEN, chip);
142 while ((sub = sensors_get_all_subfeatures(chip, feature, &j))) {
143 if ((sub->type == SENSORS_SUBFEATURE_TEMP_INPUT) &&
144 (sub->flags & SENSORS_MODE_R)) {
145 if (sensors_get_value(chip, sub->number, &st_pwr_temp_i->temp)) {
146 st_pwr_temp_i->temp = 0;
149 else if ((sub->type == SENSORS_SUBFEATURE_TEMP_MIN)) {
150 if (sensors_get_value(chip, sub->number, &st_pwr_temp_i->temp_min)) {
151 st_pwr_temp_i->temp_min = 0;
154 else if ((sub->type == SENSORS_SUBFEATURE_TEMP_MAX)) {
155 if (sensors_get_value(chip, sub->number, &st_pwr_temp_i->temp_max)) {
156 st_pwr_temp_i->temp_max = 0;
167 #endif /* HAVE_SENSORS */
171 ***************************************************************************
172 * Read voltage inputs statistics.
175 * @st_pwr_in Structure where stats will be saved.
176 * @nr_alloc Total number of structures allocated. Value is >= 1.
179 * @st_pwr_in Structure with statistics.
182 * Number of devices read, or -1 if the buffer was too small and needs to be
184 ***************************************************************************
186 __nr_t read_in(struct stats_pwr_in *st_pwr_in, __nr_t nr_alloc)
188 #if (defined(HAVE_SENSORS) && !defined(ARCH32)) || (defined(ARCH32) && defined(HAVE_SENSORS32))
190 const sensors_chip_name *chip;
191 const sensors_feature *feature;
192 const sensors_subfeature *sub;
193 struct stats_pwr_in *st_pwr_in_i;
197 memset(st_pwr_in, 0, STATS_PWR_IN_SIZE);
199 while ((chip = sensors_get_detected_chips(NULL, &chip_nr))) {
201 while ((feature = sensors_get_features(chip, &i))) {
202 if (feature->type == SENSORS_FEATURE_IN) {
204 if (in_read + 1 > nr_alloc)
206 st_pwr_in_i = st_pwr_in + in_read++;
207 sensors_snprintf_chip_name(st_pwr_in_i->device, MAX_SENSORS_DEV_LEN, chip);
209 while ((sub = sensors_get_all_subfeatures(chip, feature, &j))) {
210 if ((sub->type == SENSORS_SUBFEATURE_IN_INPUT) &&
211 (sub->flags & SENSORS_MODE_R)) {
212 if (sensors_get_value(chip, sub->number, &st_pwr_in_i->in)) {
216 else if ((sub->type == SENSORS_SUBFEATURE_IN_MIN)) {
217 if (sensors_get_value(chip, sub->number, &st_pwr_in_i->in_min)) {
218 st_pwr_in_i->in_min = 0;
221 else if ((sub->type == SENSORS_SUBFEATURE_IN_MAX)) {
222 if (sensors_get_value(chip, sub->number, &st_pwr_in_i->in_max)) {
223 st_pwr_in_i->in_max = 0;
234 #endif /* HAVE_SENSORS */
237 #if (defined(HAVE_SENSORS) && !defined(ARCH32)) || (defined(ARCH32) && defined(HAVE_SENSORS32))
239 ***************************************************************************
240 * Count the number of sensors of given type on the machine.
243 * @type Type of sensors.
247 ***************************************************************************
249 __nr_t get_sensors_nr(sensors_feature_type type) {
251 const sensors_chip_name *chip;
252 const sensors_feature *feature;
256 while ((chip = sensors_get_detected_chips(NULL, &chip_nr))) {
258 while ((feature = sensors_get_features(chip, &i))) {
259 if (feature->type == type) {
267 #endif /* HAVE_SENSORS */
270 ***************************************************************************
271 * Count the number of fans on the machine.
275 ***************************************************************************
277 __nr_t get_fan_nr(void)
279 #if (defined(HAVE_SENSORS) && !defined(ARCH32)) || (defined(ARCH32) && defined(HAVE_SENSORS32))
280 return get_sensors_nr(SENSORS_FEATURE_FAN);
283 #endif /* HAVE_SENSORS */
287 ***************************************************************************
288 * Count the number of temperature sensors on the machine.
291 * Number of temperature sensors.
292 ***************************************************************************
294 __nr_t get_temp_nr(void)
296 #if (defined(HAVE_SENSORS) && !defined(ARCH32)) || (defined(ARCH32) && defined(HAVE_SENSORS32))
297 return get_sensors_nr(SENSORS_FEATURE_TEMP);
300 #endif /* HAVE_SENSORS */
304 ***************************************************************************
305 * Count the number of voltage inputs on the machine.
308 * Number of voltage inputs.
309 ***************************************************************************
311 __nr_t get_in_nr(void)
313 #if (defined(HAVE_SENSORS) && !defined(ARCH32)) || (defined(ARCH32) && defined(HAVE_SENSORS32))
314 return get_sensors_nr(SENSORS_FEATURE_IN);
317 #endif /* HAVE_SENSORS */