readconf.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /* $Id: readconf.c,v 1.29 2008/01/30 12:06:50 dijkstra Exp $ */
  2. /*
  3. * Copyright (c) 2001-2008 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/types.h>
  32. #include <string.h>
  33. #include "conf.h"
  34. #include "data.h"
  35. #include "error.h"
  36. #include "lex.h"
  37. #include "net.h"
  38. #include "symon.h"
  39. #include "xmalloc.h"
  40. __BEGIN_DECLS
  41. int read_host_port(struct muxlist *, struct mux *, struct lex *);
  42. int read_symon_args(struct mux *, struct lex *);
  43. int read_monitor(struct muxlist *, struct lex *);
  44. __END_DECLS
  45. const char *default_symux_port = SYMUX_PORT;
  46. /* parse "(ip4addr | ip6addr | hostname) [['port' | ',' ] portnumber]" */
  47. int
  48. read_host_port(struct muxlist * mul, struct mux * mux, struct lex * l)
  49. {
  50. char muxname[_POSIX2_LINE_MAX];
  51. lex_nexttoken(l);
  52. if (!getip(l->token, AF_INET) && !getip(l->token, AF_INET6)) {
  53. warning("%.200s:%d: could not resolve '%.200s'",
  54. l->filename, l->cline, l->token);
  55. return 0;
  56. }
  57. mux->addr = xstrdup((const char *) &res_host);
  58. /* check for port statement */
  59. if (!lex_nexttoken(l))
  60. return 1;
  61. if (l->op == LXT_PORT || l->op == LXT_COMMA)
  62. lex_nexttoken(l);
  63. if (l->type != LXY_NUMBER) {
  64. lex_ungettoken(l);
  65. mux->port = xstrdup(default_symux_port);
  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("%.200s:%d: monitored data for host '%.200s' has already been specified",
  73. l->filename, l->cline, muxname);
  74. return 0;
  75. }
  76. return 1;
  77. }
  78. /* parse "resource version ['(' argument ')']", 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_CPUIOW:
  90. case LXT_DEBUG:
  91. case LXT_DF:
  92. case LXT_IF1:
  93. case LXT_IF:
  94. case LXT_IO1:
  95. case LXT_IO:
  96. case LXT_MBUF:
  97. case LXT_MEM1:
  98. case LXT_MEM:
  99. case LXT_PF:
  100. case LXT_PFQ:
  101. case LXT_PROC:
  102. case LXT_SENSOR:
  103. st = token2type(l->op);
  104. strncpy(&sn[0], l->token, _POSIX2_LINE_MAX);
  105. /* parse arg */
  106. lex_nexttoken(l);
  107. if (l->op == LXT_OPEN) {
  108. lex_nexttoken(l);
  109. if (l->op == LXT_CLOSE) {
  110. parse_error(l, "<stream argument>");
  111. return 0;
  112. }
  113. strncpy(&sa[0], l->token, _POSIX2_LINE_MAX);
  114. lex_nexttoken(l);
  115. if (l->op != LXT_CLOSE) {
  116. parse_error(l, ")");
  117. return 0;
  118. }
  119. } else {
  120. lex_ungettoken(l);
  121. sa[0] = '\0';
  122. }
  123. if (strlen(sa) > (SYMON_PS_ARGLENV2 - 1)) {
  124. warning("%.200s:%d: argument '%.200s' too long for network format, "
  125. "will send leading " SYMON_PS_ARGLENSTRV2 " chars only",
  126. l->filename, l->cline, sa);
  127. }
  128. if ((add_mux_stream(mux, st, sa)) == NULL) {
  129. warning("%.200s:%d: stream %.200s(%.200s) redefined",
  130. l->filename, l->cline, sn, sa);
  131. return 0;
  132. }
  133. break;
  134. case LXT_COMMA:
  135. break;
  136. default:
  137. parse_error(l, "{cpu|cpuiow|df|if|if1|io|io1|mem|mem1|pf|pfq|mbuf|debug|proc|sensor}");
  138. return 0;
  139. break;
  140. }
  141. }
  142. return 1;
  143. }
  144. /* parse "'monitor' '{' resources '}' ['every' time ] 'stream' ['from' host]
  145. * ['to'] host [port]" */
  146. int
  147. read_monitor(struct muxlist * mul, struct lex * l)
  148. {
  149. struct mux *mux;
  150. mux = add_mux(mul, SYMON_UNKMUX);
  151. /* parse [stream(streamarg)]+ */
  152. if (!read_symon_args(mux, l))
  153. return 0;
  154. lex_nexttoken(l);
  155. /* parse [every x seconds]? */
  156. if (l->op == LXT_EVERY) {
  157. lex_nexttoken(l);
  158. if (l->op == LXT_SECOND) {
  159. symon_interval = 1;
  160. } else if (l->type == LXY_NUMBER) {
  161. symon_interval = l->value;
  162. lex_nexttoken(l);
  163. if (l->op != LXT_SECONDS) {
  164. parse_error(l, "seconds");
  165. }
  166. } else {
  167. parse_error(l, "<number> ");
  168. return 0;
  169. }
  170. lex_nexttoken(l);
  171. }
  172. /* parse [stream [from <host>] to] */
  173. if (l->op != LXT_STREAM) {
  174. parse_error(l, "stream");
  175. return 0;
  176. }
  177. lex_nexttoken(l);
  178. if (l->op == LXT_FROM) {
  179. lex_nexttoken(l);
  180. /* check to see host is resolvable, result is discarded */
  181. if (!getip(l->token, AF_INET) && !getip(l->token, AF_INET6)) {
  182. warning("%.200s:%d: could not resolve '%.200s'",
  183. l->filename, l->cline, l->token);
  184. return 0;
  185. }
  186. mux->localaddr = xstrdup(l->token);
  187. lex_nexttoken(l);
  188. }
  189. if (l->op != LXT_TO)
  190. lex_ungettoken(l);
  191. /* parse [host [port]?] */
  192. return read_host_port(mul, mux, l);
  193. }
  194. /* Read symon.conf */
  195. int
  196. read_config_file(struct muxlist *muxlist, char *filename)
  197. {
  198. struct lex *l;
  199. struct mux *mux;
  200. SLIST_INIT(muxlist);
  201. l = open_lex(filename);
  202. while (lex_nexttoken(l)) {
  203. /* expecting keyword now */
  204. switch (l->op) {
  205. case LXT_MONITOR:
  206. if (!read_monitor(muxlist, l))
  207. return 0;
  208. break;
  209. default:
  210. parse_error(l, "monitor");
  211. return 0;
  212. break;
  213. }
  214. }
  215. /* sanity checks */
  216. SLIST_FOREACH(mux, muxlist, muxes) {
  217. if (strncmp(SYMON_UNKMUX, mux->name, sizeof(SYMON_UNKMUX)) == 0) {
  218. /* mux was not initialised for some reason */
  219. return 0;
  220. }
  221. if (SLIST_EMPTY(&mux->sl)) {
  222. warning("%.200s: no monitors selected for mux '%.200s'",
  223. l->filename, mux->name);
  224. return 0;
  225. }
  226. }
  227. if (symon_interval < SYMON_DEFAULT_INTERVAL) {
  228. warning("%.200s: monitoring set to every %d s", l->filename, symon_interval);
  229. }
  230. close_lex(l);
  231. return 1;
  232. }