| 1 |
/* |
| 2 |
* sensord |
| 3 |
* |
| 4 |
* A daemon that periodically logs sensor information to syslog. |
| 5 |
* |
| 6 |
* Copyright (c) 1999-2002 Merlin Hughes <[email protected]> |
| 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 |
|
| 35 |
struct sensord_arguments sensord_args = { |
| 36 |
.pidFile = "/run/sensord.pid", |
| 37 |
.scanTime = 60, |
| 38 |
.logTime = 30 * 60, |
| 39 |
.rrdTime = 5 * 60, |
| 40 |
.syslogFacility = LOG_DAEMON, |
| 41 |
}; |
| 42 |
|
| 43 |
static 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 |
|
| 63 |
static 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 |
|
| 80 |
static 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 |
|
| 92 |
static 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 |
" -1, --oneline -- log chip, adapter, and sensor data on one line\n" |
| 97 |
" -T, --rrd-no-average -- switch RRD in non-average mode\n" |
| 98 |
" -r, --rrd-file <file> -- RRD file (default <none>)\n" |
| 99 |
" -c, --config-file <file> -- configuration file\n" |
| 100 |
" -p, --pid-file <file> -- PID file (default /run/sensord.pid)\n" |
| 101 |
" -f, --syslog-facility <f> -- syslog facility to use (default local4)\n" |
| 102 |
" -g, --rrd-cgi <img-dir> -- output an RRD CGI script and exit\n" |
| 103 |
" -a, --load-average -- include load average in RRD file\n" |
| 104 |
" -d, --debug -- display some debug information\n" |
| 105 |
" -v, --version -- display version and exit\n" |
| 106 |
" -h, --help -- display help and exit\n" |
| 107 |
"\n" |
| 108 |
"Specify a value of 0 for any interval to disable that operation;\n" |
| 109 |
"for example, specify --log-interval 0 to only scan for alarms." |
| 110 |
"\n" |
| 111 |
"Specify the filename `-' to read the config file from stdin.\n" |
| 112 |
"\n" |
| 113 |
"If no chips are specified, all chip info will be printed.\n" |
| 114 |
"\n" |
| 115 |
"If unspecified, no RRD (round robin database) is used. If specified and the\n" |
| 116 |
"file does not exist, it will be created. For RRD updates to be successful,\n" |
| 117 |
"the RRD file configuration must EXACTLY match the sensors that are used. If\n" |
| 118 |
"your configuration changes, delete the old RRD file and restart sensord.\n"; |
| 119 |
|
| 120 |
static const char *shortOptions = "i:l:t:1Tf:r:c:p:advhg:"; |
| 121 |
|
| 122 |
static const struct option longOptions[] = { |
| 123 |
{ "interval", required_argument, NULL, 'i' }, |
| 124 |
{ "log-interval", required_argument, NULL, 'l' }, |
| 125 |
{ "rrd-interval", required_argument, NULL, 't' }, |
| 126 |
{ "oneline", no_argument, NULL, '1' }, |
| 127 |
{ "rrd-no-average", no_argument, NULL, 'T' }, |
| 128 |
{ "syslog-facility", required_argument, NULL, 'f' }, |
| 129 |
{ "rrd-file", required_argument, NULL, 'r' }, |
| 130 |
{ "config-file", required_argument, NULL, 'c' }, |
| 131 |
{ "pid-file", required_argument, NULL, 'p' }, |
| 132 |
{ "rrd-cgi", required_argument, NULL, 'g' }, |
| 133 |
{ "load-average", no_argument, NULL, 'a' }, |
| 134 |
{ "debug", no_argument, NULL, 'd' }, |
| 135 |
{ "version", no_argument, NULL, 'v' }, |
| 136 |
{ "help", no_argument, NULL, 'h' }, |
| 137 |
{ NULL, 0, NULL, 0 } |
| 138 |
}; |
| 139 |
|
| 140 |
int parseArgs(int argc, char **argv) |
| 141 |
{ |
| 142 |
int c; |
| 143 |
|
| 144 |
sensord_args.isDaemon = (argv[0][strlen (argv[0]) - 1] == 'd'); |
| 145 |
if (!sensord_args.isDaemon) { |
| 146 |
fprintf(stderr, "Sensord no longer runs as an commandline" |
| 147 |
" application.\n"); |
| 148 |
return -1; |
| 149 |
} |
| 150 |
|
| 151 |
while ((c = getopt_long(argc, argv, shortOptions, longOptions, NULL)) |
| 152 |
!= EOF) { |
| 153 |
switch(c) { |
| 154 |
case 'i': |
| 155 |
if ((sensord_args.scanTime = parseTime(optarg)) < 0) |
| 156 |
return -1; |
| 157 |
break; |
| 158 |
case 'l': |
| 159 |
if ((sensord_args.logTime = parseTime(optarg)) < 0) |
| 160 |
return -1; |
| 161 |
break; |
| 162 |
case 't': |
| 163 |
if ((sensord_args.rrdTime = parseTime(optarg)) < 0) |
| 164 |
return -1; |
| 165 |
break; |
| 166 |
case '1': |
| 167 |
sensord_args.logOneline = 1; |
| 168 |
break; |
| 169 |
case 'T': |
| 170 |
sensord_args.rrdNoAverage = 1; |
| 171 |
break; |
| 172 |
case 'f': |
| 173 |
sensord_args.syslogFacility = parseFacility(optarg); |
| 174 |
if (sensord_args.syslogFacility < 0) |
| 175 |
return -1; |
| 176 |
break; |
| 177 |
case 'a': |
| 178 |
sensord_args.doLoad = 1; |
| 179 |
break; |
| 180 |
case 'c': |
| 181 |
sensord_args.cfgFile = optarg; |
| 182 |
break; |
| 183 |
case 'p': |
| 184 |
sensord_args.pidFile = optarg; |
| 185 |
break; |
| 186 |
case 'r': |
| 187 |
sensord_args.rrdFile = optarg; |
| 188 |
break; |
| 189 |
case 'd': |
| 190 |
sensord_args.debug = 1; |
| 191 |
break; |
| 192 |
case 'g': |
| 193 |
sensord_args.doCGI = 1; |
| 194 |
sensord_args.cgiDir = optarg; |
| 195 |
break; |
| 196 |
case 'v': |
| 197 |
printf("sensord version %s\n", LM_VERSION); |
| 198 |
exit(EXIT_SUCCESS); |
| 199 |
case 'h': |
| 200 |
printf("Syntax: %s {options} {chips}\n%s", argv[0], |
| 201 |
daemonSyntax); |
| 202 |
exit(EXIT_SUCCESS); |
| 203 |
case ':': |
| 204 |
case '?': |
| 205 |
printf("Try `%s --help' for more information.\n", |
| 206 |
argv[0]); |
| 207 |
return -1; |
| 208 |
default: |
| 209 |
fprintf(stderr, |
| 210 |
"Internal error while parsing options.\n"); |
| 211 |
return -1; |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
if (sensord_args.doCGI && !sensord_args.rrdFile) { |
| 216 |
fprintf(stderr, |
| 217 |
"Error: Incompatible --rrd-cgi without --rrd-file.\n"); |
| 218 |
return -1; |
| 219 |
} |
| 220 |
|
| 221 |
if (sensord_args.rrdFile && !sensord_args.rrdTime) { |
| 222 |
fprintf(stderr, |
| 223 |
"Error: Incompatible --rrd-file without --rrd-interval.\n"); |
| 224 |
return -1; |
| 225 |
} |
| 226 |
|
| 227 |
if (!sensord_args.logTime && !sensord_args.scanTime && |
| 228 |
!sensord_args.rrdFile) { |
| 229 |
fprintf(stderr, |
| 230 |
"Error: No logging, alarm or RRD scanning.\n"); |
| 231 |
return -1; |
| 232 |
} |
| 233 |
|
| 234 |
return 0; |
| 235 |
} |
| 236 |
|
| 237 |
int parseChips(int argc, char **argv) |
| 238 |
{ |
| 239 |
int i, n = argc - optind, err; |
| 240 |
|
| 241 |
if (n == 0) { |
| 242 |
sensord_args.chipNames[0].prefix = |
| 243 |
SENSORS_CHIP_NAME_PREFIX_ANY; |
| 244 |
sensord_args.chipNames[0].bus.type = SENSORS_BUS_TYPE_ANY; |
| 245 |
sensord_args.chipNames[0].bus.nr = SENSORS_BUS_NR_ANY; |
| 246 |
sensord_args.chipNames[0].addr = SENSORS_CHIP_NAME_ADDR_ANY; |
| 247 |
sensord_args.numChipNames = 1; |
| 248 |
|
| 249 |
return 0; |
| 250 |
} |
| 251 |
|
| 252 |
if (n > MAX_CHIP_NAMES) { |
| 253 |
fprintf(stderr, "Too many chip names.\n"); |
| 254 |
return -1; |
| 255 |
} |
| 256 |
for (i = 0; i < n; ++i) { |
| 257 |
char *arg = argv[optind + i]; |
| 258 |
|
| 259 |
err = sensors_parse_chip_name(arg, sensord_args.chipNames + i); |
| 260 |
if (err) { |
| 261 |
fprintf(stderr, "Invalid chip name `%s': %s\n", arg, |
| 262 |
sensors_strerror(err)); |
| 263 |
for (i--; i >= 0; i--) |
| 264 |
sensors_free_chip_name(sensord_args.chipNames + i); |
| 265 |
return -1; |
| 266 |
} |
| 267 |
} |
| 268 |
sensord_args.numChipNames = n; |
| 269 |
|
| 270 |
return 0; |
| 271 |
} |
| 272 |
|
| 273 |
void freeChips() |
| 274 |
{ |
| 275 |
int i; |
| 276 |
|
| 277 |
for (i = 0; i < sensord_args.numChipNames; i++) |
| 278 |
sensors_free_chip_name(sensord_args.chipNames + i); |
| 279 |
} |