This file is indexed.

/etc/homer/kamailio-homer.cfg is in homer-api-postgresql 5.0.6+dfsg2-3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
#!KAMAILIO
#
# Example configuration file for a sipcapture node
#

####### Global Parameters definitions #########
#
# Please, make all your configuration changes here
#
# *** To enable extra stats
#     - define WITH_STATISTIC_METHOD_EXTRA
#     - define WITH_STATISTIC_INVITE_1XX



#!substdef "!HOMER_DB_USER!homer_user!g"
#!substdef "!HOMER_DB_PASSWORD!homer_password!g"
#!substdef "!HOMER_LISTEN_PROTO!udp!g"
#!substdef "!HOMER_LISTEN_IF!0.0.0.0!g"
#!substdef "!HOMER_LISTEN_PORT!9060!g"
#!substdef "!HOMER_STATS_SERVER!tcp:HOMER_LISTEN_IF:8888!g"

####### Global Parameters #########

debug=1
log_stderror=no

memdbg=5
memlog=5

##!define KAMAILIO_4_3
#!define WITH_HOMER_GEO
##!define WITH_HOMER_CUSTOM_STATS #enable it for HTTP custom stats

log_facility=LOG_LOCAL1

fork=yes
children=5

/* uncomment the next line to disable TCP (default on) */
disable_tcp=yes

/* IP and port for HEP capturing) */
listen=HOMER_LISTEN_PROTO:HOMER_LISTEN_IF:HOMER_LISTEN_PORT

#!ifdef WITH_HOMER_CUSTOM_STATS
listen=HOMER_STATS_SERVER
#!endif

#Max loops
max_while_loops=100

mpath="/usr/lib/x86_64-linux-gnu/kamailio/modules/"
loadmodule "pv.so"
loadmodule "db_postgres.so"
loadmodule "sipcapture.so"
loadmodule "textops.so"
loadmodule "rtimer.so"
loadmodule "xlog.so"
loadmodule "sqlops.so"
loadmodule "htable.so"
loadmodule "tm.so"
loadmodule "sl.so"
loadmodule "siputils.so"
loadmodule "exec.so"

#!ifdef WITH_HOMER_GEO
loadmodule "geoip.so"
#!endif

#!ifdef WITH_HOMER_CUSTOM_STATS
loadmodule "xhttp.so"
loadmodule "jansson.so"
loadmodule "avpops.so"
#!endif


modparam("htable", "htable", "a=>size=8;autoexpire=400")
modparam("htable", "htable", "b=>size=8;autoexpire=31")
# TODO: tune autoexpire setting for htable "c"
modparam("htable", "htable", "c=>size=8;autoexpire=31")

#!ifdef WITH_HOMER_CUSTOM_STATS
modparam("htable", "htable", "d=>size=8;autoexpire=400")
modparam("xhttp", "url_match", "^/api/v1/stat")
#!endif

modparam("rtimer", "timer", "name=ta;interval=60;mode=1;")
modparam("rtimer", "exec", "timer=ta;route=TIMER_STATS")

modparam("sqlops","sqlcon","cb=>postgres://HOMER_DB_USER:HOMER_DB_PASSWORD@127.0.0.1/homer_statistic")

# ----- mi_fifo params -----

####### Routing Logic ########
modparam("sipcapture", "db_url", "postgres://HOMER_DB_USER:HOMER_DB_PASSWORD@127.0.0.1/homer_data")
modparam("sipcapture", "capture_on", 1)
modparam("sipcapture", "hep_capture_on", 1)
modparam("sipcapture", "insert_retries", 5)
modparam("sipcapture", "insert_retry_timeout", 10)
#modparam("sipcapture", "capture_node", "homer01")

#!ifdef WITH_HOMER_GEO
modparam("geoip", "path", "/usr/share/GeoIP/GeoIP.dat")
#!endif

#Stats time
stats.min = 5 desc "My stats TIME min"


# Main SIP request routing logic
# - processing of any incoming SIP request starts with this route
route {

	if($sht(a=>method::total) == $null) $sht(a=>method::total) = 0;
	$sht(a=>method::total) = $sht(a=>method::total) + 1;
	if($sht(a=>packet::count) == $null) $sht(a=>packet::count) = 0;
        if($sht(a=>packet::size) == $null) $sht(a=>packet::size) = 0;
	
	 #Packets
        $sht(a=>packet::count) = $sht(a=>packet::count) + 1;
        $sht(a=>packet::size) = $sht(a=>packet::size) + $ml;


	if($sht(b=>$rm::$cs::$ci) != $null) {
		#$var(a) = "sip_capture_call" + "_%Y%m%d";
		#Store
		route(STORE);
		drop;
	}

	$sht(b=>$rm::$cs::$ci) = 1;	
	if($sht(a=>method::all) == $null) $sht(a=>method::all) = 0;
	$sht(a=>method::all) = $sht(a=>method::all) + 1;
	
	if (is_method("INVITE|REGISTER")) {

		if($ua =~ "(friendly-scanner|sipvicious|sipcli)") {
			sql_query("cb", "INSERT INTO alarm_data_mem (create_date, type, total, source_ip, description) VALUES(NOW(), 'scanner', 1, '$si', 'Friendly scanner alarm!')");
			route(KILL_VICIOUS);
		}

		#IP Method
		sql_query("cb", "INSERT INTO stats_ip_mem ( method, source_ip, total) VALUES('$rm', '$si', 1) ");
		xlog("INSERT INTO stats_ip_mem ( method, source_ip, total) VALUES('$rm', '$si', 1) ;");

#!ifdef WITH_HOMER_GEO
		if(geoip_match("$si", "src")) {
                        xlog("REGISTER|INVITE SIP message [$si] from: $gip(src=>cc)\n");
                        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)");
                }
#!endif


		if($au != $null)  $var(anumber) = $au;
		else $var(anumber) = $fU;

		if($(var(anumber){s.substr,0,5}) == "+204231") {
                	if($sht(a=>alarm::scam) == $null) $sht(a=>alarm::scam) = 0;
                        $sht(a=>alarm::scam) = $sht(a=>alarm::scam) + 1;
		}

		if($(rU{s.substr,0,5}) == "+204231") {
			if($sht(a=>alarm::scam) == $null) $sht(a=>alarm::scam) = 0;
			$sht(a=>alarm::scam) = $sht(a=>alarm::scam) + 1; 
		}
	
	
		#if($sel(contact.uri.host) =~ "^([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))$") {
		if($sel(contact.uri.host) =~ "^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$") {
			if($sht(a=>alarm::dns) == $null) $sht(a=>alarm::dns) = 0;
			$sht(a=>alarm::dns) = $sht(a=>alarm::dns) + 1; 
		}


		if($sel(contact.uri.host) != $si) {
			if($sht(a=>alarm::spoofing) == $null) $sht(a=>alarm::spoofing) = 0;
			$sht(a=>alarm::spoofing) = $sht(a=>alarm::spoofing) + 1; 
		}

		if($au =~ "(\=)|(\-\-)|(\')|(\#)|(\%27)|(\%24)") {
			if($sht(a=>alarm::sqlinjection) == $null) $sht(a=>alarm::sqlinjection) = 0;
			$sht(a=>alarm::sqlinjection) = $sht(a=>alarm::sqlinjection) + 1; 
		}

		if($(hdr(Record-Route)[0]{nameaddr.uri}) != $si) {
			if($sht(a=>alarm::spoofing) == $null) $sht(a=>alarm::spoofing) = 0;
			$sht(a=>alarm::spoofing) = $sht(a=>alarm::spoofing) + 1; 
		}
		

		if (is_method("INVITE")) {			

		        if (has_totag()) {
				if($sht(a=>method::reinvite) == $null) $sht(a=>method::reinvite) = 0;
				$sht(a=>method::reinvite) = $sht(a=>method::reinvite) + 1; 				
			}
			else {
				if($sht(a=>method::invite) == $null) $sht(a=>method::invite) = 0; 
				$sht(a=>method::invite) = $sht(a=>method::invite) + 1; 
				if($adu != $null) {
					if($sht(a=>method::invite::auth) == $null) $sht(a=>method::invite::auth) = 0; 
					$sht(a=>method::invite::auth) = $sht(a=>method::invite::auth) + 1; 
				}

				if($ua != $null) {
					sql_query("cb", "INSERT INTO stats_useragent_mem (useragent, method, total) VALUES('$ua', 'INVITE', 1) ");
				}

			}					
		}
		else {
			if($sht(a=>method::register) == $null) $sht(a=>method::register) = 0; 		
			$sht(a=>method::register) = $sht(a=>method::register) + 1; 

			if($adu != $null) {
				if($sht(a=>method::register::auth) == $null) $sht(a=>method::register::auth) = 0; 
				$sht(a=>method::register::auth) = $sht(a=>method::register::auth) + 1; 
			}
			
			if($ua != $null) {
				sql_query("cb", "INSERT INTO stats_useragent_mem (useragent, method, total) VALUES('$ua', 'REGISTER', 1)");
			}
		}

	}
	else if(is_method("BYE")) {
		if($sht(a=>method::bye) == $null) $sht(a=>method::bye) = 0; 
		$sht(a=>method::bye) = $sht(a=>method::bye) + 1; 		
		if(is_present_hf("Reason")) {
                       $var(cause) = $(hdr(Reason){param.value,cause}{s.int});
                       if($var(cause) != 16 && $var(cause) !=17) {
				if($sht(a=>stats::sdf) == $null) $sht(a=>stats::sdf) = 0; 
				$sht(a=>stats::sdf) = $sht(a=>stats::sdf) + 1; 
		       }
		}

#		if($si != $sht(a=>ipinit::aleg::$ci) && $si != $sht(a=>ipinit::bleg::$ci)) {
#			if($sht(a=>alarm::sessiontd) == $null) $sht(a=>alarm::sessiontd) = 0;
#			$sht(a=>alarm::sessiontd) = $sht(a=>alarm::sessiontd) + 1; 
#		}

	}
	else if(is_method("CANCEL")) {
		if($sht(a=>method::cancel) == $null) $sht(a=>method::cancel) = 0; 
		$sht(a=>method::cancel) = $sht(a=>method::cancel) + 1; 

	}
	else if(is_method("OPTIONS")) {
		if($sht(a=>method::options) == $null) $sht(a=>method::options) = 0; 
		$sht(a=>method::options) = $sht(a=>method::options) + 1; 
		#xlog("OPTIONS COUNTING.... $sht(a=>method::options)\n");

	}
	else if(is_method("REFER")) {
		if($sht(a=>method::refer) == $null) $sht(a=>method::refer) = 0; 
		$sht(a=>method::refer) = $sht(a=>method::refer) + 1; 

	}
	else if(is_method("UPDATE")) {
		if($sht(a=>method::update) == $null) $sht(a=>method::update) = 0; 
		$sht(a=>method::update) = $sht(a=>method::update) + 1; 
	}
	
	else if(is_method("PUBLISH"))
        {
                if(has_body("application/vq-rtcpxr") && $(rb{s.substr,0,1}) != "x") {
                        $var(table) = "report_capture";
                        $var(callid) = $(rb{re.subst,/.*CallID:([0-9A-Za-z@-_\.]{5,120}).*$/\1/s});
			
			#Local IP. Only for stats
			#$var(localaddress) = $(rb{re.subst,/.*LocalAddr:IP=([^\S]+).*$/\1/s});

                        #xlog("CALLID: $var(callid)");
#!ifdef KAMAILIO_4_3
			xlog("report_capture is not in < 4.3");
#!else
			report_capture("$var(table)", "$var(callid)");
#!endif                        
                        drop;
                }
        }
	
#!ifdef WITH_STATISTIC_METHOD_EXTRA
	else if(is_method("ACK")) {
                if($sht(a=>method::ack) == $null) $sht(a=>method::ack) = 0; 
                $sht(a=>method::ack) = $sht(a=>method::ack) + 1;
        }
        else {
                if($sht(a=>method::unknown) == $null) $sht(a=>method::unknown) = 0; 
                $sht(a=>method::unknown) = $sht(a=>method::uknown) + 1; 
        }     
#!endif     

	#Store
	route(STORE);
	drop;
}

onreply_route {

	if($sht(a=>method::total) == $null) $sht(a=>method::total) = 0;
	$sht(a=>method::total) = $sht(a=>method::total) + 1;

	if($sht(b=>$rs::$cs::$rm::$ci) != $null) {
		#Store
		route(STORE);
		drop;
	}

	$sht(b=>$rs::$cs::$rm::$ci) = 1;
	
	if($sht(a=>method::all) == $null) $sht(a=>method::all) = 0;
	$sht(a=>method::all) = $sht(a=>method::all) + 1;

	#413 Too large
	if(status == "413") {	
		if($sht(a=>response::413) == $null) $sht(a=>response::413) = 0; 
		$sht(a=>response::413) = $sht(a=>response::413) + 1; 

		if($sht(a=>alarm::413) == $null) $sht(a=>alarm::413) = 0; 
		$sht(a=>alarm::413) = $sht(a=>alarm::413) + 1; 
	}
	#403 Unauthorize
        else if(status == "403") {
                if($sht(a=>response::403) == $null) $sht(a=>response::403) = 0; 
                $sht(a=>response::403) = $sht(a=>response::403) + 1; 

                if($sht(a=>alarm::403) == $null) $sht(a=>alarm::403) = 0; 
                $sht(a=>alarm::403) = $sht(a=>alarm::403) + 1; 
        }
	# Too many hops
	else if(status == "483") {	
		if($sht(a=>response::483) == $null) $sht(a=>response::483) = 0; 
		$sht(a=>response::483) = $sht(a=>response::483) + 1; 

		if($sht(a=>alarm::483) == $null) $sht(a=>alarm::483) = 0; 
		$sht(a=>alarm::483) = $sht(a=>alarm::483) + 1; 

	}
	# loops
	else if(status == "482") {	
		if($sht(a=>response::482) == $null) $sht(a=>response::482) = 0; 
		$sht(a=>response::482) = $sht(a=>response::482) + 1; 

		if($sht(a=>alarm::482) == $null) $sht(a=>alarm::482) = 0; 
		$sht(a=>alarm::482) = $sht(a=>alarm::482) + 1; 

	}
	# Call Transaction Does not exist
	else if(status == "481") {	
		if($sht(a=>alarm::481) == $null) $sht(a=>alarm::481) = 0; 
		$sht(a=>alarm::481) = $sht(a=>alarm::481) + 1; 
	}
	# 408 Timeout
	else if(status == "408") {	
		if($sht(a=>alarm::408) == $null) $sht(a=>alarm::408) = 0; 
		$sht(a=>alarm::408) = $sht(a=>alarm::408) + 1; 
	}
	# 400
	else if(status == "400") {	

		if($sht(a=>alarm::400) == $null) $sht(a=>alarm::400) = 0; 
		$sht(a=>alarm::400) = $sht(a=>alarm::400) + 1; 

	}
	# MOVED
	else if(status =~ "^(30[012])$") {	
		if($sht(a=>response::300) == $null) $sht(a=>response::300) = 0; 
		$sht(a=>response::300) = $sht(a=>response::300) + 1; 
	}

	if($rm == "INVITE") {
		#ISA
		if(status =~ "^(408|50[03])$") {	
			if($sht(a=>stats::isa) == $null) $sht(a=>stats::isa) = 0; 
			$sht(a=>stats::isa) = $sht(a=>stats::isa) + 1; 	
		}
		#Bad486
		if(status =~ "^(486|487|603)$") {	
			if($sht(a=>stats::bad::invite) == $null) $sht(a=>stats::bad::invite) = 0; 
			$sht(a=>stats::bad::invite) = $sht(a=>stats::bad::invite) + 1; 	
		}

		#SD
		if(status =~ "^(50[034])$") {	
			if($sht(a=>stats::sd) == $null) $sht(a=>stats::sd) = 0; 
			$sht(a=>stats::sd) = $sht(a=>stats::sd) + 1; 	
		}

		if(status == "407") {	
			if($sht(a=>response::407::invite) == $null) $sht(a=>response::407::invite)= 0;
			$sht(a=>response::407::invite) = $sht(a=>response::407::invite) + 1; 
		}
		else if(status == "401") {			
			if($sht(a=>response::401::invite) == $null) $sht(a=>response::401::invite)= 0;
			$sht(a=>response::401::invite) = $sht(a=>response::401::invite) + 1; 
		}
		else if(status == "200") {			
			if($sht(a=>response::200::invite) == $null) $sht(a=>response::200::invite)= 0;
			$sht(a=>response::200::invite) = $sht(a=>response::200::invite) + 1; 
		}
		#Aditional stats
#!ifdef WITH_STATISTIC_INVITE_1XX		
	        else if(status == "100") {
                        if($sht(a=>response::100::invite) == $null) $sht(a=>response::100::invite)= 0;
                        $sht(a=>response::100::invite) = $sht(a=>response::100::invite) + 1;
                }
                else if(status == "180") {
                        if($sht(a=>response::180::invite) == $null) $sht(a=>response::180::invite)= 0;
                        $sht(a=>response::180::invite) = $sht(a=>response::180::invite) + 1;
                }   
                else if(status == "183") {                      
                        if($sht(a=>response::183::invite) == $null) $sht(a=>response::183::invite)= 0;
                        $sht(a=>response::183::invite) = $sht(a=>response::183::invite) + 1;
                }
#!endif                
	}
	else if($rm == "BYE") {

		if(status == "407") {	
			if($sht(a=>response::407::bye) == $null) $sht(a=>response::407::bye) = 0;
			$sht(a=>response::407::bye) = $sht(a=>response::407::bye) + 1; 
		}
		else if(status == "401") {			
			if($sht(a=>response::401::bye) == $null) $sht(a=>response::401::bye) = 0;
			$sht(a=>response::401::bye) = $sht(a=>response::401::bye) + 1; 
		}
		else if(status == "200") {			
			if($sht(a=>response::200::bye) == $null) $sht(a=>response::200::bye) = 0;
			$sht(a=>response::200::bye) = $sht(a=>response::200::bye) + 1; 
		}
	}
	
	#Store
	route(STORE);
	drop;
}

route[KILL_VICIOUS] {
	xlog("Kill-Vicious ! si : $si ru : $ru ua : $ua\n");
	return;
}

route[STATS_BY_IP] {
        if($sht(c=>$rs::$sel(via[1].host)) == $null) $sht(c=>$rs::$sel(via[1].host)) = 0;
        $sht(c=>$rs::$sel(via[1].host)) = $sht(c=>$rs::$sel(via[1].host)) + 1;
}

route[PARSE_STATS_IP] {
        sht_iterator_start("i1", "c");
        while(sht_iterator_next("i1")) {
                $var(sipcode) = $(shtitkey(i1){s.select,0,:});
                $var(ip) = $(shtitkey(i1){s.select,2,:});
                
                if($shtitval(i1) > $avp($var(sipcode))) {
                        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)')");
                }
        }
        sht_iterator_end("i1");
        sht_rm_name_re("c=>.*");
}

route[TIMER_STATS] {

    #xlog("timer routine: time is $TS\n");

    route(CHECK_ALARM);
    #Check statistics 	 
    route(CHECK_STATS);

}


route[SEND_ALARM] {
   	exec_msg('echo "Value: $var(thvalue), Type: $var(atype), Desc: $var(aname)" | mail -s "Homer Alarm $var(atype) - $var(thvalue)" $var(aemail) ') ;
}

route[CHECK_ALARM] 
{

    #POPULATE ALARM THRESHOLDS
    #Homer 5 sql schema    
    sql_query("cb", "SELECT type,value,name,notify,email FROM alarm_config WHERE NOW() between startdate AND stopdate AND active = 1", "ra");
    if($dbr(ra=>rows)>0)
    {
	$var(i) = 0;
 	while($var(i)<$dbr(ra=>rows))
        {
                $var(atype) = $dbr(ra=>[$var(i),0]);
                $var(avalue) = $dbr(ra=>[$var(i),1]);
                $var(aname) = $dbr(ra=>[$var(i),2]); 
                $var(anotify) = $(dbr(ra=>[$var(i),3]){s.int});
                $var(aemail) = $dbr(ra=>[$var(i),4]); 
                $avp($var(atype)) = $var(avalue);

                if($sht(a=>alarm::$var(atype)) != $null) {
                        $var(thvalue) = $sht(a=>alarm::$var(atype));
                }

                #If Alarm - go here
                if($var(thvalue) > $var(avalue)) {
                                                  
                        sql_query("cb", "INSERT INTO alarm_data (create_date, type, total, description) VALUES(NOW(), '$var(aname)', $var(thvalue), '$var(aname) - $var(atype)');");
                        #Notify
                        if($var(anotify) == 1) {
                                route(SEND_ALARM);
                        }                         
                }

                #Alarm for Scanner;
                if($var(atype) == "scanner") {
                        sql_query("cb", "DELETE FROM alarm_data_mem WHERE type='scanner' AND total < $var(avalue)");
                        if($var(anotify) == 1) 
                        {
                                sql_query("cb", "SELECT * FROM alarm_data_mem WHERE type='scanner' AND total  >= $var(avalue) LIMIT 2", "rd");        
                                if($dbr(rd=>rows) > 0) {
                                        route(SEND_ALARM);
                                }

                                sql_result_free("rd");
                        }
                }
         
                $sht(a=>alarm::$var(atype)) = 0;
                $var(i) = $var(i) + 1;
        }

    }


    sql_result_free("ra");


    #if(sql_xquery("cb", "select type,value,name,notify,email from alarm_config WHERE startdate > NOW() AND stopdate < NOW() AND active = 1", "trigger") == 1) {
    #    $var(i) = 0;
    #    while($xavp(trigger[$var(i)]) != $null) {
    #      $avp($xavp(trigger[$var(i)]=>type)) = $xavp(trigger[$var(i)]=>value); 
    #       $avp($xavp(trigger[$var(i)]=>type)+"_notify") = $xavp(trigger[$var(i)]=>notify); 
    #       $avp($xavp(trigger[$var(i)]=>type)+"_email") = $xavp(trigger[$var(i)]=>email); 
    #       $var(i) = $var(i) + 1;
    #    }   
    #}

    route(PARSE_STATS_IP);

    #delete old alarms
    sql_query("cb", "DELETE FROM alarm_data WHERE create_date < NOW() - interval '5 day'");
}


route[CHECK_STATS] {

    #SQL STATS
    $var(tm) = ($time(min) mod 10);
    if($var(tm) != 0 && $var(tm) != $sel(cfg_get.stats.min)) return;

    $var(t1) = $TS;
    $var(t2) = $var(t1) - ($sel(cfg_get.stats.min)*60);

    $var(t_date) = "to_timestamp(" + $var(t1) + ")";
    $var(f_date) = "to_timestamp(" + $var(t2) + ")";

    #ALARM SCANNERS
    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;");
    sql_query("cb", "TRUNCATE TABLE alarm_data_mem");

    #STATS Useragent
    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;");
    sql_query("cb", "TRUNCATE TABLE stats_useragent_mem");

    #STATS IP
    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;");
    sql_query("cb", "TRUNCATE TABLE stats_ip_mem");

#!ifdef WITH_HOMER_GEO
    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;");
    sql_query("cb", "TRUNCATE TABLE stats_geo_mem");
#!endif
    
    #INSERT SQL STATS
    #Packet HEP stats
    if($sht(a=>packet::count) != $null && $sht(a=>packet::count) > 0) {
        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))");
        $sht(a=>packet::count) = 0;
    }
    if($sht(a=>packet::size) != $null && $sht(a=>packet::size) > 0) {
        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))");
        $sht(a=>packet::size) = 0;
    }

    #SDF
    if($sht(a=>stats::sdf) != $null && $sht(a=>stats::sdf) > 0) {
        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))");
        $sht(a=>stats::sdf) = 0;
    }

    #ISA
    if($sht(a=>stats::isa) != $null && $sht(a=>stats::isa) > 0) {
        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))");
        $sht(a=>stats::isa) = 0;
    }

    #SD
    if($sht(a=>stats::sd) != $null && $sht(a=>stats::sd) > 0) {
        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))");
        $sht(a=>stats::sd) = 0;
    }

    #SSR
    if($sht(a=>stats::ssr) != $null && $sht(a=>stats::ssr) > 0) {
        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))");
        $sht(a=>stats::ssr) = 0;
    }

    #ASR
    $var(asr) = 0;
    #if($sht(a=>response::200::invite) > 0) {
    if($sht(a=>method::invite) > 0) {
        if($sht(a=>response::407::invite) == $null) $sht(a=>response::407::invite) = 0;
        if($sht(a=>response::200::invite) == $null) $sht(a=>response::200::invite) = 0;
        $var(d) = $sht(a=>method::invite) - $sht(a=>response::407::invite);
        if($var(d) > 0) {
                $var(asr) =  $sht(a=>response::200::invite) * 100 / $var(d);
                if($var(asr) > 100)  $var(asr) = 100;
        }
    }

    #Stats DATA
    sql_query("cb", "INSERT INTO stats_data (from_date, to_date, type, total) VALUES($var(f_date), $var(t_date), 'asr', $var(asr))");


    #NER
    $var(ner) = 0;
    #if($sht(a=>response::200::invite) > 0 || $sht(a=>stats::bad::invite) > 0) {
    if($sht(a=>method::invite) > 0) {

        if($sht(a=>response::200::invite) == $null) $sht(a=>response::200::invite) = 0;
        if($sht(a=>response::bad::invite) == $null) $sht(a=>response::bad::invite) = 0;
        if($sht(a=>response::407::invite) == $null) $sht(a=>response::407::invite) = 0;

        $var(d) = $sht(a=>method::invite) - $sht(a=>response::407::invite);

        if($var(d) > 0) {
                $var(ner) = ($sht(a=>response::200::invite) + $sht(a=>stats::bad::invite)) * 100 / $var(d);
                if($var(ner) > 100)  $var(ner) = 100;
        }
    }

    sql_query("cb", "INSERT INTO stats_data (from_date, to_date, type, total) VALUES($var(f_date), $var(t_date), 'ner', $var(ner))");

    #INVITE
    if($sht(a=>method::reinvite) > 0) {
        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))");
        $sht(a=>method::reinvite) = 0;
    }

    #INVITE
    if($sht(a=>method::invite) > 0) {
        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))");
        $sht(a=>method::invite) = 0;
    }

    #INVITE AUTH
    if($sht(a=>method::invite::auth) > 0) {
        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))");
        $sht(a=>method::invite::auth) = 0;
    }

    #REGISTER
    if($sht(a=>method::register) > 0) {
        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))");
        $sht(a=>method::register) = 0;
    }

    #REGISTER AUTH
    if($sht(a=>method::register::auth) > 0) {
        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))");
        $sht(a=>method::register::auth) = 0;
    }

    #BYE
    if($sht(a=>method::bye) > 0) {
        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))");
        $sht(a=>method::bye) = 0;
    }

    #CANCEL
    if($sht(a=>method::cancel) > 0) {
        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))");
        $sht(a=>method::cancel) = 0;
    }

    #OPTIONS
    if($sht(a=>method::options) > 0) {
        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))");
        $sht(a=>method::options) = 0;
    }

#!ifdef WITH_STATISTIC_METHOD_EXTRA
    #UNKNOWN
    if($sht(a=>method::unknown) > 0) {
        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))");
        $sht(a=>method::unknown) = 0;
    }
    
    #ACK
    if($sht(a=>method::ack) > 0) {
        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))");
        $sht(a=>method::ack) = 0;
    }
#!endif

    #REFER
    if($sht(a=>method::refer) > 0) {
        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))");
        $sht(a=>method::refer) = 0;
    }

    #UPDATE
    if($sht(a=>method::update) > 0) {
        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))");
        $sht(a=>method::update) = 0;
    }

    #RESPONSE

    #300
    if($sht(a=>response::300) > 0) {
        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))");
        $sht(a=>response::300) = 0;
    }

    #407 INVITE
    if($sht(a=>response::407::invite) > 0) {
        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))");
        $sht(a=>response::407::invite) = 0;
    }

    #401 INVITE
    if($sht(a=>response::401::invite) > 0) {
        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))");
        $sht(a=>response::401::invite) = 0;
    }

#!ifdef WITH_STATISTIC_INVITE_1XX
    #100 INVITE
    if($sht(a=>response::100::invite) > 0) {
        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))");
        $sht(a=>response::100::invite) = 0;
    }

    #180 INVITE
    if($sht(a=>response::180::invite) > 0) {
        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))");
        $sht(a=>response::180::invite) = 0;
    }

    #183 INVITE
    if($sht(a=>response::183::invite) > 0) {
        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))");
        $sht(a=>response::183::invite) = 0; 
    }

#!endif

    #200 INVITE
    if($sht(a=>response::200::invite) > 0) {
        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))");
        $sht(a=>response::200::invite) = 0;
    }

    #407 BYE
    if($sht(a=>response::407::bye) > 0) {
        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))");
        $sht(a=>response::407::bye) = 0;
    }

    #401 BYE
    if($sht(a=>response::401::bye) > 0) {
        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))");
        $sht(a=>response::401::bye) = 0;
    }

    #200 BYE
    if($sht(a=>response::200::bye) > 0) {
        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))");
        $sht(a=>response::200::bye) = 0;
    }

    #ALL TRANSACTIONS MESSAGES
    if($sht(a=>method::all) > 0) {
        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))");
        $sht(a=>method::all) = 0;
    }
    
     #ALL MESSAGES ON INTERFACE
    if($sht(a=>method::total) > 0) {
        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))");
        $sht(a=>method::total) = 0;
    }

#!ifdef WITH_HOMER_CUSTOM_STATS
    #Generic stats
    sht_iterator_start("i1", "d");
    while(sht_iterator_next("i1")) {
                $var(key) = $(shtitkey(i1){s.select,2,:});
                sql_query("cb", "INSERT INTO stats_generic (from_date, to_date, type, total) VALUES($var(f_date), $var(t_date), '$var(key)', $shtitval(i1))"); 
    }
    sht_iterator_end("i1");
    sht_rm_name_re("d=>.*");
#!endif

}


route[STORE] {

        if($rm == "REGISTER") {
                $var(table) = "sip_capture_registration";       
        }
        else if($rm =~ "(INVITE|UPDATE|BYE|ACK|PRACK|REFER|CANCEL)$")
        {
                $var(table) = "sip_capture_call";
        } 
        else if($rm =~ "(NOTIFY)$" && is_present_hf("Event") && $hdr(Event)=~"refer;")
        {
                $var(table) = "sip_capture_call";
        }
        else if($rm =~ "(INFO)$")
        {
                $var(table) = "sip_capture_call";
        }
        else if($rm =~ "(OPTIONS)$" )
        {
            $var(table) = "sip_capture_rest";
        }
        else {   
            $var(table) = "sip_capture_rest";
        }

	
#!ifdef KAMAILIO_4_3
	#For old models. Not accurate insert time. System vs capture time.
	#$var(utc) = $timef(%Y%m%d);	
	#as workaround for kamailio < 4.4
	sql_query("cb", "SELECT UTC_DATE()+0;", "ra");
    	if($dbr(ra=>rows)>0) {
              $var(utc) = $dbr(ra=>[0,0]);
        }
        sql_result_free("ra");
#!else
	#for Kamailio >=4.4 please uncomment this parameters:
	# New utimef can fix the timediff issue. But unfortunately only kamailio 4.4
	$var(utc) = $utimef(%Y%m%d);
	# or sipcapture style
	#$var(utc) = "%Y%m%d";
#!endif                        

	$var(a) = $var(table) + "_" + $var(utc);
	sip_capture("$var(a)");
}

event_route[sipcapture:request] {
	xlog("received sipcapture request from $si:$sp\r\n");
}

#!ifdef WITH_HOMER_CUSTOM_STATS
event_route[xhttp:request] {
        set_reply_close();
        set_reply_no_connect();
        xlog("L_WARN", "HTTP request received on $Rp, $hu\n");
        if($hu =~ "/api/v1/stats/push") {
                #Json is our body
                $var(json) = $rb;
                jansson_get("type", $var(json), "$var(n)");
                xlog("L_WARN","Type is $var(n)");
                if($var(n) == "rtp_stat") {
                         $var(i) = 0;
                         $(avp(x)[0]) = 'interval';
                         $(avp(x)[1]) = 'streams';
                         $(avp(x)[2]) = 'packets';
                         $(avp(x)[3]) = 'lost';
                         $(avp(x)[4]) = 'late';
                         $(avp(x)[5]) = 'lost_perc';
                         $(avp(x)[6]) = 'late_perc';
                         $(avp(x)[7]) = 'out_of_seq';
                         $(avp(x)[8]) = 'delay_min';
                         $(avp(x)[9]) = 'delay_max';
                         while(is_avp_set("$(avp(x)[$var(i)])")) {
                                  xlog("L_INFO", "Array value [$var(i)]: $(avp(x)[$var(i)])\n");
                                  jansson_get("$(avp(x)[$var(i)])", $var(json), "$var(d)");
                                  $var(n) = $(var(d){s.int});
                                  if($sht(d=>generic::$(avp(x)[$var(i)])) == $null) $sht(d=>generic::$(avp(x)[$var(i)])) = $var(n);
                                  else $sht(d=>generic::$(avp(x)[$var(i)])) = ($sht(d=>generic::$(avp(x)[$var(i)])) + $var(n))/2;                                  
                                  $var(i) = $var(i) + 1;
                         }
          }
          xhttp_reply("200", "Ok", "done", "");
          exit;
        }

        xhttp_reply("403", "Forbidden", "", "");
        exit;
}
#!endif