readconf.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * Copyright (c) 2001-2010 Willem Dijkstra
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * - Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * - Redistributions in binary form must reproduce the above
  12. * copyright notice, this list of conditions and the following
  13. * disclaimer in the documentation and/or other materials provided
  14. * with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  19. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  20. * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  22. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  26. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. * POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. */
  30. #include <sys/types.h>
  31. #include <string.h>
  32. #include "conf.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. /* parse "(ip4addr | ip6addr | hostname) [['port' | ',' ] portnumber]" */
  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, AF_INET) && !getip(l->token, AF_INET6)) {
  52. warning("%.200s:%d: could not resolve '%.200s'",
  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. mux->port = xstrdup(default_symux_port);
  60. else {
  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. }
  70. bzero(&muxname, sizeof(muxname));
  71. snprintf(&muxname[0], sizeof(muxname), "%s %s (%ds)", mux->addr, mux->port, mux->interval);
  72. if (rename_mux(mul, mux, muxname) == NULL) {
  73. warning("%.200s:%d: monitored data for host '%.200s' has already been specified",
  74. l->filename, l->cline, muxname);
  75. return 0;
  76. }
  77. return 1;
  78. }
  79. /* parse "resource version ['(' argument ')']", end condition == '}' */
  80. int
  81. read_symon_args(struct mux * mux, struct lex * l)
  82. {
  83. char sn[_POSIX2_LINE_MAX];
  84. char sa[_POSIX2_LINE_MAX];
  85. int st;
  86. EXPECT(l, LXT_BEGIN)
  87. while (lex_nexttoken(l) && l->op != LXT_END) {
  88. switch (l->op) {
  89. case LXT_CPU:
  90. case LXT_CPUIOW:
  91. case LXT_DEBUG:
  92. case LXT_DF:
  93. case LXT_IF1:
  94. case LXT_IF:
  95. case LXT_IO1:
  96. case LXT_IO:
  97. case LXT_MBUF:
  98. case LXT_MEM1:
  99. case LXT_MEM:
  100. case LXT_PF:
  101. case LXT_PFQ:
  102. case LXT_PROC:
  103. case LXT_SENSOR:
  104. case LXT_SMART:
  105. case LXT_LOAD:
  106. st = token2type(l->op);
  107. strncpy(&sn[0], l->token, _POSIX2_LINE_MAX);
  108. /* parse arg */
  109. lex_nexttoken(l);
  110. if (l->op == LXT_OPEN) {
  111. lex_nexttoken(l);
  112. if (l->op == LXT_CLOSE) {
  113. parse_error(l, "<stream argument>");
  114. return 0;
  115. }
  116. strncpy(&sa[0], l->token, _POSIX2_LINE_MAX);
  117. lex_nexttoken(l);
  118. if (l->op != LXT_CLOSE) {
  119. parse_error(l, ")");
  120. return 0;
  121. }
  122. } else {
  123. lex_ungettoken(l);
  124. sa[0] = '\0';
  125. }
  126. if (strlen(sa) > (SYMON_PS_ARGLENV2 - 1)) {
  127. warning("%.200s:%d: argument '%.200s' too long for network format, "
  128. "will send leading " SYMON_PS_ARGLENSTRV2 " chars only",
  129. l->filename, l->cline, sa);
  130. }
  131. if ((add_mux_stream(mux, st, sa)) == NULL) {
  132. warning("%.200s:%d: stream %.200s(%.200s) redefined",
  133. l->filename, l->cline, sn, sa);
  134. return 0;
  135. }
  136. break;
  137. case LXT_COMMA:
  138. break;
  139. default:
  140. parse_error(l, "{cpu|cpuiow|df|if|if1|io|io1|load|mem|mem1|pf|pfq|mbuf|debug|proc|sensor|smart}");
  141. return 0;
  142. break;
  143. }
  144. }
  145. return 1;
  146. }
  147. /* parse "'monitor' '{' resources '}' ['every' time ] 'stream' ['from' host]
  148. * ['to'] host [port]" */
  149. int
  150. read_monitor(struct muxlist * mul, struct lex * l)
  151. {
  152. struct mux *mux;
  153. mux = add_mux(mul, SYMON_UNKMUX);
  154. /* parse [stream(streamarg)]+ */
  155. if (!read_symon_args(mux, l))
  156. return 0;
  157. lex_nexttoken(l);
  158. /* parse [every x seconds]? */
  159. if (l->op == LXT_EVERY) {
  160. lex_nexttoken(l);
  161. if (l->op == LXT_SECOND) {
  162. mux->interval = 1;
  163. } else if (l->type == LXY_NUMBER) {
  164. mux->interval = l->value;
  165. lex_nexttoken(l);
  166. if (l->op != LXT_SECONDS) {
  167. parse_error(l, "seconds");
  168. }
  169. } else {
  170. parse_error(l, "<number> ");
  171. return 0;
  172. }
  173. lex_nexttoken(l);
  174. } else
  175. mux->interval = SYMON_DEFAULT_INTERVAL;
  176. /* parse [stream [from <host>] to] */
  177. if (l->op != LXT_STREAM) {
  178. parse_error(l, "stream");
  179. return 0;
  180. }
  181. lex_nexttoken(l);
  182. if (l->op == LXT_FROM) {
  183. lex_nexttoken(l);
  184. /* check to see host is resolvable, result is discarded */
  185. if (!getip(l->token, AF_INET) && !getip(l->token, AF_INET6)) {
  186. warning("%.200s:%d: could not resolve '%.200s'",
  187. l->filename, l->cline, l->token);
  188. return 0;
  189. }
  190. mux->localaddr = xstrdup(l->token);
  191. lex_nexttoken(l);
  192. }
  193. if (l->op != LXT_TO)
  194. lex_ungettoken(l);
  195. /* parse [host [port]?] */
  196. return read_host_port(mul, mux, l);
  197. }
  198. /* Read symon.conf */
  199. int
  200. read_config_file(struct muxlist *muxlist, char *filename)
  201. {
  202. struct lex *l;
  203. struct mux *mux;
  204. SLIST_INIT(muxlist);
  205. l = open_lex(filename);
  206. while (lex_nexttoken(l)) {
  207. /* expecting keyword now */
  208. switch (l->op) {
  209. case LXT_MONITOR:
  210. if (!read_monitor(muxlist, l))
  211. return 0;
  212. break;
  213. default:
  214. parse_error(l, "monitor");
  215. return 0;
  216. break;
  217. }
  218. }
  219. /* sanity checks */
  220. SLIST_FOREACH(mux, muxlist, muxes) {
  221. if (strncmp(SYMON_UNKMUX, mux->name, sizeof(SYMON_UNKMUX)) == 0) {
  222. /* mux was not initialised for some reason */
  223. return 0;
  224. }
  225. if (SLIST_EMPTY(&mux->sl)) {
  226. warning("%.200s: no monitors selected for mux '%.200s'",
  227. l->filename, mux->name);
  228. return 0;
  229. }
  230. if (mux->interval < SYMON_DEFAULT_INTERVAL) {
  231. warning("%.200s: monitoring set to every %d s", l->filename, mux->interval);
  232. }
  233. }
  234. close_lex(l);
  235. return 1;
  236. }