mon.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /* $Id: mon.c,v 1.22 2002/08/31 16:09:55 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 <syslog.h>
  39. #include <unistd.h>
  40. #include "data.h"
  41. #include "error.h"
  42. #include "mon.h"
  43. #include "monnet.h"
  44. #include "net.h"
  45. #include "readconf.h"
  46. #include "xmalloc.h"
  47. __BEGIN_DECLS
  48. void alarmhandler(int);
  49. void exithandler(int);
  50. void huphandler(int);
  51. __END_DECLS
  52. int flag_hup = 0;
  53. #ifdef MON_KVM
  54. kvm_t *kvmd;
  55. struct nlist mon_nl[] = {
  56. {"_disklist"}, /* MON_DL = 0 (mon.h)*/
  57. {""},
  58. };
  59. /* Read kernel memory */
  60. int
  61. kread(u_long addr, char *buf, int size)
  62. {
  63. if (kvm_read(kvmd, addr, buf, size) != size) {
  64. warning("kvm_read:%s", kvm_geterr(kvmd));
  65. return 1;
  66. }
  67. return 0;
  68. }
  69. #else /* MON_KVM */
  70. int
  71. kread(u_long addr, char *buf, int size)
  72. {
  73. warning("kvm_read not compiled in, calling probe will signal failure");
  74. return 0;
  75. }
  76. #endif
  77. /* map stream types to inits and getters */
  78. struct funcmap streamfunc[] = {
  79. {MT_IO, init_io, get_io},
  80. {MT_CPU, init_cpu, get_cpu},
  81. {MT_MEM, init_mem, get_mem},
  82. {MT_IF, init_if, get_if},
  83. {MT_PF, init_pf, get_pf},
  84. {MT_EOT, NULL, NULL}
  85. };
  86. /* Alarmhandler that gets called every MON_INTERVAL */
  87. void
  88. alarmhandler(int s) {
  89. /* EMPTY */
  90. }
  91. void
  92. exithandler(int s) {
  93. info("received signal %d - quitting", s);
  94. exit(1);
  95. }
  96. void
  97. huphandler(int s) {
  98. info("hup received");
  99. flag_hup = 1;
  100. }
  101. /*
  102. * Mon is a system measurement utility.
  103. *
  104. * The main goals mon hopes to accomplish is:
  105. * - to take fine grained measurements of system parameters
  106. * - with minimal performance impact
  107. * - in a secure way.
  108. *
  109. * Measuring system parameters (e.g. interfaces) sometimes means traversing
  110. * lists in kernel memory. Because of this the measurement of data has been
  111. * decoupled from the processing and storage of data. Storing the measured
  112. * information that mon provides is done by a second program, called monmux.
  113. *
  114. * Mon can keep track of cpu, memory, disk and network interface
  115. * interactions. Mon was built specifically for OpenBSD.
  116. */
  117. int
  118. main(int argc, char *argv[])
  119. {
  120. struct muxlist mul, newmul;
  121. struct itimerval alarminterval;
  122. struct stream *stream;
  123. struct mux *mux;
  124. FILE *f;
  125. #ifdef MON_KVM
  126. char mon_buf[_POSIX2_LINE_MAX];
  127. char *nlistf = NULL, *memf = NULL;
  128. #endif
  129. int ch;
  130. SLIST_INIT(&mul);
  131. /* reset flags */
  132. flag_debug = 0;
  133. flag_daemon = 0;
  134. while ((ch = getopt(argc, argv, "dv")) != -1) {
  135. switch (ch) {
  136. case 'd':
  137. flag_debug = 1;
  138. break;
  139. case 'v':
  140. info("mon version %s", MON_VERSION);
  141. default:
  142. info("usage: %s [-d] [-v]", __progname);
  143. exit(1);
  144. }
  145. }
  146. #ifdef MON_KVM
  147. /* Populate our kernel name list */
  148. if ((kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, mon_buf)) == NULL)
  149. fatal("kvm_open: %s", mon_buf);
  150. if (kvm_nlist(kvmd, mon_nl) < 0 || mon_nl[0].n_type == 0) {
  151. if (nlistf)
  152. fatal("%s: no namelist", nlistf);
  153. else
  154. fatal("no namelist");
  155. }
  156. #endif /* MON_KVM */
  157. setegid(getgid());
  158. setgid(getgid());
  159. if (flag_debug != 1) {
  160. if (daemon(0,0) != 0)
  161. fatal("daemonize failed");
  162. flag_daemon = 1;
  163. /* record pid */
  164. f = fopen(MON_PID_FILE, "w");
  165. if (f) {
  166. fprintf(f, "%u\n", (u_int) getpid());
  167. fclose(f);
  168. }
  169. }
  170. info("mon version %s", MON_VERSION);
  171. if (!read_config_file(&mul, MON_CONFIG_FILE))
  172. fatal("configuration contained errors; quitting");
  173. if (flag_debug == 1)
  174. info("program id=%d", (u_int) getpid());
  175. /* Setup signal handlers */
  176. signal(SIGALRM, alarmhandler);
  177. signal(SIGHUP, huphandler);
  178. signal(SIGINT, exithandler);
  179. signal(SIGQUIT, exithandler);
  180. signal(SIGTERM, exithandler);
  181. /* Prepare crc32 */
  182. init_crc32();
  183. /* init modules */
  184. SLIST_FOREACH(mux, &mul, muxes) {
  185. connect2mux(mux);
  186. SLIST_FOREACH(stream, &mux->sl, streams) {
  187. (streamfunc[stream->type].init)(stream->args);
  188. }
  189. }
  190. /* Setup alarm */
  191. timerclear(&alarminterval.it_interval);
  192. timerclear(&alarminterval.it_value);
  193. alarminterval.it_interval.tv_sec=
  194. alarminterval.it_value.tv_sec=MON_INTERVAL;
  195. if (setitimer(ITIMER_REAL, &alarminterval, NULL) != 0) {
  196. fatal("alarm setup failed -- %s", strerror(errno));
  197. }
  198. for (;;) { /* FOREVER */
  199. sleep(MON_INTERVAL*2); /* alarm will always interrupt sleep */
  200. if (flag_hup == 1) {
  201. flag_hup = 0;
  202. SLIST_INIT(&newmul);
  203. if (!read_config_file(&newmul, MON_CONFIG_FILE)) {
  204. info("new configuration contains errors; keeping old configuration");
  205. free_muxlist(&newmul);
  206. } else {
  207. free_muxlist(&mul);
  208. mul = newmul;
  209. info("read configuration file succesfully");
  210. /* init modules */
  211. SLIST_FOREACH(mux, &mul, muxes) {
  212. connect2mux(mux);
  213. SLIST_FOREACH(stream, &mux->sl, streams) {
  214. (streamfunc[stream->type].init)(stream->args);
  215. }
  216. }
  217. }
  218. }
  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. /* NOTREACHED */
  228. }