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

Revision 4825, 6.2 KB (checked in by khali, 7 years ago)

Restore support for alarms.

  • 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
36int
37getRawLabel
38(const sensors_chip_name *name, int feature, const char **label) {
39  const sensors_feature_data *rawFeature;
40  int nr = 0, err = 0;
41  do {
42    rawFeature = sensors_get_all_features (name, &nr);
43  } while (rawFeature && (rawFeature->number != feature));
44  /* TODO: Ensure labels match RRD construct and are not repeated! */
45  if (!rawFeature) {
46    err = -1;
47  } else {
48    *label = rawFeature->name;
49  }
50  return err;
51}
52
53static const char *
54chipName
55(const sensors_chip_name *chip) {
56  static char buffer[256];
57  if (sensors_snprintf_chip_name(buffer, 256, chip) < 0)
58    return NULL;
59  return buffer;
60}
61
62static int
63idChip
64(const sensors_chip_name *chip) {
65  const char *adapter;
66
67  sensorLog (LOG_INFO, "Chip: %s", chipName (chip));
68  adapter = sensors_get_adapter_name (&chip->bus);
69  if (adapter)
70    sensorLog (LOG_INFO, "Adapter: %s", adapter);
71 
72  return 0;
73}
74
75static int
76doKnownChip
77(const sensors_chip_name *chip, const ChipDescriptor *descriptor, int action) {
78  const FeatureDescriptor *features = descriptor->features;
79  int index0, subindex;
80  int ret = 0;
81  double tmp;
82
83  if (action == DO_READ)
84    ret = idChip (chip);
85  for (index0 = 0; (ret == 0) && features[index0].format; ++ index0) {
86    const FeatureDescriptor *feature = features + index0;
87    int labelNumber = feature->dataNumbers[0];
88    int alarm, beep;
89    char *label = NULL;
90
91    if (!(label = sensors_get_label (chip, labelNumber))) {
92      sensorLog (LOG_ERR, "Error getting sensor label: %s/#%d", chip->prefix, labelNumber);
93      ret = 22;
94    } else {
95      double values[MAX_DATA];
96
97      alarm = 0;
98      if (!ret && feature->alarmNumber != -1) {
99        if ((ret = sensors_get_value (chip, feature->alarmNumber, &tmp))) {
100          sensorLog (LOG_ERR, "Error getting sensor data: %s/#%d: %s", chip->prefix, feature->alarmNumber, sensors_strerror (ret));
101          ret = 20;
102        } else {
103          alarm = (int) (tmp + 0.5);
104        }
105      }
106      if ((action == DO_SCAN) && !alarm)
107        continue;
108
109      beep = 0;
110      if (!ret && feature->beepNumber != -1) {
111        if ((ret = sensors_get_value (chip, feature->beepNumber, &tmp))) {
112          sensorLog (LOG_ERR, "Error getting sensor data: %s/#%d: %s", chip->prefix, feature->beepNumber, sensors_strerror (ret));
113          ret = 21;
114        } else {
115          beep = (int) (tmp + 0.5);
116        }
117      }
118
119      for (subindex = 0; !ret && (feature->dataNumbers[subindex] >= 0); ++ subindex) {
120        if ((ret = sensors_get_value (chip, feature->dataNumbers[subindex], values + subindex))) {
121          sensorLog (LOG_ERR, "Error getting sensor data: %s/#%d: %s", chip->prefix, feature->dataNumbers[subindex], sensors_strerror (ret));
122          ret = 23;
123        }
124      }
125      if (ret == 0) {
126        if (action == DO_RRD) { // arse = "N:"
127          if (feature->rrd) {
128            const char *rrded = feature->rrd (values);
129            strcat (strcat (rrdBuff, ":"), rrded ? rrded : "U");
130          }
131        } else {
132          const char *formatted = feature->format (values, alarm, beep);
133          if (formatted) {
134            if (action == DO_READ) {
135              sensorLog (LOG_INFO, "  %s: %s", label, formatted);
136            } else {
137              sensorLog (LOG_ALERT, "Sensor alarm: Chip %s: %s: %s", chipName (chip), label, formatted);
138            }
139          }
140        }
141      }
142    }
143    if (label)
144      free (label);
145  }
146  return ret;
147}
148
149static int
150setChip
151(const sensors_chip_name *chip) {
152  int ret = 0;
153  if ((ret = idChip (chip))) {
154    sensorLog (LOG_ERR, "Error identifying chip: %s", chip->prefix);
155  } else if ((ret = sensors_do_chip_sets (chip))) {
156    sensorLog (LOG_ERR, "Error performing chip sets: %s: %s", chip->prefix, sensors_strerror (ret));
157    ret = 50;
158  } else {
159    sensorLog (LOG_INFO, "Set.");
160  }
161  return ret;
162}
163
164static int
165doChip
166(const sensors_chip_name *chip, int action) {
167  int ret = 0;
168  if (action == DO_SET) {
169    ret = setChip (chip);
170  } else {
171    ChipDescriptor *descriptor;
172    descriptor = generateChipDescriptor (chip);
173    if (descriptor) {
174      ret = doKnownChip (chip, descriptor, action);
175      free (descriptor->features);
176      free (descriptor);
177    }
178  }
179  return ret;
180}
181
182static int
183doChips
184(int action) {
185  const sensors_chip_name *chip;
186  int i = 0, j, ret = 0;
187
188  for (j = 0; (ret == 0) && (j < numChipNames); ++ j) {
189    while ((ret == 0) && ((chip = sensors_get_detected_chips (&chipNames[j], &i)) != NULL)) {
190      ret = doChip (chip, action);
191    }
192  }
193
194  return ret;
195}
196
197int
198readChips
199(void) {
200  int ret = 0;
201
202  sensorLog (LOG_DEBUG, "sensor read started");
203  ret = doChips (DO_READ);
204  sensorLog (LOG_DEBUG, "sensor read finished");
205
206  return ret;
207}
208
209int
210scanChips
211(void) {
212  int ret = 0;
213
214  sensorLog (LOG_DEBUG, "sensor sweep started"); /* only logged in debug mode */
215  ret = doChips (DO_SCAN);
216  sensorLog (LOG_DEBUG, "sensor sweep finished");
217
218  return ret;
219}
220
221int
222setChips
223(void) {
224  int ret = 0;
225
226  sensorLog (LOG_DEBUG, "sensor set started");
227  ret = doChips (DO_SET);
228  sensorLog (LOG_DEBUG, "sensor set finished");
229
230  return ret;
231}
232
233/* TODO: loadavg entry */
234
235int
236rrdChips
237(void) {
238  int ret = 0;
239
240  strcpy (rrdBuff, "N");
241
242  sensorLog (LOG_DEBUG, "sensor rrd started"); 
243  ret = doChips (DO_RRD);
244  sensorLog (LOG_DEBUG, "sensor rrd finished");
245
246  return ret;
247}
Note: See TracBrowser for help on using the browser.