symux.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /* $Id: symux.c,v 1.27 2003/12/20 16:30:44 dijkstra Exp $ */
  2. /*
  3. * Copyright (c) 2001-2003 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 <sys/socket.h>
  33. #include <netinet/in.h>
  34. #include <netdb.h>
  35. #include <machine/endian.h>
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #include <sysexits.h>
  40. #include <syslog.h>
  41. #include <unistd.h>
  42. #include <rrd.h>
  43. #include "data.h"
  44. #include "error.h"
  45. #include "limits.h"
  46. #include "symux.h"
  47. #include "symuxnet.h"
  48. #include "net.h"
  49. #include "readconf.h"
  50. #include "share.h"
  51. #include "xmalloc.h"
  52. __BEGIN_DECLS
  53. void exithandler();
  54. void huphandler(int);
  55. void signalhandler(int);
  56. __END_DECLS
  57. int flag_hup = 0;
  58. fd_set fdset;
  59. int maxfd;
  60. void
  61. exithandler(int s)
  62. {
  63. info("received signal %d - quitting", s);
  64. exit(EX_TEMPFAIL);
  65. }
  66. void
  67. huphandler(int s)
  68. {
  69. info("hup received");
  70. flag_hup = 1;
  71. }
  72. /*
  73. * symux is the receiver of symon performance measurements.
  74. *
  75. * The main goals symon hopes to accomplish is:
  76. * - to take fine grained measurements of system parameters
  77. * - with minimal performance impact
  78. * - in a secure way.
  79. *
  80. * Measuring system parameters (e.g. interfaces) sometimes means traversing
  81. * lists in kernel memory. Because of this the measurement of data has been
  82. * decoupled from the processing and storage of data. Storing the measured
  83. * information that symon provides is done by a second program, called symux.
  84. *
  85. * Symon can keep track of cpu, memory, disk and network interface
  86. * interactions. Symon was built specifically for OpenBSD.
  87. */
  88. int
  89. main(int argc, char *argv[])
  90. {
  91. struct symonpacket packet;
  92. struct packedstream ps;
  93. char *cfgfile;
  94. char *cfgpath;
  95. char *stringbuf;
  96. char *stringptr;
  97. int maxstringlen;
  98. struct muxlist mul, newmul;
  99. char *arg_ra[4];
  100. struct stream *stream;
  101. struct source *source;
  102. struct mux *mux;
  103. FILE *f;
  104. int ch;
  105. int offset;
  106. time_t timestamp;
  107. int churnbuflen;
  108. SLIST_INIT(&mul);
  109. /* reset flags */
  110. flag_debug = 0;
  111. flag_daemon = 0;
  112. cfgfile = SYMUX_CONFIG_FILE;
  113. while ((ch = getopt(argc, argv, "dvf:")) != -1) {
  114. switch (ch) {
  115. case 'd':
  116. flag_debug = 1;
  117. break;
  118. case 'f':
  119. if (optarg && optarg[0] != '/') {
  120. /* cfg path needs to be absolute, we will be a daemon soon */
  121. if ((cfgpath = getwd(NULL)) == NULL)
  122. fatal("could not get working directory");
  123. maxstringlen = strlen(cfgpath) + strlen(optarg) + 1;
  124. cfgfile = xmalloc(maxstringlen);
  125. strncpy(cfgfile, cfgpath, maxstringlen);
  126. stringptr = cfgfile + strlen(cfgpath);
  127. stringptr[0] = '/';
  128. stringptr++;
  129. strncpy(stringptr, optarg, maxstringlen - (cfgfile - stringptr));
  130. cfgfile[maxstringlen] = '\0';
  131. free(cfgpath);
  132. } else
  133. cfgfile = xstrdup(optarg);
  134. break;
  135. case 'v':
  136. info("symux version %s", SYMUX_VERSION);
  137. default:
  138. info("usage: %s [-d] [-v] [-f cfgfile]", __progname);
  139. exit(EX_USAGE);
  140. }
  141. }
  142. /* parse configuration file */
  143. if (!read_config_file(&mul, cfgfile))
  144. fatal("configuration contained errors; quitting");
  145. setegid(getgid());
  146. setgid(getgid());
  147. if (flag_debug != 1) {
  148. if (daemon(0, 0) != 0)
  149. fatal("daemonize failed");
  150. flag_daemon = 1;
  151. /* record pid */
  152. f = fopen(SYMUX_PID_FILE, "w");
  153. if (f) {
  154. fprintf(f, "%u\n", (u_int) getpid());
  155. fclose(f);
  156. }
  157. }
  158. info("symux version %s", SYMUX_VERSION);
  159. if (flag_debug == 1)
  160. info("program id=%d", (u_int) getpid());
  161. mux = SLIST_FIRST(&mul);
  162. churnbuflen = calculate_churnbuffer(&mux->sol);
  163. debug("size of churnbuffer = %d", churnbuflen);
  164. initshare(churnbuflen);
  165. /* catch signals */
  166. signal(SIGHUP, huphandler);
  167. signal(SIGINT, exithandler);
  168. signal(SIGQUIT, exithandler);
  169. signal(SIGTERM, exithandler);
  170. signal(SIGTERM, exithandler);
  171. /* prepare crc32 */
  172. init_crc32();
  173. /* prepare sockets */
  174. if (get_symon_sockets(mux) == 0)
  175. fatal("no sockets could be opened for incoming symon traffic");
  176. if (get_client_socket(mux) == 0)
  177. fatal("socket for client connections could not be opened");
  178. /* main loop */
  179. for (;;) { /* FOREVER */
  180. wait_for_traffic(mux, &source, &packet);
  181. if (flag_hup == 1) {
  182. flag_hup = 0;
  183. SLIST_INIT(&newmul);
  184. if (!read_config_file(&newmul, cfgfile)) {
  185. info("new configuration contains errors; keeping old configuration");
  186. free_muxlist(&newmul);
  187. } else {
  188. info("read configuration file '%.100s' succesfully", cfgfile);
  189. free_muxlist(&mul);
  190. mul = newmul;
  191. mux = SLIST_FIRST(&mul);
  192. get_symon_sockets(mux);
  193. get_client_socket(mux);
  194. }
  195. } else {
  196. /*
  197. * Put information from packet into stringbuf (shared region).
  198. * Note that the stringbuf is used twice: 1) to update the
  199. * rrdfile and 2) to collect all the data from a single packet
  200. * that needs to shared to the clients. This is the reason for
  201. * the hasseling with stringptr.
  202. */
  203. offset = mux->offset;
  204. maxstringlen = shared_getmaxlen();
  205. /* put time:ip: into shared region */
  206. master_forbidread();
  207. timestamp = (time_t) packet.header.timestamp;
  208. stringbuf = (char *) shared_getmem();
  209. snprintf(stringbuf, maxstringlen, "%s;", source->addr);
  210. /* hide this string region from rrd update */
  211. maxstringlen -= strlen(stringbuf);
  212. stringptr = stringbuf + strlen(stringbuf);
  213. while (offset < packet.header.length) {
  214. bzero(&ps, sizeof(struct packedstream));
  215. offset += sunpack(packet.data + offset, &ps);
  216. /* find stream in source */
  217. stream = find_source_stream(source, ps.type, ps.args);
  218. if (stream != NULL) {
  219. /* put type in and hide from rrd */
  220. snprintf(stringptr, maxstringlen, "%s:", type2str(ps.type));
  221. maxstringlen -= strlen(stringptr);
  222. stringptr += strlen(stringptr);
  223. /* put arguments in and hide from rrd */
  224. snprintf(stringptr, maxstringlen, "%s:",
  225. ((ps.args == NULL) ? "0" : ps.args));
  226. maxstringlen -= strlen(stringptr);
  227. stringptr += strlen(stringptr);
  228. /* put timestamp in and show to rrd */
  229. snprintf(stringptr, maxstringlen, "%u", timestamp);
  230. arg_ra[3] = stringptr;
  231. maxstringlen -= strlen(stringptr);
  232. stringptr += strlen(stringptr);
  233. /* put measurements in */
  234. ps2strn(&ps, stringptr, maxstringlen, PS2STR_RRD);
  235. if (stream->file != NULL) {
  236. /* clear optind for getopt call by rrdupdate */
  237. optind = 0;
  238. /* save if file specified */
  239. arg_ra[0] = "rrdupdate";
  240. arg_ra[1] = "--";
  241. arg_ra[2] = stream->file;
  242. /*
  243. * This call will cost a lot (symux will become
  244. * unresponsive and eat up massive amounts of cpu) if
  245. * the rrdfile is out of sync.
  246. */
  247. rrd_update(4, arg_ra);
  248. if (rrd_test_error()) {
  249. warning("rrd_update:%.200s", rrd_get_error());
  250. warning("%.200s %.200s %.200s %.200s", arg_ra[0], arg_ra[1],
  251. arg_ra[2], arg_ra[3]);
  252. rrd_clear_error();
  253. } else {
  254. if (flag_debug == 1)
  255. debug("%.200s %.200s %.200s %.200s", arg_ra[0], arg_ra[1],
  256. arg_ra[2], arg_ra[3]);
  257. }
  258. }
  259. maxstringlen -= strlen(stringptr);
  260. stringptr += strlen(stringptr);
  261. snprintf(stringptr, maxstringlen, ";");
  262. maxstringlen -= strlen(stringptr);
  263. stringptr += strlen(stringptr);
  264. }
  265. }
  266. /*
  267. * packet = parsed and in ascii in shared region -> copy to
  268. * clients
  269. */
  270. snprintf(stringptr, maxstringlen, "\n");
  271. stringptr += strlen(stringptr);
  272. shared_setlen((stringptr - stringbuf));
  273. debug("churnbuffer used: %d", (stringptr - stringbuf));
  274. master_permitread();
  275. } /* flag_hup == 0 */
  276. } /* forever */
  277. /* NOT REACHED */
  278. return (EX_SOFTWARE);
  279. }