symux.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /* $Id: symux.c,v 1.38 2007/01/20 12:52:50 dijkstra Exp $ */
  2. /*
  3. * Copyright (c) 2001-2006 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 sourcelist *sol;
  105. struct mux *mux;
  106. FILE *f;
  107. int ch;
  108. int churnbuflen;
  109. int flag_list;
  110. int offset;
  111. int result;
  112. unsigned int rrderrors;
  113. int slot;
  114. time_t timestamp;
  115. SLIST_INIT(&mul);
  116. /* reset flags */
  117. flag_debug = 0;
  118. flag_daemon = 0;
  119. flag_list = 0;
  120. cfgfile = SYMUX_CONFIG_FILE;
  121. while ((ch = getopt(argc, argv, "df:lv")) != -1) {
  122. switch (ch) {
  123. case 'd':
  124. flag_debug = 1;
  125. break;
  126. case 'f':
  127. if (optarg && optarg[0] != '/') {
  128. /* cfg path needs to be absolute, we will be a daemon soon */
  129. cfgpath = xmalloc(MAXPATHLEN);
  130. if ((cfgpath = getcwd(cfgpath, MAXPATHLEN)) == NULL)
  131. fatal("could not get working directory");
  132. maxstringlen = strlen(cfgpath) + strlen(optarg) + 1;
  133. cfgfile = xmalloc(maxstringlen);
  134. strncpy(cfgfile, cfgpath, maxstringlen);
  135. stringptr = cfgfile + strlen(cfgpath);
  136. stringptr[0] = '/';
  137. stringptr++;
  138. strncpy(stringptr, optarg, maxstringlen - (cfgfile - stringptr));
  139. cfgfile[maxstringlen] = '\0';
  140. free(cfgpath);
  141. } else
  142. cfgfile = xstrdup(optarg);
  143. break;
  144. case 'l':
  145. flag_list = 1;
  146. break;
  147. case 'v':
  148. info("symux version %s", SYMUX_VERSION);
  149. default:
  150. info("usage: %s [-d] [-l] [-v] [-f cfgfile]", __progname);
  151. exit(EX_USAGE);
  152. }
  153. }
  154. if (flag_list == 1) {
  155. /* read configuration without file checks */
  156. result = read_config_file(&mul, cfgfile, 0);
  157. if (!result) {
  158. fatal("configuration contained errors; quitting");
  159. }
  160. mux = SLIST_FIRST(&mul);
  161. if (mux == NULL) {
  162. fatal("%s:%d: mux not found", __FILE__, __LINE__);
  163. }
  164. sol = &mux->sol;
  165. if (sol == NULL) {
  166. fatal("%s:%d: sourcelist not found", __FILE__, __LINE__);
  167. }
  168. SLIST_FOREACH(source, sol, sources) {
  169. if (! SLIST_EMPTY(&source->sl)) {
  170. SLIST_FOREACH(stream, &source->sl, streams) {
  171. if (stream->file != NULL) {
  172. info("%.200s", stream->file);
  173. }
  174. }
  175. }
  176. }
  177. return (EX_OK);
  178. } else {
  179. /* read configuration file with file access checks */
  180. result = read_config_file(&mul, cfgfile, 1);
  181. if (!result) {
  182. fatal("configuration contained errors; quitting");
  183. }
  184. }
  185. setegid(getgid());
  186. setgid(getgid());
  187. if (flag_debug != 1) {
  188. if (daemon(0, 0) != 0)
  189. fatal("daemonize failed");
  190. flag_daemon = 1;
  191. /* record pid */
  192. f = fopen(SYMUX_PID_FILE, "w");
  193. if (f) {
  194. fprintf(f, "%u\n", (u_int) getpid());
  195. fclose(f);
  196. }
  197. }
  198. info("symux version %s", SYMUX_VERSION);
  199. if (flag_debug == 1)
  200. info("program id=%d", (u_int) getpid());
  201. mux = SLIST_FIRST(&mul);
  202. churnbuflen = calculate_churnbuffer(&mux->sol);
  203. debug("size of churnbuffer = %d", churnbuflen);
  204. initshare(churnbuflen);
  205. /* catch signals */
  206. signal(SIGHUP, huphandler);
  207. signal(SIGINT, exithandler);
  208. signal(SIGQUIT, exithandler);
  209. signal(SIGTERM, exithandler);
  210. signal(SIGTERM, exithandler);
  211. /* prepare crc32 */
  212. init_crc32();
  213. /* prepare sockets */
  214. if (get_symon_sockets(mux) == 0)
  215. fatal("no sockets could be opened for incoming symon traffic");
  216. if (get_client_socket(mux) == 0)
  217. fatal("socket for client connections could not be opened");
  218. rrderrors = 0;
  219. /* main loop */
  220. for (;;) { /* FOREVER */
  221. wait_for_traffic(mux, &source, &packet);
  222. if (flag_hup == 1) {
  223. flag_hup = 0;
  224. SLIST_INIT(&newmul);
  225. if (!read_config_file(&newmul, cfgfile, 1)) {
  226. info("new configuration contains errors; keeping old configuration");
  227. free_muxlist(&newmul);
  228. } else {
  229. info("read configuration file '%.100s' successfully", cfgfile);
  230. free_muxlist(&mul);
  231. mul = newmul;
  232. mux = SLIST_FIRST(&mul);
  233. get_symon_sockets(mux);
  234. get_client_socket(mux);
  235. }
  236. } else {
  237. /*
  238. * Put information from packet into stringbuf (shared region).
  239. * Note that the stringbuf is used twice: 1) to update the
  240. * rrdfile and 2) to collect all the data from a single packet
  241. * that needs to shared to the clients. This is the reason for
  242. * the hasseling with stringptr.
  243. */
  244. offset = mux->offset;
  245. maxstringlen = shared_getmaxlen();
  246. /* put time:ip: into shared region */
  247. slot = master_forbidread();
  248. timestamp = (time_t) packet.header.timestamp;
  249. stringbuf = shared_getmem(slot);
  250. debug("stringbuf = 0x%08x", stringbuf);
  251. snprintf(stringbuf, maxstringlen, "%s;", source->addr);
  252. /* hide this string region from rrd update */
  253. maxstringlen -= strlen(stringbuf);
  254. stringptr = stringbuf + strlen(stringbuf);
  255. while (offset < packet.header.length) {
  256. bzero(&ps, sizeof(struct packedstream));
  257. if (packet.header.symon_version == 1) {
  258. offset += sunpack1(packet.data + offset, &ps);
  259. } else if (packet.header.symon_version == 2) {
  260. offset += sunpack2(packet.data + offset, &ps);
  261. } else {
  262. debug("unsupported packet version - ignoring data");
  263. ps.type = MT_EOT;
  264. }
  265. /* find stream in source */
  266. stream = find_source_stream(source, ps.type, ps.arg);
  267. if (stream != NULL) {
  268. /* put type and arg in and hide from rrd */
  269. snprintf(stringptr, maxstringlen, "%s:%s:", type2str(ps.type), ps.arg);
  270. maxstringlen -= strlen(stringptr);
  271. stringptr += strlen(stringptr);
  272. /* put timestamp in and show to rrd */
  273. snprintf(stringptr, maxstringlen, "%u", (unsigned int)timestamp);
  274. arg_ra[3] = stringptr;
  275. maxstringlen -= strlen(stringptr);
  276. stringptr += strlen(stringptr);
  277. /* put measurements in */
  278. ps2strn(&ps, stringptr, maxstringlen, PS2STR_RRD);
  279. if (stream->file != NULL) {
  280. /* clear optind for getopt call by rrdupdate */
  281. optind = 0;
  282. /* save if file specified */
  283. arg_ra[0] = "rrdupdate";
  284. arg_ra[1] = "--";
  285. arg_ra[2] = stream->file;
  286. /*
  287. * This call will cost a lot (symux will become
  288. * unresponsive and eat up massive amounts of cpu) if
  289. * the rrdfile is out of sync.
  290. */
  291. rrd_update(4, arg_ra);
  292. if (rrd_test_error()) {
  293. if (rrderrors < SYMUX_MAXRRDERRORS) {
  294. rrderrors++;
  295. warning("rrd_update:%.200s", rrd_get_error());
  296. warning("%.200s %.200s %.200s %.200s", arg_ra[0], arg_ra[1],
  297. arg_ra[2], arg_ra[3]);
  298. if (rrderrors == SYMUX_MAXRRDERRORS) {
  299. warning("maximum rrd errors reached - will stop reporting them");
  300. }
  301. }
  302. rrd_clear_error();
  303. } else {
  304. if (flag_debug == 1)
  305. debug("%.200s %.200s %.200s %.200s", arg_ra[0], arg_ra[1],
  306. arg_ra[2], arg_ra[3]);
  307. }
  308. }
  309. maxstringlen -= strlen(stringptr);
  310. stringptr += strlen(stringptr);
  311. snprintf(stringptr, maxstringlen, ";");
  312. maxstringlen -= strlen(stringptr);
  313. stringptr += strlen(stringptr);
  314. } else {
  315. debug("ignored unaccepted stream %.16s(%.16s) from %.20s", type2str(ps.type),
  316. ((ps.arg == NULL) ? "0" : ps.arg), source->addr);
  317. }
  318. }
  319. /*
  320. * packet = parsed and in ascii in shared region -> copy to
  321. * clients
  322. */
  323. snprintf(stringptr, maxstringlen, "\n");
  324. stringptr += strlen(stringptr);
  325. shared_setlen(slot, (stringptr - stringbuf));
  326. debug("churnbuffer used: %d", (stringptr - stringbuf));
  327. master_permitread();
  328. } /* flag_hup == 0 */
  329. } /* forever */
  330. /* NOT REACHED */
  331. return (EX_SOFTWARE);
  332. }