root/lm-sensors/trunk/prog/sensord/args.c @ 5718

Revision 5718, 7.1 KB (checked in by andy, 4 years ago)

sensord: Remove the hidden commandline interface.

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