data.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /* $Id: data.h,v 1.33 2008/01/30 12:06:49 dijkstra Exp $ */
  2. /*
  3. * Copyright (c) 2001-2008 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. #define SYMON_PACKET_VER 2
  71. #define SYMON_UNKMUX "<unknown mux>" /* mux nodes without host addr */
  72. /* Sending structures over the network is dangerous as the compiler might have
  73. * added extra padding between items. symonpacketheader below is therefore also
  74. * marshalled and demarshalled via snpack and sunpack. The actual values are
  75. * copied out of memory into this structure one by one.
  76. */
  77. struct symonpacketheader {
  78. u_int64_t timestamp;
  79. u_int32_t crc;
  80. u_int16_t length;
  81. u_int8_t symon_version;
  82. u_int8_t reserved;
  83. };
  84. struct symonpacket {
  85. struct symonpacketheader header;
  86. u_int32_t offset;
  87. u_int32_t size;
  88. char *data;
  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. char *localaddr;
  116. struct sourcelist sol;
  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. *
  132. * Add new items at the bottom to preserve compatibility with older symon
  133. * instances
  134. */
  135. #define MT_IO1 0
  136. #define MT_CPU 1
  137. #define MT_MEM1 2
  138. #define MT_IF1 3
  139. #define MT_PF 4
  140. #define MT_DEBUG 5
  141. #define MT_PROC 6
  142. #define MT_MBUF 7
  143. #define MT_SENSOR 8
  144. #define MT_IO2 9
  145. #define MT_PFQ 10
  146. #define MT_DF 11
  147. #define MT_MEM2 12
  148. #define MT_IF2 13
  149. #define MT_CPUIOW 14
  150. #define MT_TEST 15
  151. #define MT_EOT 16
  152. /*
  153. * Unpacking of incoming packets is done via a packedstream structure. This
  154. * structure defines the maximum amount of data that can be contained in a
  155. * single network representation of a stream.
  156. */
  157. struct packedstream {
  158. int type;
  159. int padding;
  160. char arg[SYMON_PS_ARGLENV2]; /* V2 > V1 */
  161. union {
  162. struct symonpacketheader header;
  163. struct {
  164. u_int64_t mtotal_transfers;
  165. u_int64_t mtotal_seeks;
  166. u_int64_t mtotal_bytes;
  167. } ps_io1;
  168. struct {
  169. u_int16_t muser;
  170. u_int16_t mnice;
  171. u_int16_t msystem;
  172. u_int16_t minterrupt;
  173. u_int16_t midle;
  174. } ps_cpu;
  175. struct {
  176. u_int32_t mreal_active;
  177. u_int32_t mreal_total;
  178. u_int32_t mfree;
  179. u_int32_t mswap_used;
  180. u_int32_t mswap_total;
  181. } ps_mem1;
  182. struct {
  183. u_int32_t mipackets;
  184. u_int32_t mopackets;
  185. u_int32_t mibytes;
  186. u_int32_t mobytes;
  187. u_int32_t mimcasts;
  188. u_int32_t momcasts;
  189. u_int32_t mierrors;
  190. u_int32_t moerrors;
  191. u_int32_t mcolls;
  192. u_int32_t mdrops;
  193. } ps_if1;
  194. struct {
  195. u_int64_t bytes_v4_in;
  196. u_int64_t bytes_v4_out;
  197. u_int64_t bytes_v6_in;
  198. u_int64_t bytes_v6_out;
  199. u_int64_t packets_v4_in_pass;
  200. u_int64_t packets_v4_in_drop;
  201. u_int64_t packets_v4_out_pass;
  202. u_int64_t packets_v4_out_drop;
  203. u_int64_t packets_v6_in_pass;
  204. u_int64_t packets_v6_in_drop;
  205. u_int64_t packets_v6_out_pass;
  206. u_int64_t packets_v6_out_drop;
  207. u_int64_t states_entries;
  208. u_int64_t states_searches;
  209. u_int64_t states_inserts;
  210. u_int64_t states_removals;
  211. u_int64_t counters_match;
  212. u_int64_t counters_badoffset;
  213. u_int64_t counters_fragment;
  214. u_int64_t counters_short;
  215. u_int64_t counters_normalize;
  216. u_int64_t counters_memory;
  217. } ps_pf;
  218. struct {
  219. u_int32_t debug0;
  220. u_int32_t debug1;
  221. u_int32_t debug2;
  222. u_int32_t debug3;
  223. u_int32_t debug4;
  224. u_int32_t debug5;
  225. u_int32_t debug6;
  226. u_int32_t debug7;
  227. u_int32_t debug8;
  228. u_int32_t debug9;
  229. u_int32_t debug10;
  230. u_int32_t debug11;
  231. u_int32_t debug12;
  232. u_int32_t debug13;
  233. u_int32_t debug14;
  234. u_int32_t debug15;
  235. u_int32_t debug16;
  236. u_int32_t debug17;
  237. u_int32_t debug18;
  238. u_int32_t debug19;
  239. } ps_debug;
  240. struct {
  241. u_int32_t totmbufs;
  242. u_int32_t mt_data;
  243. u_int32_t mt_oobdata;
  244. u_int32_t mt_control;
  245. u_int32_t mt_header;
  246. u_int32_t mt_ftable;
  247. u_int32_t mt_soname;
  248. u_int32_t mt_soopts;
  249. u_int32_t pgused;
  250. u_int32_t pgtotal;
  251. u_int32_t totmem;
  252. u_int32_t totpct;
  253. u_int32_t m_drops;
  254. u_int32_t m_wait;
  255. u_int32_t m_drain;
  256. } ps_mbuf;
  257. struct {
  258. int64_t value;
  259. } ps_sensor;
  260. struct {
  261. u_int64_t mtotal_rtransfers;
  262. u_int64_t mtotal_wtransfers;
  263. u_int64_t mtotal_seeks2;
  264. u_int64_t mtotal_rbytes;
  265. u_int64_t mtotal_wbytes;
  266. } ps_io2;
  267. struct {
  268. u_int64_t sent_bytes;
  269. u_int64_t sent_packets;
  270. u_int64_t drop_bytes;
  271. u_int64_t drop_packets;
  272. } ps_pfq;
  273. struct {
  274. u_int64_t L[4];
  275. int64_t D[4];
  276. u_int32_t l[4];
  277. u_int16_t s[4];
  278. u_int16_t c[4];
  279. u_int8_t b[4];
  280. } ps_test;
  281. struct {
  282. u_int64_t blocks;
  283. u_int64_t bfree;
  284. u_int64_t bavail;
  285. u_int64_t files;
  286. u_int64_t ffree;
  287. u_int64_t syncwrites;
  288. u_int64_t asyncwrites;
  289. } ps_df;
  290. struct {
  291. u_int64_t mreal_active;
  292. u_int64_t mreal_total;
  293. u_int64_t mfree;
  294. u_int64_t mswap_used;
  295. u_int64_t mswap_total;
  296. } ps_mem2;
  297. struct {
  298. u_int64_t mipackets;
  299. u_int64_t mopackets;
  300. u_int64_t mibytes;
  301. u_int64_t mobytes;
  302. u_int64_t mimcasts;
  303. u_int64_t momcasts;
  304. u_int64_t mierrors;
  305. u_int64_t moerrors;
  306. u_int64_t mcolls;
  307. u_int64_t mdrops;
  308. } ps_if2;
  309. struct {
  310. u_int16_t muser;
  311. u_int16_t mnice;
  312. u_int16_t msystem;
  313. u_int16_t minterrupt;
  314. u_int16_t midle;
  315. u_int16_t miowait;
  316. } ps_cpuiow;
  317. } data;
  318. };
  319. /* prototypes */
  320. __BEGIN_DECLS
  321. char *type2str(const int);
  322. int bytelen_sourcelist(struct sourcelist *);
  323. int bytelen_streamlist(struct streamlist *);
  324. int getheader(char *, struct symonpacketheader *);
  325. int ps2strn(struct packedstream *, char *, int, int);
  326. int setheader(char *, struct symonpacketheader *);
  327. int snpack(char *, int, char *, int, ...);
  328. int snpack1(char *, int, char *, int, ...);
  329. int snpack2(char *, int, char *, int, ...);
  330. int snpackx(size_t, char *, int, char *, int, va_list);
  331. int strlen_sourcelist(struct sourcelist *);
  332. int strlentype(int);
  333. int sunpack1(char *, struct packedstream *);
  334. int sunpack2(char *, struct packedstream *);
  335. int sunpackx(size_t, char *, struct packedstream *);
  336. int token2type(const int);
  337. struct mux *add_mux(struct muxlist *, char *);
  338. struct mux *find_mux(struct muxlist *, char *);
  339. struct mux *rename_mux(struct muxlist *, struct mux *, char *);
  340. struct source *add_source(struct sourcelist *, char *);
  341. struct source *find_source(struct sourcelist *, char *);
  342. struct source *find_source_sockaddr(struct sourcelist *, struct sockaddr *);
  343. struct stream *add_mux_stream(struct mux *, int, char *);
  344. struct stream *add_source_stream(struct source *, int, char *);
  345. struct stream *find_mux_stream(struct mux *, int, char *);
  346. struct stream *find_source_stream(struct source *, int, char *);
  347. u_int32_t crc32(const void *, unsigned int);
  348. void free_muxlist(struct muxlist *);
  349. void free_sourcelist(struct sourcelist *);
  350. void free_streamlist(struct streamlist *);
  351. void init_crc32();
  352. void init_symon_packet(struct mux *);
  353. void init_symux_packet(struct mux *);
  354. __END_DECLS
  355. #endif /* _SYMON_LIB_DATA_H */