symon.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /* $Id: symon.c,v 1.28 2002/12/15 14:23:27 dijkstra Exp $ */
  2. /*
  3. * Copyright (c) 2001-2002 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/param.h>
  32. #include <errno.h>
  33. #include <fcntl.h>
  34. #include <limits.h>
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include <sysexits.h>
  39. #include <syslog.h>
  40. #include <unistd.h>
  41. #include "data.h"
  42. #include "error.h"
  43. #include "symon.h"
  44. #include "symonnet.h"
  45. #include "net.h"
  46. #include "readconf.h"
  47. #include "xmalloc.h"
  48. __BEGIN_DECLS
  49. void alarmhandler(int);
  50. void exithandler(int);
  51. void huphandler(int);
  52. __END_DECLS
  53. int flag_hup = 0;
  54. /* map stream types to inits and getters */
  55. struct funcmap streamfunc[] = {
  56. {MT_IO, init_io, gets_io, get_io}, /* gets_io obtains entire io state,
  57. * get_io = just a copy */
  58. {MT_CPU, init_cpu, NULL, get_cpu},
  59. {MT_MEM, init_mem, NULL, get_mem},
  60. {MT_IF, init_if, NULL, get_if},
  61. {MT_PF, init_pf, NULL, get_pf},
  62. {MT_DEBUG, init_debug, NULL, get_debug},
  63. {MT_PROC, init_proc, gets_proc, get_proc},
  64. {MT_MBUF, init_mbuf, NULL, get_mbuf},
  65. {MT_EOT, NULL, NULL}
  66. };
  67. /* alarmhandler that gets called every SYMON_INTERVAL */
  68. void
  69. alarmhandler(int s)
  70. {
  71. /* EMPTY */
  72. }
  73. void
  74. exithandler(int s)
  75. {
  76. info("received signal %d - quitting", s);
  77. exit(1);
  78. }
  79. void
  80. huphandler(int s)
  81. {
  82. info("hup received");
  83. flag_hup = 1;
  84. }
  85. /*
  86. * Symon is a system measurement utility.
  87. *
  88. * The main goals symon hopes to accomplish are:
  89. * - to take fine grained measurements of system parameters
  90. * - with minimal performance impact
  91. * - in a secure way.
  92. *
  93. * Measuring system parameters (e.g. interfaces) sometimes means traversing
  94. * lists in kernel memory. Because of this the measurement of data has been
  95. * decoupled from the processing and storage of data. Storing the measured
  96. * information that symon provides is done by a second program, called symux.
  97. *
  98. * Symon can keep track of cpu, memory, disk and network interface
  99. * interactions. Symon was built specifically for OpenBSD.
  100. */
  101. int
  102. main(int argc, char *argv[])
  103. {
  104. struct muxlist mul, newmul;
  105. struct itimerval alarminterval;
  106. struct stream *stream;
  107. struct mux *mux;
  108. FILE *f;
  109. char *cfgfile;
  110. char *cfgpath;
  111. char *stringptr;
  112. int maxstringlen;
  113. int ch;
  114. int i;
  115. SLIST_INIT(&mul);
  116. /* reset flags */
  117. flag_debug = 0;
  118. flag_daemon = 0;
  119. cfgfile = SYMON_CONFIG_FILE;
  120. while ((ch = getopt(argc, argv, "dvf:")) != -1) {
  121. switch (ch) {
  122. case 'd':
  123. flag_debug = 1;
  124. break;
  125. case 'f':
  126. if (optarg && optarg[0] != '/') {
  127. /* cfg path needs to be absolute, we will be a daemon soon */
  128. if ((cfgpath = getwd(NULL)) == NULL)
  129. fatal("could not get working directory");
  130. maxstringlen = strlen(cfgpath) + strlen(optarg) + 1;
  131. cfgfile = xmalloc(maxstringlen);
  132. strncpy(cfgfile, cfgpath, maxstringlen);
  133. stringptr = cfgfile + strlen(cfgpath);
  134. stringptr[0] = '/';
  135. stringptr++;
  136. strncpy(stringptr, optarg, maxstringlen - (cfgfile - stringptr));
  137. cfgfile[maxstringlen] = '\0';
  138. free(cfgpath);
  139. }
  140. else
  141. cfgfile = xstrdup(optarg);
  142. break;
  143. case 'v':
  144. info("symon version %s", SYMON_VERSION);
  145. default:
  146. info("usage: %s [-d] [-v] [-f cfgfile]", __progname);
  147. exit(EX_USAGE);
  148. }
  149. }
  150. /* parse configuration file */
  151. if (!read_config_file(&mul, cfgfile))
  152. fatal("configuration contained errors; quitting");
  153. setegid(getgid());
  154. setgid(getgid());
  155. if (flag_debug != 1) {
  156. if (daemon(0, 0) != 0)
  157. fatal("daemonize failed");
  158. flag_daemon = 1;
  159. /* record pid */
  160. f = fopen(SYMON_PID_FILE, "w");
  161. if (f) {
  162. fprintf(f, "%u\n", (u_int) getpid());
  163. fclose(f);
  164. }
  165. }
  166. info("symon version %s", SYMON_VERSION);
  167. if (flag_debug == 1)
  168. info("program id=%d", (u_int) getpid());
  169. /* setup signal handlers */
  170. signal(SIGALRM, alarmhandler);
  171. signal(SIGHUP, huphandler);
  172. signal(SIGINT, exithandler);
  173. signal(SIGQUIT, exithandler);
  174. signal(SIGTERM, exithandler);
  175. /* prepare crc32 */
  176. init_crc32();
  177. /* init modules */
  178. SLIST_FOREACH(mux, &mul, muxes) {
  179. connect2mux(mux);
  180. SLIST_FOREACH(stream, &mux->sl, streams) {
  181. (streamfunc[stream->type].init) (stream->args);
  182. }
  183. }
  184. /* setup alarm */
  185. timerclear(&alarminterval.it_interval);
  186. timerclear(&alarminterval.it_value);
  187. alarminterval.it_interval.tv_sec =
  188. alarminterval.it_value.tv_sec = SYMON_INTERVAL;
  189. if (setitimer(ITIMER_REAL, &alarminterval, NULL) != 0) {
  190. fatal("alarm setup failed -- %s", strerror(errno));
  191. }
  192. for (;;) { /* FOREVER */
  193. sleep(SYMON_INTERVAL * 2); /* alarm will always interrupt sleep */
  194. if (flag_hup == 1) {
  195. flag_hup = 0;
  196. SLIST_INIT(&newmul);
  197. if (!read_config_file(&newmul, cfgfile)) {
  198. info("new configuration contains errors; keeping old configuration");
  199. free_muxlist(&newmul);
  200. }
  201. else {
  202. free_muxlist(&mul);
  203. mul = newmul;
  204. info("read configuration file '%.100s' succesfully", cfgfile);
  205. /* init modules */
  206. SLIST_FOREACH(mux, &mul, muxes) {
  207. connect2mux(mux);
  208. SLIST_FOREACH(stream, &mux->sl, streams) {
  209. (streamfunc[stream->type].init) (stream->args);
  210. }
  211. }
  212. }
  213. }
  214. else {
  215. /* populate for modules that get all their measurements in one go */
  216. for (i = 0; i < MT_EOT; i++)
  217. if (streamfunc[i].gets != NULL)
  218. (streamfunc[i].gets) ();
  219. SLIST_FOREACH(mux, &mul, muxes) {
  220. prepare_packet(mux);
  221. SLIST_FOREACH(stream, &mux->sl, streams)
  222. stream_in_packet(stream, mux);
  223. finish_packet(mux);
  224. send_packet(mux);
  225. }
  226. }
  227. }
  228. /* NOTREACHED */
  229. return (EX_SOFTWARE);
  230. }