readconf.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /* $Id: readconf.c,v 1.14 2003/06/20 08:41:19 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. case LXT_SENSOR:
  97. st = token2type(l->op);
  98. strncpy(&sn[0], l->token, _POSIX2_LINE_MAX);
  99. /* parse arg */
  100. lex_nexttoken(l);
  101. if (l->op == LXT_OPEN) {
  102. lex_nexttoken(l);
  103. if (l->op == LXT_CLOSE) {
  104. parse_error(l, "<stream argument>");
  105. return 0;
  106. }
  107. strncpy(&sa[0], l->token, _POSIX2_LINE_MAX);
  108. lex_nexttoken(l);
  109. if (l->op != LXT_CLOSE) {
  110. parse_error(l, ")");
  111. return 0;
  112. }
  113. }
  114. else {
  115. lex_ungettoken(l);
  116. sa[0] = '\0';
  117. }
  118. if ((add_mux_stream(mux, st, sa)) == NULL) {
  119. warning("%s:%d: stream %s(%s) redefined",
  120. l->filename, l->cline, sn, sa);
  121. return 0;
  122. }
  123. break; /* LXT_CPU/IF/IO/MEM/PF/MBUF/DEBUG/PROC/SENSOR */
  124. case LXT_COMMA:
  125. break;
  126. default:
  127. parse_error(l, "{cpu|mem|if|io|pf|debug|sensor}");
  128. return 0;
  129. break;
  130. }
  131. }
  132. return 1;
  133. }
  134. /* parse monitor <args> stream [to] <host>:<port> */
  135. int
  136. read_monitor(struct muxlist * mul, struct lex * l)
  137. {
  138. struct mux *mux;
  139. mux = add_mux(mul, SYMON_UNKMUX);
  140. /* parse cpu|mem|if|io */
  141. if (!read_symon_args(mux, l))
  142. return 0;
  143. /* parse stream to */
  144. EXPECT(l, LXT_STREAM);
  145. lex_nexttoken(l);
  146. if (l->op != LXT_TO)
  147. lex_ungettoken(l);
  148. /* parse host */
  149. return read_host_port(mul, mux, l);
  150. }
  151. /* Read symon.conf */
  152. int
  153. read_config_file(struct muxlist * muxlist, const char *filename)
  154. {
  155. struct lex *l;
  156. struct mux *mux;
  157. SLIST_INIT(muxlist);
  158. if ((l = open_lex(filename)) == NULL)
  159. return 0;
  160. while (lex_nexttoken(l)) {
  161. /* expecting keyword now */
  162. switch (l->op) {
  163. case LXT_MONITOR:
  164. if (!read_monitor(muxlist, l))
  165. return 0;
  166. break;
  167. default:
  168. parse_error(l, "monitor");
  169. return 0;
  170. break;
  171. }
  172. }
  173. /* sanity checks */
  174. SLIST_FOREACH(mux, muxlist, muxes) {
  175. if (strncmp(SYMON_UNKMUX, mux->name, sizeof(SYMON_UNKMUX)) == 0) {
  176. /* mux was not initialised for some reason */
  177. return 0;
  178. }
  179. if (SLIST_EMPTY(&mux->sl)) {
  180. warning("%s: no monitors selected for mux '%s'",
  181. l->filename, mux->name);
  182. return 0;
  183. }
  184. }
  185. close_lex(l);
  186. return 1;
  187. }