data.h 10 KB

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