1 #include <assert.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #ifdef __linux
5 #include <bsd/string.h> /* for strlcpy() */
6 #endif
7
8 #include <axa.h>
9 #include <axa/bits.h>
10 #include <axa/axa_endian.h>
11 #include <axa/wire.h>
12 #include <axa/protocol.h>
13 #include <check.h>
14
START_TEST(test_stats_sra)15 START_TEST(test_stats_sra)
16 {
17 bool res;
18 ssize_t total;
19 axa_tag_t tag;
20 axa_emsg_t emsg;
21 axa_p_hdr_t hdr;
22 axa_p_pvers_t pvers;
23 _axa_p_stats_req_t stats_req;
24 _axa_p_stats_rsp_t stats_rsp;
25 _axa_p_stats_sys_t stats_sys;
26 _axa_p_stats_user_t stats_user;
27
28 pvers = 1;
29 tag = 1;
30
31 memset(&stats_req, 0, sizeof(stats_req));
32 memset(&stats_rsp, 0, sizeof(stats_rsp));
33 memset(&stats_sys, 0, sizeof(stats_sys));
34 memset(&stats_user, 0, sizeof(stats_user));
35
36 stats_req.version = _AXA_STATS_VERSION_ONE;
37 stats_req.type = AXA_P_STATS_M_M_SUM;
38 memset(&stats_req.user, 0, sizeof(stats_req.user));
39 stats_req.sn = 0;
40
41 total = axa_make_hdr(&emsg, &hdr, pvers, tag, _AXA_P_OP_STATS_REQ,
42 sizeof (stats_req), 0, AXA_P_TO_SRA);
43
44 ck_assert_int_eq(total, sizeof (stats_req) + sizeof (hdr));
45
46 stats_rsp.version = _AXA_STATS_VERSION_ONE;
47 stats_rsp.sys_objs_cnt = 1;
48 stats_rsp.user_objs_cnt = 1;
49 stats_rsp.result = AXA_P_STATS_R_SUCCESS;
50
51 stats_sys.type = _AXA_P_STATS_TYPE_SYS;
52 stats_sys.server_type = _AXA_STATS_SRVR_TYPE_SRA;
53 stats_sys.load[0] = AXA_H2P32(30.0 * AXA_P_OPT_SAMPLE_SCALE);
54 stats_sys.load[1] = AXA_H2P32(45.0 * AXA_P_OPT_SAMPLE_SCALE);
55 stats_sys.load[2] = AXA_H2P32(60.0 * AXA_P_OPT_SAMPLE_SCALE);
56 stats_sys.cpu_usage = 99.9 * AXA_P_OPT_SAMPLE_SCALE;
57 stats_sys.uptime = 118709950;
58 stats_sys.starttime = 118709950;
59 stats_sys.fd_pipes = 10;
60 stats_sys.fd_anon_inodes = 20;
61 stats_sys.fd_other = 30;
62 stats_sys.vmsize = AXA_H2P64(1164000000);
63 stats_sys.vmrss = AXA_H2P64(1000);
64 stats_sys.rchar = 9999;
65 stats_sys.wchar = 8888;
66 stats_sys.thread_cnt = 1000;
67 stats_sys.user_cnt = 1;
68 stats_sys.srvr.sra.watches.ipv4_cnt = 100;
69 stats_sys.srvr.sra.watches.ipv6_cnt = 200;
70 stats_sys.srvr.sra.watches.dns_cnt = 300;
71 stats_sys.srvr.sra.watches.ch_cnt = 1;
72 stats_sys.srvr.sra.watches.err_cnt = 3;
73 res = axa_set_bitwords(stats_sys.srvr.sra.ch_mask.m, 255);
74 ck_assert_int_eq(res, 0);
75
76 strlcpy(stats_user.user.name, "rumi", sizeof (stats_user.user.name));
77 stats_user.io_type = AXA_IO_TYPE_APIKEY;
78 stats_user.addr_type = AXA_AF_INET;
79 stats_user.ip.ipv4 = AXA_H2P32(167772161); /* 10.0.0.1 */
80 stats_user.sn = 1;
81 stats_user.connected_since.tv_sec = AXA_H2P32(118709950);
82 stats_user.connected_since.tv_usec = 0;
83 stats_user.ratelimit = 0;
84 stats_user.sample = 100.0 * AXA_P_OPT_SAMPLE_SCALE;
85 stats_user.last_cnt_update.tv_sec = AXA_H2P32(118709950);
86 stats_user.last_cnt_update.tv_usec = 0;
87 stats_user.filtered = 10;
88 stats_user.missed = 20;
89 stats_user.collected = 30;
90 stats_user.sent = 40;
91 stats_user.rlimit = 50;
92 stats_user.congested = 60;
93 stats_user.srvr.sra.watches.ipv4_cnt = 100;
94 stats_user.srvr.sra.watches.ipv6_cnt = 200;
95 stats_user.srvr.sra.watches.dns_cnt = 300;
96 stats_user.srvr.sra.watches.ch_cnt = 1;
97 stats_user.srvr.sra.watches.err_cnt = 0;
98 res = axa_set_bitwords(stats_user.srvr.sra.ch_mask.m, 255);
99 stats_user.srvr.sra.flags = 0;
100 ck_assert_int_eq(res, 0);
101
102 total = axa_make_hdr(&emsg, &hdr, pvers, tag, _AXA_P_OP_STATS_RSP,
103 sizeof (stats_rsp) + sizeof (stats_user), 0,
104 AXA_P_FROM_SRA);
105
106 ck_assert_int_eq(total, sizeof (stats_rsp) + sizeof (stats_user) +
107 sizeof (hdr));
108 }
109 END_TEST
110
START_TEST(test_stats_rad)111 START_TEST(test_stats_rad)
112 {
113 ssize_t total;
114 axa_tag_t tag;
115 axa_emsg_t emsg;
116 axa_p_hdr_t hdr;
117 axa_p_pvers_t pvers;
118 _axa_p_stats_req_t stats_req;
119 _axa_p_stats_rsp_t stats_rsp;
120 _axa_p_stats_sys_t stats_sys;
121 _axa_p_stats_user_t stats_user;
122
123 pvers = 1;
124 tag = 1;
125
126 memset(&stats_req, 0, sizeof(stats_req));
127 memset(&stats_rsp, 0, sizeof(stats_rsp));
128 memset(&stats_sys, 0, sizeof(stats_sys));
129 memset(&stats_user, 0, sizeof(stats_user));
130
131 stats_req.version = _AXA_STATS_VERSION_ONE;
132 stats_req.type = AXA_P_STATS_M_M_SUM;
133 memset(&stats_req.user, 0, sizeof(stats_req.user));
134 stats_req.sn = 0;
135
136 total = axa_make_hdr(&emsg, &hdr, pvers, tag, _AXA_P_OP_STATS_REQ,
137 sizeof (stats_req), 0, AXA_P_TO_RAD);
138
139 ck_assert_int_eq(total, sizeof (stats_req) + sizeof (hdr));
140
141 stats_rsp.version = _AXA_STATS_VERSION_ONE;
142 stats_rsp.sys_objs_cnt = 1;
143 stats_rsp.user_objs_cnt = 1;
144 stats_rsp.result = AXA_P_STATS_R_SUCCESS;
145
146 stats_sys.type = _AXA_P_STATS_TYPE_SYS;
147 stats_sys.server_type = _AXA_STATS_SRVR_TYPE_RAD;
148 stats_sys.load[0] = AXA_H2P32(30.0 * AXA_P_OPT_SAMPLE_SCALE);
149 stats_sys.load[1] = AXA_H2P32(45.0 * AXA_P_OPT_SAMPLE_SCALE);
150 stats_sys.load[2] = AXA_H2P32(60.0 * AXA_P_OPT_SAMPLE_SCALE);
151 stats_sys.cpu_usage = 99.9 * AXA_P_OPT_SAMPLE_SCALE;
152 stats_sys.uptime = 118709950;
153 stats_sys.starttime = 118709950;
154 stats_sys.fd_pipes = 10;
155 stats_sys.fd_anon_inodes = 20;
156 stats_sys.fd_other = 30;
157 stats_sys.vmsize = AXA_H2P64(1164000000);
158 stats_sys.vmrss = AXA_H2P64(1000);
159 stats_sys.rchar = 9999;
160 stats_sys.wchar = 8888;
161 stats_sys.thread_cnt = 1000;
162 stats_sys.user_cnt = 1;
163 stats_sys.srvr.rad.an_cnt = 1;
164
165 strlcpy(stats_user.user.name, "wink", sizeof (stats_user.user.name));
166 stats_user.io_type = AXA_IO_TYPE_APIKEY;
167 stats_user.addr_type = AXA_AF_INET;
168 stats_user.ip.ipv4 = AXA_H2P32(167772161); /* 10.0.0.1 */
169 stats_user.sn = 1;
170 stats_user.connected_since.tv_sec = AXA_H2P32(118709950);
171 stats_user.connected_since.tv_usec = 0;
172 stats_user.ratelimit = 0;
173 stats_user.sample = 100.0 * AXA_P_OPT_SAMPLE_SCALE;
174 stats_user.last_cnt_update.tv_sec = AXA_H2P32(118709950);
175 stats_user.last_cnt_update.tv_usec = 0;
176 stats_user.filtered = 10;
177 stats_user.missed = 20;
178 stats_user.collected = 30;
179 stats_user.sent = 40;
180 stats_user.rlimit = 50;
181 stats_user.congested = 60;
182 stats_user.srvr.rad.an_obj_cnt = 1;
183 stats_user.srvr.rad.an_obj_cnt_total = 1;
184 stats_user.srvr.rad.flags = 0;
185
186 total = axa_make_hdr(&emsg, &hdr, pvers, tag, _AXA_P_OP_STATS_RSP,
187 sizeof (stats_rsp) + sizeof (stats_user), 0,
188 AXA_P_FROM_RAD);
189
190 ck_assert_int_eq(total, sizeof (stats_rsp) + sizeof (stats_user) +
191 sizeof (hdr));
192 }
193 END_TEST
main(void)194 int main(void) {
195 int number_failed;
196 Suite *s;
197 TCase *tc_core;
198 SRunner *sr;
199
200 s = suite_create("axa_stats");
201 tc_core = tcase_create("core");
202 tcase_add_test(tc_core, test_stats_sra);
203 tcase_add_test(tc_core, test_stats_rad);
204 suite_add_tcase(s, tc_core);
205
206 sr = srunner_create(s);
207
208 srunner_run_all(sr, CK_NORMAL);
209 number_failed = srunner_ntests_failed(sr);
210 srunner_free(sr);
211 return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
212 }
213