1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
|
/*
* sensord
*
* A daemon that periodically logs sensor information to syslog.
*
* Copyright (c) 1999-2002 Merlin Hughes <merlin@merlin.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include "args.h"
#include "sensord.h"
#include "lib/error.h"
#define DO_READ 0
#define DO_SCAN 1
#define DO_SET 2
#define DO_RRD 3
static const char *chipName(const sensors_chip_name *chip)
{
static char buffer[256];
if (sensors_snprintf_chip_name(buffer, 256, chip) < 0)
return NULL;
return buffer;
}
static int idChip(const sensors_chip_name *chip)
{
const char *name, *adapter;
name = chipName(chip);
if (!name) {
sensorLog(LOG_ERR, "Error getting chip name");
return -1;
}
sensorLog(LOG_INFO, "Chip: %s", name);
adapter = sensors_get_adapter_name(&chip->bus);
if (!adapter)
sensorLog(LOG_INFO, "Error getting adapter name");
else
sensorLog(LOG_INFO, "Adapter: %s", adapter);
return 0;
}
static int get_flag(const sensors_chip_name *chip, int num)
{
double val;
int ret;
if (num == -1)
return 0;
ret = sensors_get_value(chip, num, &val);
if (ret) {
sensorLog(LOG_ERR, "Error getting sensor data: %s/#%d: %s",
chip->prefix, num, sensors_strerror(ret));
return -1;
}
return (int) (val + 0.5);
}
static int do_features(const sensors_chip_name *chip,
const FeatureDescriptor *feature, int action)
{
char *label;
const char *formatted;
int i, alrm, beep, ret;
double val[MAX_DATA];
/* If only scanning, take a quick exit if alarm is off */
alrm = get_flag(chip, feature->alarmNumber);
if (alrm == -1)
return -1;
if (action == DO_SCAN && !alrm)
return 0;
for (i = 0; feature->dataNumbers[i] >= 0; i++) {
ret = sensors_get_value(chip, feature->dataNumbers[i],
val + i);
if (ret) {
sensorLog(LOG_ERR,
"Error getting sensor data: %s/#%d: %s",
chip->prefix, feature->dataNumbers[i],
sensors_strerror(ret));
return -1;
}
}
/* For RRD, we don't need anything else */
if (action == DO_RRD) {
if (feature->rrd) {
const char *rrded = feature->rrd(val);
sprintf(rrdBuff + strlen(rrdBuff), ":%s",
rrded ? rrded : "U");
}
return 0;
}
/* For scanning and logging, we need extra information */
beep = get_flag(chip, feature->beepNumber);
if (beep == -1)
return -1;
formatted = feature->format(val, alrm, beep);
if (!formatted) {
sensorLog(LOG_ERR, "Error formatting sensor data");
return -1;
}
/* FIXME: It would be more efficient to store the label at
* initialization time.
*/
label = sensors_get_label(chip, feature->feature);
if (!label) {
sensorLog(LOG_ERR, "Error getting sensor label: %s/%s",
chip->prefix, feature->feature->name);
return -1;
}
if (action == DO_READ)
sensorLog(LOG_INFO, " %s: %s", label, formatted);
else
sensorLog(LOG_ALERT, "Sensor alarm: Chip %s: %s: %s",
chipName(chip), label, formatted);
free(label);
return 0;
}
static int doKnownChip(const sensors_chip_name *chip,
const ChipDescriptor *descriptor, int action)
{
const FeatureDescriptor *features = descriptor->features;
int i, ret = 0;
if (action == DO_READ) {
ret = idChip(chip);
if (ret)
return ret;
}
for (i = 0; features[i].format; i++) {
ret = do_features(chip, features + i, action);
if (ret == -1)
break;
}
return ret;
}
static int setChip(const sensors_chip_name *chip)
{
int ret = 0;
if ((ret = idChip(chip))) {
sensorLog(LOG_ERR, "Error identifying chip: %s",
chip->prefix);
} else if ((ret = sensors_do_chip_sets(chip))) {
sensorLog(LOG_ERR, "Error performing chip sets: %s: %s",
chip->prefix, sensors_strerror(ret));
ret = 50;
} else {
sensorLog(LOG_INFO, "Set.");
}
return ret;
}
static int doChip(const sensors_chip_name *chip, int action)
{
int ret = 0;
if (action == DO_SET) {
ret = setChip(chip);
} else {
int index0, chipindex = -1;
for (index0 = 0; knownChips[index0].features; ++index0) {
/*
* Trick: we compare addresses here. We know it works
* because both pointers were returned by
* sensors_get_detected_chips(), so they refer to
* libsensors internal structures, which do not move.
*/
if (knownChips[index0].name == chip) {
chipindex = index0;
break;
}
}
if (chipindex >= 0) {
ret = doKnownChip(chip, &knownChips[chipindex],
action);
}
}
return ret;
}
static int doChips(int action)
{
const sensors_chip_name *chip, *chip_arg;
int i, j, ret = 0;
for (j = 0; j < sensord_args.numChipNames; j++) {
chip_arg = &sensord_args.chipNames[j];
i = 0;
while ((chip = sensors_get_detected_chips(chip_arg, &i))) {
ret = doChip(chip, action);
if (ret)
return ret;
}
}
return ret;
}
int readChips(void)
{
int ret = 0;
sensorLog(LOG_DEBUG, "sensor read started");
ret = doChips(DO_READ);
sensorLog(LOG_DEBUG, "sensor read finished");
return ret;
}
int scanChips(void)
{
int ret = 0;
sensorLog(LOG_DEBUG, "sensor sweep started");
ret = doChips(DO_SCAN);
sensorLog(LOG_DEBUG, "sensor sweep finished");
return ret;
}
int setChips(void)
{
int ret = 0;
sensorLog(LOG_DEBUG, "sensor set started");
ret = doChips(DO_SET);
sensorLog(LOG_DEBUG, "sensor set finished");
return ret;
}
/* TODO: loadavg entry */
int rrdChips(void)
{
int ret = 0;
strcpy(rrdBuff, "N");
sensorLog(LOG_DEBUG, "sensor rrd started");
ret = doChips(DO_RRD);
sensorLog(LOG_DEBUG, "sensor rrd finished");
return ret;
}
|