Browse code

updated SQL and docs

(cherry picked from commit 76649e4bced09b45d5c470a696168929e1758111)

Alexandr Dubovikov authored on 09/05/2016 07:21:32 • Daniel-Constantin Mierla committed on 09/05/2016 19:39:32
Showing 14 changed files
... ...
@@ -10,9 +10,11 @@ Alexandr Dubovikov
10 10
 
11 11
    <alexandr.dubovikov@gmail.com>
12 12
 
13
-   Copyright � 2011 QSC AG
13
+   Copyright © 2011-15 SIPCAPTURE ORG
14 14
 
15
-   Copyright � 2011 http://www.qsc.de
15
+   Copyright © 2011 QSC AG
16
+
17
+   Copyright © 2011 http://www.qsc.de
16 18
      __________________________________________________________________
17 19
 
18 20
    Table of Contents
... ...
@@ -87,7 +89,7 @@ Alexandr Dubovikov
87 89
    1.19. Set callid_aleg_header parameter
88 90
    1.20. Set topoh_unmask parameter
89 91
    1.21. sip_capture() usage
90
-   1.22. sip_capture() usage
92
+   1.22. report_capture() usage
91 93
 
92 94
 Chapter 1. Admin Guide
93 95
 
... ...
@@ -466,13 +468,17 @@ modparam("sipcapture", "topoh_unmask", 1)
466 468
    4.1. sip_capture([table])
467 469
    4.2. report_capture([table],[data])
468 470
 
469
-4.1. sip_capture([table])
471
+4.1.  sip_capture([table])
470 472
 
471 473
    Store the current processed HEP/IPIP SIP message in database. It is
472 474
    stored in the form prior applying changes made to it.
473 475
 
474 476
    Meaning of the parameters is as follows:
475
-     * table - The table where HEP SIP message will be stored.
477
+     * table - The table where HEP SIP message will be stored. Homer 5 use
478
+       now tables with datestamp. To generate an automatic table's name
479
+       please use strftime parameters. I.e. $var(table) =
480
+       "sip_capture_call_%Y%m%d" and set the variable as an argument of
481
+       the sip_capture function.
476 482
 
477 483
    This function can be used from REQUEST_ROUTE, FAILURE_ROUTE,
478 484
    ONREPLY_ROUTE, BRANCH_ROUTE.
... ...
@@ -485,7 +491,7 @@ sip_capture();
485 491
 sip_capture("sip_capture_call_20160124");
486 492
 ...
487 493
 
488
-4.2. report_capture([table],[data])
494
+4.2.  report_capture([table],[data])
489 495
 
490 496
    Store the current processed HEP REPORT message in database.
491 497
 
... ...
@@ -497,7 +503,7 @@ sip_capture("sip_capture_call_20160124");
497 503
    ONREPLY_ROUTE, BRANCH_ROUTE.
498 504
    Default value is "NULL".
499 505
 
500
-   Example 1.22. sip_capture() usage
506
+   Example 1.22. report_capture() usage
501 507
 ...
502 508
 report_capture();
503 509
 ...
... ...
@@ -510,7 +516,7 @@ report_capture("report_data", "{\"MOS\":4.1,\"PACKET_LOST\":100"});
510 516
 
511 517
    5.1. sip_capture
512 518
 
513
-5.1. sip_capture
519
+5.1.  sip_capture
514 520
 
515 521
    Name: sip_capture
516 522
 
... ...
@@ -520,7 +526,7 @@ report_capture("report_data", "{\"MOS\":4.1,\"PACKET_LOST\":100"});
520 526
           + on
521 527
           + off
522 528
        The parameter is optional - if missing, the command will return the
523
-       status of the SIP message capturing (as string "on" or "off" )
529
+       status of the SIP message capturing (as string “on” or “off” )
524 530
        without changing anything.
525 531
 
526 532
    MI FIFO Command Format:
... ...
@@ -532,7 +538,7 @@ report_capture("report_data", "{\"MOS\":4.1,\"PACKET_LOST\":100"});
532 538
 
533 539
    6.1. sipcapture.status param
534 540
 
535
-6.1. sipcapture.status param
541
+6.1.  sipcapture.status param
536 542
 
537 543
    Name: sipcapture.status
538 544
 
... ...
@@ -540,7 +546,7 @@ report_capture("report_data", "{\"MOS\":4.1,\"PACKET_LOST\":100"});
540 546
      * on or off: turns on/off SIP message capturing. Possible values are:
541 547
           + on
542 548
           + off
543
-     * "check" does not change sipcapture status, just reports the current
549
+     * “check” does not change sipcapture status, just reports the current
544 550
        status.
545 551
 
546 552
 7. Database setup
... ...
@@ -548,13 +554,14 @@ report_capture("report_data", "{\"MOS\":4.1,\"PACKET_LOST\":100"});
548 554
    Before running Kamailio with the sipcapture module, you have to setup
549 555
    the database tables where the module will store the data. For that, if
550 556
    the table were not created by the installation script or you choose to
551
-   install everything by yourself you can use the sipcapture_create.sql,
552
-   SQL script in the sql folder of sipcapture module as template. You can
553
-   also find the complete database documentation on the project webpage,
557
+   install everything by yourself you can use the homer_databases.sql, SQL
558
+   script in the sql folder of sipcapture module as template. You can also
559
+   find the complete database documentation on the project webpage,
554 560
    http://www.kamailio.org/docs/db-tables/kamailio-db-devel.html.
555 561
 
556 562
 8. Limitations
557 563
 
558 564
      * 1. Only one capturing mode on RAW socket is supported: IPIP or
559 565
        monitoring/mirroring port. Don't activate both at the same time.
566
+       Obsolete. Please use HEP mirroring now.
560 567
      * 2. Mirroring port capturing works only on Linux.
... ...
@@ -24,6 +24,10 @@
24 24
 		    <email>alexandr.dubovikov@gmail.com</email>
25 25
 	    </editor>
26 26
 	</authorgroup>
27
+	<copyright>
28
+	    <year>2011-15</year>
29
+	    <holder>SIPCAPTURE ORG</holder>
30
+	</copyright>
27 31
 	<copyright>
28 32
 	    <year>2011</year>
29 33
 	    <holder>QSC AG</holder>
... ...
@@ -507,7 +507,9 @@ modparam("sipcapture", "topoh_unmask", 1)
507 507
 		<para>Meaning of the parameters is as follows:</para>
508 508
 		<itemizedlist>
509 509
 		<listitem>
510
-		<para><emphasis>table</emphasis> - The table where HEP SIP message will be stored.		
510
+		<para><emphasis>table</emphasis> - The table where HEP SIP message will be stored. Homer 5 use now tables with datestamp. 
511
+		To generate an automatic table's name please use strftime parameters. I.e. $var(table) = "sip_capture_call_%Y%m%d" and set the variable
512
+		as an argument of the sip_capture function.
511 513
 		</para>
512 514
 		</listitem>
513 515
 		</itemizedlist>
... ...
@@ -553,7 +555,7 @@ sip_capture("sip_capture_call_20160124");
553 555
 			Default value is "NULL".
554 556
 		</emphasis>
555 557
 		<example>
556
-		<title><function>sip_capture()</function> usage</title>
558
+		<title><function>report_capture()</function> usage</title>
557 559
 		<programlisting format="linespecific">
558 560
 ...
559 561
 report_capture();
... ...
@@ -641,7 +643,7 @@ report_capture("report_data", "{\"MOS\":4.1,\"PACKET_LOST\":100"});
641 643
 		Before running &kamailio; with the sipcapture module, you have to setup the database 
642 644
 		tables where the module will store the data. For that, if the table were not 
643 645
 		created by the installation script or you choose to install everything by 
644
-		yourself you can use the sipcapture_create.sql, <acronym>SQL</acronym> script 
646
+		yourself you can use the homer_databases.sql, <acronym>SQL</acronym> script 
645 647
 		in the sql folder of sipcapture module as template. You can also find the 
646 648
 		complete database documentation on the project webpage, &kamailiodbdocslink;.
647 649
 		</para>
... ...
@@ -651,7 +653,7 @@ report_capture("report_data", "{\"MOS\":4.1,\"PACKET_LOST\":100"});
651 653
 	<itemizedlist>
652 654
 		<listitem>
653 655
 		1. Only one capturing mode on RAW socket is supported: IPIP or monitoring/mirroring port. 
654
-		   Don't activate both at the same time.
656
+		   Don't activate both at the same time. Obsolete. Please use HEP mirroring now.
655 657
 		</listitem>
656 658
 		<listitem>
657 659
 		2. Mirroring port capturing works only on Linux.
... ...
@@ -2,6 +2,24 @@
2 2
 #
3 3
 # Example configuration file for a sipcapture node
4 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
+
5 23
 ####### Global Parameters #########
6 24
 
7 25
 debug=1
... ...
@@ -10,7 +28,11 @@ log_stderror=no
10 28
 memdbg=5
11 29
 memlog=5
12 30
 
13
-log_facility=LOG_LOCAL0
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
14 36
 
15 37
 fork=yes
16 38
 children=5
... ...
@@ -19,13 +41,15 @@ children=5
19 41
 disable_tcp=yes
20 42
 
21 43
 /* IP and port for HEP capturing) */
22
-listen=udp:10.0.1.77:9060
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
23 49
 
24 50
 #Max loops
25 51
 max_while_loops=100
26 52
 
27
-mpath="/usr/local/lib64/kamailio/modules"
28
-
29 53
 loadmodule "pv.so"
30 54
 loadmodule "db_mysql.so"
31 55
 loadmodule "sipcapture.so"
... ...
@@ -34,396 +58,511 @@ loadmodule "rtimer.so"
34 58
 loadmodule "xlog.so"
35 59
 loadmodule "sqlops.so"
36 60
 loadmodule "htable.so"
61
+loadmodule "tm.so"
37 62
 loadmodule "sl.so"
38 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
39 75
 
40 76
 
41 77
 modparam("htable", "htable", "a=>size=8;autoexpire=400")
42 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
43 86
 
44 87
 modparam("rtimer", "timer", "name=ta;interval=60;mode=1;")
45 88
 modparam("rtimer", "exec", "timer=ta;route=TIMER_STATS")
46 89
 
47
-modparam("sqlops","sqlcon","cb=>mysql://homer_user:123456@127.0.0.1/homer_db")
90
+modparam("sqlops","sqlcon","cb=>mysql://HOMER_DB_USER:HOMER_DB_PASSWORD@127.0.0.1/homer_statistic")
48 91
 
49 92
 # ----- mi_fifo params -----
50 93
 
51 94
 ####### Routing Logic ########
52
-modparam("sipcapture", "db_url", "mysql://homer_user:123456@localhost/homer_db")
95
+modparam("sipcapture", "db_url", "mysql://HOMER_DB_USER:HOMER_DB_PASSWORD@127.0.0.1/homer_data")
53 96
 modparam("sipcapture", "capture_on", 1)
54
-/* activate HEP capturing */
55 97
 modparam("sipcapture", "hep_capture_on", 1)
56
-/* IP to listen. Port/Portrange apply only on mirroring port capturing */
57
-#modparam("sipcapture", "raw_socket_listen", "192.168.0.1:5060")
58
-/* Name of interface to bind on raw socket */
59
-modparam("sipcapture", "raw_interface", "eth0")
60
-/* activate IPIP capturing */
61
-modparam("sipcapture", "raw_ipip_capture_on", 1)
62
-/* My table name*/
63
-modparam("sipcapture", "table_name", "sip_capture")
64
-/* children for raw socket */
65
-modparam("sipcapture", "raw_sock_children", 4)
66
-/* insert delayed */
67
-#modparam("sipcapture", "db_insert_mode", 1)
68
-/* activate monitoring/mirroring port capturing. Linux only */
69
-modparam("sipcapture", "raw_moni_capture_on", 0)
70
-/* Promiscious mode RAW socket. Mirroring port. Linux only */
71
-#modparam("sipcapture", "promiscious_on", 1)
72
-/* activate Linux Socket Filter (LSF/BPF) on mirroring interface. Linux only */
73
-#modparam("sipcapture", "raw_moni_bpf_on", 1)
74
-
75
-# Main SIP request routing logic
76
-# - processing of any incoming SIP request starts with this route
77
-
78
-route {
79
-
80
-        if($sht(a=>method::all) == $null) $sht(a=>method::all) = 0;
81
-        $sht(a=>method::all) = $sht(a=>method::all) + 1;
82
-
83
-        if($sht(b=>$rm::$cs::$ci) != $null) {
84
-		$var(a) = "sip_capture_call" + "_%Y%m%d";
85
-                sip_capture("$var(a)");
86
-                drop;
87
-        }
88
-
89
-        $sht(b=>$rm::$cs::$ci) = 1;
90
-
91
-        if (is_method("INVITE|REGISTER")) {
92
-
93
-                if($ua =~ "(friendly-scanner|sipvicious)") {
94
-                        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");
95
-                }
96
-
97
-                #IP Method
98
-                sql_query("cb", "INSERT INTO stats_ip_mem ( method, source_ip, total) VALUES('$rm', '$si', 1) ON DUPLICATE KEY UPDATE total=total+1");
99
-
100
-                if($au != $null)  $var(anumber) = $au;
101
-                else $var(anumber) = $fU;
102
-
103
-                #hostname in contact
104
-                if($sel(contact.uri.host) =~ "^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$") {
105
-                        if($sht(a=>alarm::dns) == $null) $sht(a=>alarm::dns) = 0;
106
-                        $sht(a=>alarm::dns) = $sht(a=>alarm::dns) + 1;
107
-                }
108
-
109
-                if($sel(contact.uri.host) != $si) {
110
-                        if($sht(a=>alarm::spoofing) == $null) $sht(a=>alarm::spoofing) = 0;
111
-                        $sht(a=>alarm::spoofing) = $sht(a=>alarm::spoofing) + 1;
112
-                }
113
-
114
-                if($au =~ "(\=)|(\-\-)|(\')|(\#)|(\%27)|(\%24)") {
115
-                        if($sht(a=>alarm::sqlinjection) == $null) $sht(a=>alarm::sqlinjection) = 0;
116
-                        $sht(a=>alarm::sqlinjection) = $sht(a=>alarm::sqlijnection) + 1;
117
-                }
118
-
119
-                if($(hdr(Record-Route)[0]{nameaddr.uri}) != $si) {
120
-                        if($sht(a=>alarm::spoofing) == $null) $sht(a=>alarm::spoofing) = 0;
121
-                        $sht(a=>alarm::spoofing) = $sht(a=>alarm::spoofing) + 1;
122
-                }
123
-
98
+modparam("sipcapture", "insert_retries", 5)
99
+modparam("sipcapture", "insert_retry_timeout", 10)
100
+#modparam("sipcapture", "capture_node", "homer01")
124 101
 
125
-                if (is_method("INVITE")) {
102
+#!ifdef WITH_HOMER_GEO
103
+modparam("geoip", "path", "/usr/share/GeoIP/GeoIP.dat")
104
+#!endif
126 105
 
127
-                        if (has_totag()) {
128
-                                if($sht(a=>method::reinvite) == $null) $sht(a=>method::reinvite) = 0;
129
-                                $sht(a=>method::reinvite) = $sht(a=>method::reinvite) + 1;
130
-                        }
131
-                        else {
132
-                                if($sht(a=>method::invite) == $null) $sht(a=>method::invite) = 0;
133
-                                $sht(a=>method::invite) = $sht(a=>method::invite) + 1;
134
-
135
-                                if($adu != $null) {
136
-                                        if($sht(a=>method::invite::auth) == $null) $sht(a=>method::invite::auth) = 0;
137
-                                        $sht(a=>method::invite::auth) = $sht(a=>method::invite::auth) + 1;
138
-                                }
106
+#Stats time
107
+stats.min = 5 desc "My stats TIME min"
139 108
 
140
-                                if($ua != $null) {
141
-                                        sql_query("cb", "INSERT INTO stats_useragent_mem (useragent, method, total) VALUES('$ua', 'INVITE', 1) ON DUPLICATE KEY UPDATE total=total+1");
142
-                                }
143
-
144
-                        }
145
-                }
146
-                else {
147
-                        if($sht(a=>method::register) == $null) $sht(a=>method::register) = 0;
148
-                        $sht(a=>method::register) = $sht(a=>method::register) + 1;
149 109
 
150
-                        if($adu != $null) {
151
-                                if($sht(a=>method::register::auth) == $null) $sht(a=>method::register::auth) = 0;
152
-                                $sht(a=>method::register::auth) = $sht(a=>method::register::auth) + 1;
153
-                        }
110
+# Main SIP request routing logic
111
+# - processing of any incoming SIP request starts with this route
112
+route {
154 113
 
155
-                        if($ua != $null) {
156
-                                sql_query("cb", "INSERT INTO stats_useragent_mem (useragent, method, total) VALUES('$ua', 'REGISTER', 1) ON DUPLICATE KEY UPDATE total=total+1");
157
-                        }
114
+	if($sht(a=>method::total) == $null) $sht(a=>method::total) = 0;
115
+	$sht(a=>method::total) = $sht(a=>method::total) + 1;
116
+	if($sht(a=>packet::count) == $null) $sht(a=>packet::count) = 0;
117
+        if($sht(a=>packet::size) == $null) $sht(a=>packet::size) = 0;
118
+	
119
+	 #Packets
120
+        $sht(a=>packet::count) = $sht(a=>packet::count) + 1;
121
+        $sht(a=>packet::size) = $sht(a=>packet::size) + $ml;
122
+
123
+
124
+	if($sht(b=>$rm::$cs::$ci) != $null) {
125
+		#$var(a) = "sip_capture_call" + "_%Y%m%d";
126
+		#Store
127
+		route(STORE);
128
+		drop;
129
+	}
130
+
131
+	$sht(b=>$rm::$cs::$ci) = 1;	
132
+	if($sht(a=>method::all) == $null) $sht(a=>method::all) = 0;
133
+	$sht(a=>method::all) = $sht(a=>method::all) + 1;
134
+	
135
+	if (is_method("INVITE|REGISTER")) {
136
+
137
+		if($ua =~ "(friendly-scanner|sipvicious|sipcli)") {
138
+			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");
139
+			route(KILL_VICIOUS);
140
+		}
141
+
142
+		#IP Method
143
+		sql_query("cb", "INSERT INTO stats_ip_mem ( method, source_ip, total) VALUES('$rm', '$si', 1) ON DUPLICATE KEY UPDATE total=total+1");
144
+
145
+#!ifdef WITH_HOMER_GEO
146
+		if(geoip_match("$si", "src")) {
147
+                        xlog("REGISTER|INVITE SIP message [$si] from: $gip(src=>cc)\n");
148
+                        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");
158 149
                 }
159
-
160
-        }
161
-        else if(is_method("BYE")) {
162
-                if($sht(a=>method::bye) == $null) $sht(a=>method::bye) = 0;
163
-                $sht(a=>method::bye) = $sht(a=>method::bye) + 1;
164
-                if(is_present_hf("Reason")) {
150
+#!endif
151
+
152
+
153
+		if($au != $null)  $var(anumber) = $au;
154
+		else $var(anumber) = $fU;
155
+
156
+		if($(var(anumber){s.substr,0,5}) == "+204231") {
157
+                	if($sht(a=>alarm::scam) == $null) $sht(a=>alarm::scam) = 0;
158
+                        $sht(a=>alarm::scam) = $sht(a=>alarm::scam) + 1;
159
+		}
160
+
161
+		if($(rU{s.substr,0,5}) == "+204231") {
162
+			if($sht(a=>alarm::scam) == $null) $sht(a=>alarm::scam) = 0;
163
+			$sht(a=>alarm::scam) = $sht(a=>alarm::scam) + 1; 
164
+		}
165
+	
166
+	
167
+		#if($sel(contact.uri.host) =~ "^([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))$") {
168
+		if($sel(contact.uri.host) =~ "^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$") {
169
+			if($sht(a=>alarm::dns) == $null) $sht(a=>alarm::dns) = 0;
170
+			$sht(a=>alarm::dns) = $sht(a=>alarm::dns) + 1; 
171
+		}
172
+
173
+
174
+		if($sel(contact.uri.host) != $si) {
175
+			if($sht(a=>alarm::spoofing) == $null) $sht(a=>alarm::spoofing) = 0;
176
+			$sht(a=>alarm::spoofing) = $sht(a=>alarm::spoofing) + 1; 
177
+		}
178
+
179
+		if($au =~ "(\=)|(\-\-)|(\')|(\#)|(\%27)|(\%24)") {
180
+			if($sht(a=>alarm::sqlinjection) == $null) $sht(a=>alarm::sqlinjection) = 0;
181
+			$sht(a=>alarm::sqlinjection) = $sht(a=>alarm::sqlinjection) + 1; 
182
+		}
183
+
184
+		if($(hdr(Record-Route)[0]{nameaddr.uri}) != $si) {
185
+			if($sht(a=>alarm::spoofing) == $null) $sht(a=>alarm::spoofing) = 0;
186
+			$sht(a=>alarm::spoofing) = $sht(a=>alarm::spoofing) + 1; 
187
+		}
188
+		
189
+
190
+		if (is_method("INVITE")) {			
191
+
192
+		        if (has_totag()) {
193
+				if($sht(a=>method::reinvite) == $null) $sht(a=>method::reinvite) = 0;
194
+				$sht(a=>method::reinvite) = $sht(a=>method::reinvite) + 1; 				
195
+			}
196
+			else {
197
+				if($sht(a=>method::invite) == $null) $sht(a=>method::invite) = 0; 
198
+				$sht(a=>method::invite) = $sht(a=>method::invite) + 1; 
199
+				if($adu != $null) {
200
+					if($sht(a=>method::invite::auth) == $null) $sht(a=>method::invite::auth) = 0; 
201
+					$sht(a=>method::invite::auth) = $sht(a=>method::invite::auth) + 1; 
202
+				}
203
+
204
+				if($ua != $null) {
205
+					sql_query("cb", "INSERT INTO stats_useragent_mem (useragent, method, total) VALUES('$ua', 'INVITE', 1) ON DUPLICATE KEY UPDATE total=total+1");
206
+				}
207
+
208
+			}					
209
+		}
210
+		else {
211
+			if($sht(a=>method::register) == $null) $sht(a=>method::register) = 0; 		
212
+			$sht(a=>method::register) = $sht(a=>method::register) + 1; 
213
+
214
+			if($adu != $null) {
215
+				if($sht(a=>method::register::auth) == $null) $sht(a=>method::register::auth) = 0; 
216
+				$sht(a=>method::register::auth) = $sht(a=>method::register::auth) + 1; 
217
+			}
218
+			
219
+			if($ua != $null) {
220
+				sql_query("cb", "INSERT INTO stats_useragent_mem (useragent, method, total) VALUES('$ua', 'REGISTER', 1) ON DUPLICATE KEY UPDATE total=total+1");
221
+			}
222
+		}
223
+
224
+	}
225
+	else if(is_method("BYE")) {
226
+		if($sht(a=>method::bye) == $null) $sht(a=>method::bye) = 0; 
227
+		$sht(a=>method::bye) = $sht(a=>method::bye) + 1; 		
228
+		if(is_present_hf("Reason")) {
165 229
                        $var(cause) = $(hdr(Reason){param.value,cause}{s.int});
166 230
                        if($var(cause) != 16 && $var(cause) !=17) {
167
-                                if($sht(a=>stats::sdf) == $null) $sht(a=>stats::sdf) = 0;
168
-                                $sht(a=>stats::sdf) = $sht(a=>stats::sdf) + 1;
169
-                       }
170
-                }
171
-
172
-        }
173
-        else if(is_method("CANCEL")) {
174
-                if($sht(a=>method::cancel) == $null) $sht(a=>method::cancel) = 0;
175
-                $sht(a=>method::cancel) = $sht(a=>method::cancel) + 1;
176
-
177
-        }
178
-        else if(is_method("OPTIONS")) {
179
-                if($sht(a=>method::options) == $null) $sht(a=>method::options) = 0;
180
-                $sht(a=>method::options) = $sht(a=>method::options) + 1;
181
-
182
-        }
183
-        else if(is_method("REFER")) {
184
-                if($sht(a=>method::refer) == $null) $sht(a=>method::refer) = 0;
185
-                $sht(a=>method::refer) = $sht(a=>method::refer) + 1;
186
-
187
-        }
188
-        else if(is_method("UPDATE")) {
189
-                if($sht(a=>method::update) == $null) $sht(a=>method::update) = 0;
190
-                $sht(a=>method::update) = $sht(a=>method::update) + 1;
191
-        }
192
-
193
-
194
-	#Sharding
195
-	if(is_method("REGISTER")) {
196
-                $var(table) = "sip_capture_registration";
197
-        }
198
-        else if(is_method("INVITE|BYE|CANCEL|UPDATE|ACK|PRACK|REFER"))
199
-        {
200
-                $var(table) = "sip_capture_call";
201
-        }
202
-        else if(is_method("INFO"))
231
+				if($sht(a=>stats::sdf) == $null) $sht(a=>stats::sdf) = 0; 
232
+				$sht(a=>stats::sdf) = $sht(a=>stats::sdf) + 1; 
233
+		       }
234
+		}
235
+
236
+#		if($si != $sht(a=>ipinit::aleg::$ci) && $si != $sht(a=>ipinit::bleg::$ci)) {
237
+#			if($sht(a=>alarm::sessiontd) == $null) $sht(a=>alarm::sessiontd) = 0;
238
+#			$sht(a=>alarm::sessiontd) = $sht(a=>alarm::sessiontd) + 1; 
239
+#		}
240
+
241
+	}
242
+	else if(is_method("CANCEL")) {
243
+		if($sht(a=>method::cancel) == $null) $sht(a=>method::cancel) = 0; 
244
+		$sht(a=>method::cancel) = $sht(a=>method::cancel) + 1; 
245
+
246
+	}
247
+	else if(is_method("OPTIONS")) {
248
+		if($sht(a=>method::options) == $null) $sht(a=>method::options) = 0; 
249
+		$sht(a=>method::options) = $sht(a=>method::options) + 1; 
250
+		#xlog("OPTIONS COUNTING.... $sht(a=>method::options)\n");
251
+
252
+	}
253
+	else if(is_method("REFER")) {
254
+		if($sht(a=>method::refer) == $null) $sht(a=>method::refer) = 0; 
255
+		$sht(a=>method::refer) = $sht(a=>method::refer) + 1; 
256
+
257
+	}
258
+	else if(is_method("UPDATE")) {
259
+		if($sht(a=>method::update) == $null) $sht(a=>method::update) = 0; 
260
+		$sht(a=>method::update) = $sht(a=>method::update) + 1; 
261
+	}
262
+	
263
+	else if(is_method("PUBLISH"))
203 264
         {
204
-                $var(table) = "sip_capture_call";
265
+                if(has_body("application/vq-rtcpxr") && $(rb{s.substr,0,1}) != "x") {
266
+                        $var(table) = "report_capture";
267
+                        $var(callid) = $(rb{re.subst,/.*CallID:([0-9A-Za-z@-_\.]{5,120}).*$/\1/s});
268
+			
269
+			#Local IP. Only for stats
270
+			#$var(localaddress) = $(rb{re.subst,/.*LocalAddr:IP=([^\S]+).*$/\1/s});
271
+
272
+                        #xlog("CALLID: $var(callid)");
273
+#!ifdef KAMAILIO_4_3
274
+			xlog("report_capture is not in < 4.3");
275
+#!else
276
+			report_capture("$var(table)", "$var(callid)");
277
+#!endif                        
278
+                        drop;
279
+                }
205 280
         }
206
-        else if(is_method("OPTIONS|PUBLISH")) 
207
-        {
208
-                $var(table) = "sip_capture_rest";
281
+	
282
+#!ifdef WITH_STATISTIC_METHOD_EXTRA
283
+	else if(is_method("ACK")) {
284
+                if($sht(a=>method::ack) == $null) $sht(a=>method::ack) = 0; 
285
+                $sht(a=>method::ack) = $sht(a=>method::ack) + 1;
209 286
         }
210 287
         else {
211
-                $var(table) = "sip_capture_rest";
212
-        }
213
-
214
-        $var(a) = $var(table) + "_%Y%m%d";
215
-        sip_capture("$var(a)");
216
-
217
-        drop;
288
+                if($sht(a=>method::unknown) == $null) $sht(a=>method::unknown) = 0; 
289
+                $sht(a=>method::unknown) = $sht(a=>method::uknown) + 1; 
290
+        }     
291
+#!endif     
292
+
293
+	#Store
294
+	route(STORE);
295
+	drop;
218 296
 }
219 297
 
220 298
 onreply_route {
221 299
 
222
-        if($sht(a=>method::all) == $null) $sht(a=>method::all) = 0;
223
-        $sht(a=>method::all) = $sht(a=>method::all) + 1;
224
-
225
-        if($sht(b=>$rs::$cs::$rm::$ci) != $null) {
226
-                $var(a) = "sip_capture";
227
-                sip_capture("$var(a)");
228
-                drop;
229
-        }
230
-
231
-        $sht(b=>$rs::$cs::$rm::$ci) = 1;
232
-
233
-        #413 Too large
234
-        if(status == "413") {
235
-
236
-                if($sht(a=>alarm::413) == $null) $sht(a=>alarm::413) = 0;
237
-                $sht(a=>alarm::413) = $sht(a=>alarm::413) + 1;
238
-        }
239
-        # Too many hops
240
-        else if(status == "483") {
241
-                if($sht(a=>alarm::483) == $null) $sht(a=>alarm::483) = 0;
242
-                $sht(a=>alarm::483) = $sht(a=>alarm::483) + 1;
243
-
244
-        }
245
-        # loops
246
-        else if(status == "482") {
247
-                if($sht(a=>alarm::482) == $null) $sht(a=>alarm::482) = 0;
248
-                $sht(a=>alarm::482) = $sht(a=>alarm::482) + 1;
249
-
250
-        }
251
-        # 400
252
-        else if(status == "400") {
253
-                if($sht(a=>alarm::400) == $null) $sht(a=>alarm::400) = 0;
254
-                $sht(a=>alarm::400) = $sht(a=>alarm::400) + 1;
255
-
256
-        }
257
-
258
-        # 500
259
-        else if(status == "500") {
260
-                if($sht(a=>alarm::500) == $null) $sht(a=>alarm::500) = 0;
261
-                $sht(a=>alarm::500) = $sht(a=>alarm::500) + 1;
262
-        }
263
-        # 503
264
-        else if(status == "503") {
265
-                if($sht(a=>alarm::503) == $null) $sht(a=>alarm::503) = 0;
266
-                $sht(a=>alarm::503) = $sht(a=>alarm::503) + 1;
267
-        }
268
-        # 403
300
+	if($sht(a=>method::total) == $null) $sht(a=>method::total) = 0;
301
+	$sht(a=>method::total) = $sht(a=>method::total) + 1;
302
+
303
+	if($sht(b=>$rs::$cs::$rm::$ci) != $null) {
304
+		#Store
305
+		route(STORE);
306
+		drop;
307
+	}
308
+
309
+	$sht(b=>$rs::$cs::$rm::$ci) = 1;
310
+	
311
+	if($sht(a=>method::all) == $null) $sht(a=>method::all) = 0;
312
+	$sht(a=>method::all) = $sht(a=>method::all) + 1;
313
+
314
+	#413 Too large
315
+	if(status == "413") {	
316
+		if($sht(a=>response::413) == $null) $sht(a=>response::413) = 0; 
317
+		$sht(a=>response::413) = $sht(a=>response::413) + 1; 
318
+
319
+		if($sht(a=>alarm::413) == $null) $sht(a=>alarm::413) = 0; 
320
+		$sht(a=>alarm::413) = $sht(a=>alarm::413) + 1; 
321
+	}
322
+	#403 Unauthorize
269 323
         else if(status == "403") {
270
-                if($sht(a=>alarm::403) == $null) $sht(a=>alarm::403) = 0;
271
-                $sht(a=>alarm::403) = $sht(a=>alarm::403) + 1;
272
-        }
273
-        # MOVED
274
-        else if(status =~ "^(30[012])$") {
275
-                if($sht(a=>response::300) == $null) $sht(a=>response::300) = 0;
276
-                $sht(a=>response::300) = $sht(a=>response::300) + 1;
277
-        }
324
+                if($sht(a=>response::403) == $null) $sht(a=>response::403) = 0; 
325
+                $sht(a=>response::403) = $sht(a=>response::403) + 1; 
278 326
 
279
-        if($rm == "INVITE") {
280
-                #ISA
281
-                if(status =~ "^(408|50[03])$") {
282
-                        if($sht(a=>stats::isa) == $null) $sht(a=>stats::isa) = 0;
283
-                        $sht(a=>stats::isa) = $sht(a=>stats::isa) + 1;
327
+                if($sht(a=>alarm::403) == $null) $sht(a=>alarm::403) = 0; 
328
+                $sht(a=>alarm::403) = $sht(a=>alarm::403) + 1; 
329
+        }
330
+	# Too many hops
331
+	else if(status == "483") {	
332
+		if($sht(a=>response::483) == $null) $sht(a=>response::483) = 0; 
333
+		$sht(a=>response::483) = $sht(a=>response::483) + 1; 
334
+
335
+		if($sht(a=>alarm::483) == $null) $sht(a=>alarm::483) = 0; 
336
+		$sht(a=>alarm::483) = $sht(a=>alarm::483) + 1; 
337
+
338
+	}
339
+	# loops
340
+	else if(status == "482") {	
341
+		if($sht(a=>response::482) == $null) $sht(a=>response::482) = 0; 
342
+		$sht(a=>response::482) = $sht(a=>response::482) + 1; 
343
+
344
+		if($sht(a=>alarm::482) == $null) $sht(a=>alarm::482) = 0; 
345
+		$sht(a=>alarm::482) = $sht(a=>alarm::482) + 1; 
346
+
347
+	}
348
+	# Call Transaction Does not exist
349
+	else if(status == "481") {	
350
+		if($sht(a=>alarm::481) == $null) $sht(a=>alarm::481) = 0; 
351
+		$sht(a=>alarm::481) = $sht(a=>alarm::481) + 1; 
352
+	}
353
+	# 408 Timeout
354
+	else if(status == "408") {	
355
+		if($sht(a=>alarm::408) == $null) $sht(a=>alarm::408) = 0; 
356
+		$sht(a=>alarm::408) = $sht(a=>alarm::408) + 1; 
357
+	}
358
+	# 400
359
+	else if(status == "400") {	
360
+
361
+		if($sht(a=>alarm::400) == $null) $sht(a=>alarm::400) = 0; 
362
+		$sht(a=>alarm::400) = $sht(a=>alarm::400) + 1; 
363
+
364
+	}
365
+	# MOVED
366
+	else if(status =~ "^(30[012])$") {	
367
+		if($sht(a=>response::300) == $null) $sht(a=>response::300) = 0; 
368
+		$sht(a=>response::300) = $sht(a=>response::300) + 1; 
369
+	}
370
+
371
+	if($rm == "INVITE") {
372
+		#ISA
373
+		if(status =~ "^(408|50[03])$") {	
374
+			if($sht(a=>stats::isa) == $null) $sht(a=>stats::isa) = 0; 
375
+			$sht(a=>stats::isa) = $sht(a=>stats::isa) + 1; 	
376
+		}
377
+		#Bad486
378
+		if(status =~ "^(486|487|603)$") {	
379
+			if($sht(a=>stats::bad::invite) == $null) $sht(a=>stats::bad::invite) = 0; 
380
+			$sht(a=>stats::bad::invite) = $sht(a=>stats::bad::invite) + 1; 	
381
+		}
382
+
383
+		#SD
384
+		if(status =~ "^(50[034])$") {	
385
+			if($sht(a=>stats::sd) == $null) $sht(a=>stats::sd) = 0; 
386
+			$sht(a=>stats::sd) = $sht(a=>stats::sd) + 1; 	
387
+		}
388
+
389
+		if(status == "407") {	
390
+			if($sht(a=>response::407::invite) == $null) $sht(a=>response::407::invite)= 0;
391
+			$sht(a=>response::407::invite) = $sht(a=>response::407::invite) + 1; 
392
+		}
393
+		else if(status == "401") {			
394
+			if($sht(a=>response::401::invite) == $null) $sht(a=>response::401::invite)= 0;
395
+			$sht(a=>response::401::invite) = $sht(a=>response::401::invite) + 1; 
396
+		}
397
+		else if(status == "200") {			
398
+			if($sht(a=>response::200::invite) == $null) $sht(a=>response::200::invite)= 0;
399
+			$sht(a=>response::200::invite) = $sht(a=>response::200::invite) + 1; 
400
+		}
401
+		#Aditional stats
402
+#!ifdef WITH_STATISTIC_INVITE_1XX		
403
+	        else if(status == "100") {
404
+                        if($sht(a=>response::100::invite) == $null) $sht(a=>response::100::invite)= 0;
405
+                        $sht(a=>response::100::invite) = $sht(a=>response::100::invite) + 1;
284 406
                 }
285
-                #Bad486
286
-                if(status =~ "^(486|487|603)$") {
287
-                        if($sht(a=>stats::bad::invite) == $null) $sht(a=>stats::bad::invite) = 0;
288
-                        $sht(a=>stats::bad::invite) = $sht(a=>stats::bad::invite) + 1;
407
+                else if(status == "180") {
408
+                        if($sht(a=>response::180::invite) == $null) $sht(a=>response::180::invite)= 0;
409
+                        $sht(a=>response::180::invite) = $sht(a=>response::180::invite) + 1;
410
+                }   
411
+                else if(status == "183") {                      
412
+                        if($sht(a=>response::183::invite) == $null) $sht(a=>response::183::invite)= 0;
413
+                        $sht(a=>response::183::invite) = $sht(a=>response::183::invite) + 1;
289 414
                 }
415
+#!endif                
416
+	}
417
+	else if($rm == "BYE") {
418
+
419
+		if(status == "407") {	
420
+			if($sht(a=>response::407::bye) == $null) $sht(a=>response::407::bye) = 0;
421
+			$sht(a=>response::407::bye) = $sht(a=>response::407::bye) + 1; 
422
+		}
423
+		else if(status == "401") {			
424
+			if($sht(a=>response::401::bye) == $null) $sht(a=>response::401::bye) = 0;
425
+			$sht(a=>response::401::bye) = $sht(a=>response::401::bye) + 1; 
426
+		}
427
+		else if(status == "200") {			
428
+			if($sht(a=>response::200::bye) == $null) $sht(a=>response::200::bye) = 0;
429
+			$sht(a=>response::200::bye) = $sht(a=>response::200::bye) + 1; 
430
+		}
431
+	}
432
+	
433
+	#Store
434
+	route(STORE);
435
+	drop;
436
+}
290 437
 
291
-                #SD
292
-                if(status =~ "^(50[034])$") {
293
-                        if($sht(a=>stats::sd) == $null) $sht(a=>stats::sd) = 0;
294
-                        $sht(a=>stats::sd) = $sht(a=>stats::sd) + 1;
295
-                }
438
+route[KILL_VICIOUS] {
439
+	xlog("Kill-Vicious ! si : $si ru : $ru ua : $ua\n");
440
+	return;
441
+}
296 442
 
297
-                if(status == "407") {
298
-                        if($sht(a=>response::407::invite) == $null) $sht(a=>response::407::invite)= 0;
299
-                        $sht(a=>response::407::invite) = $sht(a=>response::407::invite) + 1;
300
-                }
301
-                else if(status == "401") {
302
-                        if($sht(a=>response::401::invite) == $null) $sht(a=>response::401::invite)= 0;
303
-                        $sht(a=>response::401::invite) = $sht(a=>response::401::invite) + 1;
304
-                }
305
-                else if(status == "200") {
306
-                        if($sht(a=>response::200::invite) == $null) $sht(a=>response::200::invite)= 0;
307
-                        $sht(a=>response::200::invite) = $sht(a=>response::200::invite) + 1;
308
-                }
309
-        }
310
-        else if($rm == "BYE") {
443
+route[STATS_BY_IP] {
444
+        if($sht(c=>$rs::$sel(via[1].host)) == $null) $sht(c=>$rs::$sel(via[1].host)) = 0;
445
+        $sht(c=>$rs::$sel(via[1].host)) = $sht(c=>$rs::$sel(via[1].host)) + 1;
446
+}
311 447
 
312
-                if(status == "407") {
313
-                        if($sht(a=>response::407::bye) == $null) $sht(a=>response::407::bye) = 0;
314
-                        $sht(a=>response::407::bye) = $sht(a=>response::407::bye) + 1;
315
-                }
316
-                else if(status == "401") {
317
-                        if($sht(a=>response::401::bye) == $null) $sht(a=>response::401::bye) = 0;
318
-                        $sht(a=>response::401::bye) = $sht(a=>response::401::bye) + 1;
319
-                }
320
-                else if(status == "200") {
321
-                        if($sht(a=>response::200::bye) == $null) $sht(a=>response::200::bye) = 0;
322
-                        $sht(a=>response::200::bye) = $sht(a=>response::200::bye) + 1;
448
+route[PARSE_STATS_IP] {
449
+        sht_iterator_start("i1", "c");
450
+        while(sht_iterator_next("i1")) {
451
+                $var(sipcode) = $(shtitkey(i1){s.select,0,:});
452
+                $var(ip) = $(shtitkey(i1){s.select,2,:});
453
+                
454
+                if($shtitval(i1) > $avp($var(sipcode))) {
455
+                        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)')");
323 456
                 }
324 457
         }
458
+        sht_iterator_end("i1");
459
+        sht_rm_name_re("c=>.*");
460
+}
325 461
 
326
-	#sharding
327
-	if($rm == "REGISTER") {
328
-                $var(table) = "sip_capture_registration";
329
-        }
330
-        else if($rm =~ "(INVITE|UPDATE|BYE|ACK|PRACK|REFER)$")
331
-        {
332
-                $var(table) = "sip_capture_call";
333
-        } 
334
-        else if($rm =~ "(INFO)$")
335
-        {
336
-                $var(table) = "sip_capture_call";
337
-        }
338
-        else if($rm =~ "(OPTIONS|PUBLISH)$" )
339
-        {
340
-            $var(table) = "sip_capture_rest";
341
-        }
342
-        else {
343
-            $var(table) = "sip_capture_rest";
344
-        }
462
+route[TIMER_STATS] {
345 463
 
346
-        $var(a) = $var(table) + "_%Y%m%d";
464
+    #xlog("timer routine: time is $TS\n");
347 465
 
348
-        sip_capture("$var(a)");
466
+    route(CHECK_ALARM);
467
+    #Check statistics 	 
468
+    route(CHECK_STATS);
349 469
 
350
-        drop;
351 470
 }
352 471
 
353 472
 
354
-route[TIMER_STATS] {
355
-
356
-    #ALARM SCANNERS
357
-    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;");
358
-    sql_query("cb", "TRUNCATE TABLE alarm_data_mem");
359
-
360
-    #413
361
-    if($sht(a=>alarm::413) > 0) {
362
-        sql_query("cb", "INSERT INTO alarm_data (create_date, type, total, description) VALUES(NOW(), 'Too Many 413', $sht(a=>alarm::413), 'Too many big messages')");
363
-    }
473
+route[SEND_ALARM] {
474
+   	exec_msg('echo "Value: $var(thvalue), Type: $var(atype), Desc: $var(aname)" | mail -s "Homer Alarm $var(atype) - $var(thvalue)" $var(aemail) ') ;
475
+}
364 476
 
365
-    $sht(a=>alarm::413) = 0;
477
+route[CHECK_ALARM] 
478
+{
366 479
 
367
-    #483
368
-    if($sht(a=>alarm::483) > 0) {
369
-        sql_query("cb", "INSERT INTO alarm_data (create_date, type, total, description) VALUES(NOW(), 'Too Many 483', $sht(a=>alarm::483), 'Too many hops messages')");
370
-    }
480
+    #POPULATE ALARM THRESHOLDS
481
+    #Homer 5 sql schema    
482
+    sql_query("cb", "SELECT type,value,name,notify,email FROM alarm_config WHERE NOW() between startdate AND stopdate AND active = 1", "ra");
483
+    if($dbr(ra=>rows)>0)
484
+    {
485
+	$var(i) = 0;
486
+ 	while($var(i)<$dbr(ra=>rows))
487
+        {
488
+                $var(atype) = $dbr(ra=>[$var(i),0]);
489
+                $var(avalue) = $dbr(ra=>[$var(i),1]);
490
+                $var(aname) = $dbr(ra=>[$var(i),2]); 
491
+                $var(anotify) = $(dbr(ra=>[$var(i),3]){s.int});
492
+                $var(aemail) = $dbr(ra=>[$var(i),4]); 
493
+                $avp($var(atype)) = $var(avalue);
494
+
495
+                if($sht(a=>alarm::$var(atype)) != $null) {
496
+                        $var(thvalue) = $sht(a=>alarm::$var(atype));
497
+                }
371 498
 
372
-    $sht(a=>alarm::483) = 0;
499
+                #If Alarm - go here
500
+                if($var(thvalue) > $var(avalue)) {
501
+                                                  
502
+                        sql_query("cb", "INSERT INTO alarm_data (create_date, type, total, description) VALUES(NOW(), '$var(aname)', $var(thvalue), '$var(aname) - $var(atype)');");
503
+                        #Notify
504
+                        if($var(anotify) == 1) {
505
+                                route(SEND_ALARM);
506
+                        }                         
507
+                }
373 508
 
374
-    #482
375
-    if($sht(a=>alarm::482) > 0) {
376
-        sql_query("cb", "INSERT INTO alarm_data (create_date, type, total, description) VALUES(NOW(), 'Too Many 482', $sht(a=>alarm::482), 'Too many loops messages')");
377
-    }
509
+                #Alarm for Scanner;
510
+                if($var(atype) == "scanner") {
511
+                        sql_query("cb", "DELETE FROM alarm_data_mem WHERE type='scanner' AND total < $var(avalue)");
512
+                        if($var(anotify) == 1) 
513
+                        {
514
+                                sql_query("cb", "SELECT * FROM alarm_data_mem WHERE type='scanner' AND total  >= $var(avalue) LIMIT 2", "rd");        
515
+                                if($dbr(rd=>rows) > 0) {
516
+                                        route(SEND_ALARM);
517
+                                }
378 518
 
379
-    $sht(a=>alarm::482) = 0;
519
+                                sql_result_free("rd");
520
+                        }
521
+                }
522
+         
523
+                $sht(a=>alarm::$var(atype)) = 0;
524
+                $var(i) = $var(i) + 1;
525
+        }
380 526
 
381
-    #403
382
-    if($sht(a=>alarm::403) > 0) {
383
-        sql_query("cb", "INSERT INTO alarm_data (create_date, type, total, description) VALUES(NOW(), 'Too Many 403', $sht(a=>alarm::403), 'fraud alarm')");
384 527
     }
385
-    $sht(a=>alarm::403) = 0;
386 528
 
387
-    #503
388
-    if($sht(a=>alarm::503) > 0) {
389
-        sql_query("cb", "INSERT INTO alarm_data (create_date, type, total, description) VALUES(NOW(), 'Too Many 503', $sht(a=>alarm::503), 'service unavailable')");
390
-    }
391
-    $sht(a=>alarm::503) = 0;
392 529
 
393
-    #500
394
-    if($sht(a=>alarm::500) > 0) {
395
-        sql_query("cb", "INSERT INTO alarm_data (create_date, type, total, description) VALUES(NOW(), 'Too Many 500', $sht(a=>alarm::500), 'server errors')");
396
-    }
397
-    $sht(a=>alarm::500) = 0;
530
+    sql_result_free("ra");
398 531
 
399
-    #408
400
-    if($sht(a=>alarm::408) > 0) {
401
-        sql_query("cb", "INSERT INTO alarm_data (create_date, type, total, description) VALUES(NOW(), 'Too Many 408', $sht(a=>alarm::408), 'Timeout')");
402
-    }
403 532
 
404
-    $sht(a=>alarm::408) = 0;
533
+    #if(sql_xquery("cb", "select type,value,name,notify,email from alarm_config WHERE startdate > NOW() AND stopdate < NOW() AND active = 1", "trigger") == 1) {
534
+    #    $var(i) = 0;
535
+    #    while($xavp(trigger[$var(i)]) != $null) {
536
+    #      $avp($xavp(trigger[$var(i)]=>type)) = $xavp(trigger[$var(i)]=>value); 
537
+    #       $avp($xavp(trigger[$var(i)]=>type)+"_notify") = $xavp(trigger[$var(i)]=>notify); 
538
+    #       $avp($xavp(trigger[$var(i)]=>type)+"_email") = $xavp(trigger[$var(i)]=>email); 
539
+    #       $var(i) = $var(i) + 1;
540
+    #    }   
541
+    #}
405 542
 
406
-    #400
407
-    if($sht(a=>alarm::400) > 0) {
408
-        sql_query("cb", "INSERT INTO alarm_data (create_date, type, total, description) VALUES(NOW(), 'Too Many 400', $sht(a=>alarm::400), 'Too many bad request')");
409
-    }
410
-    $sht(a=>alarm::400) = 0;
543
+    route(PARSE_STATS_IP);
411 544
 
412 545
     #delete old alarms
413 546
     sql_query("cb", "DELETE FROM alarm_data WHERE create_date < DATE_SUB(NOW(), INTERVAL 5 DAY)");
547
+}
414 548
 
415
-    #SQL STATS
416 549
 
417
-    $var(tm) = ($time(min) mod 10);
550
+route[CHECK_STATS] {
418 551
 
419
-    if($var(tm) != 0 && $var(tm) != 5) return;
552
+    #SQL STATS
553
+    $var(tm) = ($time(min) mod 10);
554
+    if($var(tm) != 0 && $var(tm) != $sel(cfg_get.stats.min)) return;
420 555
 
421 556
     $var(t1) = $TS;
422
-    $var(t2) = $var(t1) - 300;
557
+    $var(t2) = $var(t1) - ($sel(cfg_get.stats.min)*60);
423 558
 
424 559
     $var(t_date) = "FROM_UNIXTIME(" + $var(t1) + ", '%Y-%m-%d %H:%i:00')";
425 560
     $var(f_date) = "FROM_UNIXTIME(" + $var(t2) + ", '%Y-%m-%d %H:%i:00')";
426 561
 
562
+    #ALARM SCANNERS
563
+    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;");
564
+    sql_query("cb", "TRUNCATE TABLE alarm_data_mem");
565
+
427 566
     #STATS Useragent
428 567
     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;");
429 568
     sql_query("cb", "TRUNCATE TABLE stats_useragent_mem");
... ...
@@ -432,28 +571,43 @@ route[TIMER_STATS] {
432 571
     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;");
433 572
     sql_query("cb", "TRUNCATE TABLE stats_ip_mem");
434 573
 
574
+#!ifdef WITH_HOMER_GEO
575
+    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;");
576
+    sql_query("cb", "TRUNCATE TABLE stats_geo_mem");
577
+#!endif
578
+    
435 579
     #INSERT SQL STATS
580
+    #Packet HEP stats
581
+    if($sht(a=>packet::count) != $null && $sht(a=>packet::count) > 0) {
582
+        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)");
583
+        $sht(a=>packet::count) = 0;
584
+    }
585
+    if($sht(a=>packet::size) != $null && $sht(a=>packet::size) > 0) {
586
+        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)");
587
+        $sht(a=>packet::size) = 0;
588
+    }
589
+
436 590
     #SDF
437 591
     if($sht(a=>stats::sdf) != $null && $sht(a=>stats::sdf) > 0) {
438
-        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))");
592
+        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)");
439 593
         $sht(a=>stats::sdf) = 0;
440 594
     }
441 595
 
442 596
     #ISA
443 597
     if($sht(a=>stats::isa) != $null && $sht(a=>stats::isa) > 0) {
444
-        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))");
598
+        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)");
445 599
         $sht(a=>stats::isa) = 0;
446 600
     }
447 601
 
448 602
     #SD
449 603
     if($sht(a=>stats::sd) != $null && $sht(a=>stats::sd) > 0) {
450
-        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))");
604
+        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)");
451 605
         $sht(a=>stats::sd) = 0;
452 606
     }
453 607
 
454 608
     #SSR
455 609
     if($sht(a=>stats::ssr) != $null && $sht(a=>stats::ssr) > 0) {
456
-        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))");
610
+        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)");
457 611
         $sht(a=>stats::ssr) = 0;
458 612
     }
459 613
 
... ...
@@ -465,13 +619,13 @@ route[TIMER_STATS] {
465 619
         if($sht(a=>response::200::invite) == $null) $sht(a=>response::200::invite) = 0;
466 620
         $var(d) = $sht(a=>method::invite) - $sht(a=>response::407::invite);
467 621
         if($var(d) > 0) {
468
-                $var(asr) =  $sht(a=>response::200::invite) / $var(d) * 100;
622
+                $var(asr) =  $sht(a=>response::200::invite) * 100 / $var(d);
469 623
                 if($var(asr) > 100)  $var(asr) = 100;
470 624
         }
471 625
     }
472 626
 
473 627
     #Stats DATA
474
-    sql_query("cb", "INSERT INTO stats_data (from_date, to_date, type, total) VALUES($var(f_date), $var(t_date), 'asr', $var(asr))");
628
+    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");
475 629
 
476 630
 
477 631
     #NER
... ...
@@ -486,70 +640,84 @@ route[TIMER_STATS] {
486 640
         $var(d) = $sht(a=>method::invite) - $sht(a=>response::407::invite);
487 641
 
488 642
         if($var(d) > 0) {
489
-                $var(ner) =  ($sht(a=>response::200::invite) + $sht(a=>stats::bad::invite)) / $var(d) * 100;
643
+                $var(ner) = ($sht(a=>response::200::invite) + $sht(a=>stats::bad::invite)) * 100 / $var(d);
490 644
                 if($var(ner) > 100)  $var(ner) = 100;
491 645
         }
492 646
     }
493 647
 
494
-    sql_query("cb", "INSERT INTO stats_data (from_date, to_date, type, total) VALUES($var(f_date), $var(t_date), 'ner', $var(ner))");
648
+    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");
495 649
 
496 650
     #INVITE
497 651
     if($sht(a=>method::reinvite) > 0) {
498
-        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))");
652
+        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)");
499 653
         $sht(a=>method::reinvite) = 0;
500 654
     }
501 655
 
502 656
     #INVITE
503 657
     if($sht(a=>method::invite) > 0) {
504
-        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))");
658
+        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)");
505 659
         $sht(a=>method::invite) = 0;
506 660
     }
507 661
 
508 662
     #INVITE AUTH
509 663
     if($sht(a=>method::invite::auth) > 0) {
510
-        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))");
664
+        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)");
511 665
         $sht(a=>method::invite::auth) = 0;
512 666
     }
513 667
 
514 668
     #REGISTER
515 669
     if($sht(a=>method::register) > 0) {
516
-        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))");
670
+        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)");
517 671
         $sht(a=>method::register) = 0;
518 672
     }
519 673
 
520 674
     #REGISTER AUTH
521 675
     if($sht(a=>method::register::auth) > 0) {
522
-        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))");
676
+        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)");
523 677
         $sht(a=>method::register::auth) = 0;
524 678
     }
525 679
 
526 680
     #BYE
527 681
     if($sht(a=>method::bye) > 0) {
528
-        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))");
682
+        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)");
529 683
         $sht(a=>method::bye) = 0;
530 684
     }
531 685
 
532 686
     #CANCEL
533 687
     if($sht(a=>method::cancel) > 0) {
534
-        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))");
688
+        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)");
535 689
         $sht(a=>method::cancel) = 0;
536 690
     }
537 691
 
538 692
     #OPTIONS
539 693
     if($sht(a=>method::options) > 0) {
540
-        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))");
694
+        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)");
541 695
         $sht(a=>method::options) = 0;
542 696
     }
543 697
 
698
+#!ifdef WITH_STATISTIC_METHOD_EXTRA
699
+    #UNKNOWN
700
+    if($sht(a=>method::unknown) > 0) {
701
+        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)");
702
+        $sht(a=>method::unknown) = 0;
703
+    }
704
+    
705
+    #ACK
706
+    if($sht(a=>method::ack) > 0) {
707
+        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)");
708
+        $sht(a=>method::ack) = 0;
709
+    }
710
+#!endif
711
+
544 712
     #REFER
545 713
     if($sht(a=>method::refer) > 0) {
546
-        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))");
714
+        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)");
547 715
         $sht(a=>method::refer) = 0;
548 716
     }
549 717
 
550 718
     #UPDATE
551 719
     if($sht(a=>method::update) > 0) {
552
-        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))");
720
+        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)");
553 721
         $sht(a=>method::update) = 0;
554 722
     }
555 723
 
... ...
@@ -557,50 +725,180 @@ route[TIMER_STATS] {
557 725
 
558 726
     #300
559 727
     if($sht(a=>response::300) > 0) {
560
-        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))");
728
+        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)");
561 729
         $sht(a=>response::300) = 0;
562 730
     }
563 731
 
564 732
     #407 INVITE
565 733
     if($sht(a=>response::407::invite) > 0) {
566
-        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))");
734
+        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)");
567 735
         $sht(a=>response::407::invite) = 0;
568 736
     }
569 737
 
570 738
     #401 INVITE
571 739
     if($sht(a=>response::401::invite) > 0) {
572
-        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))");
740
+        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)");
573 741
         $sht(a=>response::401::invite) = 0;
574 742
     }
575 743
 
744
+#!ifdef WITH_STATISTIC_INVITE_1XX
745
+    #100 INVITE
746
+    if($sht(a=>response::100::invite) > 0) {
747
+        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)");
748
+        $sht(a=>response::100::invite) = 0;
749
+    }
750
+
751
+    #180 INVITE
752
+    if($sht(a=>response::180::invite) > 0) {
753
+        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)");
754
+        $sht(a=>response::180::invite) = 0;
755
+    }
756
+
757
+    #183 INVITE
758
+    if($sht(a=>response::183::invite) > 0) {
759
+        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)");
760
+        $sht(a=>response::183::invite) = 0; 
761
+    }
762
+
763
+#!endif
764
+
576 765
     #200 INVITE
577 766
     if($sht(a=>response::200::invite) > 0) {
578
-        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))");
767
+        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)");
579 768
         $sht(a=>response::200::invite) = 0;
580 769
     }
581 770
 
582 771
     #407 BYE
583 772
     if($sht(a=>response::407::bye) > 0) {
584
-        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))");
773
+        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)");
585 774
         $sht(a=>response::407::bye) = 0;
586 775
     }
587 776
 
588 777
     #401 BYE
589 778
     if($sht(a=>response::401::bye) > 0) {
590
-        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))");
779
+        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)");
591 780
         $sht(a=>response::401::bye) = 0;
592 781
     }
593 782
 
594 783
     #200 BYE
595 784
     if($sht(a=>response::200::bye) > 0) {
596
-        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))");
785
+        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)");
597 786
         $sht(a=>response::200::bye) = 0;
598 787
     }
599 788
 
600
-    #ALL MESSAGES
789
+    #ALL TRANSACTIONS MESSAGES
601 790
     if($sht(a=>method::all) > 0) {
602
-        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))");
791
+        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)");
603 792
         $sht(a=>method::all) = 0;
604 793
     }
794
+    
795
+     #ALL MESSAGES ON INTERFACE
796
+    if($sht(a=>method::total) > 0) {
797
+        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)");
798
+        $sht(a=>method::total) = 0;
799
+    }
800
+
801
+#!ifdef WITH_HOMER_CUSTOM_STATS
802
+    #Generic stats
803
+    sht_iterator_start("i1", "d");
804
+    while(sht_iterator_next("i1")) {
805
+                $var(key) = $(shtitkey(i1){s.select,2,:});
806
+                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");
807
+    }
808
+    sht_iterator_end("i1");
809
+    sht_rm_name_re("d=>.*");
810
+#!endif
811
+
605 812
 }
606 813
 
814
+
815
+route[STORE] {
816
+
817
+        if($rm == "REGISTER") {
818
+                $var(table) = "sip_capture_registration";       
819
+        }
820
+        else if($rm =~ "(INVITE|UPDATE|BYE|ACK|PRACK|REFER|CANCEL)$")
821
+        {
822
+                $var(table) = "sip_capture_call";
823
+        } 
824
+        else if($rm =~ "(NOTIFY)$" && is_present_hf("Event") && $hdr(Event)=~"refer;")
825
+        {
826
+                $var(table) = "sip_capture_call";
827
+        }
828
+        else if($rm =~ "(INFO)$")
829
+        {
830
+                $var(table) = "sip_capture_call";
831
+        }
832
+        else if($rm =~ "(OPTIONS)$" )
833
+        {
834
+            $var(table) = "sip_capture_rest";
835
+        }
836
+        else {   
837
+            $var(table) = "sip_capture_rest";
838
+        }
839
+
840
+	
841
+#!ifdef KAMAILIO_4_3
842
+	#For old models. Not accurate insert time. System vs capture time.
843
+	#$var(utc) = $timef(%Y%m%d);	
844
+	#as workaround for kamailio < 4.4
845
+	sql_query("cb", "SELECT UTC_DATE()+0;", "ra");
846
+    	if($dbr(ra=>rows)>0) {
847
+              $var(utc) = $dbr(ra=>[0,0]);
848
+        }
849
+        sql_result_free("ra");
850
+#!else
851
+	#for Kamailio >=4.4 please uncomment this parameters:
852
+	# New utimef can fix the timediff issue. But unfortunately only kamailio 4.4
853
+	$var(utc) = $utimef(%Y%m%d);
854
+	# or sipcapture style
855
+	#$var(utc) = "%Y%m%d";
856
+#!endif                        
857
+
858
+	$var(a) = $var(table) + "_" + $var(utc);
859
+	sip_capture("$var(a)");
860
+}
861
+
862
+event_route[sipcapture:request] {
863
+	xlog("received sipcapture request from $si:$sp\r\n");
864
+}
865
+
866
+#!ifdef WITH_HOMER_CUSTOM_STATS
867
+event_route[xhttp:request] {
868
+        set_reply_close();
869
+        set_reply_no_connect();
870
+        xlog("L_WARN", "HTTP request received on $Rp, $hu\n");
871
+        if($hu =~ "/api/v1/stats/push") {
872
+                #Json is our body
873
+                $var(json) = $rb;
874
+                jansson_get("type", $var(json), "$var(n)");
875
+                xlog("L_WARN","Type is $var(n)");
876
+                if($var(n) == "rtp_stat") {
877
+                         $var(i) = 0;
878
+                         $(avp(x)[0]) = 'interval';
879
+                         $(avp(x)[1]) = 'streams';
880
+                         $(avp(x)[2]) = 'packets';
881
+                         $(avp(x)[3]) = 'lost';
882
+                         $(avp(x)[4]) = 'late';
883
+                         $(avp(x)[5]) = 'lost_perc';
884
+                         $(avp(x)[6]) = 'late_perc';
885
+                         $(avp(x)[7]) = 'out_of_seq';
886
+                         $(avp(x)[8]) = 'delay_min';
887