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

Revision 5719, 7.3 KB (checked in by andy, 4 years ago)

sensord: Introduce struct sensord_args.

This structure encapsulate all the variables holding the commandline
arguments. So we get rid of plenty of extern variables. This reduces
namespace collisions and unifies access.

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