root/lm-sensors/branches/lm-sensors-3.0.0/prog/sensord/args.c @ 4987

Revision 4987, 9.0 KB (checked in by khali, 6 years ago)

"sensord" no longer needs a default configuration file name. If no
file name is provided on the command line, use the libsensors default.

  • 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#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <getopt.h>
27#include <syslog.h>
28
29#include "sensord.h"
30#include "lib/error.h"
31#include "version.h"
32
33#define MAX_CHIP_NAMES 32
34
35int isDaemon = 0;
36const char *sensorsCfgFile = NULL;
37const char *pidFile = "/var/run/sensord.pid";
38const char *rrdFile = NULL;
39const char *cgiDir = NULL;
40int scanTime = 60;
41int logTime = 30 * 60;
42int rrdTime = 5 * 60;
43int rrdNoAverage = 0;
44int syslogFacility = LOG_LOCAL4;
45int doScan = 0;
46int doSet = 0;
47int doCGI = 0;
48int doLoad = 0;
49int debug = 0;
50sensors_chip_name chipNames[MAX_CHIP_NAMES];
51int numChipNames = 0;
52
53static int
54parseTime
55(char *arg) {
56  char *end;
57  int value = strtoul (arg, &end, 10);
58  if ((end > arg) && (*end == 's')) {
59    ++ end;
60  } else if ((end > arg) && (*end == 'm')) {
61    value *= 60;
62    ++ end;
63  } else if ((end > arg) && (*end == 'h')) {
64    value *= 60 * 60;
65    ++ end;
66  }
67  if ((end == arg) || *end) {
68    fprintf (stderr, "Error parsing time value `%s'.\n", arg);
69    return -1;
70  }
71  return value;
72}
73
74static struct {
75  const char *name;
76  int id;
77} facilities[] = {
78  { "local0", LOG_LOCAL0 }, { "local1", LOG_LOCAL1 },
79  { "local2", LOG_LOCAL2 }, { "local3", LOG_LOCAL3 },
80  { "local4", LOG_LOCAL4 }, { "local5", LOG_LOCAL5 },
81  { "local6", LOG_LOCAL6 }, { "local7", LOG_LOCAL7 },
82  { "daemon", LOG_DAEMON }, { "user", LOG_USER },
83  { NULL, 0 }
84};
85
86static int
87parseFacility
88(char *arg) {
89  int i = 0;
90  while (facilities[i].name && strcasecmp (arg, facilities[i].name))
91    ++ i;
92  if (!facilities[i].name) {
93    fprintf (stderr, "Error parsing facility value `%s'.\n", arg);
94    return -1;
95  }
96  return facilities[i].id;
97}
98
99static const char *daemonSyntax =
100  "  -i, --interval <time>     -- interval between scanning alarms (default 60s)\n"
101  "  -l, --log-interval <time> -- interval between logging sensors (default 30m)\n"
102  "  -t, --rrd-interval <time> -- interval between updating RRD file (default 5m)\n"
103  "  -T, --rrd-no-average      -- switch RRD in non-average mode\n"
104  "  -r, --rrd-file <file>     -- RRD file (default <none>)\n"
105  "  -c, --config-file <file>  -- configuration file\n"
106  "  -p, --pid-file <file>     -- PID file (default /var/run/sensord.pid)\n"
107  "  -f, --syslog-facility <f> -- syslog facility to use (default local4)\n"
108  "  -g, --rrd-cgi <img-dir>   -- output an RRD CGI script and exit\n"
109  "  -a, --load-average        -- include load average in RRD file\n"
110  "  -d, --debug               -- display some debug information\n"
111  "  -v, --version             -- display version and exit\n"
112  "  -h, --help                -- display help and exit\n"
113  "\n"
114  "Specify a value of 0 for any interval to disable that operation;\n"
115  "for example, specify --log-interval 0 to only scan for alarms."
116  "\n"
117  "Specify the filename `-' to read the config file from stdin.\n"
118  "\n"
119  "If no chips are specified, all chip info will be printed.\n"
120  "\n"
121  "If unspecified, no RRD (round robin database) is used. If specified and the\n"
122  "file does not exist, it will be created. For RRD updates to be successful,\n"
123  "the RRD file configuration must EXACTLY match the sensors that are used. If\n"
124  "your configuration changes, delete the old RRD file and restart sensord.\n";
125
126static const char *appSyntax =
127  "  -a, --alarm-scan          -- only scan for alarms\n"
128  "  -s, --set                 -- execute set statements (root only)\n"
129  "  -r, --rrd-file <file>     -- only update RRD file\n"
130  "  -c, --config-file <file>  -- configuration file\n"
131  "  -d, --debug               -- display some debug information\n"
132  "  -v, --version             -- display version and exit\n"
133  "  -h, --help                -- display help and exit\n"
134  "\n"
135  "Specify the filename `-' to read the config file from stdin.\n"
136  "\n"
137  "If no chips are specified, all chip info will be printed.\n";
138
139static const char *daemonShortOptions = "i:l:t:Tf:r:c:p:advhg:";
140
141static const struct option daemonLongOptions[] = {
142  { "interval", required_argument, NULL, 'i' },
143  { "log-interval", required_argument, NULL, 'l' },
144  { "rrd-interval", required_argument, NULL, 't' },
145  { "rrd-no-average", no_argument, NULL, 'T' },
146  { "syslog-facility", required_argument, NULL, 'f' },
147  { "rrd-file", required_argument, NULL, 'r' },
148  { "config-file", required_argument, NULL, 'c' },
149  { "pid-file", required_argument, NULL, 'p' },
150  { "rrd-cgi", required_argument, NULL, 'g' },
151  { "load-average", no_argument, NULL, 'a' },
152  { "debug", no_argument, NULL, 'd' },
153  { "version", no_argument, NULL, 'v' },
154  { "help", no_argument, NULL, 'h' },
155  { NULL, 0, NULL, 0 }
156};
157
158static const char *appShortOptions = "asr:c:dvh";
159
160static const struct option appLongOptions[] = {
161  { "alarm-scan", no_argument, NULL, 'a' },
162  { "set", no_argument, NULL, 's' },
163  { "rrd-file", required_argument, NULL, 'r' },
164  { "config-file", required_argument, NULL, 'c' },
165  { "debug", no_argument, NULL, 'd' },
166  { "version", no_argument, NULL, 'v' },
167  { "help", no_argument, NULL, 'h' },
168  { NULL, 0, NULL, 0 }
169};
170
171int
172parseArgs
173(int argc, char **argv) {
174  int c;
175  const char *shortOptions;
176  const struct option *longOptions;
177
178  isDaemon = (argv[0][strlen (argv[0]) - 1] == 'd');
179  shortOptions = isDaemon ? daemonShortOptions : appShortOptions;
180  longOptions = isDaemon ? daemonLongOptions : appLongOptions;
181
182  while ((c = getopt_long (argc, argv, shortOptions, longOptions, NULL)) != EOF) {
183    switch(c) {
184      case 'i':
185        if ((scanTime = parseTime (optarg)) < 0)
186          return -1;
187        break;
188      case 'l':
189        if ((logTime = parseTime (optarg)) < 0)
190          return -1;
191        break;
192      case 't':
193        if ((rrdTime = parseTime (optarg)) < 0)
194          return -1;
195        break;
196      case 'T':
197        rrdNoAverage = 1;
198        break;
199      case 'f':
200        if ((syslogFacility = parseFacility (optarg)) < 0)
201          return -1;
202        break;
203      case 'a':
204        if (isDaemon)
205          doLoad = 1;
206        else
207          doScan = 1;
208        break;
209      case 's':
210        doSet = 1;
211        break;
212      case 'c':
213        sensorsCfgFile = optarg;
214        break;
215      case 'p':
216        pidFile = optarg;
217        break;
218      case 'r':
219        rrdFile = optarg;
220        break;
221      case 'd':
222        debug = 1;
223        break;
224      case 'g':
225        doCGI = 1;
226        cgiDir = optarg;
227        break;
228      case 'v':
229        printf ("sensord version %s\n", LM_VERSION);
230        exit (EXIT_SUCCESS);
231        break;
232      case 'h':
233        printf ("Syntax: %s {options} {chips}\n%s", argv[0], isDaemon ? daemonSyntax : appSyntax);
234        exit (EXIT_SUCCESS);
235        break;
236      case ':':
237      case '?':
238        printf ("Try `%s --help' for more information.\n", argv[0]);
239        return -1;
240        break;
241      default:
242        fprintf (stderr, "Internal error while parsing options.\n");
243        return -1;
244        break;
245    }
246  }
247
248  if (doScan && doSet) {
249    fprintf (stderr, "Error: Incompatible --set and --alarm-scan.\n");
250    return -1;
251  }
252 
253  if (rrdFile && doSet) {
254    fprintf (stderr, "Error: Incompatible --set and --rrd-file.\n");
255    return -1;
256  }
257 
258  if (doScan && rrdFile) {
259    fprintf (stderr, "Error: Incompatible --rrd-file and --alarm-scan.\n");
260    return -1;
261  }
262
263  if (doCGI && !rrdFile) {
264    fprintf (stderr, "Error: Incompatible --rrd-cgi without --rrd-file.\n");
265    return -1;
266  }
267 
268  if (rrdFile && !rrdTime) {
269    fprintf (stderr, "Error: Incompatible --rrd-file without --rrd-interval.\n");
270    return -1;
271  }
272 
273  if (!logTime && !scanTime && !rrdFile) {
274    fprintf (stderr, "Error: No logging, alarm or RRD scanning.\n");
275    return -1;
276  }
277
278  return 0;
279}
280
281int
282parseChips
283(int argc, char **argv) {
284  if (optind == argc) {
285    chipNames[0].prefix = SENSORS_CHIP_NAME_PREFIX_ANY;
286    chipNames[0].bus.type = SENSORS_BUS_TYPE_ANY;
287    chipNames[0].bus.nr = SENSORS_BUS_NR_ANY;
288    chipNames[0].addr = SENSORS_CHIP_NAME_ADDR_ANY;
289    numChipNames = 1;
290  } else {
291    int i, n = argc - optind, err;
292    if (n > MAX_CHIP_NAMES) {
293      fprintf (stderr, "Too many chip names.\n");
294      return -1;
295    }
296    for (i = 0; i < n; ++ i) {
297      char *arg = argv[optind + i];
298      if ((err = sensors_parse_chip_name (arg, chipNames + i))) {
299        fprintf (stderr, "Invalid chip name `%s': %s\n", arg, sensors_strerror (err));
300        return -1;
301      }
302    }
303    numChipNames = n;
304  }
305  return 0;
306}
Note: See TracBrowser for help on using the browser.