/* used to list all the registered events */ struct mi_root * mi_events_list(struct mi_root *cmd_tree, void *param) { struct mi_root *rpl_tree; struct mi_node *node=NULL, *rpl=NULL; unsigned i; rpl_tree = init_mi_tree( 200, MI_OK_S, MI_OK_LEN); if (rpl_tree==0) return 0; rpl = &rpl_tree->node; for (i = 0; i < events_no; i++) { node = add_mi_node_child(rpl, 0, "Event", 5, events[i].name.s, events[i].name.len); if(node == NULL) goto error; if (!addf_mi_attr(node, 0, "id", 2, "%d", events[i].id)) goto error; if ((i + 1) % 50 == 0) { flush_mi_tree(rpl_tree); } } return rpl_tree; error: free_mi_tree(rpl_tree); return 0; }
static int internal_mi_print_dlgs(struct mi_root *rpl_tree,struct mi_node *rpl, int with_context, unsigned int idx, unsigned int cnt) { struct dlg_cell *dlg; unsigned int i; unsigned int n; unsigned int total; char *p; total = 0; if (cnt) { for(i=0;i<d_table->size ; total+=d_table->entries[i++].cnt ); p = int2str((unsigned long)total, (int*)&n); if (add_mi_node_child(rpl,MI_DUP_VALUE,"dlg_counter",11,p,n)==0) return -1; } LM_DBG("printing %i dialogs, idx=%d, cnt=%d\n", total,idx,cnt); rpl->flags |= MI_NOT_COMPLETED; for( i=0,n=0 ; i<d_table->size ; i++ ) { dlg_lock( d_table, &(d_table->entries[i]) ); for( dlg=d_table->entries[i].first ; dlg ; dlg=dlg->next ) { if (cnt && n<idx) {n++;continue;} if (internal_mi_print_dlg(rpl, dlg, with_context)!=0) goto error; n++; if (cnt && n>=idx+cnt) { dlg_unlock( d_table, &(d_table->entries[i]) ); return 0; } if ( (n % 50) == 0 ) flush_mi_tree(rpl_tree); } dlg_unlock( d_table, &(d_table->entries[i]) ); } return 0; error: dlg_unlock( d_table, &(d_table->entries[i]) ); LM_ERR("failed to print dialog\n"); return -1; }
static int evi_print_event(struct evi_mi_param *param, evi_event_t *ev, evi_subs_p subs) { struct mi_node *node=NULL; struct mi_node *rpl = param->node; /* add event only if there are subscribers */ if (!subs && !ev->subscribers) return 0; node = add_mi_node_child(rpl, 0, "Event", 5, ev->name.s, ev->name.len); if(node == NULL) goto error; if (!addf_mi_attr(node, 0, "id", 2, "%d", ev->id)) goto error; if (subs) { if (evi_print_subscriber(node, subs) < 0) { LM_ERR("cannot print subscriber info\n"); goto error; } } else { for (subs = ev->subscribers; subs; subs = subs->next) { if (evi_print_subscriber(node, subs) < 0) { LM_ERR("cannot print subscriber info\n"); goto error; } if (++param->nr % 50 == 0) flush_mi_tree(param->root); } } return 0; error: return -1; }
struct mi_root *mi_tcp_list_conns(struct mi_root *cmd, void *param) { struct mi_root *rpl_tree; struct mi_node* node; struct mi_attr *attr; struct tcp_connection *conn; time_t _ts; char date_buf[MI_DATE_BUF_LEN]; int date_buf_len; unsigned int i,n,part; char proto[PROTO_NAME_MAX_SIZE]; char *p; int len; rpl_tree = init_mi_tree( 200, MI_SSTR(MI_OK)); if (rpl_tree==NULL) return 0; if (tcp_disabled) return rpl_tree; for( part=0 ; part<TCP_PARTITION_SIZE ; part++) { TCPCONN_LOCK(part); for( i=0,n=0 ; i<TCP_ID_HASH_SIZE ; i++ ) { for(conn=TCP_PART(part).tcpconn_id_hash[i];conn;conn=conn->id_next){ /* add one node for each conn */ node = add_mi_node_child(&rpl_tree->node, 0, MI_SSTR("Connection"), 0, 0 ); if (node==0) goto error; /* add ID */ p = int2str((unsigned long)conn->id, &len); attr = add_mi_attr( node, MI_DUP_VALUE, MI_SSTR("ID"), p, len); if (attr==0) goto error; /* add type/proto */ p = proto2str(conn->type, proto); attr = add_mi_attr( node, MI_DUP_VALUE, MI_SSTR("Type"), proto, (int)(long)(p-proto)); if (attr==0) goto error; /* add state */ p = int2str((unsigned long)conn->state, &len); attr = add_mi_attr( node, MI_DUP_VALUE,MI_SSTR("State"),p,len); if (attr==0) goto error; /* add Source */ attr = addf_mi_attr( node, MI_DUP_VALUE, MI_SSTR("Source"), "%s:%d",ip_addr2a(&conn->rcv.src_ip), conn->rcv.src_port); if (attr==0) goto error; /* add Destination */ attr = addf_mi_attr( node, MI_DUP_VALUE,MI_SSTR("Destination"), "%s:%d",ip_addr2a(&conn->rcv.dst_ip), conn->rcv.dst_port); if (attr==0) goto error; /* add lifetime */ _ts = (time_t)conn->lifetime + startup_time; date_buf_len = strftime(date_buf, MI_DATE_BUF_LEN - 1, "%Y-%m-%d %H:%M:%S", localtime(&_ts)); if (date_buf_len != 0) { attr = add_mi_attr( node, MI_DUP_VALUE,MI_SSTR("Lifetime"), date_buf, date_buf_len); } else { p = int2str((unsigned long)_ts, &len); attr = add_mi_attr( node, MI_DUP_VALUE,MI_SSTR("Lifetime"), p,len); } if (attr==0) goto error; n++; /* at each 50 conns, flush the tree */ if ( (n % 50) == 0 ) flush_mi_tree(rpl_tree); } } TCPCONN_UNLOCK(part); } return rpl_tree; error: TCPCONN_UNLOCK(part); LM_ERR("failed to add node\n"); free_mi_tree(rpl_tree); return 0; }
static int rl_map_print(void *param, str key, void *value) { struct mi_attr* attr; char* p; int len; struct rl_param_t * rl_param = (struct rl_param_t *)param; struct mi_node * rpl; rl_pipe_t *pipe = (rl_pipe_t *)value; struct mi_node * node; str *alg; if (!pipe) { LM_ERR("invalid pipe value\n"); return -1; } if (!rl_param || !rl_param->node || !rl_param->root) { LM_ERR("no reply node\n"); return -1; } rpl = rl_param->node; if (!key.len || !key.s) { LM_ERR("no key found\n"); return -1; } /* skip if no algo */ if (pipe->algo == PIPE_ALGO_NOP) return 0; if (!(node = add_mi_node_child(rpl, 0, "PIPE", 4, 0, 0))) return -1; if (!(attr = add_mi_attr(node, MI_DUP_VALUE, "id", 2, key.s, key.len))) return -1; if (!(alg = get_rl_algo_name(pipe->algo))) { LM_ERR("[BUG] unknown algorithm %d\n", pipe->algo); return -1; } if (!(attr = add_mi_attr(node, MI_DUP_VALUE, "algorithm", 9, alg->s, alg->len))) return -1; p = int2str((unsigned long)(pipe->limit), &len); if (!(attr = add_mi_attr(node, MI_DUP_VALUE, "limit", 5, p, len))) return -1; p = int2str((unsigned long)rl_get_all_counters(pipe), &len); if (!(attr = add_mi_attr(node, MI_DUP_VALUE, "counter", 7, p, len))) return -1; if ((++rl_param->counter % 50) == 0) { LM_DBG("flush mi tree - number %d\n", rl_param->counter); flush_mi_tree(rl_param->root); } return 0; }