readconf.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /* $Id: readconf.c,v 1.28 2007/12/11 14:17:59 dijkstra Exp $ */
  2. /*
  3. * Copyright (c) 2001-2005 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. /* <hostname> (port|:|,| ) <number> */
  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 "<cpu(arg)|df(arg)|mem|if(arg)|io(arg)|debug|pf|pfq(arg)|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_DF:
  90. case LXT_IF:
  91. case LXT_IF1:
  92. case LXT_IO:
  93. case LXT_IO1:
  94. case LXT_MEM:
  95. case LXT_MEM1:
  96. case LXT_PF:
  97. case LXT_PFQ:
  98. case LXT_MBUF:
  99. case LXT_DEBUG:
  100. case LXT_PROC:
  101. case LXT_SENSOR:
  102. st = token2type(l->op);
  103. strncpy(&sn[0], l->token, _POSIX2_LINE_MAX);
  104. /* parse arg */
  105. lex_nexttoken(l);
  106. if (l->op == LXT_OPEN) {
  107. lex_nexttoken(l);
  108. if (l->op == LXT_CLOSE) {
  109. parse_error(l, "<stream argument>");
  110. return 0;
  111. }
  112. strncpy(&sa[0], l->token, _POSIX2_LINE_MAX);
  113. lex_nexttoken(l);
  114. if (l->op != LXT_CLOSE) {
  115. parse_error(l, ")");
  116. return 0;
  117. }
  118. } else {
  119. lex_ungettoken(l);
  120. sa[0] = '\0';
  121. }
  122. if (strlen(sa) > (SYMON_PS_ARGLENV2 - 1)) {
  123. warning("%.200s:%d: argument '%.200s' too long for network format, "
  124. "will send leading " SYMON_PS_ARGLENSTRV2 " chars only",
  125. l->filename, l->cline, sa);
  126. }
  127. if ((add_mux_stream(mux, st, sa)) == NULL) {
  128. warning("%.200s:%d: stream %.200s(%.200s) redefined",
  129. l->filename, l->cline, sn, sa);
  130. return 0;
  131. }
  132. break;
  133. case LXT_COMMA:
  134. break;
  135. default:
  136. parse_error(l, "{cpu|df|if|if1|io|io1|mem|mem1|pf|pfq|mbuf|debug|proc|sensor}");
  137. return 0;
  138. break;
  139. }
  140. }
  141. return 1;
  142. }
  143. /* parse monitor <args> stream [from <host>] [to] <host>:<port> */
  144. int
  145. read_monitor(struct muxlist * mul, struct lex * l)
  146. {
  147. struct mux *mux;
  148. mux = add_mux(mul, SYMON_UNKMUX);
  149. /* parse [stream(streamarg)]+ */
  150. if (!read_symon_args(mux, l))
  151. return 0;
  152. lex_nexttoken(l);
  153. /* parse [every x seconds]? */
  154. if (l->op == LXT_EVERY) {
  155. lex_nexttoken(l);
  156. if (l->op == LXT_SECOND) {
  157. symon_interval = 1;
  158. } else if (l->type == LXY_NUMBER) {
  159. symon_interval = l->value;
  160. lex_nexttoken(l);
  161. if (l->op != LXT_SECONDS) {
  162. parse_error(l, "seconds");
  163. }
  164. } else {
  165. parse_error(l, "<number> ");
  166. return 0;
  167. }
  168. lex_nexttoken(l);
  169. }
  170. /* parse [stream [from <host>] to] */
  171. if (l->op != LXT_STREAM) {
  172. parse_error(l, "stream");
  173. return 0;
  174. }
  175. lex_nexttoken(l);
  176. if (l->op == LXT_FROM) {
  177. lex_nexttoken(l);
  178. /* check to see host is resolvable, result is discarded */
  179. if (!getip(l->token, AF_INET) && !getip(l->token, AF_INET6)) {
  180. warning("%.200s:%d: could not resolve '%.200s'",
  181. l->filename, l->cline, l->token);
  182. return 0;
  183. }
  184. mux->localaddr = xstrdup(l->token);
  185. lex_nexttoken(l);
  186. }
  187. if (l->op != LXT_TO)
  188. lex_ungettoken(l);
  189. /* parse [host [port]?] */
  190. return read_host_port(mul, mux, l);
  191. }
  192. /* Read symon.conf */
  193. int
  194. read_config_file(struct muxlist *muxlist, char *filename)
  195. {
  196. struct lex *l;
  197. struct mux *mux;
  198. SLIST_INIT(muxlist);
  199. l = open_lex(filename);
  200. while (lex_nexttoken(l)) {
  201. /* expecting keyword now */
  202. switch (l->op) {
  203. case LXT_MONITOR:
  204. if (!read_monitor(muxlist, l))
  205. return 0;
  206. break;
  207. default:
  208. parse_error(l, "monitor");
  209. return 0;
  210. break;
  211. }
  212. }
  213. /* sanity checks */
  214. SLIST_FOREACH(mux, muxlist, muxes) {
  215. if (strncmp(SYMON_UNKMUX, mux->name, sizeof(SYMON_UNKMUX)) == 0) {
  216. /* mux was not initialised for some reason */
  217. return 0;
  218. }
  219. if (SLIST_EMPTY(&mux->sl)) {
  220. warning("%.200s: no monitors selected for mux '%.200s'",
  221. l->filename, mux->name);
  222. return 0;
  223. }
  224. }
  225. if (symon_interval < SYMON_DEFAULT_INTERVAL) {
  226. warning("%.200s: monitoring set to every %d s", l->filename, symon_interval);
  227. }
  228. close_lex(l);
  229. return 1;
  230. }