forked from confluentinc/librdkafka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
misc.c
287 lines (231 loc) · 8.85 KB
/
misc.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
* librdkafka - Apache Kafka C library
*
* Copyright (c) 2022, Magnus Edenhill
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/**
* A collection of smaller usage examples
*/
#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#ifdef _WIN32
#include "../win32/wingetopt.h"
#else
#include <getopt.h>
#endif
/* Typical include path would be <librdkafka/rdkafka.h>, but this program
* is builtin from within the librdkafka source tree and thus differs. */
#include "rdkafka.h"
const char *argv0;
static void usage(const char *reason, ...) {
fprintf(stderr,
"Miscellaneous librdkafka usage examples\n"
"\n"
"Usage: %s <options> <command> [<command arguments>]\n"
"\n"
"Commands:\n"
" List groups:\n"
" %s -b <brokers> list_groups <group>\n"
"\n"
" Show librdkafka version:\n"
" %s version\n"
"\n"
"Common options for all commands:\n"
" -b <brokers> Bootstrap server list to connect to.\n"
" -X <prop=val> Set librdkafka configuration property.\n"
" See CONFIGURATION.md for full list.\n"
" -d <dbg,..> Enable librdkafka debugging (%s).\n"
"\n",
argv0, argv0, argv0, rd_kafka_get_debug_contexts());
if (reason) {
va_list ap;
char reasonbuf[512];
va_start(ap, reason);
vsnprintf(reasonbuf, sizeof(reasonbuf), reason, ap);
va_end(ap);
fprintf(stderr, "ERROR: %s\n", reasonbuf);
}
exit(reason ? 1 : 0);
}
#define fatal(...) \
do { \
fprintf(stderr, "ERROR: "); \
fprintf(stderr, __VA_ARGS__); \
fprintf(stderr, "\n"); \
exit(2); \
} while (0)
/**
* @brief Set config property. Exit on failure.
*/
static void conf_set(rd_kafka_conf_t *conf, const char *name, const char *val) {
char errstr[512];
if (rd_kafka_conf_set(conf, name, val, errstr, sizeof(errstr)) !=
RD_KAFKA_CONF_OK)
fatal("Failed to set %s=%s: %s", name, val, errstr);
}
/**
* Commands
*
*/
/**
* @brief Just print the librdkafka version
*/
static void cmd_version(rd_kafka_conf_t *conf, int argc, char **argv) {
if (argc)
usage("version command takes no arguments");
printf("librdkafka v%s\n", rd_kafka_version_str());
rd_kafka_conf_destroy(conf);
}
/**
* @brief Call rd_kafka_list_groups() with an optional groupid argument.
*/
static void cmd_list_groups(rd_kafka_conf_t *conf, int argc, char **argv) {
rd_kafka_t *rk;
const char *groupid = NULL;
char errstr[512];
rd_kafka_resp_err_t err;
const struct rd_kafka_group_list *grplist;
int i;
int retval = 0;
if (argc > 1)
usage("too many arguments to list_groups");
if (argc == 1)
groupid = argv[0];
/*
* Create consumer instance
* NOTE: rd_kafka_new() takes ownership of the conf object
* and the application must not reference it again after
* this call.
*/
rk = rd_kafka_new(RD_KAFKA_CONSUMER, conf, errstr, sizeof(errstr));
if (!rk)
fatal("Failed to create new consumer: %s", errstr);
/*
* List groups
*/
err = rd_kafka_list_groups(rk, groupid, &grplist, 10 * 1000 /*10s*/);
if (err)
fatal("rd_kafka_list_groups(%s) failed: %s", groupid,
rd_kafka_err2str(err));
if (grplist->group_cnt == 0) {
if (groupid) {
fprintf(stderr, "Group %s not found\n", groupid);
retval = 1;
} else {
fprintf(stderr, "No groups in cluster\n");
}
}
/*
* Print group information
*/
for (i = 0; i < grplist->group_cnt; i++) {
int j;
const struct rd_kafka_group_info *grp = &grplist->groups[i];
printf(
"Group \"%s\" protocol-type %s, protocol %s, "
"state %s, with %d member(s))",
grp->group, grp->protocol_type, grp->protocol, grp->state,
grp->member_cnt);
if (grp->err)
printf(" error: %s", rd_kafka_err2str(grp->err));
printf("\n");
for (j = 0; j < grp->member_cnt; j++) {
const struct rd_kafka_group_member_info *mb =
&grp->members[j];
printf(
" Member \"%s\" with client-id %s, host %s, "
"%d bytes of metadat, %d bytes of assignment\n",
mb->member_id, mb->client_id, mb->client_host,
mb->member_metadata_size,
mb->member_assignment_size);
}
}
rd_kafka_group_list_destroy(grplist);
/* Destroy the client instance */
rd_kafka_destroy(rk);
exit(retval);
}
int main(int argc, char **argv) {
rd_kafka_conf_t *conf; /**< Client configuration object */
int opt, i;
const char *cmd;
static const struct {
const char *cmd;
void (*func)(rd_kafka_conf_t *conf, int argc, char **argv);
} cmds[] = {
{"version", cmd_version},
{"list_groups", cmd_list_groups},
{NULL},
};
argv0 = argv[0];
if (argc == 1)
usage(NULL);
/*
* Create Kafka client configuration place-holder
*/
conf = rd_kafka_conf_new();
/*
* Parse common options
*/
while ((opt = getopt(argc, argv, "b:X:d:")) != -1) {
switch (opt) {
case 'b':
conf_set(conf, "bootstrap.servers", optarg);
break;
case 'X': {
char *name = optarg, *val;
if (!(val = strchr(name, '=')))
fatal("-X expects a name=value argument");
*val = '\0';
val++;
conf_set(conf, name, val);
break;
}
case 'd':
conf_set(conf, "debug", optarg);
break;
default:
usage("Unknown option %c", (char)opt);
}
}
if (optind == argc)
usage("No command specified");
cmd = argv[optind++];
/*
* Find matching command and run it
*/
for (i = 0; cmds[i].cmd; i++) {
if (!strcmp(cmds[i].cmd, cmd)) {
cmds[i].func(conf, argc - optind, &argv[optind]);
exit(0);
}
}
usage("Unknown command: %s", cmd);
/* NOTREACHED */
return 0;
}