root/lm-sensors/branches/lm-sensors-3.0.0/prog/sensord/sense.c @ 4834

Revision 4834, 6.0 KB (checked in by khali, 7 years ago)

Define a separate structure to represent main features. This allows
for faster main features lookup. One side effect of this change is
that subfeatures can no longer have labels nor be ignored. I do not
think that this is a problem in practice, and actually this makes a
lot of sense.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/*
2 * sensord
3 *
4 * A daemon that periodically logs sensor information to syslog.
5 *
6 * Copyright (c) 1999-2002 Merlin Hughes <merlin@merlin.org>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <syslog.h>
27
28#include "sensord.h"
29#include "lib/error.h"
30
31#define DO_READ 0
32#define DO_SCAN 1
33#define DO_SET 2
34#define DO_RRD 3
35
36static const char *
37chipName
38(const sensors_chip_name *chip) {
39  static char buffer[256];
40  if (sensors_snprintf_chip_name(buffer, 256, chip) < 0)
41    return NULL;
42  return buffer;
43}
44
45static int
46idChip
47(const sensors_chip_name *chip) {
48  const char *adapter;
49
50  sensorLog (LOG_INFO, "Chip: %s", chipName (chip));
51  adapter = sensors_get_adapter_name (&chip->bus);
52  if (adapter)
53    sensorLog (LOG_INFO, "Adapter: %s", adapter);
54 
55  return 0;
56}
57
58static int
59doKnownChip
60(const sensors_chip_name *chip, const ChipDescriptor *descriptor, int action) {
61  const FeatureDescriptor *features = descriptor->features;
62  int index0, subindex;
63  int ret = 0;
64  double tmp;
65
66  if (action == DO_READ)
67    ret = idChip (chip);
68  for (index0 = 0; (ret == 0) && features[index0].format; ++ index0) {
69    const FeatureDescriptor *feature = features + index0;
70    int alarm, beep;
71    char *label = NULL;
72
73    if (!(label = sensors_get_label (chip, feature->feature))) {
74      sensorLog (LOG_ERR, "Error getting sensor label: %s/%s", chip->prefix, feature->feature->name);
75      ret = 22;
76    } else {
77      double values[MAX_DATA];
78
79      alarm = 0;
80      if (!ret && feature->alarmNumber != -1) {
81        if ((ret = sensors_get_value (chip, feature->alarmNumber, &tmp))) {
82          sensorLog (LOG_ERR, "Error getting sensor data: %s/#%d: %s", chip->prefix, feature->alarmNumber, sensors_strerror (ret));
83          ret = 20;
84        } else {
85          alarm = (int) (tmp + 0.5);
86        }
87      }
88      if ((action == DO_SCAN) && !alarm)
89        continue;
90
91      beep = 0;
92      if (!ret && feature->beepNumber != -1) {
93        if ((ret = sensors_get_value (chip, feature->beepNumber, &tmp))) {
94          sensorLog (LOG_ERR, "Error getting sensor data: %s/#%d: %s", chip->prefix, feature->beepNumber, sensors_strerror (ret));
95          ret = 21;
96        } else {
97          beep = (int) (tmp + 0.5);
98        }
99      }
100
101      for (subindex = 0; !ret && (feature->dataNumbers[subindex] >= 0); ++ subindex) {
102        if ((ret = sensors_get_value (chip, feature->dataNumbers[subindex], values + subindex))) {
103          sensorLog (LOG_ERR, "Error getting sensor data: %s/#%d: %s", chip->prefix, feature->dataNumbers[subindex], sensors_strerror (ret));
104          ret = 23;
105        }
106      }
107      if (ret == 0) {
108        if (action == DO_RRD) { // arse = "N:"
109          if (feature->rrd) {
110            const char *rrded = feature->rrd (values);
111            strcat (strcat (rrdBuff, ":"), rrded ? rrded : "U");
112          }
113        } else {
114          const char *formatted = feature->format (values, alarm, beep);
115          if (formatted) {
116            if (action == DO_READ) {
117              sensorLog (LOG_INFO, "  %s: %s", label, formatted);
118            } else {
119              sensorLog (LOG_ALERT, "Sensor alarm: Chip %s: %s: %s", chipName (chip), label, formatted);
120            }
121          }
122        }
123      }
124    }
125    if (label)
126      free (label);
127  }
128  return ret;
129}
130
131static int
132setChip
133(const sensors_chip_name *chip) {
134  int ret = 0;
135  if ((ret = idChip (chip))) {
136    sensorLog (LOG_ERR, "Error identifying chip: %s", chip->prefix);
137  } else if ((ret = sensors_do_chip_sets (chip))) {
138    sensorLog (LOG_ERR, "Error performing chip sets: %s: %s", chip->prefix, sensors_strerror (ret));
139    ret = 50;
140  } else {
141    sensorLog (LOG_INFO, "Set.");
142  }
143  return ret;
144}
145
146static int
147doChip
148(const sensors_chip_name *chip, int action) {
149  int ret = 0;
150  if (action == DO_SET) {
151    ret = setChip (chip);
152  } else {
153    int index0, chipindex = -1;
154    for (index0 = 0; knownChips[index0].features; ++ index0)
155      /* Trick: we compare addresses here. We know it works because both
156         pointers were returned by sensors_get_detected_chips(), so they
157         refer to libsensors internal structures, which do not move. */
158      if (knownChips[index0].name == chip) {
159        chipindex = index0;
160        break;
161      }
162    if (chipindex >= 0)
163      ret = doKnownChip (chip, &knownChips[chipindex], action);
164  }
165  return ret;
166}
167
168static int
169doChips
170(int action) {
171  const sensors_chip_name *chip;
172  int i = 0, j, ret = 0;
173
174  for (j = 0; (ret == 0) && (j < numChipNames); ++ j) {
175    while ((ret == 0) && ((chip = sensors_get_detected_chips (&chipNames[j], &i)) != NULL)) {
176      ret = doChip (chip, action);
177    }
178  }
179
180  return ret;
181}
182
183int
184readChips
185(void) {
186  int ret = 0;
187
188  sensorLog (LOG_DEBUG, "sensor read started");
189  ret = doChips (DO_READ);
190  sensorLog (LOG_DEBUG, "sensor read finished");
191
192  return ret;
193}
194
195int
196scanChips
197(void) {
198  int ret = 0;
199
200  sensorLog (LOG_DEBUG, "sensor sweep started"); /* only logged in debug mode */
201  ret = doChips (DO_SCAN);
202  sensorLog (LOG_DEBUG, "sensor sweep finished");
203
204  return ret;
205}
206
207int
208setChips
209(void) {
210  int ret = 0;
211
212  sensorLog (LOG_DEBUG, "sensor set started");
213  ret = doChips (DO_SET);
214  sensorLog (LOG_DEBUG, "sensor set finished");
215
216  return ret;
217}
218
219/* TODO: loadavg entry */
220
221int
222rrdChips
223(void) {
224  int ret = 0;
225
226  strcpy (rrdBuff, "N");
227
228  sensorLog (LOG_DEBUG, "sensor rrd started"); 
229  ret = doChips (DO_RRD);
230  sensorLog (LOG_DEBUG, "sensor rrd finished");
231
232  return ret;
233}
Note: See TracBrowser for help on using the browser.