| 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 | /* |
|---|
| 24 | * RRD is the Round Robin Database |
|---|
| 25 | * |
|---|
| 26 | * Get this package from: |
|---|
| 27 | * http://people.ee.ethz.ch/~oetiker/webtools/rrdtool/ |
|---|
| 28 | * |
|---|
| 29 | * For compilation you need the development libraries; |
|---|
| 30 | * for execution you need the runtime libraries; for |
|---|
| 31 | * Web-based graph access you need the binary rrdtool. |
|---|
| 32 | */ |
|---|
| 33 | |
|---|
| 34 | #include <errno.h> |
|---|
| 35 | #include <stdio.h> |
|---|
| 36 | #include <stdlib.h> |
|---|
| 37 | #include <string.h> |
|---|
| 38 | #include <syslog.h> |
|---|
| 39 | #include <unistd.h> |
|---|
| 40 | #include <sys/stat.h> |
|---|
| 41 | #include <sys/types.h> |
|---|
| 42 | |
|---|
| 43 | #include <getopt.h> |
|---|
| 44 | #include <rrd.h> |
|---|
| 45 | |
|---|
| 46 | #include "sensord.h" |
|---|
| 47 | #include "lib/error.h" |
|---|
| 48 | |
|---|
| 49 | #define DO_READ 0 |
|---|
| 50 | #define DO_SCAN 1 |
|---|
| 51 | #define DO_SET 2 |
|---|
| 52 | #define DO_RRD 3 |
|---|
| 53 | |
|---|
| 54 | /* one integer */ |
|---|
| 55 | #define STEP_BUFF 64 |
|---|
| 56 | /* RRA:AVERAGE:0.5:1:12345 */ |
|---|
| 57 | #define RRA_BUFF 256 |
|---|
| 58 | /* weak: max sensors for RRD .. TODO: fix */ |
|---|
| 59 | #define MAX_RRD_SENSORS 256 |
|---|
| 60 | /* weak: max raw label length .. TODO: fix */ |
|---|
| 61 | #define RAW_LABEL_LENGTH 32 |
|---|
| 62 | /* DS:label:GAUGE:900:U:U | :3000 .. TODO: fix */ |
|---|
| 63 | #define RRD_BUFF 64 |
|---|
| 64 | |
|---|
| 65 | char rrdBuff[MAX_RRD_SENSORS * RRD_BUFF + 1]; |
|---|
| 66 | static char rrdLabels[MAX_RRD_SENSORS][RAW_LABEL_LENGTH + 1]; |
|---|
| 67 | |
|---|
| 68 | #define LOADAVG "loadavg" |
|---|
| 69 | #define LOAD_AVERAGE "Load Average" |
|---|
| 70 | |
|---|
| 71 | typedef int (*FeatureFN) (void *data, const char *rawLabel, const char *label, const FeatureDescriptor *feature); |
|---|
| 72 | |
|---|
| 73 | static char |
|---|
| 74 | rrdNextChar |
|---|
| 75 | (char c) { |
|---|
| 76 | if (c == '9') { |
|---|
| 77 | return 'A'; |
|---|
| 78 | } else if (c == 'Z') { |
|---|
| 79 | return 'a'; |
|---|
| 80 | } else if (c == 'z') { |
|---|
| 81 | return 0; |
|---|
| 82 | } else { |
|---|
| 83 | return c + 1; |
|---|
| 84 | } |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | static void |
|---|
| 88 | rrdCheckLabel |
|---|
| 89 | (const char *rawLabel, int index0) { |
|---|
| 90 | char *buffer = rrdLabels[index0]; |
|---|
| 91 | int i, j, okay; |
|---|
| 92 | |
|---|
| 93 | i = 0; |
|---|
| 94 | while ((i < RAW_LABEL_LENGTH) && rawLabel[i]) { /* contrain raw label to [A-Za-z0-9_] */ |
|---|
| 95 | char c = rawLabel[i]; |
|---|
| 96 | if (((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z')) || ((c >= '0') && (c <= '9')) || (c == '_')) { |
|---|
| 97 | buffer[i] = c; |
|---|
| 98 | } else { |
|---|
| 99 | buffer[i] = '_'; |
|---|
| 100 | } |
|---|
| 101 | ++ i; |
|---|
| 102 | } |
|---|
| 103 | buffer[i] = '\0'; |
|---|
| 104 | |
|---|
| 105 | j = 0; |
|---|
| 106 | okay = (i > 0); |
|---|
| 107 | while (okay && (j < index0)) /* locate duplicates */ |
|---|
| 108 | okay = strcmp (rrdLabels[j ++], buffer); |
|---|
| 109 | |
|---|
| 110 | while (!okay) { /* uniquify duplicate labels with _? or _?? */ |
|---|
| 111 | if (!buffer[i]) { |
|---|
| 112 | if (i > RAW_LABEL_LENGTH - 3) |
|---|
| 113 | i = RAW_LABEL_LENGTH - 3; |
|---|
| 114 | buffer[i] = '_'; |
|---|
| 115 | buffer[i + 1] = '0'; |
|---|
| 116 | buffer[i + 2] = '\0'; |
|---|
| 117 | } else if (!buffer[i + 2]) { |
|---|
| 118 | if (!(buffer[i + 1] = rrdNextChar (buffer[i + 1]))) { |
|---|
| 119 | buffer[i + 1] = '0'; |
|---|
| 120 | buffer[i + 2] = '0'; |
|---|
| 121 | buffer[i + 3] = '\0'; |
|---|
| 122 | } |
|---|
| 123 | } else { |
|---|
| 124 | if (!(buffer[i + 2] = rrdNextChar (buffer[i + 2]))) { |
|---|
| 125 | buffer[i + 1] = rrdNextChar (buffer[i + 1]); |
|---|
| 126 | buffer[i + 2] = '0'; |
|---|
| 127 | } |
|---|
| 128 | } |
|---|
| 129 | j = 0; |
|---|
| 130 | okay = 1; |
|---|
| 131 | while (okay && (j < index0)) |
|---|
| 132 | okay = strcmp (rrdLabels[j ++], buffer); |
|---|
| 133 | } |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | static int |
|---|
| 137 | applyToFeatures |
|---|
| 138 | (FeatureFN fn, void *data) { |
|---|
| 139 | const sensors_chip_name *chip; |
|---|
| 140 | int i = 0, j, ret = 0, num = 0; |
|---|
| 141 | |
|---|
| 142 | for (j = 0; (ret == 0) && (j < numChipNames); ++ j) { |
|---|
| 143 | while ((ret == 0) && ((chip = sensors_get_detected_chips (&chipNames[j], &i)) != NULL)) { |
|---|
| 144 | ChipDescriptor *descriptor; |
|---|
| 145 | descriptor = generateChipDescriptor (chip); |
|---|
| 146 | if (descriptor) { |
|---|
| 147 | const FeatureDescriptor *features = descriptor->features; |
|---|
| 148 | int index0; |
|---|
| 149 | |
|---|
| 150 | for (index0 = 0; (ret == 0) && (num < MAX_RRD_SENSORS) && features[index0].format; ++ index0) { |
|---|
| 151 | const FeatureDescriptor *feature = features + index0; |
|---|
| 152 | int labelNumber = feature->dataNumbers[0]; |
|---|
| 153 | const char *rawLabel = NULL; |
|---|
| 154 | char *label = NULL; |
|---|
| 155 | |
|---|
| 156 | if (getRawLabel (chip, labelNumber, &rawLabel)) { |
|---|
| 157 | sensorLog (LOG_ERR, "Error getting raw sensor label: %s/#%d", chip->prefix, labelNumber); |
|---|
| 158 | ret = -1; |
|---|
| 159 | } else if (!(label = sensors_get_label (chip, labelNumber))) { |
|---|
| 160 | sensorLog (LOG_ERR, "Error getting sensor label: %s/#%d", chip->prefix, labelNumber); |
|---|
| 161 | ret = -1; |
|---|
| 162 | } else { |
|---|
| 163 | rrdCheckLabel (rawLabel, num); |
|---|
| 164 | ret = fn (data, rrdLabels[num], label, feature); |
|---|
| 165 | ++ num; |
|---|
| 166 | } |
|---|
| 167 | if (label) |
|---|
| 168 | free (label); |
|---|
| 169 | } |
|---|
| 170 | free (descriptor->features); |
|---|
| 171 | free (descriptor); |
|---|
| 172 | } |
|---|
| 173 | } |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | return ret; |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | struct ds { |
|---|
| 180 | int num; |
|---|
| 181 | const char **argv; |
|---|
| 182 | }; |
|---|
| 183 | |
|---|
| 184 | static int |
|---|
| 185 | rrdGetSensors_DS |
|---|
| 186 | (void *_data, const char *rawLabel, const char *label, const FeatureDescriptor *feature) { |
|---|
| 187 | if (!feature || feature->rrd) { |
|---|
| 188 | struct ds *data = (struct ds *) _data; |
|---|
| 189 | char *ptr = rrdBuff + data->num * RRD_BUFF; |
|---|
| 190 | const char *min, *max; |
|---|
| 191 | data->argv[data->num ++] = ptr; |
|---|
| 192 | switch (feature ? feature->type : DataType_other) { /* arbitrary sanity limits */ |
|---|
| 193 | case DataType_voltage: |
|---|
| 194 | min="-25"; |
|---|
| 195 | max="25"; |
|---|
| 196 | break; |
|---|
| 197 | case DataType_rpm: |
|---|
| 198 | min = "0"; |
|---|
| 199 | max = "12000"; |
|---|
| 200 | break; |
|---|
| 201 | case DataType_temperature: |
|---|
| 202 | min = "0"; |
|---|
| 203 | max = "250"; |
|---|
| 204 | break; |
|---|
| 205 | default: |
|---|
| 206 | min = max = "U"; |
|---|
| 207 | break; |
|---|
| 208 | } |
|---|
| 209 | sprintf (ptr, "DS:%s:GAUGE:%d:%s:%s", rawLabel, /* number of seconds downtime during which average be used instead of unknown */ 5 * rrdTime, min, max); |
|---|
| 210 | } |
|---|
| 211 | return 0; |
|---|
| 212 | } |
|---|
| 213 | |
|---|
| 214 | static int |
|---|
| 215 | rrdGetSensors |
|---|
| 216 | (const char **argv) { |
|---|
| 217 | int ret = 0; |
|---|
| 218 | struct ds data = { 0, argv}; |
|---|
| 219 | ret = applyToFeatures (rrdGetSensors_DS, &data); |
|---|
| 220 | if (!ret && doLoad) |
|---|
| 221 | ret = rrdGetSensors_DS (&data, LOADAVG, LOAD_AVERAGE, NULL); |
|---|
| 222 | return ret ? -1 : data.num; |
|---|
| 223 | } |
|---|
| 224 | |
|---|
| 225 | int |
|---|
| 226 | rrdInit |
|---|
| 227 | (void) { |
|---|
| 228 | int ret = 0; |
|---|
| 229 | struct stat tmp; |
|---|
| 230 | |
|---|
| 231 | sensorLog (LOG_DEBUG, "sensor RRD init"); |
|---|
| 232 | if (stat (rrdFile, &tmp)) { |
|---|
| 233 | if (errno == ENOENT) { |
|---|
| 234 | char stepBuff[STEP_BUFF], rraBuff[RRA_BUFF]; |
|---|
| 235 | int argc = 4, num; |
|---|
| 236 | const char *argv[6 + MAX_RRD_SENSORS] = { |
|---|
| 237 | "sensord", rrdFile, "-s", stepBuff |
|---|
| 238 | }; |
|---|
| 239 | |
|---|
| 240 | sensorLog (LOG_INFO, "creating round robin database"); |
|---|
| 241 | num = rrdGetSensors (argv + argc); |
|---|
| 242 | if (num == 0) { |
|---|
| 243 | sensorLog (LOG_ERR, "Error creating RRD: %s: %s", rrdFile, "No sensors detected"); |
|---|
| 244 | ret = 2; |
|---|
| 245 | } else if (num < 0) { |
|---|
| 246 | ret = -num; |
|---|
| 247 | } else { |
|---|
| 248 | sprintf (stepBuff, "%d", rrdTime); |
|---|
| 249 | sprintf (rraBuff, "RRA:%s:%f:%d:%d", rrdNoAverage?"LAST":"AVERAGE", 0.5 /* fraction of non-unknown samples needed per entry */, 1 /* samples per entry */, 7 * 24 * 60 * 60 / rrdTime /* 1 week */); |
|---|
| 250 | argc += num; |
|---|
| 251 | argv[argc ++] = rraBuff; |
|---|
| 252 | argv[argc] = NULL; |
|---|
| 253 | optind = 1; |
|---|
| 254 | opterr = 0; |
|---|
| 255 | optopt = '?'; |
|---|
| 256 | optarg = NULL; |
|---|
| 257 | if ((ret = rrd_create (argc, (char **) /* WEAK */ argv))) { |
|---|
| 258 | sensorLog (LOG_ERR, "Error creating RRD file: %s: %s", rrdFile, rrd_get_error ()); |
|---|
| 259 | } |
|---|
| 260 | } |
|---|
| 261 | } else { |
|---|
| 262 | sensorLog (LOG_ERR, "Error stat()ing RRD: %s: %s", rrdFile, strerror (errno)); |
|---|
| 263 | ret = 1; |
|---|
| 264 | } |
|---|
| 265 | } |
|---|
| 266 | sensorLog (LOG_DEBUG, "sensor RRD inited"); |
|---|
| 267 | |
|---|
| 268 | return ret; |
|---|
| 269 | } |
|---|
| 270 | |
|---|
| 271 | #define RRDCGI "/usr/bin/rrdcgi" |
|---|
| 272 | #define WWWDIR "/sensord" |
|---|
| 273 | |
|---|
| 274 | struct gr { |
|---|
| 275 | DataType type; |
|---|
| 276 | const char *h2; |
|---|
| 277 | const char *image; |
|---|
| 278 | const char *title; |
|---|
| 279 | const char *axisTitle; |
|---|
| 280 | const char *axisDefn; |
|---|
| 281 | const char *options; |
|---|
| 282 | int loadAvg; |
|---|
| 283 | }; |
|---|
| 284 | |
|---|
| 285 | static int |
|---|
| 286 | rrdCGI_DEF |
|---|
| 287 | (void *_data, const char *rawLabel, const char *label, const FeatureDescriptor *feature) { |
|---|
| 288 | struct gr *data = (struct gr *) _data; |
|---|
| 289 | if (!feature || (feature->rrd && (feature->type == data->type))) |
|---|
| 290 | printf ("\n\tDEF:%s=%s:%s:AVERAGE", rawLabel, rrdFile, rawLabel); |
|---|
| 291 | return 0; |
|---|
| 292 | } |
|---|
| 293 | |
|---|
| 294 | static int |
|---|
| 295 | rrdCGI_LINE |
|---|
| 296 | (void *_data, const char *rawLabel, const char *label, const FeatureDescriptor *feature) { |
|---|
| 297 | struct gr *data = (struct gr *) _data; |
|---|
| 298 | if (!feature || (feature->rrd && (feature->type == data->type))) |
|---|
| 299 | printf ("\n\tLINE2:%s#%.6x:\"%s\"", rawLabel, (int) random () & 0xffffff, label); |
|---|
| 300 | return 0; |
|---|
| 301 | } |
|---|
| 302 | |
|---|
| 303 | static struct gr graphs[] = { |
|---|
| 304 | { |
|---|
| 305 | DataType_temperature, |
|---|
| 306 | "Daily Temperature Summary", |
|---|
| 307 | "daily-temperature", |
|---|
| 308 | "Temperature", |
|---|
| 309 | "Temperature (C)", |
|---|
| 310 | "HOUR:1:HOUR:3:HOUR:3:0:%b %d %H:00", |
|---|
| 311 | "-s -1d -l 0", |
|---|
| 312 | 1 |
|---|
| 313 | }, { |
|---|
| 314 | DataType_rpm, |
|---|
| 315 | "Daily Fan Speed Summary", |
|---|
| 316 | "daily-rpm", |
|---|
| 317 | "Fan Speed", |
|---|
| 318 | "Speed (RPM)", |
|---|
| 319 | "HOUR:1:HOUR:3:HOUR:3:0:%b %d %H:00", |
|---|
| 320 | "-s -1d -l 0", |
|---|
| 321 | 0 |
|---|
| 322 | }, { |
|---|
| 323 | DataType_voltage, |
|---|
| 324 | "Daily Voltage Summary", |
|---|
| 325 | "daily-voltage", |
|---|
| 326 | "Power Supply", |
|---|
| 327 | "Voltage (V)", |
|---|
| 328 | "HOUR:1:HOUR:3:HOUR:3:0:%b %d %H:00", |
|---|
| 329 | "-s -1d --alt-autoscale", |
|---|
| 330 | 0 |
|---|
| 331 | }, { |
|---|
| 332 | DataType_temperature, |
|---|
| 333 | "Weekly Temperature Summary", |
|---|
| 334 | "weekly-temperature", |
|---|
| 335 | "Temperature", |
|---|
| 336 | "Temperature (C)", |
|---|
| 337 | "HOUR:6:DAY:1:DAY:1:86400:%a %b %d", |
|---|
| 338 | "-s -1w -l 0", |
|---|
| 339 | 1 |
|---|
| 340 | }, { |
|---|
| 341 | DataType_rpm, |
|---|
| 342 | "Weekly Fan Speed Summary", |
|---|
| 343 | "weekly-rpm", |
|---|
| 344 | "Fan Speed", |
|---|
| 345 | "Speed (RPM)", |
|---|
| 346 | "HOUR:6:DAY:1:DAY:1:86400:%a %b %d", |
|---|
| 347 | "-s -1w -l 0", |
|---|
| 348 | 0 |
|---|
| 349 | }, { |
|---|
| 350 | DataType_voltage, |
|---|
| 351 | "Weekly Voltage Summary", |
|---|
| 352 | "weekly-voltage", |
|---|
| 353 | "Power Supply", |
|---|
| 354 | "Voltage (V)", |
|---|
| 355 | "HOUR:6:DAY:1:DAY:1:86400:%a %b %d", |
|---|
| 356 | "-s -1w --alt-autoscale", |
|---|
| 357 | 0 |
|---|
| 358 | }, { |
|---|
| 359 | DataType_other, |
|---|
| 360 | NULL, |
|---|
| 361 | NULL, |
|---|
| 362 | NULL, |
|---|
| 363 | NULL, |
|---|
| 364 | NULL, |
|---|
| 365 | NULL, |
|---|
| 366 | 0 |
|---|
| 367 | } |
|---|
| 368 | }; |
|---|
| 369 | |
|---|
| 370 | int |
|---|
| 371 | rrdUpdate |
|---|
| 372 | (void) { |
|---|
| 373 | int ret = rrdChips (); |
|---|
| 374 | if (!ret && doLoad) { |
|---|
| 375 | FILE *loadavg; |
|---|
| 376 | if (!(loadavg = fopen ("/proc/loadavg", "r"))) { |
|---|
| 377 | sensorLog (LOG_ERR, "Error opening `/proc/loadavg': %s", strerror (errno)); |
|---|
| 378 | ret = 1; |
|---|
| 379 | } else { |
|---|
| 380 | float value; |
|---|
| 381 | if (fscanf (loadavg, "%f", &value) != 1) { |
|---|
| 382 | sensorLog (LOG_ERR, "Error reading load average"); |
|---|
| 383 | ret = 2; |
|---|
| 384 | } else { |
|---|
| 385 | sprintf (rrdBuff + strlen (rrdBuff), ":%f", value); |
|---|
| 386 | } |
|---|
| 387 | fclose (loadavg); |
|---|
| 388 | } |
|---|
| 389 | } |
|---|
| 390 | if (!ret) { |
|---|
| 391 | const char *argv[] = { |
|---|
| 392 | "sensord", rrdFile, rrdBuff, NULL |
|---|
| 393 | }; |
|---|
| 394 | optind = 1; |
|---|
| 395 | opterr = 0; |
|---|
| 396 | optopt = '?'; |
|---|
| 397 | optarg = NULL; |
|---|
| 398 | if ((ret = rrd_update (3, (char **) /* WEAK */ argv))) { |
|---|
| 399 | sensorLog (LOG_ERR, "Error updating RRD file: %s: %s", rrdFile, rrd_get_error ()); |
|---|
| 400 | } |
|---|
| 401 | } |
|---|
| 402 | sensorLog (LOG_DEBUG, "sensor rrd updated"); |
|---|
| 403 | |
|---|
| 404 | return ret; |
|---|
| 405 | } |
|---|
| 406 | |
|---|
| 407 | int |
|---|
| 408 | rrdCGI |
|---|
| 409 | (void) { |
|---|
| 410 | int ret = 0; |
|---|
| 411 | struct gr *graph = graphs; |
|---|
| 412 | |
|---|
| 413 | printf ("#!" RRDCGI "\n\n<HTML>\n<HEAD>\n<TITLE>sensord</TITLE>\n</HEAD>\n<BODY>\n<H1>sensord</H1>\n"); |
|---|
| 414 | while (graph->type != DataType_other) { |
|---|
| 415 | printf ("<H2>%s</H2>\n", graph->h2); |
|---|
| 416 | printf ("<P>\n<RRD::GRAPH %s/%s.png\n\t--imginfo '<IMG SRC=" WWWDIR "/%%s WIDTH=%%lu HEIGHT=%%lu>'\n\t-a PNG\n\t-h 200 -w 800\n", cgiDir, graph->image); |
|---|
| 417 | printf ("\t--lazy\n\t-v '%s'\n\t-t '%s'\n\t-x '%s'\n\t%s", graph->axisTitle, graph->title, graph->axisDefn, graph->options); |
|---|
| 418 | if (!ret) |
|---|
| 419 | ret = applyToFeatures (rrdCGI_DEF, graph); |
|---|
| 420 | if (!ret && doLoad && graph->loadAvg) |
|---|
| 421 | ret = rrdCGI_DEF (graph, LOADAVG, LOAD_AVERAGE, NULL); |
|---|
| 422 | if (!ret) |
|---|
| 423 | ret = applyToFeatures (rrdCGI_LINE, graph); |
|---|
| 424 | if (!ret && doLoad && graph->loadAvg) |
|---|
| 425 | ret = rrdCGI_LINE (graph, LOADAVG, LOAD_AVERAGE, NULL); |
|---|
| 426 | printf (">\n</P>\n"); |
|---|
| 427 | ++ graph; |
|---|
| 428 | } |
|---|
| 429 | printf ("<p>\n<small><b>sensord</b> by <a href=\"mailto:merlin@merlin.org\">Merlin Hughes</a>, all credit to the <a href=\"http://www.lm-sensors.org/\">lm_sensors</a> crew.</small>\n</p>\n"); |
|---|
| 430 | printf ("</BODY>\n</HTML>\n"); |
|---|
| 431 | |
|---|
| 432 | return ret; |
|---|
| 433 | } |
|---|