-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrequest_info.c
193 lines (159 loc) · 4.34 KB
/
request_info.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
* Part of `snmp-query-engine`.
*
* Copyright 2012-2013, Anton Berezin <tobez@tobez.org>
* Modified BSD license.
* (See LICENSE file in the distribution.)
*
*/
#include "sqe.h"
/*
* info request:
* [ 0, $cid ]
*
* dest_info request:
* [ RT_DEST_INFO, $cid, $ip, $port ]
*
*/
static int
pack_stats(struct program_stats *PS, msgpack_packer *pk)
{
int n = 0;
#define STAT(what) if (PS->what >= 0) { n++; if (pk) msgpack_pack_named_int(pk, #what, PS->what); }
STAT(active_client_connections);
STAT(total_client_connections);
STAT(client_requests);
STAT(invalid_requests);
STAT(setopt_requests);
STAT(getopt_requests);
STAT(info_requests);
STAT(get_requests);
STAT(gettable_requests);
STAT(dest_info_requests);
STAT(snmp_retries);
STAT(snmp_sends);
STAT(snmp_v1_sends);
STAT(snmp_v2c_sends);
STAT(snmp_timeouts);
STAT(udp_timeouts);
STAT(bad_snmp_responses);
STAT(good_snmp_responses);
STAT(oids_non_increasing);
STAT(oids_requested);
STAT(oids_returned_from_snmp);
STAT(oids_returned_to_client);
STAT(oids_ignored);
STAT(octets_received);
STAT(octets_sent);
STAT(active_timers_sec);
STAT(active_timers_usec);
STAT(total_timers_sec);
STAT(total_timers_usec);
STAT(uptime);
STAT(active_sid_infos);
STAT(total_sid_infos);
STAT(active_oid_infos);
STAT(total_oid_infos);
STAT(active_cid_infos);
STAT(total_cid_infos);
STAT(active_cr_infos);
STAT(total_cr_infos);
STAT(destination_throttles);
STAT(destination_ignores);
STAT(udp_receive_buffer_size);
STAT(udp_send_buffer_size);
STAT(udp_send_buffer_overflow);
STAT(packets_on_the_wire);
STAT(max_packets_on_the_wire);
STAT(global_throttles);
STAT(program_version);
#undef STAT
return n;
}
int
handle_info_request(struct socket_info *si, unsigned cid, msgpack_object *o)
{
msgpack_sbuffer* buffer;
msgpack_packer* pk;
char *key;
int l;
if (o->via.array.size < 2)
return error_reply(si, RT_INFO|RT_ERROR, cid, "bad request length");
PS.info_requests++;
si->PS.info_requests++;
buffer = msgpack_sbuffer_new();
pk = msgpack_packer_new(buffer, msgpack_sbuffer_write);
msgpack_pack_array(pk, 3);
msgpack_pack_int(pk, RT_INFO|RT_REPLY);
msgpack_pack_int(pk, cid);
if (o->via.array.size == 3 &&
o->via.array.ptr[2].type == MSGPACK_OBJECT_POSITIVE_INTEGER)
{
switch (o->via.array.ptr[2].via.u64) {
case 1: /* dump destinations */
dump_all_destinations(pk);
goto send;
}
}
PS.uptime = ms_passed_since(&prog_start);
si->PS.uptime = ms_passed_since(&si->created);
msgpack_pack_map(pk, 2);
key = "global";
l = strlen(key);
msgpack_pack_bin(pk, l);
msgpack_pack_bin_body(pk, key, l);
msgpack_pack_map(pk, pack_stats(&PS, NULL));
pack_stats(&PS, pk);
key = "connection";
l = strlen(key);
msgpack_pack_bin(pk, l);
msgpack_pack_bin_body(pk, key, l);
msgpack_pack_map(pk, pack_stats(&si->PS, NULL));
pack_stats(&si->PS, pk);
send:
tcp_send(si, buffer->data, buffer->size);
msgpack_sbuffer_free(buffer);
msgpack_packer_free(pk);
return 0;
}
static int
pack_dest_stats(struct destination *dest, msgpack_packer *pk)
{
int n = 0;
#define STAT(what) if (dest->what >= 0) { n++; if (pk) msgpack_pack_named_int(pk, #what, dest->what); }
STAT(octets_received);
STAT(octets_sent);
#undef STAT
return n;
}
int
handle_dest_info_request(struct socket_info *si, unsigned cid, msgpack_object *o)
{
msgpack_sbuffer* buffer;
msgpack_packer* pk;
unsigned port = 65536;
struct in_addr ip;
struct client_requests_info *cri;
if (o->via.array.size != 4)
return error_reply(si, RT_DEST_INFO|RT_ERROR, cid, "bad request length");
if (o->via.array.ptr[RI_DEST_INFO_PORT].type == MSGPACK_OBJECT_POSITIVE_INTEGER)
port = o->via.array.ptr[RI_DEST_INFO_PORT].via.u64;
if (port > 65535)
return error_reply(si, RT_DEST_INFO|RT_ERROR, cid, "bad port number");
if (!object2ip(&o->via.array.ptr[RI_DEST_INFO_IP], &ip))
return error_reply(si, RT_DEST_INFO|RT_ERROR, cid, "bad IP");
PS.dest_info_requests++;
si->PS.dest_info_requests++;
buffer = msgpack_sbuffer_new();
pk = msgpack_packer_new(buffer, msgpack_sbuffer_write);
msgpack_pack_array(pk, 3);
msgpack_pack_int(pk, RT_DEST_INFO|RT_REPLY);
msgpack_pack_int(pk, cid);
cri = get_client_requests_info(&ip, port, si);
msgpack_pack_map(pk, pack_dest_stats(cri->dest, NULL));
pack_dest_stats(cri->dest, pk);
tcp_send(si, buffer->data, buffer->size);
msgpack_sbuffer_free(buffer);
msgpack_packer_free(pk);
return 0;
}