data.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. * Copyright (c) 2001-2010 Willem Dijkstra
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * - Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * - Redistributions in binary form must reproduce the above
  12. * copyright notice, this list of conditions and the following
  13. * disclaimer in the documentation and/or other materials provided
  14. * with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  19. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  20. * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  22. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  26. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. * POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. */
  30. /*
  31. * A host carrying a 'symon' is considered a 'source' of information. A single
  32. * data 'stream' of information has a particular type: <cpu|mem|if|io>. A
  33. * source can provide multiple 'streams' simultaneously. A source spools
  34. * information towards a 'mux'. A 'stream' that has been converted to network
  35. * representation is called a 'packedstream'.
  36. */
  37. #ifndef _SYMON_LIB_DATA_H
  38. #define _SYMON_LIB_DATA_H
  39. #include "platform.h"
  40. #include <stdarg.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 and 2:
  66. * symon_version:timestamp:length:crc:n*packedstream
  67. * packedstream = type:arg[<SYMON_PS_ARGLENVx]:data
  68. */
  69. #define SYMON_PACKET_VER 2
  70. #define SYMON_UNKMUX "<unknown mux>" /* mux nodes without host addr */
  71. /* Sending structures over the network is dangerous as the compiler might have
  72. * added extra padding between items. symonpacketheader below is therefore also
  73. * marshalled and demarshalled via snpack and sunpack. The actual values are
  74. * copied out of memory into this structure one by one.
  75. */
  76. struct symonpacketheader {
  77. u_int64_t timestamp;
  78. u_int32_t crc;
  79. u_int16_t length;
  80. u_int8_t symon_version;
  81. u_int8_t reserved;
  82. };
  83. struct symonpacket {
  84. struct symonpacketheader header;
  85. u_int32_t offset;
  86. u_int32_t size;
  87. char *data;
  88. };
  89. /* The difference between a stream and a packed stream:
  90. * - A stream ties stream information to a file.
  91. * - A packed stream is the measured data itself
  92. *
  93. * A stream is meta data describing properties, a packed stream is the data itself.
  94. */
  95. struct stream {
  96. int type;
  97. char *arg;
  98. char *file;
  99. SLIST_ENTRY(stream) streams;
  100. union stream_parg parg;
  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. char *localaddr;
  115. struct sourcelist sol;
  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. int last;
  120. int interval;
  121. struct symonpacket packet;
  122. struct sockaddr_storage sockaddr;
  123. struct streamlist sl;
  124. u_int32_t senderr;
  125. SLIST_ENTRY(mux) muxes;
  126. };
  127. SLIST_HEAD(muxlist, mux);
  128. /* ps2str types */
  129. #define PS2STR_PRETTY 0
  130. #define PS2STR_RRD 1
  131. /* Stream types
  132. *
  133. * Add new items at the bottom, before entries for test and eot to preserve
  134. * compatibility with older symon instances
  135. */
  136. #define MT_IO1 0
  137. #define MT_CPU 1
  138. #define MT_MEM1 2
  139. #define MT_IF1 3
  140. #define MT_PF 4
  141. #define MT_DEBUG 5
  142. #define MT_PROC 6
  143. #define MT_MBUF 7
  144. #define MT_SENSOR 8
  145. #define MT_IO2 9
  146. #define MT_PFQ 10
  147. #define MT_DF 11
  148. #define MT_MEM2 12
  149. #define MT_IF2 13
  150. #define MT_CPUIOW 14
  151. #define MT_SMART 15
  152. #define MT_LOAD 16
  153. #define MT_TEST 17
  154. #define MT_EOT 18
  155. /*
  156. * Unpacking of incoming packets is done via a packedstream structure. This
  157. * structure defines the maximum amount of data that can be contained in a
  158. * single network representation of a stream.
  159. */
  160. struct packedstream {
  161. int type;
  162. int padding;
  163. char arg[SYMON_PS_ARGLENV2]; /* V2 > V1 */
  164. union {
  165. struct symonpacketheader header;
  166. struct {
  167. u_int64_t mtotal_transfers;
  168. u_int64_t mtotal_seeks;
  169. u_int64_t mtotal_bytes;
  170. } ps_io1;
  171. struct {
  172. u_int16_t muser;
  173. u_int16_t mnice;
  174. u_int16_t msystem;
  175. u_int16_t minterrupt;
  176. u_int16_t midle;
  177. } ps_cpu;
  178. struct {
  179. u_int32_t mreal_active;
  180. u_int32_t mreal_total;
  181. u_int32_t mfree;
  182. u_int32_t mswap_used;
  183. u_int32_t mswap_total;
  184. } ps_mem1;
  185. struct {
  186. u_int32_t mipackets;
  187. u_int32_t mopackets;
  188. u_int32_t mibytes;
  189. u_int32_t mobytes;
  190. u_int32_t mimcasts;
  191. u_int32_t momcasts;
  192. u_int32_t mierrors;
  193. u_int32_t moerrors;
  194. u_int32_t mcolls;
  195. u_int32_t mdrops;
  196. } ps_if1;
  197. struct {
  198. u_int64_t bytes_v4_in;
  199. u_int64_t bytes_v4_out;
  200. u_int64_t bytes_v6_in;
  201. u_int64_t bytes_v6_out;
  202. u_int64_t packets_v4_in_pass;
  203. u_int64_t packets_v4_in_drop;
  204. u_int64_t packets_v4_out_pass;
  205. u_int64_t packets_v4_out_drop;
  206. u_int64_t packets_v6_in_pass;
  207. u_int64_t packets_v6_in_drop;
  208. u_int64_t packets_v6_out_pass;
  209. u_int64_t packets_v6_out_drop;
  210. u_int64_t states_entries;
  211. u_int64_t states_searches;
  212. u_int64_t states_inserts;
  213. u_int64_t states_removals;
  214. u_int64_t counters_match;
  215. u_int64_t counters_badoffset;
  216. u_int64_t counters_fragment;
  217. u_int64_t counters_short;
  218. u_int64_t counters_normalize;
  219. u_int64_t counters_memory;
  220. } ps_pf;
  221. struct {
  222. u_int32_t debug0;
  223. u_int32_t debug1;
  224. u_int32_t debug2;
  225. u_int32_t debug3;
  226. u_int32_t debug4;
  227. u_int32_t debug5;
  228. u_int32_t debug6;
  229. u_int32_t debug7;
  230. u_int32_t debug8;
  231. u_int32_t debug9;
  232. u_int32_t debug10;
  233. u_int32_t debug11;
  234. u_int32_t debug12;
  235. u_int32_t debug13;
  236. u_int32_t debug14;
  237. u_int32_t debug15;
  238. u_int32_t debug16;
  239. u_int32_t debug17;
  240. u_int32_t debug18;
  241. u_int32_t debug19;
  242. } ps_debug;
  243. struct {
  244. u_int32_t totmbufs;
  245. u_int32_t mt_data;
  246. u_int32_t mt_oobdata;
  247. u_int32_t mt_control;
  248. u_int32_t mt_header;
  249. u_int32_t mt_ftable;
  250. u_int32_t mt_soname;
  251. u_int32_t mt_soopts;
  252. u_int32_t pgused;
  253. u_int32_t pgtotal;
  254. u_int32_t totmem;
  255. u_int32_t totpct;
  256. u_int32_t m_drops;
  257. u_int32_t m_wait;
  258. u_int32_t m_drain;
  259. } ps_mbuf;
  260. struct {
  261. int64_t value;
  262. } ps_sensor;
  263. struct {
  264. u_int64_t mtotal_rtransfers;
  265. u_int64_t mtotal_wtransfers;
  266. u_int64_t mtotal_seeks2;
  267. u_int64_t mtotal_rbytes;
  268. u_int64_t mtotal_wbytes;
  269. } ps_io2;
  270. struct {
  271. u_int64_t sent_bytes;
  272. u_int64_t sent_packets;
  273. u_int64_t drop_bytes;
  274. u_int64_t drop_packets;
  275. } ps_pfq;
  276. struct {
  277. u_int64_t L[4];
  278. int64_t D[4];
  279. u_int32_t l[4];
  280. u_int16_t s[4];
  281. u_int16_t c[4];
  282. u_int8_t b[4];
  283. } ps_test;
  284. struct {
  285. u_int64_t blocks;
  286. u_int64_t bfree;
  287. u_int64_t bavail;
  288. u_int64_t files;
  289. u_int64_t ffree;
  290. u_int64_t syncwrites;
  291. u_int64_t asyncwrites;
  292. } ps_df;
  293. struct {
  294. u_int64_t mreal_active;
  295. u_int64_t mreal_total;
  296. u_int64_t mfree;
  297. u_int64_t mswap_used;
  298. u_int64_t mswap_total;
  299. } ps_mem2;
  300. struct {
  301. u_int64_t mipackets;
  302. u_int64_t mopackets;
  303. u_int64_t mibytes;
  304. u_int64_t mobytes;
  305. u_int64_t mimcasts;
  306. u_int64_t momcasts;
  307. u_int64_t mierrors;
  308. u_int64_t moerrors;
  309. u_int64_t mcolls;
  310. u_int64_t mdrops;
  311. } ps_if2;
  312. struct {
  313. u_int16_t muser;
  314. u_int16_t mnice;
  315. u_int16_t msystem;
  316. u_int16_t minterrupt;
  317. u_int16_t midle;
  318. u_int16_t miowait;
  319. } ps_cpuiow;
  320. struct {
  321. u_int8_t read_error_rate;
  322. u_int8_t reallocated_sectors;
  323. u_int8_t spin_retries;
  324. u_int8_t air_flow_temp;
  325. u_int8_t temperature;
  326. u_int8_t reallocations;
  327. u_int8_t current_pending;
  328. u_int8_t uncorrectables;
  329. u_int8_t soft_read_error_rate;
  330. u_int8_t g_sense_error_rate;
  331. u_int8_t temperature2;
  332. u_int8_t free_fall_protection;
  333. } ps_smart;
  334. struct {
  335. u_int16_t mload1;
  336. u_int16_t mload2;
  337. u_int16_t mload3;
  338. } ps_load;
  339. } data;
  340. };
  341. /* prototypes */
  342. __BEGIN_DECLS
  343. char *type2str(const int);
  344. int bytelen_sourcelist(struct sourcelist *);
  345. int bytelen_streamlist(struct streamlist *);
  346. int gcd(int a, int b);
  347. int getheader(char *, struct symonpacketheader *);
  348. int ps2strn(struct packedstream *, char *, int, int);
  349. int setheader(char *, struct symonpacketheader *);
  350. int snpack(char *, int, char *, int, ...);
  351. int snpack1(char *, int, char *, int, ...);
  352. int snpack2(char *, int, char *, int, ...);
  353. int snpackx(size_t, char *, int, char *, int, va_list);
  354. int strlen_sourcelist(struct sourcelist *);
  355. int strlentype(int);
  356. int sunpack1(char *, struct packedstream *);
  357. int sunpack2(char *, struct packedstream *);
  358. int sunpackx(size_t, char *, struct packedstream *);
  359. int token2type(const int);
  360. struct mux *add_mux(struct muxlist *, char *);
  361. struct mux *find_mux(struct muxlist *, char *);
  362. struct mux *rename_mux(struct muxlist *, struct mux *, char *);
  363. struct source *add_source(struct sourcelist *, char *);
  364. struct source *find_source(struct sourcelist *, char *);
  365. struct source *find_source_sockaddr(struct sourcelist *, struct sockaddr *);
  366. struct stream *add_mux_stream(struct mux *, int, char *);
  367. struct stream *add_source_stream(struct source *, int, char *);
  368. struct stream *find_mux_stream(struct mux *, int, char *);
  369. struct stream *find_source_stream(struct source *, int, char *);
  370. u_int32_t crc32(const void *, unsigned int);
  371. void free_muxlist(struct muxlist *);
  372. void free_sourcelist(struct sourcelist *);
  373. void free_streamlist(struct streamlist *);
  374. void init_crc32();
  375. void init_symon_packet(struct mux *);
  376. void init_symux_packet(struct mux *);
  377. __END_DECLS
  378. #endif /* _SYMON_LIB_DATA_H */