Julien Chavanton shared via Kamailio sr-users mailing list a Python script that helps monitoring dispatcher latency stats with statsd.

Latency stats for dispatcher were added by Julien during the development cycle of Kamailio 5.1 (this is the next major release to be out in about one week).

The sample script is attached, it will post data to statsd and is working in cron

The feature can be enabled :

You have to enable the latency stats in dispatcher via the module parameter ds_ping_latency_stats:

This is one use case of this feature, another one will be to load balance based on congestion detected on latency, a follow up on using it is planned for the future.

The Python script is:

#!/usr/bin/python
import subprocess
from time import sleep
import time

# URI: sip:10.10.1.1:5060
#0 FLAGS: AP
#1 PRIORITY: 12
#2 LATENCY: {
#3      AVG: 30.529000
#4      STD: 4.849000
#5      EST: 30.999000
#6      MAX: 1270
#7      TIMEOUT: 0

def server_stats(ip):
   idx = -1
   for line in dlist.splitlines():
      if idx == 5:
         est_latency = line.strip()
      if idx == 7:
         timeout = line.strip()
      if idx >= 0:
         idx = idx + 1
      if line.find("URI: sip:{}".format(ip)) > 0:
         idx = 0

   if timeout[:9] == "TIMEOUT: ":
      timeout_count = timeout[9:]
      cmd = "echo \"fastlink.timeout.{}:{}|g\" | nc -w 1 -u 127.0.0.1 8125".format(ip, timeout_count)
      subprocess.call(cmd, shell=True)
      print(cmd)
   if est_latency[:5] == "EST: ":
      latency = est_latency[5:]
      cmd = "echo \"fastlink.latency.{}:{}|g\" | nc -w 1 -u 127.0.0.1 8125".format(ip, latency)
      subprocess.call(cmd, shell=True)
      print("est latency:{}".format(latency))

servers = ["10.40.5.175", "10.40.2.233", "10.40.1.103"]
interval = 10
it = 0
while it < 6:
# while 1:
   ts = time.time()
   print("[{}]now:{}".format(it,ts))
   next_ts = ts + 10
   it = it + 1
   dlist = subprocess.check_output(["/usr/bin/docker","exec","kamailio","kamcmd","dispatcher.list"])
   for ip in servers:
      print(ip)
      server_stats(ip)
   ts = time.time()
   print("sleeping:{}".format(next_ts - ts))
   wait_ts = next_ts - ts;
   if wait_ts < 0:
      wait_ts = 7
   sleep(wait_ts)

You can see the message on the mailing list as well as the attached Python script at:

Thanks for flying Kamailio!