summaryrefslogtreecommitdiff
path: root/infra/monitor/monitor_server.c
blob: d2b187a7456d2ee0c8e2b136bccc3afcb15d04a9 (plain)
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stddef.h>
#include <pthread.h>
#include <assert.h>
#include <evhttp.h>
#include <event2/event.h>
#include <event2/keyvalq_struct.h>
#include <sys/queue.h>
#include "stellar/log.h"
#include "monitor_private.h"
#include "monitor_cmd_assistant.h"
#include "monitor/monitor_utils.h"
#include "toml/toml.h"
#include "sds/sds.h"
#include "uthash/utlist.h"

static __thread struct stellar_monitor *__thread_local_stm;
static __thread pthread_t __thread_local_tid;
static __thread pthread_t __stm_libevevt_callback_thread_local_tid;
static __thread struct logger *__stm_thread_local_logger = NULL;

static void stm_save_thread_local_context(struct stellar_monitor *stm)
{
    __thread_local_stm = stm;
    __thread_local_tid = pthread_self();
    __stm_thread_local_logger = stm->logger_ref;
}

static void stm_connection_close_notify(struct stellar_monitor *stm, UNUSED struct evhttp_connection *evconn)
{
    struct stm_conn_close_cb_manager *ele, *tmp;
    DL_FOREACH_SAFE(stm->conn_close_mgr, ele, tmp)
    {
        ele->cb(&stm->current_conn, ele->arg);
    }
}

static void on_connection_close_cb(UNUSED struct evhttp_connection *ev_conn, UNUSED void *arg)
{
    __stm_libevevt_callback_thread_local_tid = pthread_self();
    struct stellar_monitor *stm = stellar_monitor_get();
    stm->current_conn.current_evconn_ref = ev_conn;
    stm_spinlock_lock(stm->lock);
    stm_connection_delete(ev_conn);
    stm_connection_close_notify(stm, ev_conn);
    stm_spinlock_unlock(stm->lock);
    char *peer_ip_addr;
    uint16_t peer_port;
    evhttp_connection_get_peer(ev_conn, &peer_ip_addr, &peer_port);
    STELLAR_LOG_INFO(stm->logger_ref, STM_LOG_MODULE_NAME, "cli connection closed, client %s:%u\n", peer_ip_addr, peer_port);
    stm_stat_update(stm->stat, stm->worker_thread_num, STM_STAT_CLI_CONNECTION_CLOSE, 1);
    stm->current_conn.current_evconn_ref = NULL;
}

static void stm_command_send_reply_by_cstr(struct evhttp_request *request, int http_status_code, const char *reply, UNUSED void *params)
{
    struct evbuffer *buffer = evbuffer_new();
    evbuffer_add(buffer, reply, strlen(reply));
    evhttp_send_reply(request, http_status_code, "OK", buffer);
    evbuffer_free(buffer);
}

static void stm_command_send_reply(struct evhttp_request *request, struct monitor_reply *reply)
{
    struct evbuffer *buffer = evbuffer_new();
    sds reply_str = monitor_reply_to_string(reply);
    evbuffer_add(buffer, reply_str, sdslen(reply_str));
    evhttp_send_reply(request, reply->http_code, reply->http_reason, buffer);
    evbuffer_free(buffer);
    sdsfree(reply_str);
    monitor_reply_free(reply);
}

static void stm_command_notfound(struct evhttp_request *request, UNUSED void *arg)
{
    struct stellar_monitor *stm = stellar_monitor_get();
    const char *req_str_uri = evhttp_request_get_uri(request);
    struct evkeyvalq headers = {};
    evhttp_parse_query(req_str_uri, &headers);
    const char *raw_cmd_content = evhttp_find_header(&headers, STM_RESTFUL_URI_CMD_KEY);

    stm_command_send_reply(request, monitor_reply_new_error(error_format_unknown_command, raw_cmd_content));
    STELLAR_LOG_ERROR(stm->logger_ref, STM_LOG_MODULE_NAME, "invlid http uri: %s\r\n", evhttp_request_get_uri(request));
    evhttp_clear_headers(&headers);
}

static void stm_exec_command(struct stellar_monitor *stm, struct evhttp_request *request, const char *cmd_line)
{
    stm_spinlock_lock(stm->lock);
    monitor_cmd_cb *cmd_cb = stm_cmd_assistant_get_cb(stm->aide, cmd_line);
    if (NULL == cmd_cb)
    {
        stm_command_notfound(request, NULL);
        stm_spinlock_unlock(stm->lock);
        return;
    }
    void *cmd_user_arg = stm_cmd_assistant_get_user_arg(stm->aide, cmd_line);
    int argc;
    sds *cmd_argv = sdssplitargs(cmd_line, &argc);
    struct monitor_reply *reply = cmd_cb(stm, argc, cmd_argv, cmd_user_arg);

    stm_command_send_reply(request, reply);
    sdsfreesplitres(cmd_argv, argc);
    stm_spinlock_unlock(stm->lock);
}

static void stm_new_request_cb(struct evhttp_request *request, UNUSED void *privParams)
{
    __stm_libevevt_callback_thread_local_tid = pthread_self();
    struct stellar_monitor *stm = stellar_monitor_get();
    struct evhttp_connection *ev_conn = evhttp_request_get_connection(request);
    stm->current_conn.current_evconn_ref = ev_conn;
    stm_spinlock_lock(stm->lock);
    stm_connection_insert(ev_conn);
    stm_spinlock_unlock(stm->lock);
    evhttp_connection_set_closecb(ev_conn, on_connection_close_cb, request);
    // evhttp_request_set_error_cb(request, on_request_error_cb);

    const char *req_str_uri = evhttp_request_get_uri(request);
    char *peer_ip_addr;
    uint16_t peer_port;
    evhttp_connection_get_peer(ev_conn, &peer_ip_addr, &peer_port);

    STELLAR_LOG_INFO(stm->logger_ref, STM_LOG_MODULE_NAME, "new cli request, client:%s:%u, uri: %s\n", peer_ip_addr, peer_port, req_str_uri);

    struct evkeyvalq headers = {};
    evhttp_parse_query(req_str_uri, &headers);
    const char *raw_cmd_content = evhttp_find_header(&headers, STM_RESTFUL_URI_CMD_KEY);
    if (NULL == raw_cmd_content)
    {
        stm_command_send_reply_by_cstr(request, HTTP_BADREQUEST, "http uri syntax error\r\n", NULL);
        evhttp_clear_headers(&headers);
        return;
    }
    stm_exec_command(stm, request, raw_cmd_content);
    evhttp_clear_headers(&headers);
}

static int stm_event_http_init(struct stellar_monitor *stm)
{
    // Create a new event handler
    stm->evt_base = event_base_new();
    // Create a http server using that handler
    stm->evt_http_server = evhttp_new(stm->evt_base);
    // Limit serving GET requests
    evhttp_set_allowed_methods(stm->evt_http_server, EVHTTP_REQ_GET);

    char restful_path[256] = {0}; /* must start with '/' */
    snprintf(restful_path, sizeof(restful_path), "/%s/%s", STM_RESTFUL_VERSION, STM_RESTFUL_RESOURCE);
    evhttp_set_cb(stm->evt_http_server, restful_path, stm_new_request_cb, stm->evt_base);

    // Set the callback for anything not recognized
    evhttp_set_gencb(stm->evt_http_server, stm_command_notfound, NULL);
    if (evhttp_bind_socket(stm->evt_http_server, stm->config->listen_ipaddr, stm->config->listen_port_host_order) != 0)
    {
        STELLAR_LOG_FATAL(stm->logger_ref, STM_LOG_MODULE_NAME, "Could not bind to %s:%u\r\n", stm->config->listen_ipaddr, stm->config->listen_port_host_order);
        return -1;
    }
    evhttp_set_timeout(stm->evt_http_server, stm->config->connection_idle_timeout);
    STELLAR_LOG_INFO(stm->logger_ref, STM_LOG_MODULE_NAME, "accept http uri path: %s\r\n", restful_path);
    return 0;
}

static void *stm_event_main_loop(void *arg)
{
    struct stellar_monitor *stm = (struct stellar_monitor *)arg;
    stm_save_thread_local_context(stm);
    event_base_dispatch(stm->evt_base);
    return NULL;
}

static void stm_event_http_free(struct stellar_monitor *stm)
{
    event_base_loopbreak(stm->evt_base);
    pthread_cancel(stm->evt_main_loop_tid);
    pthread_join(stm->evt_main_loop_tid, NULL);
    evhttp_free(stm->evt_http_server);
    // event_free(stm->ev_timeout);
    event_base_free(stm->evt_base);
}

static void stm_server_set_default_cfg(struct stellar_monitor_config *config)
{
    config->ringbuf_size = STM_RINGBUF_SIZE;
    config->connection_idle_timeout = STM_CONNECTION_IDLE_TIMEOUT;
    config->cli_request_timeout = STM_REQUEST_TIMEOUT;
    config->listen_ipaddr = "0.0.0.0";
    config->listen_port_host_order = STM_SERVER_LISTEN_PORT;
    config->data_link_bind_port_host_order = STM_TZSP_UDP_PORT;
    config->output_interval_ms = STM_STAT_OUTPUT_INTERVAL_MS;
}

struct stellar_monitor_config *stellar_monitor_config_new(const char *toml_file)
{
    struct stellar_monitor_config *config = CALLOC(struct stellar_monitor_config, 1);
    stm_server_set_default_cfg(config);
    int64_t int64_val = 0;
    char errbuf[256];
    FILE *fp = NULL;
    toml_table_t *root = NULL;
    toml_table_t *table = NULL;
    toml_raw_t ptr = NULL;

    fp = fopen(toml_file, "r");
    if (fp == NULL)
    {
        fprintf(stderr, "config file %s open failed, %s", toml_file, strerror(errno));
        goto fail_exit;
    }

    root = toml_parse_file(fp, errbuf, sizeof(errbuf));
    if (root == NULL)
    {
        fprintf(stderr, "config file %s parse failed, %s", toml_file, errbuf);
        goto fail_exit;
    }

    table = toml_table_in(root, "monitor");
    if (table == NULL)
    {
        fprintf(stderr, "config file %s missing [monitor]", toml_file);
        goto fail_exit;
    }

    /* listen_port */
    ptr = toml_raw_in(table, "listen_port");
    if (ptr != NULL && toml_rtoi(ptr, &int64_val) == 0)
    {
        if (int64_val < 1 || int64_val > 65535)
        {
            fprintf(stderr, "invalid monitor.listen_port %ld\n", int64_val);
            FREE(config);
            goto fail_exit;
        }
        config->listen_port_host_order = (uint16_t)int64_val;
    }

    /* data link bind port */
    ptr = toml_raw_in(table, "data_link_bind_port");
    if (ptr != NULL && toml_rtoi(ptr, &int64_val) == 0)
    {
        if (int64_val < 1 || int64_val > 65535)
        {
            fprintf(stderr, "invalid monitor.data_link_bind_port %ld\n", int64_val);
            FREE(config);
            goto fail_exit;
        }
        config->data_link_bind_port_host_order = (uint16_t)int64_val;
    }

    /* connection_idle_timeout */
    ptr = toml_raw_in(table, "connection_idle_timeout");
    if (ptr != NULL && toml_rtoi(ptr, &int64_val) == 0)
    {
        if (int64_val < 1 || int64_val > 3600)
        {
            fprintf(stderr, "invalid monitor.connection_idle_timeout %ld, should be [1, 3600]\n", int64_val);
            FREE(config);
            goto fail_exit;
        }
        config->connection_idle_timeout = (int)int64_val;
    }

    /* cli_request_timeout */
    ptr = toml_raw_in(table, "cli_request_timeout");
    if (ptr != NULL || toml_rtoi(ptr, &int64_val) == 0)
    {
        if (int64_val < 1 || int64_val > 360)
        {
            fprintf(stderr, "invalid monitor.cli_request_timeout %ld, , should be [1, 360]\n", int64_val);
            FREE(config);
            goto fail_exit;
        }
        config->cli_request_timeout = (int)int64_val;
    }

    /* stat */
    ptr = toml_raw_in(table, "stat_output_path");
    if (ptr == NULL || toml_rtos(ptr, &config->output_path) != 0)
    {
        config->output_path = strdup(STM_STAT_OUTPUT_PATH);
    }

    ptr = toml_raw_in(table, "stat_output_interval_ms");
    if (ptr != NULL && toml_rtoi(ptr, &int64_val) == 0)
    {
        if (int64_val < 1000 || int64_val > 1000 * 60)
        {
            fprintf(stderr, "invalid monitor.stat_output_interval_ms %ld, , should be [1, 600000]\n", int64_val);
            FREE(config);
            goto fail_exit;
        }
        config->output_interval_ms = (int)int64_val;
    }

fail_exit:
    if (root)
    {
        toml_free(root);
    }
    if (fp)
    {
        fclose(fp);
    }
    return config;
}

struct stellar_monitor *stellar_monitor_get(void)
{
    if (pthread_self() != __thread_local_tid)
    {
        assert(0);
        // fprintf(stderr, "ERR stellar_monitor_get() failed, caller must in same thread context!\n");
        return NULL;
    }
    return __thread_local_stm;
}

// support dynamic register command, independent of the order of initialization
int monitor_register_cmd(struct stellar_monitor *stm, const char *cmd, monitor_cmd_cb *cb, const char *flags,
                         const char *hint, const char *desc, void *arg)
{
    stm_spinlock_lock(stm->lock);
    int ret = stm_cmd_assistant_register_cmd(stm->aide, cmd, cb, arg, flags, hint, desc);
    stm_spinlock_unlock(stm->lock);
    return ret;
}

int monitor_register_connection_close_cb(struct stellar_monitor *stm, monitor_connection_close_cb *cb, void *arg)
{
    stm_spinlock_lock(stm->lock);
    struct stm_conn_close_cb_manager *ele = CALLOC(struct stm_conn_close_cb_manager, 1);
    ele->cb = cb;
    ele->arg = arg;
    DL_APPEND(stm->conn_close_mgr, ele);
    stm_spinlock_unlock(stm->lock);
    return 0;
}

static struct monitor_reply *monitor_cmd_show_brief_cb(struct stellar_monitor *stm)
{
    sds cmd_brief = sdsempty();
    cmd_brief = sdscatfmt(cmd_brief, "%s, %s\r\n", "\"command\"", "description");
    cmd_brief = sdscatfmt(cmd_brief, "-----------------------------\r\n");
    sds cmd_brief_cont = stm_cmd_assistant_list_cmd_brief(stm->aide);
    cmd_brief = sdscatsds(cmd_brief, cmd_brief_cont);
    struct monitor_reply *reply = monitor_reply_new_string("%s", cmd_brief);
    sdsfree(cmd_brief);
    sdsfree(cmd_brief_cont);
    return reply;
}

static struct monitor_reply *monitor_cmd_show_verbose_cb(struct stellar_monitor *stm)
{
    sds cmd_verbose = stm_cmd_assistant_list_cmd_verbose(stm->aide);
    struct monitor_reply *reply = monitor_reply_new_string("%s", cmd_verbose);
    sdsfree(cmd_verbose);
    return reply;
}

static struct monitor_reply *monitor_server_builtin_show_command_cb(struct stellar_monitor *stm UNUSED, int argc, char *argv[], UNUSED void *arg)
{
    if (argc != 3)
    {
        return monitor_reply_new_error(error_format_wrong_number_of_args, "show command");
    }
    if (stm_strncasecmp_exactly(argv[2], "brief", 5) == 0)
    {
        return monitor_cmd_show_brief_cb((struct stellar_monitor *)arg);
    }
    else if (stm_strncasecmp_exactly(argv[2], "verbose", 7) == 0)
    {
        return monitor_cmd_show_verbose_cb((struct stellar_monitor *)arg);
    }
    return monitor_reply_new_error(error_format_unknown_arg, argv[2]);
}

static struct monitor_reply *monitor_server_builtin_ping_cb(struct stellar_monitor *stm UNUSED, int argc, char *argv[], UNUSED void *arg)
{
    if (argc == 1)
    {
        return monitor_reply_new_string("pong");
    }
    else if (argc == 2)
    {
        return monitor_reply_new_string("%s", argv[1]);
    }
    return monitor_reply_new_error(error_format_wrong_number_of_args, "ping");
}

static struct monitor_reply *monitor_server_builtin_who_cb(struct stellar_monitor *stm, int argc UNUSED, char *argv[] UNUSED, UNUSED void *arg)
{
    struct stm_connection_manager *conn_mgr = stm->connection_mgr;
    struct stm_connection_manager *ele, *tmp;
    sds who = sdsempty();
    char timestr[64];
    DL_FOREACH_SAFE(conn_mgr, ele, tmp)
    {
        struct timeval tv = ele->link_start_time;
        struct tm *tm = localtime(&tv.tv_sec);
        strftime(timestr, sizeof(timestr), "%Y-%m-%d %H:%M:%S", tm);
        who = sdscatprintf(who, "%s %s:%u", timestr, ele->peer_ipaddr, ele->peer_port_host_order);
        if (stm->current_conn.current_evconn_ref == ele->conn)
        {
            who = sdscat(who, "\033[1m [current]\033[0m");
        }
        who = sdscat(who, "\r\n");
    }
    sdsIncrLen(who, -2); // delete last \r\n
    struct monitor_reply *reply = monitor_reply_new_string("%s", who);
    sdsfree(who);
    return reply;
}

static int stm_builtin_cmd_register(struct stellar_monitor *stm)
{
    int ret = 0;
    ret += monitor_register_cmd(stm, "show command", monitor_server_builtin_show_command_cb, "readonly", "[ brief|verbose ]", "show all registered commands info", (void *)stm);
    assert(ret == 0);
    ret += monitor_register_cmd(stm, "who", monitor_server_builtin_who_cb, "readonly", "<cr>", "show who is logged on", (void *)stm);
    assert(ret == 0);
    ret += monitor_register_cmd(stm, "ping", monitor_server_builtin_ping_cb, "readonly", "[message]", "ping the server", (void *)stm);
    assert(ret == 0);
    return ret;
}

struct monitor_connection *monitor_get_current_connection(struct stellar_monitor *monitor)
{
    if (__stm_libevevt_callback_thread_local_tid != pthread_self())
    {
        return NULL;
    }
    return &monitor->current_conn;
}

int monitor_get_peer_addr(struct monitor_connection *conn, char **peer_ip, uint16_t *peer_port)
{
    if (NULL == conn || conn->current_evconn_ref == NULL)
    {
        if (peer_ip)
        {
            *peer_ip = NULL;
        }
        if (peer_port)
        {
            *peer_port = 0;
        }
        return -1;
    }
    evhttp_connection_get_peer(conn->current_evconn_ref, peer_ip, peer_port);
    return 0;
}

void monitor_free(struct stellar_monitor *stm)
{
    STELLAR_LOG_FATAL(stm->logger_ref, STM_LOG_MODULE_NAME, "free and exit\n");
    stm_event_http_free(stm);
    stm_stat_free(stm->stat);
    stm_cmd_assistant_free(stm->aide);
    stm_spinlock_free(stm->lock);
    if (stm->rpc_ins_array)
    {
        for (int tid = 0; tid < stm->worker_thread_num; tid++)
        {
            monitor_rpc_free(stm->rpc_ins_array[tid]);
        }
        free(stm->rpc_ins_array);
    }

    __thread_local_stm = NULL;
    FREE(stm->config->output_path);
    FREE(stm->config);
    FREE(stm);
}

struct stellar_monitor *monitor_module_to_monitor(struct module *monitor_module)
{
    if (monitor_module == NULL)
    {
        return NULL;
    }
    return (struct stellar_monitor *)module_get_ctx(monitor_module);
}

struct stellar_monitor *stellar_module_get_monitor(struct module_manager *mod_mgr)
{
    assert(mod_mgr);
    struct module *monitor_mod = module_manager_get_module(mod_mgr, MONITOR_MODULE_NAME);
    return monitor_module_to_monitor(monitor_mod);
}

void monitor_on_exit(struct module_manager *mod_mgr __attribute__((unused)), struct module *mod)
{
    if (mod)
    {
        struct stellar_monitor *stm = module_get_ctx(mod);
        monitor_free(stm);
        module_free(mod);
    }
}

struct stellar_monitor *monitor_new(const char *toml_file, struct module_manager *mod_mgr, struct logger *logh)
{
    struct stellar_monitor *stm = (struct stellar_monitor *)calloc(1, sizeof(struct stellar_monitor));
    stm->logger_ref = logh;
    stm->mod_mgr_ref = mod_mgr;

    struct stellar_monitor_config *config = stellar_monitor_config_new(toml_file);
    if (NULL == config)
    {
        STELLAR_LOG_FATAL(logh, STM_LOG_MODULE_NAME, "get config failed!\n");
        goto fail_exit;
    }
    stm->config = config;

    stm->worker_thread_num = module_manager_get_max_thread_num(mod_mgr);

    stm->lock = stm_spinlock_new();
    stm->worker_thread_num = module_manager_get_max_thread_num(mod_mgr);
    assert(stm->worker_thread_num > 0);
    stm->aide = stm_cmd_assistant_new();
    if (stm_event_http_init(stm) < 0)
    {
        STELLAR_LOG_FATAL(stm->logger_ref, STM_LOG_MODULE_NAME, "libevent http server init() failed!\n");
        goto fail_exit;
    }
    stm->stat = stm_stat_init(stm);
    stm_builtin_cmd_register(stm);
    stm_save_thread_local_context(stm);
    pthread_create(&stm->evt_main_loop_tid, NULL, stm_event_main_loop, (void *)stm);
    sds config_print = stm_config_print(config);
    STELLAR_LOG_FATAL(stm->logger_ref, STM_LOG_MODULE_NAME, "config: %s\n", config_print);
    sdsfree(config_print);

    stm->rpc_ins_array = (struct monitor_rpc **)calloc(stm->worker_thread_num, sizeof(struct monitor_rpc *));
    for (int tid = 0; tid < stm->worker_thread_num; tid++)
    {
        stm->rpc_ins_array[tid] = monitor_rpc_new(stm, mod_mgr);
        if (stm->rpc_ins_array[tid] == NULL)
        {
            STELLAR_LOG_FATAL(stm->logger_ref, STM_LOG_MODULE_NAME, "rpc init failed\n");
            goto fail_exit;
        }
    }

    stm_save_thread_local_context(stm);
    return stm;

fail_exit:
    monitor_free(stm);
    return NULL;
}

struct module *monitor_on_init(struct module_manager *mod_mgr)
{
    assert(mod_mgr);
    const char *toml_file = module_manager_get_toml_path(mod_mgr);
    assert(toml_file);
    struct logger *logh = module_manager_get_logger(mod_mgr);
    assert(logh);

    struct stellar_monitor *stm = monitor_new(toml_file, mod_mgr, logh);
    struct module *stm_mod = module_new(MONITOR_MODULE_NAME, (void *)stm);
    if (stm_mod == NULL)
    {
        STELLAR_LOG_FATAL(logh, STM_LOG_MODULE_NAME, "moudule new '%s' fail\n", MONITOR_MODULE_NAME);
        monitor_free(stm);
        return NULL;
    }

    // show_session_enforcer_init(mod_mgr, stm);
    // stm_pktdump_enforcer_init(mod_mgr, stm);

    STELLAR_LOG_FATAL(logh, STM_LOG_MODULE_NAME, "init success\n");
    return stm_mod;
}