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

Revision 5163, 9.1 KB (checked in by khali, 6 years ago)

Patch from Aurelien Jarno:

I have just noticed that the FSF address is the old one in all files
except COPYING. Please find a patch below to fix that.

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