From fac9dcbd43f6b4a3c3ac95b9d578259608d1d150 Mon Sep 17 00:00:00 2001 From: Sergey Safarov Date: Fri, 25 Aug 2017 08:20:20 -0400 Subject: [PATCH] KAZOO-5623: Added PostgreSQL support --- kamailio/db_queries_kazoo.cfg | 13 ++++++++++++- kamailio/db_queries_mysql.cfg | 13 ++++++++++++- kamailio/db_queries_postgres.cfg | 13 +++++++++++++ kamailio/default.cfg | 2 +- kamailio/dispatcher-role.cfg | 2 +- kamailio/local.cfg | 4 ++++ kamailio/presence-reset.cfg | 16 ++++++++-------- kamailio/presence-role.cfg | 10 +++++----- kamailio/presence_query-role.cfg | 4 ++-- kamailio/registrar-sync-role.cfg | 2 +- 10 files changed, 59 insertions(+), 20 deletions(-) create mode 100644 kamailio/db_queries_postgres.cfg diff --git a/kamailio/db_queries_kazoo.cfg b/kamailio/db_queries_kazoo.cfg index aad4b0b..adc29fa 100644 --- a/kamailio/db_queries_kazoo.cfg +++ b/kamailio/db_queries_kazoo.cfg @@ -1,2 +1,13 @@ ####### Database queries ######## -#!substdef "!DISPATCHER_CHECK_MEDIA_SERVER_INSERT_QUERY!insert into dispatcher (setid, destination) select \$var(SetId), \"\$var(MediaUrl)\" where not exists(select * from dispatcher where destination = \"\$var(MediaUrl)\")!g" +#!substdef "!KZQ_CHECK_MEDIA_SERVER_INSERT!insert into dispatcher (setid, destination) select \$var(SetId), \"\$var(MediaUrl)\" where not exists(select * from dispatcher where destination = \"\$var(MediaUrl)\")!g" +#!substdef "!KZQ_COUNT_ALL_SUBSCRIBERS!select a.event, count(distinct watcher_username || \"@\" || watcher_domain) count_unique, count(*) count from event_list a, active_watchers b where b.event = a.event group by a.event!g" +#!substdef "!KZQ_COUNT_PRESENTITIES!select event, (select count(*) from presentity b where username = \"\$(kzE{kz.json,From}{uri.user})\" and domain = \"\$(kzE{kz.json,From}{uri.domain})\" and b.event = a.event) count from event_list a!g" +#!substdef "!KZQ_COUNT_SUBSCRIBERS!select event, (select count(*) from active_watchers b where presentity_uri = \"\$var(presentity)\" and b.event = a.event) count from event_list a!g" +#!substdef "!KZQ_EVENT_PRESENCE_RESET_DELETE!delete from presentity where domain=\"\$(kzE{kz.json,Realm})\" and username = \"\$(kzE{kz.json,Username})\"!g" +#!substdef "!KZQ_HANDLE_NEW_SUBSCRIBE_DELETE1!delete from active_watchers where callid = \"\$ci\"!g" +#!substdef "!KZQ_HANDLE_NEW_SUBSCRIBE_DELETE2!delete from active_watchers where watcher_username=\"\$fU\" and presentity_uri=\"\$var(presentity_uri)\" and to_user=\"\$tU\" and watcher_domain=\"\$fd\" and event=\"\$hdr(Event)\"!g" +#!substdef "!KZQ_RESET_ACCOUNT_DELETE!delete from presentity where domain=\"\$(kzE{kz.json,Realm})\"!g" +#!substdef "!KZQ_RESET_ACCOUNT_UPDATE!update active_watchers set expires = \$TS where watcher_domain=\"\$(kzE{kz.json,Realm})\"!g" +#!substdef "!KZQ_RESET_PUBLISHER_UPDATE!update active_watchers set expires = \$TS where id in (select b.id from presentity a inner join active_watchers b on a.username = b.to_user and a.domain = b.to_domain and a.event = b.event where a.sender = "\$var(MediaUrl)\")!g" +#!substdef "!KZQ_PRESENCE_SEARCH_DETAIL!select * from active_watchers_log where presentity_uri = \"\$var(presentity_uri)\"!g" +#!substdef "!KZQ_PRESENCE_SEARCH_SUMMARY!select * from active_watchers where watcher_domain = \"\$var(Domain)\"!g" diff --git a/kamailio/db_queries_mysql.cfg b/kamailio/db_queries_mysql.cfg index 853c53f..a2a8ef1 100644 --- a/kamailio/db_queries_mysql.cfg +++ b/kamailio/db_queries_mysql.cfg @@ -1,2 +1,13 @@ ####### Database queries ######## -#!substdef "!DISPATCHER_CHECK_MEDIA_SERVER_INSERT_QUERY!insert into dispatcher (setid, destination) select \$var(SetId), \"\$var(MediaUrl)\" from DUAL where not exists(select * from dispatcher where destination = \"\$var(MediaUrl)\")!g" +#!substdef "!KZQ_CHECK_MEDIA_SERVER_INSERT!insert into dispatcher (setid, destination) select \$var(SetId), \"\$var(MediaUrl)\" from DUAL where not exists(select * from dispatcher where destination = \"\$var(MediaUrl)\")!g" +#!substdef "!KZQ_COUNT_ALL_SUBSCRIBERS!select a.event, count(distinct watcher_username || \"@\" || watcher_domain) count_unique, count(*) count from event_list a, active_watchers b where b.event = a.event group by a.event!g" +#!substdef "!KZQ_COUNT_PRESENTITIES!select event, (select count(*) from presentity b where username = \"\$(kzE{kz.json,From}{uri.user})\" and domain = \"\$(kzE{kz.json,From}{uri.domain})\" and b.event = a.event) count from event_list a!g" +#!substdef "!KZQ_COUNT_SUBSCRIBERS!select event, (select count(*) from active_watchers b where presentity_uri = \"\$var(presentity)\" and b.event = a.event) count from event_list a!g" +#!substdef "!KZQ_EVENT_PRESENCE_RESET_DELETE!delete from presentity where domain=\"\$(kzE{kz.json,Realm})\" and username = \"\$(kzE{kz.json,Username})\"!g" +#!substdef "!KZQ_HANDLE_NEW_SUBSCRIBE_DELETE1!delete from active_watchers where callid = \"\$ci\"!g" +#!substdef "!KZQ_HANDLE_NEW_SUBSCRIBE_DELETE2!delete from active_watchers where watcher_username=\"\$fU\" and presentity_uri=\"\$var(presentity_uri)\" and to_user=\"\$tU\" and watcher_domain=\"\$fd\" and event=\"\$hdr(Event)\"!g" +#!substdef "!KZQ_RESET_ACCOUNT_DELETE!delete from presentity where domain=\"\$(kzE{kz.json,Realm})\"!g" +#!substdef "!KZQ_RESET_ACCOUNT_UPDATE!update active_watchers set expires = \$TS where watcher_domain=\"\$(kzE{kz.json,Realm})\"!g" +#!substdef "!KZQ_RESET_PUBLISHER_UPDATE!update active_watchers set expires = \$TS where id in (select b.id from presentity a inner join active_watchers b on a.username = b.to_user and a.domain = b.to_domain and a.event = b.event where a.sender = "\$var(MediaUrl)\")!g" +#!substdef "!KZQ_PRESENCE_SEARCH_DETAIL!select * from active_watchers_log where presentity_uri = \"\$var(presentity_uri)\"!g" +#!substdef "!KZQ_PRESENCE_SEARCH_SUMMARY!select * from active_watchers where watcher_domain = \"\$var(Domain)\"!g" diff --git a/kamailio/db_queries_postgres.cfg b/kamailio/db_queries_postgres.cfg new file mode 100644 index 0000000..f7cb517 --- /dev/null +++ b/kamailio/db_queries_postgres.cfg @@ -0,0 +1,13 @@ +####### Database queries ######## +#!substdef "!KZQ_CHECK_MEDIA_SERVER_INSERT!insert into dispatcher (setid, destination) select \$var(SetId), '\$var(MediaUrl)' where not exists(select * from dispatcher where destination = '\$var(MediaUrl)')!g" +#!substdef "!KZQ_COUNT_ALL_SUBSCRIBERS!select a.event, count(distinct watcher_username || '@' || watcher_domain) count_unique, count(*) count from event_list a, active_watchers b where b.event = a.event group by a.event!g" +#!substdef "!KZQ_COUNT_PRESENTITIES!select event, (select count(*) from presentity b where username = '\$(kzE{kz.json,From}{uri.user})' and domain = '\$(kzE{kz.json,From}{uri.domain})' and b.event = a.event) count from event_list a!g" +#!substdef "!KZQ_COUNT_SUBSCRIBERS!select event, (select count(*) from active_watchers b where presentity_uri = '\$var(presentity)' and b.event = a.event) count from event_list a!g" +#!substdef "!KZQ_EVENT_PRESENCE_RESET_DELETE!delete from presentity where domain='\$(kzE{kz.json,Realm})' and username = '\$(kzE{kz.json,Username})'!g" +#!substdef "!KZQ_HANDLE_NEW_SUBSCRIBE_DELETE1!delete from active_watchers where callid = '\$ci'!g" +#!substdef "!KZQ_HANDLE_NEW_SUBSCRIBE_DELETE2!delete from active_watchers where watcher_username='\$fU' and presentity_uri='\$var(presentity_uri)' and to_user='\$tU' and watcher_domain='\$fd' and event='\$hdr(Event)'!g" +#!substdef "!KZQ_RESET_ACCOUNT_DELETE!delete from presentity where domain='\$(kzE{kz.json,Realm})'!g" +#!substdef "!KZQ_RESET_ACCOUNT_UPDATE!update active_watchers set expires = \$TS where watcher_domain='\$(kzE{kz.json,Realm})'!g" +#!substdef "!KZQ_RESET_PUBLISHER_UPDATE!update active_watchers set expires = \$TS where id in (select b.id from presentity a inner join active_watchers b on a.username = b.to_user and a.domain = b.to_domain and a.event = b.event where a.sender = '\$var(MediaUrl)')!g" +#!substdef "!KZQ_PRESENCE_SEARCH_DETAIL!select * from active_watchers_log where presentity_uri = '\$var(presentity_uri)'!g" +#!substdef "!KZQ_PRESENCE_SEARCH_SUMMARY!select * from active_watchers where watcher_domain = '\$var(Domain)'!g" diff --git a/kamailio/default.cfg b/kamailio/default.cfg index 3788b22..10a6f39 100644 --- a/kamailio/default.cfg +++ b/kamailio/default.cfg @@ -181,7 +181,7 @@ modparam("sqlops","sqlcon", "cb=>KAZOO_DB_URL") modparam("sqlops","sqlcon", "exec=>KAZOO_DB_URL") ####### DATABASE module ########## -loadmodule "db_kazoo.so" +loadmodule "db_KAMAILIO_DBMS.so" ####### Kazoo Integration module ########## loadmodule "kazoo.so" diff --git a/kamailio/dispatcher-role.cfg b/kamailio/dispatcher-role.cfg index 89683b8..bb662b1 100644 --- a/kamailio/dispatcher-role.cfg +++ b/kamailio/dispatcher-role.cfg @@ -221,7 +221,7 @@ route[DISPATCHER_CHECK_MEDIA_SERVER] if($var(Zone) != "MY_AMQP_ZONE") { $var(SetId) = 2; } - sql_query("exec", "DISPATCHER_CHECK_MEDIA_SERVER_INSERT_QUERY"); + sql_query("exec", "KZQ_CHECK_MEDIA_SERVER_INSERT"); if($sqlrows(exec) > 0) { xlog("L_WARNING", "reloading dispatcher table\n"); ds_reload(); diff --git a/kamailio/local.cfg b/kamailio/local.cfg index e3d054c..11bf46f 100644 --- a/kamailio/local.cfg +++ b/kamailio/local.cfg @@ -23,6 +23,10 @@ # # #!trydef REGISTRAR_SYNC_ROLE # # #!trydef PRESENCE_NOTIFY_SYNC_ROLE +## PostgreSQL usage - remove all but the last '#' to enable +# # #!substdef "!KAMAILIO_DBMS!postgres!g" +# # #!substdef "!KAZOO_DB_URL!postgres://kamailio:kamailio@127.0.0.1/kamailio!g" + ################################################################################ ## SERVER INFORMATION ################################################################################ diff --git a/kamailio/presence-reset.cfg b/kamailio/presence-reset.cfg index d8ec858..7a8193d 100644 --- a/kamailio/presence-reset.cfg +++ b/kamailio/presence-reset.cfg @@ -27,9 +27,9 @@ route[PRESENCE_RESET_BINDINGS] route[RESET_PUBLISHER] { - xlog("L_INFO", "$var(Msg-ID)|reset|received presence reset for publisher $var(MediaUrl))\n"); + xlog("L_INFO", "$var(Msg-ID)|reset|received presence reset for publisher $var(MediaUrl))\n"); if(@cfg_get.kazoo.presence_reset_blf_defer_update == 0) { - $var(Query) = $_s(update active_watchers set expires = $TS where id in (select b.id from presentity a inner join active_watchers b on a.username = b.to_user and a.domain = b.to_domain and a.event = b.event where a.sender = "$var(MediaUrl)")); + $var(Query) = $_s(KZQ_RESET_PUBLISHER_UPDATE); sql_query("exec", "$var(Query)"); } $var(Query) = $_s(delete from presentity where sender = "$var(MediaUrl)"); @@ -61,9 +61,9 @@ route[RESET_SERVER] route[RESET_ACCOUNT] { xlog("L_INFO", "$(kzE{kz.json,Msg-ID})|reset|received presence reset for realm $(kzE{kz.json,Realm})\n"); - sql_query("exec", 'delete from presentity where domain="$(kzE{kz.json,Realm})" '); + sql_query("exec", 'KZQ_RESET_ACCOUNT_DELETE'); $var(presentities) = $sqlrows(exec); - sql_query("exec", 'update active_watchers set expires = $TS where watcher_domain="$(kzE{kz.json,Realm})" '); + sql_query("exec", 'KZQ_RESET_ACCOUNT_UPDATE'); $var(watchers) = $sqlrows(exec); xlog("L_INFO", "$(kzE{kz.json,Msg-ID})|reset|removed $var(presentities) presentities and expired $var(watchers) subscribers for realm $(kzE{kz.json,Realm})\n"); } @@ -94,7 +94,7 @@ route[RESET_WILDCARD] route(RESET_ACCOUNT); } } - + } @@ -104,12 +104,12 @@ event_route[kazoo:consumer-event-presence-reset] route(RESET_WILDCARD); exit(); } - + xlog("L_INFO", "$(kzE{kz.json,Msg-ID})|reset|received presence reset for $(kzE{kz.json,Username})@$(kzE{kz.json,Realm})\n"); $var(presentity) = $_s(sip:$(kzE{kz.json,Username})@$(kzE{kz.json,Realm})); route(COUNT_SUBSCRIBERS); - sql_query("exec", 'delete from presentity where domain="$(kzE{kz.json,Realm})" and username = "$(kzE{kz.json,Username})"'); - + sql_query("exec", 'KZQ_EVENT_PRESENCE_RESET_DELETE'); + if($xavp(watchers=>message-summary) > 0) { xlog("L_INFO", "$(kzE{kz.json,Msg-ID})|reset|notifying $xavp(watchers=>message-summary) message-summary subscribers of $var(presentity)\n"); pres_refresh_watchers("$var(presentity)", "message-summary", 1); diff --git a/kamailio/presence-role.cfg b/kamailio/presence-role.cfg index 1311968..faf372d 100644 --- a/kamailio/presence-role.cfg +++ b/kamailio/presence-role.cfg @@ -131,7 +131,7 @@ route[HANDLE_NEW_SUBSCRIBE] } if($shtinc(first=>$ci) > 1) { - sql_query("exec", 'delete from active_watchers where callid = "$ci"'); + sql_query("exec", 'KZQ_HANDLE_NEW_SUBSCRIBE_DELETE1'); xlog("L_INFO", "$ci|subscribe|resetting $hdr(Event) subscription from $fU to $tU in realm $fd : $sqlrows(exec)\n"); } else { $var(presentity_uri) = $ru; @@ -139,7 +139,7 @@ route[HANDLE_NEW_SUBSCRIBE] $var(presentity_uri) = $tu; } if($shtinc(first=>$fU::$var(presentity_uri)::$fd::$hdr(Event)) > 1) { - sql_query("exec", 'delete from active_watchers where watcher_username="$fU" and presentity_uri="$var(presentity_uri)" and to_user="$tU" and watcher_domain="$fd" and event="$hdr(Event)"'); + sql_query("exec", 'KZQ_HANDLE_NEW_SUBSCRIBE_DELETE2'); xlog("L_INFO", "$ci|subscribe|resetting $hdr(Event) subscription from $fU to $var(presentity_uri) in realm $fd : $sqlrows(exec)\n"); } } @@ -181,7 +181,7 @@ route[HANDLE_PUBLISH] route[COUNT_PRESENTITIES] { - $var(Query) = $_s(select event, (select count(*) from presentity b where username = "$(kzE{kz.json,From}{uri.user})" and domain = "$(kzE{kz.json,From}{uri.domain})" and b.event = a.event) count from event_list a); + $var(Query) = $_s(KZQ_COUNT_PRESENTITIES); $var(p) = "uri="+$(kzE{kz.json,From}); if (sql_xquery("cb", "$var(Query)", "subs") == 1) { @@ -209,7 +209,7 @@ route[COUNT_ALL_PRESENTITIES] route[COUNT_ALL_SUBSCRIBERS] { - $var(Query) = $_s(select a.event, count(distinct watcher_username || "@" || watcher_domain) count_unique, count(*) count from event_list a, active_watchers b where b.event = a.event group by a.event); + $var(Query) = $_s(KZQ_COUNT_ALL_SUBSCRIBERS); if (sql_xquery("cb", "$var(Query)", "subs") == 1) { $var(sep) = ""; @@ -226,7 +226,7 @@ route[COUNT_ALL_SUBSCRIBERS] route[COUNT_SUBSCRIBERS] { - $var(Query) = $_s(select event, (select count(*) from active_watchers b where presentity_uri = "$var(presentity)" and b.event = a.event) count from event_list a); + $var(Query) = $_s(KZQ_COUNT_SUBSCRIBERS); $var(p) = $_s(uri=$var(presentity)); if (sql_xquery("cb", "$var(Query)", "subs") == 1) { diff --git a/kamailio/presence_query-role.cfg b/kamailio/presence_query-role.cfg index 5f46ebf..2b9364d 100644 --- a/kamailio/presence_query-role.cfg +++ b/kamailio/presence_query-role.cfg @@ -9,7 +9,7 @@ route[PRESENCE_SEARCH_SUMMARY] $var(Username) = $(kzE{kz.json,Username}); $var(Now) = $TS; $var(Items) = ""; - $var(Query) = $_s(select * from active_watchers where watcher_domain = "$var(Domain)"); + $var(Query) = $_s(KZQ_PRESENCE_SEARCH_SUMMARY); if($var(Event) != "") { $var(Query) = $var(Query) + $_s( and event = "$var(Event)"); } @@ -62,7 +62,7 @@ route[PRESENCE_SEARCH_DETAIL] $var(presentity_uri) = "sip:" + $var(Username) + "@" + $var(Domain); $var(Now) = $TS; $var(Items) = ""; - $var(Query) = $_s(select * from active_watchers_log where presentity_uri = "$var(presentity_uri)"); + $var(Query) = $_s(KZQ_PRESENCE_SEARCH_DETAIL); if($var(Event) != "") { $var(Query) = $var(Query) + $_s( and event = "$var(Event)"); } diff --git a/kamailio/registrar-sync-role.cfg b/kamailio/registrar-sync-role.cfg index af2254b..4b1961c 100644 --- a/kamailio/registrar-sync-role.cfg +++ b/kamailio/registrar-sync-role.cfg @@ -4,7 +4,7 @@ event_route[kazoo:consumer-event-directory-reg-sync] { $var(Server) = $(kzE{kz.json,Server-ID}); xlog("L_INFO", "received registrar sync from $var(Server) : $kzE\n"); - + if (sql_xquery("cb", "select * from location", "ra") == 1) { while($xavp(ra) != $null) {