blob: 37da7b32a5970efc75dc0ea77bed6fa69a736fb3 [file] [log] [blame]
Max7a79dd32022-12-02 23:13:43 +03001/* Rate counter and statsd test application */
2/* (C) 2022 by by sysmocom - s.f.m.c. GmbH
3 * All Rights Reserved
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 */
16
17#include <errno.h>
18#include <stdlib.h>
19#include <stdio.h>
20#include <getopt.h>
21#include <signal.h>
22#include <unistd.h>
23#include <inttypes.h>
24
25#include <osmocom/core/select.h>
26#include <osmocom/core/application.h>
27#include <osmocom/core/stats.h>
28#include <osmocom/ctrl/control_if.h>
29#include <osmocom/ctrl/control_vty.h>
30#include <osmocom/vty/vty.h>
31#include <osmocom/vty/telnet_interface.h>
32#include <osmocom/vty/command.h>
33#include <osmocom/vty/ports.h>
34#include <osmocom/vty/tdef_vty.h>
35#include <osmocom/vty/logging.h>
36#include <osmocom/vty/stats.h>
37#include <osmocom/vty/misc.h>
38
Pau Espin Pedrol88955fb2023-01-18 18:54:00 +010039#include "config.h"
Max7a79dd32022-12-02 23:13:43 +030040
41void *tall_statdummy_ctx = NULL;
42static bool quit = false;
43static bool config_given = false;
44struct rate_ctr_group *g_ctrg;
45
46enum dummy_rate_ctr_idx {
47 DUMMY_VTY = 0,
48 DUMMY_AUTO,
49};
50
51static void print_help(void)
52{
53 printf("Some useful options:\n"
54 " -h --help This text\n"
55 " -c --config-file Specify the filename of the config file\n"
56 " -V --version Print version\n"
57 "\nVTY reference generation:\n"
58 " --vty-ref-mode MODE VTY reference generation mode (e.g. 'expert').\n"
59 " --vty-ref-xml Generate the VTY reference XML output and exit.\n"
60 );
61}
62
63static void handle_long_options(const char *prog_name, const int long_option)
64{
65 static int vty_ref_mode = VTY_REF_GEN_MODE_DEFAULT;
66
67 switch (long_option) {
68 case 1:
69 vty_ref_mode = get_string_value(vty_ref_gen_mode_names, optarg);
70 if (vty_ref_mode < 0) {
71 fprintf(stderr, "%s: Unknown VTY reference generation mode '%s'\n", prog_name, optarg);
72 exit(2);
73 }
74 break;
75 case 2:
76 fprintf(stderr, "Generating the VTY reference in mode '%s' (%s)\n",
77 get_value_string(vty_ref_gen_mode_names, vty_ref_mode),
78 get_value_string(vty_ref_gen_mode_desc, vty_ref_mode));
79 vty_dump_xml_ref_mode(stdout, (enum vty_ref_gen_mode) vty_ref_mode);
80 exit(0);
81 default:
82 fprintf(stderr, "%s: error parsing cmdline options\n", prog_name);
83 exit(2);
84 }
85}
86
87static char *handle_options(int argc, char **argv)
88{
89 char *config_file = NULL;
90
91 while (1) {
92 int option_idx = 0, c;
93 static int long_option = 0;
94 static const struct option long_options[] = {
95 { "help", 0, 0, 'h' },
96 { "config-file", 1, 0, 'c' },
97 { "version", 0, 0, 'V' },
98 { "vty-ref-mode", 1, &long_option, 1 },
99 { "vty-ref-xml", 0, &long_option, 2 },
100 { 0, 0, 0, 0 }
101 };
102
103 c = getopt_long(argc, argv, "hc:V", long_options, &option_idx);
104 if (c == -1)
105 break;
106
107 switch (c) {
108 case 'h':
109 print_help();
110 exit(0);
111 break;
112 case 0:
113 handle_long_options(argv[0], long_option);
114 break;
115 case 'c':
116 if (config_file)
117 free(config_file);
118 config_file = optarg;
119 config_given = true;
120 break;
121 case 'V':
122 print_version(1);
123 exit(0);
124 break;
125 default:
126 fprintf(stderr, "Unknown option '%c'\n", c);
127 exit(0);
128 break;
129 }
130 }
131
132 if (!config_file)
133 return "osmo-stat-dummy.cfg";
134
135 return config_file;
136}
137
138void sighandler(int sigset)
139{
140 if (sigset == SIGPIPE)
141 return;
142
143 fprintf(stderr, "Signal %d received.\n", sigset);
144
145 switch (sigset) {
146 case SIGINT:
147 case SIGTERM:
148 /* If another signal is received afterwards, the program
149 * is terminated without finishing shutdown process.
150 */
151 signal(SIGINT, SIG_DFL);
152 signal(SIGTERM, SIG_DFL);
153 signal(SIGPIPE, SIG_DFL);
154 signal(SIGABRT, SIG_DFL);
155 signal(SIGUSR1, SIG_DFL);
156 signal(SIGUSR2, SIG_DFL);
157
158 quit = 1;
159 break;
160 case SIGABRT:
161 /* in case of abort, we want to obtain a talloc report and
162 * then run default SIGABRT handler, who will generate coredump
163 * and abort the process. abort() should do this for us after we
164 * return, but program wouldn't exit if an external SIGABRT is
165 * received.
166 */
167 talloc_report_full(tall_statdummy_ctx, stderr);
168 signal(SIGABRT, SIG_DFL);
169 raise(SIGABRT);
170 break;
171 case SIGUSR1:
172 case SIGUSR2:
173 talloc_report_full(tall_statdummy_ctx, stderr);
174 break;
175 }
176}
177
178static int rate_ctr_timer_cb(struct osmo_fd *ofd, unsigned int what)
179{
180 uint64_t expire_count;
181 int rc;
182
183 /* check that the timer has actually expired */
184 if (!(what & OSMO_FD_READ))
185 return 0;
186
187 /* read from timerfd: number of expirations of periodic timer */
188 rc = read(ofd->fd, (void *) &expire_count, sizeof(expire_count));
189 if (rc < 0 && errno == EAGAIN)
190 return 0;
191
192 OSMO_ASSERT(rc == sizeof(expire_count));
193
194 if (expire_count > 1)
195 LOGP(DLGLOBAL, LOGL_NOTICE, "Stats timer expire_count=%" PRIu64 ": We missed %" PRIu64 " timers\n",
196 expire_count, expire_count-1);
197
198 /* Increment the counter value */
199 rate_ctr_inc(rate_ctr_group_get_ctr(g_ctrg, DUMMY_AUTO));
200
201 return 0;
202}
203
204DEFUN(update_rate_ctr, update_rate_ctr_cmd,
205 "update-rate-ctr <0-100000>",
206 "Update dummy rate counter\n"
207 "Value to add to rate counter\n")
208{
209 rate_ctr_add(rate_ctr_group_get_ctr(g_ctrg, DUMMY_VTY), atoi(argv[0]));
210
211 return CMD_SUCCESS;
212}
213
214static int statdummy_vty_init(void)
215{
216 install_element_ve(&update_rate_ctr_cmd);
217
218 return 0;
219}
220
221int main(int argc, char *argv[])
222{
223 struct log_info log_info = {};
224 char *config_file;
225 void *ctx = tall_statdummy_ctx = talloc_named_const(NULL, 0, "osmo-stat-dummy");
226 struct ctrl_handle *ctrl;
227 struct osmo_fd rate_ctr_timer = { .fd = -1 };
228 struct timespec ts_interval = { .tv_sec = 0, .tv_nsec = 500000000 }; /* 0.5 seconds */
229 int rc = 0;
230
231 const char vty_copyright[] =
232 "Copyright (C) 2022 by by sysmocom - s.f.m.c. GmbH\r\n"
233 "Author: Max Suraev <msuraev@sysmocom.de>\r\n"
234 "License GNU GPL version 3 or later\r\n"
235 "This is free software: you are free to change and redistribute it.\r\n"
236 "There is NO WARRANTY, to the extent permitted by law.\r\n";
237
238 struct vty_app_info vty_info = {
239 .name = "OsmoSTATdummy",
240 .version = PACKAGE_VERSION,
241 .copyright = vty_copyright,
242 .tall_ctx = ctx
243 };
244
245 const struct rate_ctr_desc dummy_ctr_desc[] = {
246 [DUMMY_VTY] = { "dummy:vty", "Dummy counter updated via VTY" },
247 [DUMMY_AUTO] = { "dummy:auto", "Dummy counter autoupdated via timer" },
248 };
249
250 const struct rate_ctr_group_desc dummy_ctrg_desc = {
251 "dummy",
252 "dummy stat tester",
253 OSMO_STATS_CLASS_GLOBAL,
254 ARRAY_SIZE(dummy_ctr_desc),
255 dummy_ctr_desc,
256 };
257
258 osmo_init_logging2(ctx, &log_info);
259 log_set_use_color(osmo_stderr_target, 0);
260 log_set_print_filename2(osmo_stderr_target, LOG_FILENAME_NONE);
261 log_set_log_level(osmo_stderr_target, LOGL_INFO);
262
263 msgb_talloc_ctx_init(ctx, 0);
264
265 vty_init(&vty_info);
266 ctrl_vty_init(ctx);
267 logging_vty_add_cmds();
268 osmo_stats_vty_add_cmds();
269 osmo_talloc_vty_add_cmds();
270
271 config_file = handle_options(argc, argv);
272
273 statdummy_vty_init();
274 rc = vty_read_config_file(config_file, NULL);
275 if (rc < 0) {
276 if (config_given) {
277 fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
278 exit(1);
279 }
280 fprintf(stderr, "No config file: '%s' Using default config.\n", config_file);
281 }
282
283 rc = telnet_init_default(ctx, NULL, -1);
284 if (rc < 0) {
285 fprintf(stderr, "Error initializing telnet\n");
286 exit(1);
287 }
288
289 ctrl = ctrl_interface_setup(NULL, 1234, NULL);
290 if (!ctrl) {
291 fprintf(stderr, "Failed to initialize control interface. Exiting.\n");
292 exit(1);
293 }
294
295 g_ctrg = rate_ctr_group_alloc(ctx, &dummy_ctrg_desc, 0);
296 if (!g_ctrg) {
297 fprintf(stderr, "Failed to initialize rate counters. Exiting.\n");
298 return -1;
299 }
300
301 osmo_stats_init(ctx);
302 rate_ctr_init(ctx);
303
304 rc = osmo_timerfd_setup(&rate_ctr_timer, rate_ctr_timer_cb, NULL);
305 if (rc < 0) {
306 LOGP(DLGLOBAL, LOGL_ERROR, "Failed to setup the timer with error code %d (fd=%d)\n",
307 rc, rate_ctr_timer.fd);
308 return rc;
309 }
310
311 rc = osmo_timerfd_schedule(&rate_ctr_timer, NULL, &ts_interval);
312 if (rc < 0) {
313 LOGP(DLGLOBAL, LOGL_ERROR, "Failed to schedule the timer with error code %d (fd=%d)\n",
314 rc, rate_ctr_timer.fd);
315 }
316
317 signal(SIGINT, sighandler);
318 signal(SIGTERM, sighandler);
319 signal(SIGPIPE, sighandler);
320 signal(SIGABRT, sighandler);
321 signal(SIGUSR1, sighandler);
322 signal(SIGUSR2, sighandler);
323 osmo_init_ignore_signals();
324
325 while (!quit) {
326 osmo_select_main(0);
327 }
328
329 telnet_exit();
330
331 talloc_report_full(tall_statdummy_ctx, stderr);
332 talloc_free(tall_statdummy_ctx);
333
334 return 0;
335}