data.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /* $Id: data.h,v 1.26 2005/03/20 16:17:22 dijkstra Exp $ */
  2. /*
  3. * Copyright (c) 2001-2005 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 'symon' 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 _SYMON_LIB_DATA_H
  39. #define _SYMON_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 SYMON_CRCPOLY 0x04c11db7
  48. #ifndef ntohq
  49. #if BYTE_ORDER == BIG_ENDIAN
  50. #define htonq(n) (n)
  51. #define ntohq(n) (n)
  52. #else
  53. static inline u_int64_t
  54. htonq(u_int64_t v)
  55. {
  56. return (u_int64_t) htonl(v) << 32 | htonl(v >> 32);
  57. }
  58. static inline u_int64_t
  59. ntohq(u_int64_t v)
  60. {
  61. return (u_int64_t) ntohl(v) << 32 | ntohl(v >> 32);
  62. }
  63. #endif /* BYTE_ORDER */
  64. #endif /* ntohq */
  65. /* Symon packet version
  66. * version 1:
  67. * symon_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 symon packet.
  72. */
  73. #define SYMON_PACKET_VER 1
  74. /* Sending structures over the network is dangerous as the compiler might have
  75. * added extra padding between items. symonpacketheader below is therefore also
  76. * marshalled and demarshalled via snpack and sunpack. The actual values are
  77. * copied out of memory into this structure one by one.
  78. */
  79. struct symonpacketheader {
  80. u_int64_t timestamp;
  81. u_int32_t crc;
  82. u_int16_t length;
  83. u_int8_t symon_version;
  84. u_int8_t reserved;
  85. };
  86. struct symonpacket {
  87. struct symonpacketheader header;
  88. char data[_POSIX2_LINE_MAX];
  89. };
  90. /* The difference between a stream and a packed stream:
  91. * - A stream ties stream information to a file.
  92. * - A packed stream is the measured data itself
  93. *
  94. * A stream is meta data describing properties, a packed stream is the data itself.
  95. */
  96. struct stream {
  97. int type;
  98. char *args;
  99. char *file;
  100. SLIST_ENTRY(stream) streams;
  101. };
  102. SLIST_HEAD(streamlist, stream);
  103. struct source {
  104. char *addr;
  105. struct sockaddr_storage sockaddr;
  106. struct streamlist sl;
  107. SLIST_ENTRY(source) sources;
  108. };
  109. SLIST_HEAD(sourcelist, source);
  110. struct mux {
  111. char *name;
  112. char *addr;
  113. char *port;
  114. struct sourcelist sol;
  115. int offset;
  116. int clientsocket; /* symux; incoming tcp connections */
  117. int symonsocket[AF_MAX]; /* symux; incoming symon data */
  118. int symuxsocket; /* symon; outgoing data to mux */
  119. struct symonpacket packet;
  120. struct sockaddr_storage sockaddr;
  121. struct streamlist sl;
  122. u_int32_t senderr;
  123. SLIST_ENTRY(mux) muxes;
  124. };
  125. SLIST_HEAD(muxlist, mux);
  126. /* ps2str types */
  127. #define PS2STR_PRETTY 0
  128. #define PS2STR_RRD 1
  129. /* Stream types */
  130. #define MT_IO1 0
  131. #define MT_CPU 1
  132. #define MT_MEM 2
  133. #define MT_IF 3
  134. #define MT_PF 4
  135. #define MT_DEBUG 5
  136. #define MT_PROC 6
  137. #define MT_MBUF 7
  138. #define MT_SENSOR 8
  139. #define MT_IO2 9
  140. #define MT_PFQ 10
  141. #define MT_TEST 11
  142. #define MT_EOT 12
  143. /*
  144. * Unpacking of incoming packets is done via a packedstream structure. This
  145. * structure defines the maximum amount of data that can be contained in a
  146. * single network representation of a stream.
  147. */
  148. #define SYMON_UNKMUX "<unknown mux>" /* mux nodes without host addr */
  149. #define SYMON_PS_ARGLEN 16 /* maximum argument length */
  150. #define SYMON_PS_ARGLENSTR "15" /* maximum number of chars in an argument, as str */
  151. struct packedstream {
  152. int type;
  153. int padding;
  154. char args[SYMON_PS_ARGLEN];
  155. union {
  156. struct symonpacketheader header;
  157. struct {
  158. u_int64_t mtotal_transfers;
  159. u_int64_t mtotal_seeks;
  160. u_int64_t mtotal_bytes;
  161. } ps_io;
  162. struct {
  163. u_int16_t muser;
  164. u_int16_t mnice;
  165. u_int16_t msystem;
  166. u_int16_t minterrupt;
  167. u_int16_t midle;
  168. } ps_cpu;
  169. struct {
  170. u_int32_t mreal_active;
  171. u_int32_t mreal_total;
  172. u_int32_t mfree;
  173. u_int32_t mswap_used;
  174. u_int32_t mswap_total;
  175. } ps_mem;
  176. struct {
  177. u_int32_t mipackets;
  178. u_int32_t mopackets;
  179. u_int32_t mibytes;
  180. u_int32_t mobytes;
  181. u_int32_t mimcasts;
  182. u_int32_t momcasts;
  183. u_int32_t mierrors;
  184. u_int32_t moerrors;
  185. u_int32_t mcolls;
  186. u_int32_t mdrops;
  187. } ps_if;
  188. struct {
  189. u_int64_t bytes_v4_in;
  190. u_int64_t bytes_v4_out;
  191. u_int64_t bytes_v6_in;
  192. u_int64_t bytes_v6_out;
  193. u_int64_t packets_v4_in_pass;
  194. u_int64_t packets_v4_in_drop;
  195. u_int64_t packets_v4_out_pass;
  196. u_int64_t packets_v4_out_drop;
  197. u_int64_t packets_v6_in_pass;
  198. u_int64_t packets_v6_in_drop;
  199. u_int64_t packets_v6_out_pass;
  200. u_int64_t packets_v6_out_drop;
  201. u_int64_t states_entries;
  202. u_int64_t states_searches;
  203. u_int64_t states_inserts;
  204. u_int64_t states_removals;
  205. u_int64_t counters_match;
  206. u_int64_t counters_badoffset;
  207. u_int64_t counters_fragment;
  208. u_int64_t counters_short;
  209. u_int64_t counters_normalize;
  210. u_int64_t counters_memory;
  211. } ps_pf;
  212. struct {
  213. u_int32_t debug0;
  214. u_int32_t debug1;
  215. u_int32_t debug2;
  216. u_int32_t debug3;
  217. u_int32_t debug4;
  218. u_int32_t debug5;
  219. u_int32_t debug6;
  220. u_int32_t debug7;
  221. u_int32_t debug8;
  222. u_int32_t debug9;
  223. u_int32_t debug10;
  224. u_int32_t debug11;
  225. u_int32_t debug12;
  226. u_int32_t debug13;
  227. u_int32_t debug14;
  228. u_int32_t debug15;
  229. u_int32_t debug16;
  230. u_int32_t debug17;
  231. u_int32_t debug18;
  232. u_int32_t debug19;
  233. } ps_debug;
  234. struct {
  235. u_int32_t totmbufs;
  236. u_int32_t mt_data;
  237. u_int32_t mt_oobdata;
  238. u_int32_t mt_control;
  239. u_int32_t mt_header;
  240. u_int32_t mt_ftable;
  241. u_int32_t mt_soname;
  242. u_int32_t mt_soopts;
  243. u_int32_t pgused;
  244. u_int32_t pgtotal;
  245. u_int32_t totmem;
  246. u_int32_t totpct;
  247. u_int32_t m_drops;
  248. u_int32_t m_wait;
  249. u_int32_t m_drain;
  250. } ps_mbuf;
  251. struct {
  252. int64_t value;
  253. } ps_sensor;
  254. struct {
  255. u_int64_t mtotal_rtransfers;
  256. u_int64_t mtotal_wtransfers;
  257. u_int64_t mtotal_seeks2;
  258. u_int64_t mtotal_rbytes;
  259. u_int64_t mtotal_wbytes;
  260. } ps_io2;
  261. struct {
  262. u_int64_t sent_bytes;
  263. u_int64_t sent_packets;
  264. u_int64_t drop_bytes;
  265. u_int64_t drop_packets;
  266. } ps_pfq;
  267. struct {
  268. u_int64_t L[4];
  269. int64_t D[4];
  270. u_int32_t l[4];
  271. u_int16_t s[4];
  272. u_int16_t c[4];
  273. u_int8_t b[4];
  274. } ps_test;
  275. } data;
  276. };
  277. /* prototypes */
  278. __BEGIN_DECLS
  279. const char *type2str(const int);
  280. const int token2type(const int);
  281. int calculate_churnbuffer(struct sourcelist *);
  282. int getheader(char *, struct symonpacketheader *);
  283. int ps2strn(struct packedstream *, char *, int, int);
  284. int setheader(char *, struct symonpacketheader *);
  285. int snpack(char *, int, char *, int,...);
  286. int strlentype(int);
  287. int sunpack(char *, struct packedstream *);
  288. struct mux *add_mux(struct muxlist *, char *);
  289. struct mux *find_mux(struct muxlist *, char *);
  290. struct mux *rename_mux(struct muxlist *, struct mux *, char *);
  291. struct source *add_source(struct sourcelist *, char *);
  292. struct source *find_source(struct sourcelist *, char *);
  293. struct source *find_source_sockaddr(struct sourcelist *, struct sockaddr *);
  294. struct stream *add_mux_stream(struct mux *, int, char *);
  295. struct stream *add_source_stream(struct source *, int, char *);
  296. struct stream *find_mux_stream(struct mux *, int, char *);
  297. struct stream *find_source_stream(struct source *, int, char *);
  298. u_int32_t crc32(const void *, unsigned int);
  299. void free_muxlist(struct muxlist *);
  300. void free_sourcelist(struct sourcelist *);
  301. void free_streamlist(struct streamlist *);
  302. void init_crc32();
  303. __END_DECLS
  304. #endif /* _SYMON_LIB_DATA_H */