readconf.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /* $Id: readconf.c,v 1.13 2002/12/15 14:23:27 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 <string.h>
  33. #include "data.h"
  34. #include "error.h"
  35. #include "lex.h"
  36. #include "net.h"
  37. #include "symon.h"
  38. #include "xmalloc.h"
  39. __BEGIN_DECLS
  40. int read_host_port(struct muxlist *, struct mux *, struct lex *);
  41. int read_symon_args(struct mux *, struct lex *);
  42. int read_monitor(struct muxlist *, struct lex *);
  43. __END_DECLS
  44. const char *default_symux_port = SYMUX_PORT;
  45. /* <hostname> (port|:|,| ) <number> */
  46. int
  47. read_host_port(struct muxlist * mul, struct mux * mux, struct lex * l)
  48. {
  49. char muxname[_POSIX2_LINE_MAX];
  50. lex_nexttoken(l);
  51. if (!getip(l->token)) {
  52. warning("%s:%d: could not resolve '%s'",
  53. l->filename, l->cline, l->token);
  54. return 0;
  55. }
  56. mux->addr = xstrdup((const char *) &res_host);
  57. /* check for port statement */
  58. if (!lex_nexttoken(l))
  59. return 1;
  60. if (l->op == LXT_PORT || l->op == LXT_COMMA)
  61. lex_nexttoken(l);
  62. if (l->type != LXY_NUMBER) {
  63. lex_ungettoken(l);
  64. mux->port = xstrdup(default_symux_port);
  65. }
  66. else {
  67. mux->port = xstrdup((const char *) l->token);
  68. }
  69. bzero(&muxname, sizeof(muxname));
  70. snprintf(&muxname[0], sizeof(muxname), "%s %s", mux->addr, mux->port);
  71. if (rename_mux(mul, mux, muxname) == NULL) {
  72. warning("%s:%d: monitored data for host '%s' has already been specified",
  73. l->filename, l->cline, muxname);
  74. return 0;
  75. }
  76. return 1;
  77. }
  78. /* parse "<cpu(arg)|mem|if(arg)|io(arg)|debug|pf|proc(arg)>", end condition == "}" */
  79. int
  80. read_symon_args(struct mux * mux, struct lex * l)
  81. {
  82. char sn[_POSIX2_LINE_MAX];
  83. char sa[_POSIX2_LINE_MAX];
  84. int st;
  85. EXPECT(l, LXT_BEGIN)
  86. while (lex_nexttoken(l) && l->op != LXT_END) {
  87. switch (l->op) {
  88. case LXT_CPU:
  89. case LXT_IF:
  90. case LXT_IO:
  91. case LXT_MEM:
  92. case LXT_PF:
  93. case LXT_MBUF:
  94. case LXT_DEBUG:
  95. case LXT_PROC:
  96. st = token2type(l->op);
  97. strncpy(&sn[0], l->token, _POSIX2_LINE_MAX);
  98. /* parse arg */
  99. lex_nexttoken(l);
  100. if (l->op == LXT_OPEN) {
  101. lex_nexttoken(l);
  102. if (l->op == LXT_CLOSE) {
  103. parse_error(l, "<stream argument>");
  104. return 0;
  105. }
  106. strncpy(&sa[0], l->token, _POSIX2_LINE_MAX);
  107. lex_nexttoken(l);
  108. if (l->op != LXT_CLOSE) {
  109. parse_error(l, ")");
  110. return 0;
  111. }
  112. }
  113. else {
  114. lex_ungettoken(l);
  115. sa[0] = '\0';
  116. }
  117. if ((add_mux_stream(mux, st, sa)) == NULL) {
  118. warning("%s:%d: stream %s(%s) redefined",
  119. l->filename, l->cline, sn, sa);
  120. return 0;
  121. }
  122. break; /* LXT_CPU/IF/IO/MEM/PF/MBUF/DEBUG/PROC */
  123. case LXT_COMMA:
  124. break;
  125. default:
  126. parse_error(l, "{cpu|mem|if|io|pf|debug}");
  127. return 0;
  128. break;
  129. }
  130. }
  131. return 1;
  132. }
  133. /* parse monitor <args> stream [to] <host>:<port> */
  134. int
  135. read_monitor(struct muxlist * mul, struct lex * l)
  136. {
  137. struct mux *mux;
  138. mux = add_mux(mul, SYMON_UNKMUX);
  139. /* parse cpu|mem|if|io */
  140. if (!read_symon_args(mux, l))
  141. return 0;
  142. /* parse stream to */
  143. EXPECT(l, LXT_STREAM);
  144. lex_nexttoken(l);
  145. if (l->op != LXT_TO)
  146. lex_ungettoken(l);
  147. /* parse host */
  148. return read_host_port(mul, mux, l);
  149. }
  150. /* Read symon.conf */
  151. int
  152. read_config_file(struct muxlist * muxlist, const char *filename)
  153. {
  154. struct lex *l;
  155. struct mux *mux;
  156. SLIST_INIT(muxlist);
  157. if ((l = open_lex(filename)) == NULL)
  158. return 0;
  159. while (lex_nexttoken(l)) {
  160. /* expecting keyword now */
  161. switch (l->op) {
  162. case LXT_MONITOR:
  163. if (!read_monitor(muxlist, l))
  164. return 0;
  165. break;
  166. default:
  167. parse_error(l, "monitor");
  168. return 0;
  169. break;
  170. }
  171. }
  172. /* sanity checks */
  173. SLIST_FOREACH(mux, muxlist, muxes) {
  174. if (strncmp(SYMON_UNKMUX, mux->name, sizeof(SYMON_UNKMUX)) == 0) {
  175. /* mux was not initialised for some reason */
  176. return 0;
  177. }
  178. if (SLIST_EMPTY(&mux->sl)) {
  179. warning("%s: no monitors selected for mux '%s'",
  180. l->filename, mux->name);
  181. return 0;
  182. }
  183. }
  184. close_lex(l);
  185. return 1;
  186. }