root/lm-sensors/branches/lm-sensors-3.0.0/prog/sensord/rrd.c @ 4849

Revision 4849, 11.7 KB (checked in by khali, 7 years ago)

Hide warnings about unused parameters.

  • 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/*
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
65char rrdBuff[MAX_RRD_SENSORS * RRD_BUFF + 1];
66static char rrdLabels[MAX_RRD_SENSORS][RAW_LABEL_LENGTH + 1];
67
68#define LOADAVG "loadavg"
69#define LOAD_AVERAGE "Load Average"
70
71typedef int (*FeatureFN) (void *data, const char *rawLabel, const char *label, const FeatureDescriptor *feature);
72
73static char
74rrdNextChar
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
87static void
88rrdCheckLabel
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
136static int
137applyToFeatures
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      int index0, chipindex = -1;
145      for (index0 = 0; knownChips[index0].features; ++ index0)
146        /* Trick: we compare addresses here. We know it works because both
147           pointers were returned by sensors_get_detected_chips(), so they
148           refer to libsensors internal structures, which do not move. */
149        if (knownChips[index0].name == chip) {
150          chipindex = index0;
151          break;
152        }
153      if (chipindex >= 0) {
154        const ChipDescriptor *descriptor = &knownChips[chipindex];
155        const FeatureDescriptor *features = descriptor->features;
156
157        for (index0 = 0; (ret == 0) && (num < MAX_RRD_SENSORS) && features[index0].format; ++ index0) {
158          const FeatureDescriptor *feature = features + index0;
159          const char *rawLabel = features->feature->name;
160          char *label = NULL;
161
162          if (!(label = sensors_get_label (chip, features->feature))) {
163            sensorLog (LOG_ERR, "Error getting sensor label: %s/%s", chip->prefix, rawLabel);
164            ret = -1;
165          } else  {
166            rrdCheckLabel (rawLabel, num);
167            ret = fn (data, rrdLabels[num], label, feature);
168            ++ num;
169          }
170          if (label)
171            free (label);
172        }
173      }
174    }
175  }
176
177  return ret;
178}
179
180struct ds {
181  int num;
182  const char **argv;
183};
184
185static int
186rrdGetSensors_DS
187(void *_data, const char *rawLabel, const char *label, const FeatureDescriptor *feature) {
188  (void) label; /* no warning */
189  if (!feature || feature->rrd) {
190    struct ds *data = (struct ds *) _data;
191    char *ptr = rrdBuff + data->num * RRD_BUFF;
192    const char *min, *max;
193    data->argv[data->num ++] = ptr;
194    switch (feature ? feature->type : DataType_other) { /* arbitrary sanity limits */
195      case DataType_voltage:
196        min="-25";
197        max="25";
198        break;
199      case DataType_rpm:
200        min = "0";
201        max = "12000";
202        break;
203      case DataType_temperature:
204        min = "0";
205        max = "250";
206        break;
207      default:
208        min = max = "U";
209        break;
210    }
211    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);
212  }
213  return 0;
214}
215
216static int
217rrdGetSensors
218(const char **argv) {
219  int ret = 0;
220  struct ds data = { 0, argv};
221  ret = applyToFeatures (rrdGetSensors_DS, &data);
222  if (!ret && doLoad)
223    ret = rrdGetSensors_DS (&data, LOADAVG, LOAD_AVERAGE, NULL);
224  return ret ? -1 : data.num;
225}
226
227int
228rrdInit
229(void) {
230  int ret = 0;
231  struct stat tmp;
232 
233  sensorLog (LOG_DEBUG, "sensor RRD init"); 
234  if (stat (rrdFile, &tmp)) {
235    if (errno == ENOENT) {
236      char stepBuff[STEP_BUFF], rraBuff[RRA_BUFF];
237      int argc = 4, num;
238      const char *argv[6 + MAX_RRD_SENSORS] = {
239        "sensord", rrdFile, "-s", stepBuff
240      };
241     
242      sensorLog (LOG_INFO, "creating round robin database");
243      num = rrdGetSensors (argv + argc);
244      if (num == 0) {
245        sensorLog (LOG_ERR, "Error creating RRD: %s: %s", rrdFile, "No sensors detected");
246        ret = 2;
247      } else if (num < 0) {
248        ret = -num;
249      } else {
250        sprintf (stepBuff, "%d", rrdTime);
251        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 */);
252        argc += num;
253        argv[argc ++] = rraBuff;
254        argv[argc] = NULL;
255        optind = 1;
256        opterr = 0;
257        optopt = '?';
258        optarg = NULL;
259        if ((ret = rrd_create (argc, (char **) /* WEAK */ argv))) {
260          sensorLog (LOG_ERR, "Error creating RRD file: %s: %s", rrdFile, rrd_get_error ());
261        }
262      }
263    } else {
264      sensorLog (LOG_ERR, "Error stat()ing RRD: %s: %s", rrdFile, strerror (errno));
265      ret = 1;
266    }
267  }
268  sensorLog (LOG_DEBUG, "sensor RRD inited"); 
269 
270  return ret;
271}
272
273#define RRDCGI "/usr/bin/rrdcgi"
274#define WWWDIR "/sensord"
275
276struct gr {
277  DataType type;
278  const char *h2;
279  const char *image;
280  const char *title;
281  const char *axisTitle;
282  const char *axisDefn;
283  const char *options;
284  int loadAvg;
285};
286
287static int
288rrdCGI_DEF
289(void *_data, const char *rawLabel, const char *label, const FeatureDescriptor *feature) {
290  struct gr *data = (struct gr *) _data;
291  (void) label; /* no warning */
292  if (!feature || (feature->rrd && (feature->type == data->type)))
293    printf ("\n\tDEF:%s=%s:%s:AVERAGE", rawLabel, rrdFile, rawLabel);
294  return 0;
295}
296
297static int
298rrdCGI_LINE
299(void *_data, const char *rawLabel, const char *label, const FeatureDescriptor *feature) {
300  struct gr *data = (struct gr *) _data;
301  if (!feature || (feature->rrd && (feature->type == data->type)))
302    printf ("\n\tLINE2:%s#%.6x:\"%s\"", rawLabel, (int) random () & 0xffffff, label);
303  return 0;
304}
305
306static struct gr graphs[] = {
307  {
308    DataType_temperature,
309    "Daily Temperature Summary",
310    "daily-temperature",
311    "Temperature",
312    "Temperature (C)",
313    "HOUR:1:HOUR:3:HOUR:3:0:%b %d %H:00",
314    "-s -1d -l 0",
315    1
316  }, {
317    DataType_rpm,
318    "Daily Fan Speed Summary",
319    "daily-rpm",
320    "Fan Speed",
321    "Speed (RPM)",
322    "HOUR:1:HOUR:3:HOUR:3:0:%b %d %H:00",
323    "-s -1d -l 0",
324    0
325  }, {
326    DataType_voltage,
327    "Daily Voltage Summary",
328    "daily-voltage",
329    "Power Supply",
330    "Voltage (V)",
331    "HOUR:1:HOUR:3:HOUR:3:0:%b %d %H:00",
332    "-s -1d --alt-autoscale",
333    0
334  }, {
335    DataType_temperature,
336    "Weekly Temperature Summary",
337    "weekly-temperature",
338    "Temperature",
339    "Temperature (C)",
340    "HOUR:6:DAY:1:DAY:1:86400:%a %b %d",
341    "-s -1w -l 0",
342    1
343  }, {
344    DataType_rpm,
345    "Weekly Fan Speed Summary",
346    "weekly-rpm",
347    "Fan Speed",
348    "Speed (RPM)",
349    "HOUR:6:DAY:1:DAY:1:86400:%a %b %d",
350    "-s -1w -l 0",
351    0
352  }, {
353    DataType_voltage,
354    "Weekly Voltage Summary",
355    "weekly-voltage",
356    "Power Supply",
357    "Voltage (V)",
358    "HOUR:6:DAY:1:DAY:1:86400:%a %b %d",
359    "-s -1w --alt-autoscale",
360    0
361  }, {
362    DataType_other,
363    NULL,
364    NULL,
365    NULL,
366    NULL,
367    NULL,
368    NULL,
369    0
370  } 
371};
372
373int
374rrdUpdate
375(void) {
376  int ret = rrdChips ();
377  if (!ret && doLoad) {
378    FILE *loadavg;
379    if (!(loadavg = fopen ("/proc/loadavg", "r"))) {
380      sensorLog (LOG_ERR, "Error opening `/proc/loadavg': %s", strerror (errno));
381      ret = 1;
382    } else {
383      float value;
384      if (fscanf (loadavg, "%f", &value) != 1) {
385        sensorLog (LOG_ERR, "Error reading load average");
386        ret = 2;
387      } else {
388        sprintf (rrdBuff + strlen (rrdBuff), ":%f", value);
389      }
390      fclose (loadavg);
391    }
392  }
393  if (!ret) {
394    const char *argv[] = {
395      "sensord", rrdFile, rrdBuff, NULL
396    };
397    optind = 1;
398    opterr = 0;
399    optopt = '?';
400    optarg = NULL;
401    if ((ret = rrd_update (3, (char **) /* WEAK */ argv))) {
402      sensorLog (LOG_ERR, "Error updating RRD file: %s: %s", rrdFile, rrd_get_error ());
403    }
404  }
405  sensorLog (LOG_DEBUG, "sensor rrd updated"); 
406 
407  return ret;
408}
409
410int
411rrdCGI
412(void) {
413  int ret = 0;
414  struct gr *graph = graphs;
415
416  printf ("#!" RRDCGI "\n\n<HTML>\n<HEAD>\n<TITLE>sensord</TITLE>\n</HEAD>\n<BODY>\n<H1>sensord</H1>\n");
417  while (graph->type != DataType_other) {
418    printf ("<H2>%s</H2>\n", graph->h2);
419    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);
420    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);
421    if (!ret)
422      ret = applyToFeatures (rrdCGI_DEF, graph);
423    if (!ret && doLoad && graph->loadAvg)
424      ret = rrdCGI_DEF (graph, LOADAVG, LOAD_AVERAGE, NULL);
425    if (!ret)
426      ret = applyToFeatures (rrdCGI_LINE, graph);
427    if (!ret && doLoad && graph->loadAvg)
428      ret = rrdCGI_LINE (graph, LOADAVG, LOAD_AVERAGE, NULL);
429    printf (">\n</P>\n");
430    ++ graph;
431  }
432  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");
433  printf ("</BODY>\n</HTML>\n");
434 
435  return ret;
436}
Note: See TracBrowser for help on using the browser.