3d42a78f47f141c2eecab2006031b89daa640d99
[sip-router] / src / modules / sipcapture / examples / kamailio.cfg
1 #!KAMAILIO
2 #
3 # Example configuration file for a sipcapture node
4 #
5
6 ####### Global Parameters definitions #########
7 #
8 # Please, make all your configuration changes here
9 #
10 # *** To enable extra stats
11 #     - define WITH_STATISTIC_METHOD_EXTRA
12 #     - define WITH_STATISTIC_INVITE_1XX
13
14
15
16 #!substdef "!HOMER_DB_USER!homer_user!g"
17 #!substdef "!HOMER_DB_PASSWORD!homer_password!g"
18 #!substdef "!HOMER_LISTEN_PROTO!udp!g"
19 #!substdef "!HOMER_LISTEN_IF!0.0.0.0!g"
20 #!substdef "!HOMER_LISTEN_PORT!9060!g"
21 #!substdef "!HOMER_STATS_SERVER!tcp:HOMER_LISTEN_IF:8888!g"
22
23 ####### Global Parameters #########
24
25 debug=1
26 log_stderror=no
27
28 memdbg=5
29 memlog=5
30
31 ##!define KAMAILIO_4_3
32 ##!define WITH_HOMER_GEO
33 ##!define WITH_HOMER_CUSTOM_STATS #enable it for HTTP custom stats
34
35 log_facility=LOG_LOCAL1
36
37 fork=yes
38 children=5
39
40 /* uncomment the next line to disable TCP (default on) */
41 disable_tcp=yes
42
43 /* IP and port for HEP capturing) */
44 listen=HOMER_LISTEN_PROTO:HOMER_LISTEN_IF:HOMER_LISTEN_PORT
45
46 #!ifdef WITH_HOMER_CUSTOM_STATS
47 listen=HOMER_STATS_SERVER
48 #!endif
49
50 #Max loops
51 max_while_loops=100
52
53 loadmodule "pv.so"
54 loadmodule "db_mysql.so"
55 loadmodule "sipcapture.so"
56 loadmodule "textops.so"
57 loadmodule "rtimer.so"
58 loadmodule "xlog.so"
59 loadmodule "sqlops.so"
60 loadmodule "htable.so"
61 loadmodule "tm.so"
62 loadmodule "sl.so"
63 loadmodule "siputils.so"
64 loadmodule "exec.so"
65
66 #!ifdef WITH_HOMER_GEO
67 loadmodule "geoip.so"
68 #!endif
69
70 #!ifdef WITH_HOMER_CUSTOM_STATS
71 loadmodule "xhttp.so"
72 loadmodule "jansson.so"
73 loadmodule "avpops.so"
74 #!endif
75
76
77 modparam("htable", "htable", "a=>size=8;autoexpire=400")
78 modparam("htable", "htable", "b=>size=8;autoexpire=31")
79 # TODO: tune autoexpire setting for htable "c"
80 modparam("htable", "htable", "c=>size=8;autoexpire=31")
81
82 #!ifdef WITH_HOMER_CUSTOM_STATS
83 modparam("htable", "htable", "d=>size=8;autoexpire=400")
84 modparam("xhttp", "url_match", "^/api/v1/stat")
85 #!endif
86
87 modparam("rtimer", "timer", "name=ta;interval=60;mode=1;")
88 modparam("rtimer", "exec", "timer=ta;route=TIMER_STATS")
89
90 modparam("sqlops","sqlcon","cb=>mysql://HOMER_DB_USER:HOMER_DB_PASSWORD@127.0.0.1/homer_statistic")
91
92 # ----- mi_fifo params -----
93
94 ####### Routing Logic ########
95 modparam("sipcapture", "db_url", "mysql://HOMER_DB_USER:HOMER_DB_PASSWORD@127.0.0.1/homer_data")
96 modparam("sipcapture", "capture_on", 1)
97 modparam("sipcapture", "hep_capture_on", 1)
98 modparam("sipcapture", "insert_retries", 5)
99 modparam("sipcapture", "insert_retry_timeout", 10)
100 #new event sipcapture socket 
101 modparam("sipcapture", "nonsip_hook", 1)
102 #modparam("sipcapture", "capture_node", "homer01")
103
104 #!ifdef WITH_HOMER_GEO
105 modparam("geoip", "path", "/usr/share/GeoIP/GeoIP.dat")
106 #!endif
107
108 #Stats time
109 stats.min = 5 desc "My stats TIME min"
110
111
112 # Main SIP request routing logic
113 # - processing of any incoming SIP request starts with this route
114 route {
115
116         if($sht(a=>method::total) == $null) $sht(a=>method::total) = 0;
117         $sht(a=>method::total) = $sht(a=>method::total) + 1;
118         if($sht(a=>packet::count) == $null) $sht(a=>packet::count) = 0;
119         if($sht(a=>packet::size) == $null) $sht(a=>packet::size) = 0;
120         
121          #Packets
122         $sht(a=>packet::count) = $sht(a=>packet::count) + 1;
123         $sht(a=>packet::size) = $sht(a=>packet::size) + $ml;
124
125
126         if($sht(b=>$rm::$cs::$ci) != $null) {
127                 #$var(a) = "sip_capture_call" + "_%Y%m%d";
128                 #Store
129                 route(STORE);
130                 drop;
131         }
132
133         $sht(b=>$rm::$cs::$ci) = 1;     
134         if($sht(a=>method::all) == $null) $sht(a=>method::all) = 0;
135         $sht(a=>method::all) = $sht(a=>method::all) + 1;
136         
137         if (is_method("INVITE|REGISTER")) {
138
139                 if($ua =~ "(friendly-scanner|sipvicious|sipcli)") {
140                         sql_query("cb", "INSERT INTO alarm_data_mem (create_date, type, total, source_ip, description) VALUES(NOW(), 'scanner', 1, '$si', 'Friendly scanner alarm!') ON DUPLICATE KEY UPDATE total=total+1");
141                         route(KILL_VICIOUS);
142                 }
143
144                 #IP Method
145                 sql_query("cb", "INSERT INTO stats_ip_mem ( method, source_ip, total) VALUES('$rm', '$si', 1) ON DUPLICATE KEY UPDATE total=total+1");
146
147 #!ifdef WITH_HOMER_GEO
148                 if(geoip_match("$si", "src")) {
149                         xlog("REGISTER|INVITE SIP message [$si] from: $gip(src=>cc)\n");
150                         sql_query("cb", "INSERT INTO stats_geo_mem ( method, country, lat, lon, total) VALUES('$rm', '$gip(src=>cc)', '$gip(src=>lat)', '$gip(src=>lon)', 1) ON DUPLICATE KEY UPDATE total=total+1");
151                 }
152 #!endif
153
154
155                 if($au != $null)  $var(anumber) = $au;
156                 else $var(anumber) = $fU;
157
158                 if($(var(anumber){s.substr,0,5}) == "+204231") {
159                         if($sht(a=>alarm::scam) == $null) $sht(a=>alarm::scam) = 0;
160                         $sht(a=>alarm::scam) = $sht(a=>alarm::scam) + 1;
161                 }
162
163                 if($(rU{s.substr,0,5}) == "+204231") {
164                         if($sht(a=>alarm::scam) == $null) $sht(a=>alarm::scam) = 0;
165                         $sht(a=>alarm::scam) = $sht(a=>alarm::scam) + 1; 
166                 }
167         
168         
169                 #if($sel(contact.uri.host) =~ "^([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))$") {
170                 if($sel(contact.uri.host) =~ "^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$") {
171                         if($sht(a=>alarm::dns) == $null) $sht(a=>alarm::dns) = 0;
172                         $sht(a=>alarm::dns) = $sht(a=>alarm::dns) + 1; 
173                 }
174
175
176                 if($sel(contact.uri.host) != $si) {
177                         if($sht(a=>alarm::spoofing) == $null) $sht(a=>alarm::spoofing) = 0;
178                         $sht(a=>alarm::spoofing) = $sht(a=>alarm::spoofing) + 1; 
179                 }
180
181                 if($au =~ "(\=)|(\-\-)|(\')|(\#)|(\%27)|(\%24)") {
182                         if($sht(a=>alarm::sqlinjection) == $null) $sht(a=>alarm::sqlinjection) = 0;
183                         $sht(a=>alarm::sqlinjection) = $sht(a=>alarm::sqlinjection) + 1; 
184                 }
185
186                 if($(hdr(Record-Route)[0]{nameaddr.uri}) != $si) {
187                         if($sht(a=>alarm::spoofing) == $null) $sht(a=>alarm::spoofing) = 0;
188                         $sht(a=>alarm::spoofing) = $sht(a=>alarm::spoofing) + 1; 
189                 }
190                 
191
192                 if (is_method("INVITE")) {                      
193
194                         if (has_totag()) {
195                                 if($sht(a=>method::reinvite) == $null) $sht(a=>method::reinvite) = 0;
196                                 $sht(a=>method::reinvite) = $sht(a=>method::reinvite) + 1;                              
197                         }
198                         else {
199                                 if($sht(a=>method::invite) == $null) $sht(a=>method::invite) = 0; 
200                                 $sht(a=>method::invite) = $sht(a=>method::invite) + 1; 
201                                 if($adu != $null) {
202                                         if($sht(a=>method::invite::auth) == $null) $sht(a=>method::invite::auth) = 0; 
203                                         $sht(a=>method::invite::auth) = $sht(a=>method::invite::auth) + 1; 
204                                 }
205
206                                 if($ua != $null) {
207                                         sql_query("cb", "INSERT INTO stats_useragent_mem (useragent, method, total) VALUES('$ua', 'INVITE', 1) ON DUPLICATE KEY UPDATE total=total+1");
208                                 }
209
210                         }                                       
211                 }
212                 else {
213                         if($sht(a=>method::register) == $null) $sht(a=>method::register) = 0;           
214                         $sht(a=>method::register) = $sht(a=>method::register) + 1; 
215
216                         if($adu != $null) {
217                                 if($sht(a=>method::register::auth) == $null) $sht(a=>method::register::auth) = 0; 
218                                 $sht(a=>method::register::auth) = $sht(a=>method::register::auth) + 1; 
219                         }
220                         
221                         if($ua != $null) {
222                                 sql_query("cb", "INSERT INTO stats_useragent_mem (useragent, method, total) VALUES('$ua', 'REGISTER', 1) ON DUPLICATE KEY UPDATE total=total+1");
223                         }
224                 }
225
226         }
227         else if(is_method("BYE")) {
228                 if($sht(a=>method::bye) == $null) $sht(a=>method::bye) = 0; 
229                 $sht(a=>method::bye) = $sht(a=>method::bye) + 1;                
230                 if(is_present_hf("Reason")) {
231                        $var(cause) = $(hdr(Reason){param.value,cause}{s.int});
232                        if($var(cause) != 16 && $var(cause) !=17) {
233                                 if($sht(a=>stats::sdf) == $null) $sht(a=>stats::sdf) = 0; 
234                                 $sht(a=>stats::sdf) = $sht(a=>stats::sdf) + 1; 
235                        }
236                 }
237
238 #               if($si != $sht(a=>ipinit::aleg::$ci) && $si != $sht(a=>ipinit::bleg::$ci)) {
239 #                       if($sht(a=>alarm::sessiontd) == $null) $sht(a=>alarm::sessiontd) = 0;
240 #                       $sht(a=>alarm::sessiontd) = $sht(a=>alarm::sessiontd) + 1; 
241 #               }
242
243         }
244         else if(is_method("CANCEL")) {
245                 if($sht(a=>method::cancel) == $null) $sht(a=>method::cancel) = 0; 
246                 $sht(a=>method::cancel) = $sht(a=>method::cancel) + 1; 
247
248         }
249         else if(is_method("OPTIONS")) {
250                 if($sht(a=>method::options) == $null) $sht(a=>method::options) = 0; 
251                 $sht(a=>method::options) = $sht(a=>method::options) + 1; 
252                 #xlog("OPTIONS COUNTING.... $sht(a=>method::options)\n");
253
254         }
255         else if(is_method("REFER")) {
256                 if($sht(a=>method::refer) == $null) $sht(a=>method::refer) = 0; 
257                 $sht(a=>method::refer) = $sht(a=>method::refer) + 1; 
258
259         }
260         else if(is_method("UPDATE")) {
261                 if($sht(a=>method::update) == $null) $sht(a=>method::update) = 0; 
262                 $sht(a=>method::update) = $sht(a=>method::update) + 1; 
263         }
264         
265         else if(is_method("PUBLISH"))
266         {
267                 if(has_body("application/vq-rtcpxr") && $(rb{s.substr,0,1}) != "x") {
268                         $var(table) = "report_capture";
269                         $var(callid) = $(rb{re.subst,/.*CallID:([0-9A-Za-z@-_\.]{5,120}).*$/\1/s});
270                         
271                         #Local IP. Only for stats
272                         #$var(localaddress) = $(rb{re.subst,/.*LocalAddr:IP=([^\S]+).*$/\1/s});
273
274                         #xlog("CALLID: $var(callid)");
275 #!ifdef KAMAILIO_4_3
276                         xlog("report_capture is not in < 4.3");
277 #!else
278                         report_capture("$var(table)", "$var(callid)");
279 #!endif                        
280                         drop;
281                 }
282         }
283         
284 #!ifdef WITH_STATISTIC_METHOD_EXTRA
285         else if(is_method("ACK")) {
286                 if($sht(a=>method::ack) == $null) $sht(a=>method::ack) = 0; 
287                 $sht(a=>method::ack) = $sht(a=>method::ack) + 1;
288         }
289         else {
290                 if($sht(a=>method::unknown) == $null) $sht(a=>method::unknown) = 0; 
291                 $sht(a=>method::unknown) = $sht(a=>method::uknown) + 1; 
292         }     
293 #!endif     
294
295         #Store
296         route(STORE);
297         drop;
298 }
299
300 onreply_route {
301
302         if($sht(a=>method::total) == $null) $sht(a=>method::total) = 0;
303         $sht(a=>method::total) = $sht(a=>method::total) + 1;
304
305         if($sht(b=>$rs::$cs::$rm::$ci) != $null) {
306                 #Store
307                 route(STORE);
308                 drop;
309         }
310
311         $sht(b=>$rs::$cs::$rm::$ci) = 1;
312         
313         if($sht(a=>method::all) == $null) $sht(a=>method::all) = 0;
314         $sht(a=>method::all) = $sht(a=>method::all) + 1;
315
316         #413 Too large
317         if(status == "413") {   
318                 if($sht(a=>response::413) == $null) $sht(a=>response::413) = 0; 
319                 $sht(a=>response::413) = $sht(a=>response::413) + 1; 
320
321                 if($sht(a=>alarm::413) == $null) $sht(a=>alarm::413) = 0; 
322                 $sht(a=>alarm::413) = $sht(a=>alarm::413) + 1; 
323         }
324         #403 Unauthorize
325         else if(status == "403") {
326                 if($sht(a=>response::403) == $null) $sht(a=>response::403) = 0; 
327                 $sht(a=>response::403) = $sht(a=>response::403) + 1; 
328
329                 if($sht(a=>alarm::403) == $null) $sht(a=>alarm::403) = 0; 
330                 $sht(a=>alarm::403) = $sht(a=>alarm::403) + 1; 
331         }
332         # Too many hops
333         else if(status == "483") {      
334                 if($sht(a=>response::483) == $null) $sht(a=>response::483) = 0; 
335                 $sht(a=>response::483) = $sht(a=>response::483) + 1; 
336
337                 if($sht(a=>alarm::483) == $null) $sht(a=>alarm::483) = 0; 
338                 $sht(a=>alarm::483) = $sht(a=>alarm::483) + 1; 
339
340         }
341         # loops
342         else if(status == "482") {      
343                 if($sht(a=>response::482) == $null) $sht(a=>response::482) = 0; 
344                 $sht(a=>response::482) = $sht(a=>response::482) + 1; 
345
346                 if($sht(a=>alarm::482) == $null) $sht(a=>alarm::482) = 0; 
347                 $sht(a=>alarm::482) = $sht(a=>alarm::482) + 1; 
348
349         }
350         # Call Transaction Does not exist
351         else if(status == "481") {      
352                 if($sht(a=>alarm::481) == $null) $sht(a=>alarm::481) = 0; 
353                 $sht(a=>alarm::481) = $sht(a=>alarm::481) + 1; 
354         }
355         # 408 Timeout
356         else if(status == "408") {      
357                 if($sht(a=>alarm::408) == $null) $sht(a=>alarm::408) = 0; 
358                 $sht(a=>alarm::408) = $sht(a=>alarm::408) + 1; 
359         }
360         # 400
361         else if(status == "400") {      
362
363                 if($sht(a=>alarm::400) == $null) $sht(a=>alarm::400) = 0; 
364                 $sht(a=>alarm::400) = $sht(a=>alarm::400) + 1; 
365
366         }
367         # MOVED
368         else if(status =~ "^(30[012])$") {      
369                 if($sht(a=>response::300) == $null) $sht(a=>response::300) = 0; 
370                 $sht(a=>response::300) = $sht(a=>response::300) + 1; 
371         }
372
373         if($rm == "INVITE") {
374                 #ISA
375                 if(status =~ "^(408|50[03])$") {        
376                         if($sht(a=>stats::isa) == $null) $sht(a=>stats::isa) = 0; 
377                         $sht(a=>stats::isa) = $sht(a=>stats::isa) + 1;  
378                 }
379                 #Bad486
380                 if(status =~ "^(486|487|603)$") {       
381                         if($sht(a=>stats::bad::invite) == $null) $sht(a=>stats::bad::invite) = 0; 
382                         $sht(a=>stats::bad::invite) = $sht(a=>stats::bad::invite) + 1;  
383                 }
384
385                 #SD
386                 if(status =~ "^(50[034])$") {   
387                         if($sht(a=>stats::sd) == $null) $sht(a=>stats::sd) = 0; 
388                         $sht(a=>stats::sd) = $sht(a=>stats::sd) + 1;    
389                 }
390
391                 if(status == "407") {   
392                         if($sht(a=>response::407::invite) == $null) $sht(a=>response::407::invite)= 0;
393                         $sht(a=>response::407::invite) = $sht(a=>response::407::invite) + 1; 
394                 }
395                 else if(status == "401") {                      
396                         if($sht(a=>response::401::invite) == $null) $sht(a=>response::401::invite)= 0;
397                         $sht(a=>response::401::invite) = $sht(a=>response::401::invite) + 1; 
398                 }
399                 else if(status == "200") {                      
400                         if($sht(a=>response::200::invite) == $null) $sht(a=>response::200::invite)= 0;
401                         $sht(a=>response::200::invite) = $sht(a=>response::200::invite) + 1; 
402                 }
403                 #Aditional stats
404 #!ifdef WITH_STATISTIC_INVITE_1XX               
405                 else if(status == "100") {
406                         if($sht(a=>response::100::invite) == $null) $sht(a=>response::100::invite)= 0;
407                         $sht(a=>response::100::invite) = $sht(a=>response::100::invite) + 1;
408                 }
409                 else if(status == "180") {
410                         if($sht(a=>response::180::invite) == $null) $sht(a=>response::180::invite)= 0;
411                         $sht(a=>response::180::invite) = $sht(a=>response::180::invite) + 1;
412                 }   
413                 else if(status == "183") {                      
414                         if($sht(a=>response::183::invite) == $null) $sht(a=>response::183::invite)= 0;
415                         $sht(a=>response::183::invite) = $sht(a=>response::183::invite) + 1;
416                 }
417 #!endif                
418         }
419         else if($rm == "BYE") {
420
421                 if(status == "407") {   
422                         if($sht(a=>response::407::bye) == $null) $sht(a=>response::407::bye) = 0;
423                         $sht(a=>response::407::bye) = $sht(a=>response::407::bye) + 1; 
424                 }
425                 else if(status == "401") {                      
426                         if($sht(a=>response::401::bye) == $null) $sht(a=>response::401::bye) = 0;
427                         $sht(a=>response::401::bye) = $sht(a=>response::401::bye) + 1; 
428                 }
429                 else if(status == "200") {                      
430                         if($sht(a=>response::200::bye) == $null) $sht(a=>response::200::bye) = 0;
431                         $sht(a=>response::200::bye) = $sht(a=>response::200::bye) + 1; 
432                 }
433         }
434         
435         #Store
436         route(STORE);
437         drop;
438 }
439
440 route[KILL_VICIOUS] {
441         xlog("Kill-Vicious ! si : $si ru : $ru ua : $ua\n");
442         return;
443 }
444
445 route[STATS_BY_IP] {
446         if($sht(c=>$rs::$sel(via[1].host)) == $null) $sht(c=>$rs::$sel(via[1].host)) = 0;
447         $sht(c=>$rs::$sel(via[1].host)) = $sht(c=>$rs::$sel(via[1].host)) + 1;
448 }
449
450 route[PARSE_STATS_IP] {
451         sht_iterator_start("i1", "c");
452         while(sht_iterator_next("i1")) {
453                 $var(sipcode) = $(shtitkey(i1){s.select,0,:});
454                 $var(ip) = $(shtitkey(i1){s.select,2,:});
455                 
456                 if($shtitval(i1) > $avp($var(sipcode))) {
457                         sql_query("cb", "INSERT INTO alarm_data (create_date, type, total, source_ip, description) VALUES(NOW(), 'Too Many $var(sipcode)', $shtitval(i1), '$var(ip)', 'Too Many $var(sipcode)')");
458                 }
459         }
460         sht_iterator_end("i1");
461         sht_rm_name_re("c=>.*");
462 }
463
464 route[TIMER_STATS] {
465
466     #xlog("timer routine: time is $TS\n");
467
468     route(CHECK_ALARM);
469     #Check statistics    
470     route(CHECK_STATS);
471
472 }
473
474
475 route[SEND_ALARM] {
476         exec_msg('echo "Value: $var(thvalue), Type: $var(atype), Desc: $var(aname)" | mail -s "Homer Alarm $var(atype) - $var(thvalue)" $var(aemail) ') ;
477 }
478
479 route[CHECK_ALARM] 
480 {
481
482     #POPULATE ALARM THRESHOLDS
483     #Homer 5 sql schema    
484     sql_query("cb", "SELECT type,value,name,notify,email FROM alarm_config WHERE NOW() between startdate AND stopdate AND active = 1", "ra");
485     if($dbr(ra=>rows)>0)
486     {
487         $var(i) = 0;
488         while($var(i)<$dbr(ra=>rows))
489         {
490                 $var(atype) = $dbr(ra=>[$var(i),0]);
491                 $var(avalue) = $dbr(ra=>[$var(i),1]);
492                 $var(aname) = $dbr(ra=>[$var(i),2]); 
493                 $var(anotify) = $(dbr(ra=>[$var(i),3]){s.int});
494                 $var(aemail) = $dbr(ra=>[$var(i),4]); 
495                 $avp($var(atype)) = $var(avalue);
496
497                 if($sht(a=>alarm::$var(atype)) != $null) {
498                         $var(thvalue) = $sht(a=>alarm::$var(atype));
499                 }
500
501                 #If Alarm - go here
502                 if($var(thvalue) > $var(avalue)) {
503                                                   
504                         sql_query("cb", "INSERT INTO alarm_data (create_date, type, total, description) VALUES(NOW(), '$var(aname)', $var(thvalue), '$var(aname) - $var(atype)');");
505                         #Notify
506                         if($var(anotify) == 1) {
507                                 route(SEND_ALARM);
508                         }                         
509                 }
510
511                 #Alarm for Scanner;
512                 if($var(atype) == "scanner") {
513                         sql_query("cb", "DELETE FROM alarm_data_mem WHERE type='scanner' AND total < $var(avalue)");
514                         if($var(anotify) == 1) 
515                         {
516                                 sql_query("cb", "SELECT * FROM alarm_data_mem WHERE type='scanner' AND total  >= $var(avalue) LIMIT 2", "rd");        
517                                 if($dbr(rd=>rows) > 0) {
518                                         route(SEND_ALARM);
519                                 }
520
521                                 sql_result_free("rd");
522                         }
523                 }
524          
525                 $sht(a=>alarm::$var(atype)) = 0;
526                 $var(i) = $var(i) + 1;
527         }
528
529     }
530
531
532     sql_result_free("ra");
533
534
535     #if(sql_xquery("cb", "select type,value,name,notify,email from alarm_config WHERE startdate > NOW() AND stopdate < NOW() AND active = 1", "trigger") == 1) {
536     #    $var(i) = 0;
537     #    while($xavp(trigger[$var(i)]) != $null) {
538     #      $avp($xavp(trigger[$var(i)]=>type)) = $xavp(trigger[$var(i)]=>value); 
539     #       $avp($xavp(trigger[$var(i)]=>type)+"_notify") = $xavp(trigger[$var(i)]=>notify); 
540     #       $avp($xavp(trigger[$var(i)]=>type)+"_email") = $xavp(trigger[$var(i)]=>email); 
541     #       $var(i) = $var(i) + 1;
542     #    }   
543     #}
544
545     route(PARSE_STATS_IP);
546
547     #delete old alarms
548     sql_query("cb", "DELETE FROM alarm_data WHERE create_date < DATE_SUB(NOW(), INTERVAL 5 DAY)");
549 }
550
551
552 route[CHECK_STATS] {
553
554     #SQL STATS
555     $var(tm) = ($time(min) mod 10);
556     if($var(tm) != 0 && $var(tm) != $sel(cfg_get.stats.min)) return;
557
558     $var(t1) = $TS;
559     $var(t2) = $var(t1) - ($sel(cfg_get.stats.min)*60);
560
561     $var(t_date) = "FROM_UNIXTIME(" + $var(t1) + ", '%Y-%m-%d %H:%i:00')";
562     $var(f_date) = "FROM_UNIXTIME(" + $var(t2) + ", '%Y-%m-%d %H:%i:00')";
563
564     #ALARM SCANNERS
565     sql_query("cb", "INSERT INTO alarm_data (create_date, type, total, source_ip, description) SELECT create_date, type, total, source_ip, description FROM alarm_data_mem;");
566     sql_query("cb", "TRUNCATE TABLE alarm_data_mem");
567
568     #STATS Useragent
569     sql_query("cb", "INSERT INTO stats_useragent (from_date, to_date, useragent, method, total) SELECT $var(f_date) as from_date, $var(t_date) as to_date, useragent, method, total FROM stats_useragent_mem;");
570     sql_query("cb", "TRUNCATE TABLE stats_useragent_mem");
571
572     #STATS IP
573     sql_query("cb", "INSERT INTO stats_ip (from_date, to_date, method, source_ip, total) SELECT $var(f_date) as from_date, $var(t_date) as to_date, method, source_ip, total FROM stats_ip_mem;");
574     sql_query("cb", "TRUNCATE TABLE stats_ip_mem");
575
576 #!ifdef WITH_HOMER_GEO
577     sql_query("cb", "INSERT INTO stats_geo (from_date, to_date, method, country, lat, lon, total) SELECT $var(f_date) as from_date, $var(t_date) as to_date, method, country, lat, lon, total FROM stats_geo_mem;");
578     sql_query("cb", "TRUNCATE TABLE stats_geo_mem");
579 #!endif
580     
581     #INSERT SQL STATS
582     #Packet HEP stats
583     if($sht(a=>packet::count) != $null && $sht(a=>packet::count) > 0) {
584         sql_query("cb", "INSERT INTO stats_data (from_date, to_date, type, total) VALUES($var(f_date), $var(t_date), 'packet_count', $sht(a=>packet::count)) ON DUPLICATE KEY UPDATE total=total+$sht(a=>packet::count)");
585         $sht(a=>packet::count) = 0;
586     }
587     if($sht(a=>packet::size) != $null && $sht(a=>packet::size) > 0) {
588         sql_query("cb", "INSERT INTO stats_data (from_date, to_date, type, total) VALUES($var(f_date), $var(t_date), 'packet_size', $sht(a=>packet::size)) ON DUPLICATE KEY UPDATE total=total+$sht(a=>packet::size)");
589         $sht(a=>packet::size) = 0;
590     }
591
592     #SDF
593     if($sht(a=>stats::sdf) != $null && $sht(a=>stats::sdf) > 0) {
594         sql_query("cb", "INSERT INTO stats_data (from_date, to_date, type, total) VALUES($var(f_date), $var(t_date), 'sdf', $sht(a=>stats::sdf)) ON DUPLICATE KEY UPDATE total=total+$sht(a=>stats::sdf)");
595         $sht(a=>stats::sdf) = 0;
596     }
597
598     #ISA
599     if($sht(a=>stats::isa) != $null && $sht(a=>stats::isa) > 0) {
600         sql_query("cb", "INSERT INTO stats_data (from_date, to_date, type, total) VALUES($var(f_date), $var(t_date), 'isa', $sht(a=>stats::isa)) ON DUPLICATE KEY UPDATE total=total+$sht(a=>stats::isa)");
601         $sht(a=>stats::isa) = 0;
602     }
603
604     #SD
605     if($sht(a=>stats::sd) != $null && $sht(a=>stats::sd) > 0) {
606         sql_query("cb", "INSERT INTO stats_data (from_date, to_date, type, total) VALUES($var(f_date), $var(t_date), 'isa', $sht(a=>stats::sd)) ON DUPLICATE KEY UPDATE total=total+$sht(a=>stats::sd)");
607         $sht(a=>stats::sd) = 0;
608     }
609
610     #SSR
611     if($sht(a=>stats::ssr) != $null && $sht(a=>stats::ssr) > 0) {
612         sql_query("cb", "INSERT INTO stats_data (from_date, to_date, type, total) VALUES($var(f_date), $var(t_date), 'ssr', $sht(a=>stats::ssr)) ON DUPLICATE KEY UPDATE total=total+$sht(a=>stats::ssr)");
613         $sht(a=>stats::ssr) = 0;
614     }
615
616     #ASR
617     $var(asr) = 0;
618     #if($sht(a=>response::200::invite) > 0) {
619     if($sht(a=>method::invite) > 0) {
620         if($sht(a=>response::407::invite) == $null) $sht(a=>response::407::invite) = 0;
621         if($sht(a=>response::200::invite) == $null) $sht(a=>response::200::invite) = 0;
622         $var(d) = $sht(a=>method::invite) - $sht(a=>response::407::invite);
623         if($var(d) > 0) {
624                 $var(asr) =  $sht(a=>response::200::invite) * 100 / $var(d);
625                 if($var(asr) > 100)  $var(asr) = 100;
626         }
627     }
628
629     #Stats DATA
630     sql_query("cb", "INSERT INTO stats_data (from_date, to_date, type, total) VALUES($var(f_date), $var(t_date), 'asr', $var(asr)) ON DUPLICATE KEY UPDATE total=(total+$var(asr))/2");
631
632
633     #NER
634     $var(ner) = 0;
635     #if($sht(a=>response::200::invite) > 0 || $sht(a=>stats::bad::invite) > 0) {
636     if($sht(a=>method::invite) > 0) {
637
638         if($sht(a=>response::200::invite) == $null) $sht(a=>response::200::invite) = 0;
639         if($sht(a=>response::bad::invite) == $null) $sht(a=>response::bad::invite) = 0;
640         if($sht(a=>response::407::invite) == $null) $sht(a=>response::407::invite) = 0;
641
642         $var(d) = $sht(a=>method::invite) - $sht(a=>response::407::invite);
643
644         if($var(d) > 0) {
645                 $var(ner) = ($sht(a=>response::200::invite) + $sht(a=>stats::bad::invite)) * 100 / $var(d);
646                 if($var(ner) > 100)  $var(ner) = 100;
647         }
648     }
649
650     sql_query("cb", "INSERT INTO stats_data (from_date, to_date, type, total) VALUES($var(f_date), $var(t_date), 'ner', $var(ner)) ON DUPLICATE KEY UPDATE total=(total+$var(ner))/2");
651
652     #INVITE
653     if($sht(a=>method::reinvite) > 0) {
654         sql_query("cb", "INSERT INTO stats_method (from_date, to_date, method, totag, total) VALUES($var(f_date), $var(t_date),'INVITE', 1, $sht(a=>method::reinvite)) ON DUPLICATE KEY UPDATE total=total+$sht(a=>method::reinvite)");
655         $sht(a=>method::reinvite) = 0;
656     }
657
658     #INVITE
659     if($sht(a=>method::invite) > 0) {
660         sql_query("cb", "INSERT INTO stats_method (from_date, to_date, method, total) VALUES($var(f_date), $var(t_date), 'INVITE', $sht(a=>method::invite)) ON DUPLICATE KEY UPDATE total=total+$sht(a=>method::invite)");
661         $sht(a=>method::invite) = 0;
662     }
663
664     #INVITE AUTH
665     if($sht(a=>method::invite::auth) > 0) {
666         sql_query("cb", "INSERT INTO stats_method (from_date, to_date, method, auth, total) VALUES($var(f_date), $var(t_date), 'INVITE', 1, $sht(a=>method::invite::auth)) ON DUPLICATE KEY UPDATE total=total+$sht(a=>method::invite::auth)");
667         $sht(a=>method::invite::auth) = 0;
668     }
669
670     #REGISTER
671     if($sht(a=>method::register) > 0) {
672         sql_query("cb", "INSERT INTO stats_method (from_date, to_date, method, total) VALUES($var(f_date), $var(t_date), 'REGISTER', $sht(a=>method::register)) ON DUPLICATE KEY UPDATE total=total+$sht(a=>method::register)");
673         $sht(a=>method::register) = 0;
674     }
675
676     #REGISTER AUTH
677     if($sht(a=>method::register::auth) > 0) {
678         sql_query("cb", "INSERT INTO stats_method (from_date, to_date, method, auth, total) VALUES($var(f_date), $var(t_date), 'REGISTER', 1, $sht(a=>method::register::auth)) ON DUPLICATE KEY UPDATE total=total+$sht(a=>method::register::auth)");
679         $sht(a=>method::register::auth) = 0;
680     }
681
682     #BYE
683     if($sht(a=>method::bye) > 0) {
684         sql_query("cb", "INSERT INTO stats_method (from_date, to_date, method, total) VALUES($var(f_date), $var(t_date), 'BYE', $sht(a=>method::bye)) ON DUPLICATE KEY UPDATE total=total+$sht(a=>method::bye)");
685         $sht(a=>method::bye) = 0;
686     }
687
688     #CANCEL
689     if($sht(a=>method::cancel) > 0) {
690         sql_query("cb", "INSERT INTO stats_method (from_date, to_date, method, total) VALUES($var(f_date), $var(t_date), 'CANCEL', $sht(a=>method::cancel)) ON DUPLICATE KEY UPDATE total=total+$sht(a=>method::cancel)");
691         $sht(a=>method::cancel) = 0;
692     }
693
694     #OPTIONS
695     if($sht(a=>method::options) > 0) {
696         sql_query("cb", "INSERT INTO stats_method (from_date, to_date, method, total) VALUES($var(f_date), $var(t_date), 'OPTIONS', $sht(a=>method::options)) ON DUPLICATE KEY UPDATE total=total+$sht(a=>method::options)");
697         $sht(a=>method::options) = 0;
698     }
699
700 #!ifdef WITH_STATISTIC_METHOD_EXTRA
701     #UNKNOWN
702     if($sht(a=>method::unknown) > 0) {
703         sql_query("cb", "INSERT INTO stats_method (from_date, to_date, method, total) VALUES($var(f_date), $var(t_date), 'UNKNOWN', $sht(a=>method::unknown)) ON DUPLICATE KEY UPDATE total=total+$sht(a=>method::unknown)");
704         $sht(a=>method::unknown) = 0;
705     }
706     
707     #ACK
708     if($sht(a=>method::ack) > 0) {
709         sql_query("cb", "INSERT INTO stats_method (from_date, to_date, method, total) VALUES($var(f_date), $var(t_date), 'ACK', $sht(a=>method::ack)) ON DUPLICATE KEY UPDATE total=total+$sht(a=>method::ack)");
710         $sht(a=>method::ack) = 0;
711     }
712 #!endif
713
714     #REFER
715     if($sht(a=>method::refer) > 0) {
716         sql_query("cb", "INSERT INTO stats_method (from_date, to_date, method, total) VALUES($var(f_date), $var(t_date), 'REFER', $sht(a=>method::refer)) ON DUPLICATE KEY UPDATE total=total+$sht(a=>method::refer)");
717         $sht(a=>method::refer) = 0;
718     }
719
720     #UPDATE
721     if($sht(a=>method::update) > 0) {
722         sql_query("cb", "INSERT INTO stats_method (from_date, to_date, method, total) VALUES($var(f_date), $var(t_date), 'UPDATE', $sht(a=>method::update)) ON DUPLICATE KEY UPDATE total=total+$sht(a=>method::update)");
723         $sht(a=>method::update) = 0;
724     }
725
726     #RESPONSE
727
728     #300
729     if($sht(a=>response::300) > 0) {
730         sql_query("cb", "INSERT INTO stats_method (from_date, to_date, method, total) VALUES($var(f_date), $var(t_date), '300', $sht(a=>response::300)) ON DUPLICATE KEY UPDATE total=total+$sht(a=>response::300)");
731         $sht(a=>response::300) = 0;
732     }
733
734     #407 INVITE
735     if($sht(a=>response::407::invite) > 0) {
736         sql_query("cb", "INSERT INTO stats_method (from_date, to_date, method, cseq, total) VALUES($var(f_date), $var(t_date), '407', 'INVITE', $sht(a=>response::407::invite)) ON DUPLICATE KEY UPDATE total=total+$sht(a=>response::407::invite)");
737         $sht(a=>response::407::invite) = 0;
738     }
739
740     #401 INVITE
741     if($sht(a=>response::401::invite) > 0) {
742         sql_query("cb", "INSERT INTO stats_method (from_date, to_date, method, cseq, total) VALUES($var(f_date), $var(t_date), '401', 'INVITE', $sht(a=>response::401::invite)) ON DUPLICATE KEY UPDATE total=total+$sht(a=>response::401::invite)");
743         $sht(a=>response::401::invite) = 0;
744     }
745
746 #!ifdef WITH_STATISTIC_INVITE_1XX
747     #100 INVITE
748     if($sht(a=>response::100::invite) > 0) {
749         sql_query("cb", "INSERT INTO stats_method (from_date, to_date, method, cseq, total) VALUES($var(f_date), $var(t_date), '100', 'INVITE', $sht(a=>response::100::invite)) ON DUPLICATE KEY UPDATE total=total+$sht(a=>response::100::invite)");
750         $sht(a=>response::100::invite) = 0;
751     }
752
753     #180 INVITE
754     if($sht(a=>response::180::invite) > 0) {
755         sql_query("cb", "INSERT INTO stats_method (from_date, to_date, method, cseq, total) VALUES($var(f_date), $var(t_date), '180', 'INVITE', $sht(a=>response::180::invite)) ON DUPLICATE KEY UPDATE total=total+$sht(a=>response::180::invite)");
756         $sht(a=>response::180::invite) = 0;
757     }
758
759     #183 INVITE
760     if($sht(a=>response::183::invite) > 0) {
761         sql_query("cb", "INSERT INTO stats_method (from_date, to_date, method, cseq, total) VALUES($var(f_date), $var(t_date), '183', 'INVITE', $sht(a=>response::183::invite)) ON DUPLICATE KEY UPDATE total=total+$sht(a=>response::183::invite)");
762         $sht(a=>response::183::invite) = 0; 
763     }
764
765 #!endif
766
767     #200 INVITE
768     if($sht(a=>response::200::invite) > 0) {
769         sql_query("cb", "INSERT INTO stats_method (from_date, to_date, method, cseq, total) VALUES($var(f_date), $var(t_date), '200', 'INVITE', $sht(a=>response::200::invite)) ON DUPLICATE KEY UPDATE total=total+$sht(a=>response::200::invite)");
770         $sht(a=>response::200::invite) = 0;
771     }
772
773     #407 BYE
774     if($sht(a=>response::407::bye) > 0) {
775         sql_query("cb", "INSERT INTO stats_method (from_date, to_date, method, cseq, total) VALUES($var(f_date), $var(t_date), '407', 'BYE', $sht(a=>response::407::bye)) ON DUPLICATE KEY UPDATE total=total+$sht(a=>response::407::bye)");
776         $sht(a=>response::407::bye) = 0;
777     }
778
779     #401 BYE
780     if($sht(a=>response::401::bye) > 0) {
781         sql_query("cb", "INSERT INTO stats_method (from_date, to_date, method, cseq, total) VALUES($var(f_date), $var(t_date), '401', 'BYE', $sht(a=>response::401::bye)) ON DUPLICATE KEY UPDATE total=total+$sht(a=>response::401::bye)");
782         $sht(a=>response::401::bye) = 0;
783     }
784
785     #200 BYE
786     if($sht(a=>response::200::bye) > 0) {
787         sql_query("cb", "INSERT INTO stats_method (from_date, to_date, method, cseq, total) VALUES($var(f_date), $var(t_date), '200', 'BYE', $sht(a=>response::200::bye)) ON DUPLICATE KEY UPDATE total=total+$sht(a=>response::200::bye)");
788         $sht(a=>response::200::bye) = 0;
789     }
790
791     #ALL TRANSACTIONS MESSAGES
792     if($sht(a=>method::all) > 0) {
793         sql_query("cb", "INSERT INTO stats_method (from_date, to_date, method, total) VALUES($var(f_date), $var(t_date), 'ALL', $sht(a=>method::all)) ON DUPLICATE KEY UPDATE total=total+$sht(a=>method::all)");
794         $sht(a=>method::all) = 0;
795     }
796     
797      #ALL MESSAGES ON INTERFACE
798     if($sht(a=>method::total) > 0) {
799         sql_query("cb", "INSERT INTO stats_method (from_date, to_date, method, total) VALUES($var(f_date), $var(t_date), 'TOTAL', $sht(a=>method::total)) ON DUPLICATE KEY UPDATE total=total+$sht(a=>method::total)");
800         $sht(a=>method::total) = 0;
801     }
802
803 #!ifdef WITH_HOMER_CUSTOM_STATS
804     #Generic stats
805     sht_iterator_start("i1", "d");
806     while(sht_iterator_next("i1")) {
807                 $var(key) = $(shtitkey(i1){s.select,2,:});
808                 sql_query("cb", "INSERT INTO stats_generic (from_date, to_date, type, total) VALUES($var(f_date), $var(t_date), '$var(key)', $shtitval(i1)) ON DUPLICATE KEY UPDATE total=(total+$shtitval(i1))/2");
809     }
810     sht_iterator_end("i1");
811     sht_rm_name_re("d=>.*");
812 #!endif
813
814 }
815
816
817 route[STORE] {
818
819         if($rm == "REGISTER") {
820                 $var(table) = "sip_capture_registration";       
821         }
822         else if($rm =~ "(INVITE|UPDATE|BYE|ACK|PRACK|REFER|CANCEL)$")
823         {
824                 $var(table) = "sip_capture_call";
825         } 
826         else if($rm =~ "(NOTIFY)$" && is_present_hf("Event") && $hdr(Event)=~"refer;")
827         {
828                 $var(table) = "sip_capture_call";
829         }
830         else if($rm =~ "(INFO)$")
831         {
832                 $var(table) = "sip_capture_call";
833         }
834         else if($rm =~ "(OPTIONS)$" )
835         {
836             $var(table) = "sip_capture_rest";
837         }
838         else {   
839             $var(table) = "sip_capture_rest";
840         }
841
842         
843 #!ifdef KAMAILIO_4_3
844         #For old models. Not accurate insert time. System vs capture time.
845         #$var(utc) = $timef(%Y%m%d);    
846         #as workaround for kamailio < 4.4
847         sql_query("cb", "SELECT UTC_DATE()+0;", "ra");
848         if($dbr(ra=>rows)>0) {
849               $var(utc) = $dbr(ra=>[0,0]);
850         }
851         sql_result_free("ra");
852 #!else
853         #for Kamailio >=4.4 please uncomment this parameters:
854         # New utimef can fix the timediff issue. But unfortunately only kamailio 4.4
855         $var(utc) = $utimef(%Y%m%d);
856         # or sipcapture style
857         #$var(utc) = "%Y%m%d";
858 #!endif                        
859
860         $var(a) = $var(table) + "_" + $var(utc);
861         sip_capture("$var(a)");
862 }
863
864 event_route[sipcapture:request] {
865         xlog("received sipcapture request from $si:$sp\r\n");
866 }
867
868 #!ifdef WITH_HOMER_CUSTOM_STATS
869 event_route[xhttp:request] {
870         set_reply_close();
871         set_reply_no_connect();
872         xlog("L_WARN", "HTTP request received on $Rp, $hu\n");
873         if($hu =~ "/api/v1/stats/push") {
874                 #Json is our body
875                 $var(json) = $rb;
876                 jansson_get("type", $var(json), "$var(n)");
877                 xlog("L_WARN","Type is $var(n)");
878                 if($var(n) == "rtp_stat") {
879                          $var(i) = 0;
880                          $(avp(x)[0]) = 'interval';
881                          $(avp(x)[1]) = 'streams';
882                          $(avp(x)[2]) = 'packets';
883                          $(avp(x)[3]) = 'lost';
884                          $(avp(x)[4]) = 'late';
885                          $(avp(x)[5]) = 'lost_perc';
886                          $(avp(x)[6]) = 'late_perc';
887                          $(avp(x)[7]) = 'out_of_seq';
888                          $(avp(x)[8]) = 'delay_min';
889                          $(avp(x)[9]) = 'delay_max';
890                          while(is_avp_set("$(avp(x)[$var(i)])")) {
891                                   xlog("L_INFO", "Array value [$var(i)]: $(avp(x)[$var(i)])\n");
892                                   jansson_get("$(avp(x)[$var(i)])", $var(json), "$var(d)");
893                                   $var(n) = $(var(d){s.int});
894                                   if($sht(d=>generic::$(avp(x)[$var(i)])) == $null) $sht(d=>generic::$(avp(x)[$var(i)])) = $var(n);
895                                   else $sht(d=>generic::$(avp(x)[$var(i)])) = ($sht(d=>generic::$(avp(x)[$var(i)])) + $var(n))/2;                                  
896                                   $var(i) = $var(i) + 1;
897                          }
898           }
899           xhttp_reply("200", "Ok", "done", "");
900           exit;
901         }
902
903         xhttp_reply("403", "Forbidden", "", "");
904         exit;
905 }
906 #!endif
907
908
909 event_route[sipcapture:request] {
910
911         xlog("HEP Request!\n");
912         xlog("received sipcapture request from $si:$sp\r\n");
913         xlog("HEP VERSION $hep(version) request from $si:$sp\r\n");
914         xlog("HEP CHUNK Source IP $hep(0x002) request from $si:$sp\r\n");
915         #Is it SIP ?
916         if($hep(0x00b) == 1){
917
918                 #Do parsing internal
919                 return 1;
920         }
921         else 
922         {
923                 #If report lets proceed here with payload
924                 xlog("HEP CHUNK PAYLOAD $hep(0x00f) request from $si:$sp\r\n");
925                 return 0;   
926         }
927 }
928