readconf.c 7.5 KB

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