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