data.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /* $Id: data.h,v 1.27 2005/10/16 15:26:51 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 "platform.h"
  41. #include <netinet/in.h>
  42. #include <limits.h>
  43. #include "lex.h"
  44. #include "sylimits.h"
  45. /* Polynominal to use for CRC generation */
  46. #define SYMON_CRCPOLY 0x04c11db7
  47. #ifndef ntohq
  48. #if BYTE_ORDER == BIG_ENDIAN
  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 (u_int64_t) htonl(v) << 32 | htonl(v >> 32);
  56. }
  57. static inline u_int64_t
  58. ntohq(u_int64_t v)
  59. {
  60. return (u_int64_t) ntohl(v) << 32 | ntohl(v >> 32);
  61. }
  62. #endif /* BYTE_ORDER */
  63. #endif /* ntohq */
  64. /* Symon packet version
  65. * version 1:
  66. * symon_version:timestamp:length:crc:n*packedstream
  67. *
  68. * Note that the data portion is limited. The current (31/03/2002) largest
  69. * streamtype (if) needs 42 bytes without arguments. My _POSIX2_LINE_MAX is 2k,
  70. * so that works out to about 38 packedstreams in a single symon packet.
  71. */
  72. #define SYMON_PACKET_VER 1
  73. #define SYMON_UNKMUX "<unknown mux>" /* mux nodes without host addr */
  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 *arg;
  99. char *file;
  100. SLIST_ENTRY(stream) streams;
  101. union stream_parg parg;
  102. };
  103. SLIST_HEAD(streamlist, stream);
  104. struct source {
  105. char *addr;
  106. struct sockaddr_storage sockaddr;
  107. struct streamlist sl;
  108. SLIST_ENTRY(source) sources;
  109. };
  110. SLIST_HEAD(sourcelist, source);
  111. struct mux {
  112. char *name;
  113. char *addr;
  114. char *port;
  115. struct sourcelist sol;
  116. int offset;
  117. int clientsocket; /* symux; incoming tcp connections */
  118. int symonsocket[AF_MAX]; /* symux; incoming symon data */
  119. int symuxsocket; /* symon; outgoing data to mux */
  120. struct symonpacket packet;
  121. struct sockaddr_storage sockaddr;
  122. struct streamlist sl;
  123. u_int32_t senderr;
  124. SLIST_ENTRY(mux) muxes;
  125. };
  126. SLIST_HEAD(muxlist, mux);
  127. /* ps2str types */
  128. #define PS2STR_PRETTY 0
  129. #define PS2STR_RRD 1
  130. /* Stream types */
  131. #define MT_IO1 0
  132. #define MT_CPU 1
  133. #define MT_MEM 2
  134. #define MT_IF 3
  135. #define MT_PF 4
  136. #define MT_DEBUG 5
  137. #define MT_PROC 6
  138. #define MT_MBUF 7
  139. #define MT_SENSOR 8
  140. #define MT_IO2 9
  141. #define MT_PFQ 10
  142. #define MT_DF 11
  143. #define MT_TEST 12
  144. #define MT_EOT 13
  145. /*
  146. * Unpacking of incoming packets is done via a packedstream structure. This
  147. * structure defines the maximum amount of data that can be contained in a
  148. * single network representation of a stream.
  149. */
  150. struct packedstream {
  151. int type;
  152. int padding;
  153. char arg[SYMON_PS_ARGLEN];
  154. union {
  155. struct symonpacketheader header;
  156. struct {
  157. u_int64_t mtotal_transfers;
  158. u_int64_t mtotal_seeks;
  159. u_int64_t mtotal_bytes;
  160. } ps_io;
  161. struct {
  162. u_int16_t muser;
  163. u_int16_t mnice;
  164. u_int16_t msystem;
  165. u_int16_t minterrupt;
  166. u_int16_t midle;
  167. } ps_cpu;
  168. struct {
  169. u_int32_t mreal_active;
  170. u_int32_t mreal_total;
  171. u_int32_t mfree;
  172. u_int32_t mswap_used;
  173. u_int32_t mswap_total;
  174. } ps_mem;
  175. struct {
  176. u_int32_t mipackets;
  177. u_int32_t mopackets;
  178. u_int32_t mibytes;
  179. u_int32_t mobytes;
  180. u_int32_t mimcasts;
  181. u_int32_t momcasts;
  182. u_int32_t mierrors;
  183. u_int32_t moerrors;
  184. u_int32_t mcolls;
  185. u_int32_t mdrops;
  186. } ps_if;
  187. struct {
  188. u_int64_t bytes_v4_in;
  189. u_int64_t bytes_v4_out;
  190. u_int64_t bytes_v6_in;
  191. u_int64_t bytes_v6_out;
  192. u_int64_t packets_v4_in_pass;
  193. u_int64_t packets_v4_in_drop;
  194. u_int64_t packets_v4_out_pass;
  195. u_int64_t packets_v4_out_drop;
  196. u_int64_t packets_v6_in_pass;
  197. u_int64_t packets_v6_in_drop;
  198. u_int64_t packets_v6_out_pass;
  199. u_int64_t packets_v6_out_drop;
  200. u_int64_t states_entries;
  201. u_int64_t states_searches;
  202. u_int64_t states_inserts;
  203. u_int64_t states_removals;
  204. u_int64_t counters_match;
  205. u_int64_t counters_badoffset;
  206. u_int64_t counters_fragment;
  207. u_int64_t counters_short;
  208. u_int64_t counters_normalize;
  209. u_int64_t counters_memory;
  210. } ps_pf;
  211. struct {
  212. u_int32_t debug0;
  213. u_int32_t debug1;
  214. u_int32_t debug2;
  215. u_int32_t debug3;
  216. u_int32_t debug4;
  217. u_int32_t debug5;
  218. u_int32_t debug6;
  219. u_int32_t debug7;
  220. u_int32_t debug8;
  221. u_int32_t debug9;
  222. u_int32_t debug10;
  223. u_int32_t debug11;
  224. u_int32_t debug12;
  225. u_int32_t debug13;
  226. u_int32_t debug14;
  227. u_int32_t debug15;
  228. u_int32_t debug16;
  229. u_int32_t debug17;
  230. u_int32_t debug18;
  231. u_int32_t debug19;
  232. } ps_debug;
  233. struct {
  234. u_int32_t totmbufs;
  235. u_int32_t mt_data;
  236. u_int32_t mt_oobdata;
  237. u_int32_t mt_control;
  238. u_int32_t mt_header;
  239. u_int32_t mt_ftable;
  240. u_int32_t mt_soname;
  241. u_int32_t mt_soopts;
  242. u_int32_t pgused;
  243. u_int32_t pgtotal;
  244. u_int32_t totmem;
  245. u_int32_t totpct;
  246. u_int32_t m_drops;
  247. u_int32_t m_wait;
  248. u_int32_t m_drain;
  249. } ps_mbuf;
  250. struct {
  251. int64_t value;
  252. } ps_sensor;
  253. struct {
  254. u_int64_t mtotal_rtransfers;
  255. u_int64_t mtotal_wtransfers;
  256. u_int64_t mtotal_seeks2;
  257. u_int64_t mtotal_rbytes;
  258. u_int64_t mtotal_wbytes;
  259. } ps_io2;
  260. struct {
  261. u_int64_t sent_bytes;
  262. u_int64_t sent_packets;
  263. u_int64_t drop_bytes;
  264. u_int64_t drop_packets;
  265. } ps_pfq;
  266. struct {
  267. u_int64_t L[4];
  268. int64_t D[4];
  269. u_int32_t l[4];
  270. u_int16_t s[4];
  271. u_int16_t c[4];
  272. u_int8_t b[4];
  273. } ps_test;
  274. struct {
  275. u_int64_t blocks;
  276. u_int64_t bfree;
  277. u_int64_t bavail;
  278. u_int64_t files;
  279. u_int64_t ffree;
  280. u_int64_t syncwrites;
  281. u_int64_t asyncwrites;
  282. } ps_df;
  283. } data;
  284. };
  285. /* prototypes */
  286. __BEGIN_DECLS
  287. const char *type2str(const int);
  288. const int token2type(const int);
  289. int calculate_churnbuffer(struct sourcelist *);
  290. int getheader(char *, struct symonpacketheader *);
  291. int ps2strn(struct packedstream *, char *, int, int);
  292. int setheader(char *, struct symonpacketheader *);
  293. int snpack(char *, int, char *, int,...);
  294. int strlentype(int);
  295. int sunpack(char *, struct packedstream *);
  296. struct mux *add_mux(struct muxlist *, char *);
  297. struct mux *find_mux(struct muxlist *, char *);
  298. struct mux *rename_mux(struct muxlist *, struct mux *, char *);
  299. struct source *add_source(struct sourcelist *, char *);
  300. struct source *find_source(struct sourcelist *, char *);
  301. struct source *find_source_sockaddr(struct sourcelist *, struct sockaddr *);
  302. struct stream *add_mux_stream(struct mux *, int, char *);
  303. struct stream *add_source_stream(struct source *, int, char *);
  304. struct stream *find_mux_stream(struct mux *, int, char *);
  305. struct stream *find_source_stream(struct source *, int, char *);
  306. u_int32_t crc32(const void *, unsigned int);
  307. void free_muxlist(struct muxlist *);
  308. void free_sourcelist(struct sourcelist *);
  309. void free_streamlist(struct streamlist *);
  310. void init_crc32();
  311. __END_DECLS
  312. #endif /* _SYMON_LIB_DATA_H */