| 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., 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 "sensord.h" |
|---|
| 30 | #include "lib/error.h" |
|---|
| 31 | |
|---|
| 32 | #define DO_READ 0 |
|---|
| 33 | #define DO_SCAN 1 |
|---|
| 34 | #define DO_SET 2 |
|---|
| 35 | #define DO_RRD 3 |
|---|
| 36 | |
|---|
| 37 | static const char * |
|---|
| 38 | chipName |
|---|
| 39 | (const sensors_chip_name *chip) { |
|---|
| 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 |
|---|
| 47 | idChip |
|---|
| 48 | (const sensors_chip_name *chip) { |
|---|
| 49 | const char *adapter; |
|---|
| 50 | |
|---|
| 51 | sensorLog (LOG_INFO, "Chip: %s", chipName (chip)); |
|---|
| 52 | adapter = sensors_get_adapter_name (&chip->bus); |
|---|
| 53 | if (adapter) |
|---|
| 54 | sensorLog (LOG_INFO, "Adapter: %s", adapter); |
|---|
| 55 | |
|---|
| 56 | return 0; |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | static int |
|---|
| 60 | doKnownChip |
|---|
| 61 | (const sensors_chip_name *chip, const ChipDescriptor *descriptor, int action) { |
|---|
| 62 | const FeatureDescriptor *features = descriptor->features; |
|---|
| 63 | int index0, subindex; |
|---|
| 64 | int ret = 0; |
|---|
| 65 | double tmp; |
|---|
| 66 | |
|---|
| 67 | if (action == DO_READ) |
|---|
| 68 | ret = idChip (chip); |
|---|
| 69 | for (index0 = 0; (ret == 0) && features[index0].format; ++ index0) { |
|---|
| 70 | const FeatureDescriptor *feature = features + index0; |
|---|
| 71 | int alarm, beep; |
|---|
| 72 | char *label = NULL; |
|---|
| 73 | |
|---|
| 74 | if (!(label = sensors_get_label (chip, feature->feature))) { |
|---|
| 75 | sensorLog (LOG_ERR, "Error getting sensor label: %s/%s", chip->prefix, feature->feature->name); |
|---|
| 76 | ret = 22; |
|---|
| 77 | } else { |
|---|
| 78 | double values[MAX_DATA]; |
|---|
| 79 | |
|---|
| 80 | alarm = 0; |
|---|
| 81 | if (!ret && feature->alarmNumber != -1) { |
|---|
| 82 | if ((ret = sensors_get_value (chip, feature->alarmNumber, &tmp))) { |
|---|
| 83 | sensorLog (LOG_ERR, "Error getting sensor data: %s/#%d: %s", chip->prefix, feature->alarmNumber, sensors_strerror (ret)); |
|---|
| 84 | ret = 20; |
|---|
| 85 | } else { |
|---|
| 86 | alarm = (int) (tmp + 0.5); |
|---|
| 87 | } |
|---|
| 88 | } |
|---|
| 89 | if ((action == DO_SCAN) && !alarm) |
|---|
| 90 | continue; |
|---|
| 91 | |
|---|
| 92 | beep = 0; |
|---|
| 93 | if (!ret && feature->beepNumber != -1) { |
|---|
| 94 | if ((ret = sensors_get_value (chip, feature->beepNumber, &tmp))) { |
|---|
| 95 | sensorLog (LOG_ERR, "Error getting sensor data: %s/#%d: %s", chip->prefix, feature->beepNumber, sensors_strerror (ret)); |
|---|
| 96 | ret = 21; |
|---|
| 97 | } else { |
|---|
| 98 | beep = (int) (tmp + 0.5); |
|---|
| 99 | } |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | for (subindex = 0; !ret && (feature->dataNumbers[subindex] >= 0); ++ subindex) { |
|---|
| 103 | if ((ret = sensors_get_value (chip, feature->dataNumbers[subindex], values + subindex))) { |
|---|
| 104 | sensorLog (LOG_ERR, "Error getting sensor data: %s/#%d: %s", chip->prefix, feature->dataNumbers[subindex], sensors_strerror (ret)); |
|---|
| 105 | ret = 23; |
|---|
| 106 | } |
|---|
| 107 | } |
|---|
| 108 | if (ret == 0) { |
|---|
| 109 | if (action == DO_RRD) { // arse = "N:" |
|---|
| 110 | if (feature->rrd) { |
|---|
| 111 | const char *rrded = feature->rrd (values); |
|---|
| 112 | strcat (strcat (rrdBuff, ":"), rrded ? rrded : "U"); |
|---|
| 113 | } |
|---|
| 114 | } else { |
|---|
| 115 | const char *formatted = feature->format (values, alarm, beep); |
|---|
| 116 | if (formatted) { |
|---|
| 117 | if (action == DO_READ) { |
|---|
| 118 | sensorLog (LOG_INFO, " %s: %s", label, formatted); |
|---|
| 119 | } else { |
|---|
| 120 | sensorLog (LOG_ALERT, "Sensor alarm: Chip %s: %s: %s", chipName (chip), label, formatted); |
|---|
| 121 | } |
|---|
| 122 | } |
|---|
| 123 | } |
|---|
| 124 | } |
|---|
| 125 | } |
|---|
| 126 | if (label) |
|---|
| 127 | free (label); |
|---|
| 128 | } |
|---|
| 129 | return ret; |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | static int |
|---|
| 133 | setChip |
|---|
| 134 | (const sensors_chip_name *chip) { |
|---|
| 135 | int ret = 0; |
|---|
| 136 | if ((ret = idChip (chip))) { |
|---|
| 137 | sensorLog (LOG_ERR, "Error identifying chip: %s", chip->prefix); |
|---|
| 138 | } else if ((ret = sensors_do_chip_sets (chip))) { |
|---|
| 139 | sensorLog (LOG_ERR, "Error performing chip sets: %s: %s", chip->prefix, sensors_strerror (ret)); |
|---|
| 140 | ret = 50; |
|---|
| 141 | } else { |
|---|
| 142 | sensorLog (LOG_INFO, "Set."); |
|---|
| 143 | } |
|---|
| 144 | return ret; |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | static int |
|---|
| 148 | doChip |
|---|
| 149 | (const sensors_chip_name *chip, int action) { |
|---|
| 150 | int ret = 0; |
|---|
| 151 | if (action == DO_SET) { |
|---|
| 152 | ret = setChip (chip); |
|---|
| 153 | } else { |
|---|
| 154 | int index0, chipindex = -1; |
|---|
| 155 | for (index0 = 0; knownChips[index0].features; ++ index0) |
|---|
| 156 | /* Trick: we compare addresses here. We know it works because both |
|---|
| 157 | pointers were returned by sensors_get_detected_chips(), so they |
|---|
| 158 | refer to libsensors internal structures, which do not move. */ |
|---|
| 159 | if (knownChips[index0].name == chip) { |
|---|
| 160 | chipindex = index0; |
|---|
| 161 | break; |
|---|
| 162 | } |
|---|
| 163 | if (chipindex >= 0) |
|---|
| 164 | ret = doKnownChip (chip, &knownChips[chipindex], action); |
|---|
| 165 | } |
|---|
| 166 | return ret; |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | static int |
|---|
| 170 | doChips |
|---|
| 171 | (int action) { |
|---|
| 172 | const sensors_chip_name *chip; |
|---|
| 173 | int i = 0, j, ret = 0; |
|---|
| 174 | |
|---|
| 175 | for (j = 0; (ret == 0) && (j < numChipNames); ++ j) { |
|---|
| 176 | while ((ret == 0) && ((chip = sensors_get_detected_chips (&chipNames[j], &i)) != NULL)) { |
|---|
| 177 | ret = doChip (chip, action); |
|---|
| 178 | } |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | return ret; |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | int |
|---|
| 185 | readChips |
|---|
| 186 | (void) { |
|---|
| 187 | int ret = 0; |
|---|
| 188 | |
|---|
| 189 | sensorLog (LOG_DEBUG, "sensor read started"); |
|---|
| 190 | ret = doChips (DO_READ); |
|---|
| 191 | sensorLog (LOG_DEBUG, "sensor read finished"); |
|---|
| 192 | |
|---|
| 193 | return ret; |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | int |
|---|
| 197 | scanChips |
|---|
| 198 | (void) { |
|---|
| 199 | int ret = 0; |
|---|
| 200 | |
|---|
| 201 | sensorLog (LOG_DEBUG, "sensor sweep started"); /* only logged in debug mode */ |
|---|
| 202 | ret = doChips (DO_SCAN); |
|---|
| 203 | sensorLog (LOG_DEBUG, "sensor sweep finished"); |
|---|
| 204 | |
|---|
| 205 | return ret; |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | int |
|---|
| 209 | setChips |
|---|
| 210 | (void) { |
|---|
| 211 | int ret = 0; |
|---|
| 212 | |
|---|
| 213 | sensorLog (LOG_DEBUG, "sensor set started"); |
|---|
| 214 | ret = doChips (DO_SET); |
|---|
| 215 | sensorLog (LOG_DEBUG, "sensor set finished"); |
|---|
| 216 | |
|---|
| 217 | return ret; |
|---|
| 218 | } |
|---|
| 219 | |
|---|
| 220 | /* TODO: loadavg entry */ |
|---|
| 221 | |
|---|
| 222 | int |
|---|
| 223 | rrdChips |
|---|
| 224 | (void) { |
|---|
| 225 | int ret = 0; |
|---|
| 226 | |
|---|
| 227 | strcpy (rrdBuff, "N"); |
|---|
| 228 | |
|---|
| 229 | sensorLog (LOG_DEBUG, "sensor rrd started"); |
|---|
| 230 | ret = doChips (DO_RRD); |
|---|
| 231 | sensorLog (LOG_DEBUG, "sensor rrd finished"); |
|---|
| 232 | |
|---|
| 233 | return ret; |
|---|
| 234 | } |
|---|