data.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /* $Id: data.h,v 1.12 2002/08/11 20:02:53 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. /*
  32. * A host carrying a 'mon' is considered a 'source' of information. A single
  33. * data 'stream' of information has a particular type: <cpu|mem|if|io>. A
  34. * source can provide multiple 'streams' simultaneously. A source spools
  35. * information towards a 'mux'. A 'stream' that has been converted to network
  36. * representation is called a 'packedstream'.
  37. */
  38. #ifndef _MON_LIB_DATA_H
  39. #define _MON_LIB_DATA_H
  40. #include <sys/queue.h>
  41. #include <sys/types.h>
  42. #include <sys/socket.h>
  43. #include <netinet/in.h>
  44. #include <limits.h>
  45. #include "lex.h"
  46. /* Polynominal to use for CRC generation */
  47. #define MON_CRCPOLY 0x04c11db7
  48. #ifdef WORDS_BIGENDIAN
  49. #define htonq(n) n
  50. #define ntohq(n) n
  51. #else
  52. static inline u_int64_t
  53. htonq (u_int64_t v)
  54. {
  55. return htonl ((u_int32_t) (v >> 32))
  56. | (u_int64_t) ntohl ((u_int32_t) v) << 32;
  57. }
  58. static inline u_int64_t
  59. ntohq (u_int64_t v)
  60. {
  61. return ntohl ((u_int32_t) (v >>32))
  62. | (u_int64_t) ntohl ((u_int32_t) v) << 32;
  63. }
  64. #endif
  65. /* Mon packet version
  66. * version 1:
  67. * mon_version:timestamp:length:crc:n*packedstream
  68. *
  69. * Note that the data portion is limited. The current (31/03/2002) largest
  70. * streamtype (if) needs 42 bytes without arguments. My _POSIX2_LINE_MAX is 2k,
  71. * so that works out to about 38 packedstreams in a single mon packet.
  72. */
  73. #define MON_PACKET_VER 1
  74. struct monpacket {
  75. struct {
  76. u_int8_t mon_version;
  77. u_int64_t timestamp;
  78. u_int16_t length;
  79. u_int32_t crc;
  80. } header;
  81. char data[_POSIX2_LINE_MAX];
  82. };
  83. /* The difference between a stream and a packed stream:
  84. * - A stream ties stream information to a file.
  85. * - A packed stream is the measured data itself
  86. *
  87. * A stream is meta data describing properties, a packed stream is the data itself.
  88. */
  89. struct stream {
  90. int type;
  91. char *args;
  92. char *file;
  93. SLIST_ENTRY(stream) streams;
  94. };
  95. SLIST_HEAD(streamlist, stream);
  96. struct source {
  97. char *name;
  98. u_int32_t ip;
  99. u_int16_t port;
  100. struct streamlist sl;
  101. SLIST_ENTRY(source) sources;
  102. };
  103. SLIST_HEAD(sourcelist, source);
  104. struct mux {
  105. char *name;
  106. int offset;
  107. int clientsocket;
  108. int monsocket;
  109. struct monpacket packet;
  110. struct sockaddr_in sockaddr;
  111. struct streamlist sl;
  112. u_int32_t senderr;
  113. u_int32_t ip;
  114. u_int16_t port;
  115. SLIST_ENTRY(mux) muxes;
  116. };
  117. SLIST_HEAD(muxlist, mux);
  118. /* ps2str types */
  119. #define PS2STR_PRETTY 0
  120. #define PS2STR_RRD 1
  121. /* Stream types */
  122. #define MT_IO 0
  123. #define MT_CPU 1
  124. #define MT_MEM 2
  125. #define MT_IF 3
  126. #define MT_EOT 4
  127. /* NOTE: struct packetstream
  128. *
  129. * Unpacking of incoming packets is done via a packedstream structure. This
  130. * structure defines the maximum amount of data that can be contained in a
  131. * single network representation of a stream.
  132. *
  133. * This sucks in the way of portability (e.g. adding multiple cpu types) and
  134. * maintainabilty. This code will need to be refactored when I port it to other
  135. * oses.
  136. */
  137. #define MON_PS_ARGLEN 16
  138. struct packedstream {
  139. int type;
  140. int padding;
  141. char args[MON_PS_ARGLEN];
  142. union {
  143. struct {
  144. u_int64_t mtotal_transfers;
  145. u_int64_t mtotal_seeks;
  146. u_int64_t mtotal_bytes;
  147. } ps_io;
  148. struct {
  149. u_int16_t muser;
  150. u_int16_t mnice;
  151. u_int16_t msystem;
  152. u_int16_t minterrupt;
  153. u_int16_t midle;
  154. } ps_cpu;
  155. struct {
  156. u_int32_t mreal_active;
  157. u_int32_t mreal_total;
  158. u_int32_t mfree;
  159. u_int32_t mswap_used;
  160. u_int32_t mswap_total;
  161. } ps_mem;
  162. struct {
  163. u_int32_t mipackets;
  164. u_int32_t mopackets;
  165. u_int32_t mibytes;
  166. u_int32_t mobytes;
  167. u_int32_t mimcasts;
  168. u_int32_t momcasts;
  169. u_int32_t mierrors;
  170. u_int32_t moerrors;
  171. u_int32_t mcolls;
  172. u_int32_t mdrops;
  173. } ps_if;
  174. } data;
  175. };
  176. #define mio_total_transfers data.ps_io.mtotal_transfers
  177. #define mio_total_seeks data.ps_io.mtotal_seeks
  178. #define mio_total_bytes data.ps_io.mtotal_bytes
  179. #define mcpm_user data.ps_cpu.uuser
  180. #define mcpm_nice data.ps_cpu.unice
  181. #define mcpm_system data.ps_cpu.usystem
  182. #define mcpm_interrupt data.ps_cpu.uinterrupt
  183. #define mcpm_idle data.ps_cpu.uidle
  184. #define mmem_real_active data.ps_mem.mreal_active
  185. #define mmem_real_total data.ps_mem.mreal_total
  186. #define mmem_free data.ps_mem.mfree
  187. #define mmem_swap_msed data.ps_mem.uswap_used
  188. #define mmem_swap_total data.ps_mem.mswap_total
  189. #define mif_ipackets data.ps_if.mipackets
  190. #define mif_opackets data.ps_if.mopackets
  191. #define mif_ibytes data.ps_if.mibytes
  192. #define mif_obytes data.ps_if.mobytes
  193. #define mif_imcasts data.ps_if.mimcasts
  194. #define mif_omcasts data.ps_if.momcasts
  195. #define mif_ierrors data.ps_if.mierrors
  196. #define mif_oerrors data.ps_if.moerrors
  197. #define mif_colls data.ps_if.mcolls
  198. #define mif_drops data.ps_if.mdrops
  199. /* prototypes */
  200. __BEGIN_DECLS
  201. const char *type2str(const int);
  202. const int token2type(const int);
  203. int calculate_churnbuffer(struct sourcelist *);
  204. int ps2strn(struct packedstream *, char *, int, int);
  205. int snpack(char *, int, char*, int, ...);
  206. int strlentype(int);
  207. int sunpack(char *, struct packedstream *);
  208. struct mux *add_mux(struct muxlist *, char *);
  209. struct mux *find_mux(struct muxlist *, char *);
  210. struct mux * rename_mux(struct muxlist *, struct mux *, char *);
  211. struct source *add_source(struct sourcelist *, char *);
  212. struct source *find_source(struct sourcelist *, char *);
  213. struct source *find_source_ip(struct sourcelist *, u_int32_t);
  214. struct stream *add_mux_stream(struct mux *, int, char *);
  215. struct stream *add_source_stream(struct source *, int, char *);
  216. struct stream *find_mux_stream(struct mux *, int, char *);
  217. struct stream *find_source_stream(struct source *, int, char *);
  218. u_int32_t crc32(const void*, unsigned int);
  219. void free_muxlist(struct muxlist *);
  220. void free_sourcelist(struct sourcelist *);
  221. void free_streamlist(struct streamlist *);
  222. void init_crc32();
  223. __END_DECLS
  224. #endif /*_MON_LIB_DATA_H*/