mon.c 6.3 KB

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