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