readconf.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. /* $Id: readconf.c,v 1.20 2003/10/10 15:20:03 dijkstra Exp $ */
  2. /*
  3. * Copyright (c) 2001-2002 Willem Dijkstra
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * - Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following
  14. * disclaimer in the documentation and/or other materials provided
  15. * with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  25. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  27. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. #include <sys/queue.h>
  32. #include <sys/stat.h>
  33. #include <string.h>
  34. #include <fcntl.h>
  35. #include <unistd.h>
  36. #include "data.h"
  37. #include "error.h"
  38. #include "lex.h"
  39. #include "net.h"
  40. #include "readconf.h"
  41. #include "xmalloc.h"
  42. __BEGIN_DECLS
  43. int read_mux(struct muxlist * mul, struct lex *);
  44. int read_source(struct sourcelist * sol, struct lex *);
  45. int insert_filename(char *, int, int, char *);
  46. __END_DECLS
  47. const char *default_symux_port = SYMUX_PORT;
  48. int
  49. insert_filename(char *path, int maxlen, int type, char *args)
  50. {
  51. char *ts;
  52. char *ta;
  53. ts = ta = NULL;
  54. switch (type) {
  55. case MT_CPU:
  56. ts = "cpu";
  57. ta = args;
  58. break;
  59. case MT_IF:
  60. ts = "if_";
  61. ta = args;
  62. break;
  63. case MT_IO:
  64. ts = "io_";
  65. ta = args;
  66. break;
  67. case MT_MEM:
  68. ts = "mem";
  69. ta = "";
  70. break;
  71. case MT_PF:
  72. ts = "pf";
  73. ta = "";
  74. break;
  75. case MT_MBUF:
  76. ts = "mbuf";
  77. ta = "";
  78. break;
  79. case MT_DEBUG:
  80. ts = "debug";
  81. ta = "";
  82. break;
  83. case MT_PROC:
  84. ts = "proc_";
  85. ta = args;
  86. break;
  87. case MT_SENSOR:
  88. ts = "sensor";
  89. ta = args;
  90. break;
  91. default:
  92. warning("%s:%d: internal error: type (%d) unknown",
  93. __FILE__, __LINE__, type);
  94. return 0;
  95. }
  96. if ((snprintf(path, maxlen, "/%s%s.rrd", ts, ta)) >= maxlen)
  97. return 0;
  98. else
  99. return 1;
  100. }
  101. /* mux <host> (port|,| ) <number> */
  102. int
  103. read_mux(struct muxlist * mul, struct lex * l)
  104. {
  105. char muxname[_POSIX2_LINE_MAX];
  106. struct mux *mux;
  107. if (!SLIST_EMPTY(mul)) {
  108. warning("%s:%d: only one mux statement allowed",
  109. l->filename, l->cline);
  110. return 0;
  111. }
  112. lex_nexttoken(l);
  113. if (!getip(l->token)) {
  114. warning("%s:%d: could not resolve '%s'",
  115. l->filename, l->cline, l->token);
  116. return 0;
  117. }
  118. mux = add_mux(mul, SYMON_UNKMUX);
  119. mux->addr = xstrdup((const char *) &res_host);
  120. /* check for port statement */
  121. lex_nexttoken(l);
  122. if (l->op == LXT_PORT || l->op == LXT_COMMA)
  123. lex_nexttoken(l);
  124. if (l->type != LXY_NUMBER) {
  125. lex_ungettoken(l);
  126. mux->port = xstrdup(default_symux_port);
  127. }
  128. else {
  129. mux->port = xstrdup((const char *) l->token);
  130. }
  131. bzero(&muxname, sizeof(muxname));
  132. snprintf(&muxname[0], sizeof(muxname), "%s %s", mux->addr, mux->port);
  133. if (rename_mux(mul, mux, muxname) == NULL)
  134. fatal("%s:%d: internal error: dual mux", __FILE__, __LINE__);
  135. return 1;
  136. }
  137. /* source <host> { accept ... | write ... | datadir ... } */
  138. int
  139. read_source(struct sourcelist * sol, struct lex * l)
  140. {
  141. struct source *source;
  142. struct stream *stream;
  143. struct stat sb;
  144. char path[_POSIX2_LINE_MAX];
  145. char sn[_POSIX2_LINE_MAX];
  146. char sa[_POSIX2_LINE_MAX];
  147. int st;
  148. int pc;
  149. int fd;
  150. /* get hostname */
  151. lex_nexttoken(l);
  152. if (!getip(l->token)) {
  153. warning("%s:%d: could not resolve '%s'",
  154. l->filename, l->cline, l->token);
  155. return 0;
  156. }
  157. source = add_source(sol, res_host);
  158. EXPECT(l, LXT_BEGIN);
  159. while (lex_nexttoken(l)) {
  160. switch (l->op) {
  161. /* accept { cpu(x), ... } */
  162. case LXT_ACCEPT:
  163. EXPECT(l, LXT_BEGIN);
  164. while (lex_nexttoken(l) && l->op != LXT_END) {
  165. switch (l->op) {
  166. case LXT_CPU:
  167. case LXT_IF:
  168. case LXT_IO:
  169. case LXT_MEM:
  170. case LXT_PF:
  171. case LXT_MBUF:
  172. case LXT_DEBUG:
  173. case LXT_PROC:
  174. case LXT_SENSOR:
  175. st = token2type(l->op);
  176. strncpy(&sn[0], l->token, _POSIX2_LINE_MAX);
  177. /* parse arg */
  178. lex_nexttoken(l);
  179. if (l->op == LXT_OPEN) {
  180. lex_nexttoken(l);
  181. if (l->op == LXT_CLOSE) {
  182. parse_error(l, "<stream argument>");
  183. return 0;
  184. }
  185. strncpy(&sa[0], l->token, _POSIX2_LINE_MAX);
  186. lex_nexttoken(l);
  187. if (l->op != LXT_CLOSE) {
  188. parse_error(l, ")");
  189. return 0;
  190. }
  191. }
  192. else {
  193. lex_ungettoken(l);
  194. sa[0] = '\0';
  195. }
  196. if ((stream = add_source_stream(source, st, sa)) == NULL) {
  197. warning("%s:%d: stream %s(%s) redefined",
  198. l->filename, l->cline, sn, sa);
  199. return 0;
  200. }
  201. break; /* LXT_CPU/IF/IO/MEM/PF/MBUF/DEBUG/PROC */
  202. case LXT_COMMA:
  203. break;
  204. default:
  205. parse_error(l, "{cpu|mem|if|io|pf|debug|mbuf|proc|sensor}");
  206. return 0;
  207. break;
  208. }
  209. }
  210. break; /* LXT_ACCEPT */
  211. /* datadir "path" */
  212. case LXT_DATADIR:
  213. lex_nexttoken(l);
  214. /* is path absolute */
  215. if (l->token && l->token[0] != '/') {
  216. warning("%s:%d: datadir path '%s' is not absolute",
  217. l->filename, l->cline, l->token);
  218. return 0;
  219. }
  220. /* make sure that directory exists */
  221. bzero(&sb, sizeof(struct stat));
  222. if (stat(l->token, &sb) == 0) {
  223. if (!(sb.st_mode & S_IFDIR)) {
  224. warning("%s:%d: datadir path '%s' is not a directory",
  225. l->filename, l->cline, l->token);
  226. return 0;
  227. }
  228. }
  229. else {
  230. warning("%s:%d: could not stat datadir path '%s'",
  231. l->filename, l->cline, l->token);
  232. return 0;
  233. }
  234. strncpy(&path[0], l->token, _POSIX2_LINE_MAX);
  235. path[_POSIX2_LINE_MAX - 1] = '\0';
  236. pc = strlen(path);
  237. if (path[pc - 1] == '/') {
  238. path[pc - 1] = '\0';
  239. pc--;
  240. }
  241. /* add path to empty streams */
  242. SLIST_FOREACH(stream, &source->sl, streams) {
  243. if (stream->file == NULL) {
  244. if (!(insert_filename(&path[pc],
  245. _POSIX2_LINE_MAX - pc,
  246. stream->type,
  247. stream->args))) {
  248. if (stream->args && strlen(stream->args)) {
  249. warning("%s:%d: failed to construct stream "
  250. "%s(%s) filename using datadir '%s'",
  251. l->filename, l->cline,
  252. type2str(stream->type),
  253. stream->args, l->token);
  254. }
  255. else {
  256. warning("%s:%d: failed to construct stream "
  257. "%s) filename using datadir '%s'",
  258. l->filename, l->cline,
  259. type2str(stream->type),
  260. l->token);
  261. }
  262. return 0;
  263. }
  264. /* try filename */
  265. if ((fd = open(path, O_RDWR | O_NONBLOCK, 0)) == -1) {
  266. warning("%s:%d: file '%s', guessed by datadir, cannot be opened",
  267. l->filename, l->cline, path);
  268. return 0;
  269. }
  270. else {
  271. close(fd);
  272. stream->file = xstrdup(path);
  273. }
  274. }
  275. }
  276. break; /* LXT_DATADIR */
  277. /* write cpu(0) in "filename" */
  278. case LXT_WRITE:
  279. lex_nexttoken(l);
  280. switch (l->op) {
  281. case LXT_CPU:
  282. case LXT_IF:
  283. case LXT_IO:
  284. case LXT_MEM:
  285. case LXT_PF:
  286. case LXT_MBUF:
  287. case LXT_DEBUG:
  288. case LXT_PROC:
  289. case LXT_SENSOR:
  290. st = token2type(l->op);
  291. strncpy(&sn[0], l->token, _POSIX2_LINE_MAX);
  292. /* parse arg */
  293. lex_nexttoken(l);
  294. if (l->op == LXT_OPEN) {
  295. lex_nexttoken(l);
  296. if (l->op == LXT_CLOSE) {
  297. parse_error(l, "<stream argument>");
  298. return 0;
  299. }
  300. strncpy(&sa[0], l->token, _POSIX2_LINE_MAX);
  301. lex_nexttoken(l);
  302. if (l->op != LXT_CLOSE) {
  303. parse_error(l, ")");
  304. return 0;
  305. }
  306. }
  307. else {
  308. lex_ungettoken(l);
  309. sa[0] = '\0';
  310. }
  311. EXPECT(l, LXT_IN);
  312. lex_nexttoken(l);
  313. if ((stream = find_source_stream(source, st, sa)) == NULL) {
  314. if (strlen(sa)) {
  315. warning("%s:%d: stream %s(%s) is not accepted for %s",
  316. l->filename, l->cline, sn, sa, source->addr);
  317. return 0;
  318. }
  319. else {
  320. warning("%s:%d: stream %s is not accepted for %s",
  321. l->filename, l->cline, sn, source->addr);
  322. return 0;
  323. }
  324. }
  325. else {
  326. /* try filename */
  327. if ((fd = open(l->token, O_RDWR | O_NONBLOCK, 0)) == -1) {
  328. warning("%s:%d: file '%s' cannot be opened",
  329. l->filename, l->cline, l->token);
  330. return 0;
  331. }
  332. else {
  333. close(fd);
  334. if (stream->file != NULL) {
  335. warning("%s:%d: file '%s' overwrites previous definition '%s'",
  336. l->filename, l->cline, l->token, stream->file);
  337. xfree(stream->file);
  338. }
  339. stream->file = xstrdup(l->token);
  340. }
  341. }
  342. break; /* LXT_CPU/IF/IO/MEM/PF/MBUF/DEBUG/PROC/SENSOR */
  343. default:
  344. parse_error(l, "{cpu|if|io|mem|pf|mbuf|debug|proc|sensor}");
  345. return 0;
  346. break;
  347. }
  348. break; /* LXT_WRITE */
  349. case LXT_END:
  350. return 1;
  351. default:
  352. parse_error(l, "accept|write");
  353. return 0;
  354. }
  355. }
  356. warning("%s:%d: missing close brace on source statement",
  357. l->filename, l->cline);
  358. return 0;
  359. }
  360. /* Read symux.conf */
  361. int
  362. read_config_file(struct muxlist * mul, const char *filename)
  363. {
  364. struct lex *l;
  365. struct source *source;
  366. struct stream *stream;
  367. struct mux *mux;
  368. struct sourcelist sol;
  369. SLIST_INIT(mul);
  370. SLIST_INIT(&sol);
  371. if ((l = open_lex(filename)) == NULL)
  372. return 0;
  373. while (lex_nexttoken(l)) {
  374. /* expecting keyword now */
  375. switch (l->op) {
  376. case LXT_MUX:
  377. if (!read_mux(mul, l)) {
  378. free_sourcelist(&sol);
  379. return 0;
  380. }
  381. break;
  382. case LXT_SOURCE:
  383. if (!read_source(&sol, l)) {
  384. free_sourcelist(&sol);
  385. return 0;
  386. }
  387. break;
  388. default:
  389. parse_error(l, "mux|source");
  390. free_sourcelist(&sol);
  391. return 0;
  392. break;
  393. }
  394. }
  395. /* sanity checks */
  396. if (SLIST_EMPTY(mul)) {
  397. free_sourcelist(&sol);
  398. warning("%s: no mux statement seen",
  399. l->filename);
  400. return 0;
  401. }
  402. else {
  403. mux = SLIST_FIRST(mul);
  404. mux->sol = sol;
  405. if (strncmp(SYMON_UNKMUX, mux->name, sizeof(SYMON_UNKMUX)) == 0) {
  406. /* mux was not initialised for some reason */
  407. return 0;
  408. }
  409. }
  410. if (SLIST_EMPTY(&sol)) {
  411. warning("%s: no source section seen",
  412. l->filename);
  413. return 0;
  414. }
  415. else {
  416. SLIST_FOREACH(source, &sol, sources) {
  417. if (SLIST_EMPTY(&source->sl)) {
  418. warning("%s: no streams accepted for source '%s'",
  419. l->filename, source->addr);
  420. return 0;
  421. }
  422. else {
  423. SLIST_FOREACH(stream, &source->sl, streams) {
  424. if (stream->file == NULL) {
  425. warning("%s: no filename specified for stream '%s(%s)' in source '%s'",
  426. l->filename, type2str(stream->type), stream->args, source->addr);
  427. }
  428. }
  429. }
  430. }
  431. }
  432. close_lex(l);
  433. return 1;
  434. }