背景
mi_script
提供了方法调用mi管理接口, 支持同步或者异步方式.
官方地址:mi_script
本次测试的opensips版本为:
version: opensips 3.5.5 (x86_64/linux)
配置解析
1
2
3
4
5
6
|
# 是否格式化json返回值打印,默认:0
modparam("mi_script", "pretty_printing", 1)
# 设置trace的地址,要结合`sip_trace`使用
modparam("mi_stream", "trace_destination", "hep_dest")
# 设置trace的黑白名单
modparam("mi_stream", "trace_bwlist", "w: sip_trace")
|
主要函数
mi(command, [ret_var [,params_avp[, vals_avp]]])
同步调用mi接口,返回值为0表示成功.
如果想异步执行,可以这样:
1
2
3
4
5
6
7
|
route {
async(mi("dr_reload"), after_reload);
}
route[after_reload] {
xlog("reload completed\n");
}
|
实战
配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
loadmodule "mi_script.so"
modparam("mi_script", "pretty_printing", 1)
route {
...
if (is_method("REGISTER")) {
# indicate that the client supports DTLS
# so we know when he is called
if (isflagset("SRC_WS"))
setbflag("DST_WS");
fix_nated_register();
if (!save("location"))
sl_reply_error();
mi("ul_dump", $var(ret));
xlog("L_DBG","[$cfg_line][$ci]---main-1-:$rm|$rs|$tu|-dump: $var(ret)\n");
exit;
}
...
}
|
官方的示例:
1
2
3
|
modparam("proto_hep", "trace_destination", "[hep_dest]10.0.0.2;transport=tcp;version=3")
modparam("mi_stream", "trace_destination", "hep_dest")
|
存在的问题,已提issue
:
proto_hep
没有trance_destination
参数.
mi_stream
应该为mi_script
测试
mi("ul_dump", $var(ret))
打印的日志为:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
ul 9 11:01:34 [693] DBG:[201][fef6cc7387cd433189b0fde83b2f3942]---main-1-:REGISTER|<null>|sip:1005@172.16.4.111|-dump: {
"Domains": [{
"name": "location",
"hash_size": 512,
"AORs": [{
"AOR": "1005",
"Contacts": [{
"Contact": "sip:1005@172.16.80.13:60773;ob",
"ContactID": "3784712536851487464",
"Expires": "deleted",
"Q": "",
"Callid": "fef6cc7387cd433189b0fde83b2f3942",
"Cseq": 51796,
"User-agent": "MicroSIP/3.21.6",
"Received": "sip:172.16.80.13:60773",
"State": "CS_SYNC",
"Flags": 0,
"Cflags": "",
"Socket": "udp:172.16.4.111:5261",
"Methods": 8063
}]
}]
}]
}
|
mi(ul_show_contact, $var(ret), $avp(params))
参考示例:
1
2
3
4
5
6
|
$avp(params) = "local";
$avp(params) = "password_user1";
mi("cache_remove",,$avp(params));
# the following command is similar to the above
mi("cache_remove local password_user1");
|
按照示例方式配置, 目前请求失败:Table not found
:
1
2
3
|
$avp(params) = "location";
$avp(params) = "1005";
mi("ul_show_contact",$var(ret),$avp(params));
|
改成下面的设置:
1
|
mi("ul_show_contact location 1005",$var(ret));
|
可以正常返回结果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
{
"AOR": "1005",
"Contacts": [{
"Contact": "sip:1005@172.16.80.13:60773;ob",
"ContactID": "3784712536851487479",
"Expires": "deleted",
"Q": "",
"Callid": "9d06d410ffee4f65b9a20dc771a0076c",
"Cseq": 46793,
"User-agent": "MicroSIP/3.21.6",
"Received": "sip:172.16.80.13:60773",
"State": "CS_SYNC",
"Flags": 0,
"Cflags": "",
"Socket": "udp:172.16.4.111:5261",
"Methods": 8063
}]
}
|
总结
使用mi_script
模块,可以调用mi
接口,查看服务状态。