symux.c 8.9 KB

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