Hi Stefan, following your advise, I created a python script to use statsd with 
SEMS DSM script. Here's the code in case anybody finds it useful: 

1) Install Python Statsd Module:
http://statsd.readthedocs.org/en/v3.1/

2) Set up xmlrpclib.conf, dsm.conf, sems.conf to export the monitoring values 
from the DSM script into the python script

xmlrpclib.conf:

export_di=yes
direct_export=dsm;monitoring

dsm.conf

conf_dir=/usr/local/etc/sems/dsm/
monitoring_full_stategraph=yes
monitoring_full_transitions=yes

sems.conf

load_plugins=wav;announcement;dsm;monitoring;xmlrpc2di

3) Create ENVIRONMENT variables in your bashrc profile so that you don't have 
to define them on the script

vi ~/.bashrc

# STATSD Server
export STATSD_HOST=127.0.0.1
# STATSD Port
export STATSD_PORT=8125
# STATSD Prefix (it is added to the metric name)
export STATSD_PREFIX=SEMS_Server
# STATSD Max UDP packet size
export STATSD_MAXUDPSIZE=512

3) Script code
#!/usr/bin/python 
from xmlrpclib import * 
from statsd.defaults.env import statsd 
import time 

s = ServerProxy("http://127.0.0.1:8090";) 

while 1: 
     inboundCalls = s.listActive() 
     for cid in inboundCalls: 
          aux = s.get(cid) 
          statsd.set('inboundCalls',cid) 
     time.sleep(1) 

This would send the statsd server a set with the Call-ID's active. 

4) You can also define custom variables with the monitoring module on DSM, for 
example:

-------- DSM script (xyz.dsm) -------------
import(mod_monitoring);

initial state START;

transition "recibimos un INVITE" START - invite -> RUN_INVITE;

state RUN_INVITE
enter {
        monitoring.set(active_call,"1");
        monitoring.set(dest,"none");
        monitoring.set(response,"none");
};

[...]
-------- DSM script (xyz.dsm) -------------

5) Then on the python script you can access this variables like this

#!/usr/bin/python
from xmlrpclib import *
from statsd.defaults.env import statsd
import time
s = ServerProxy("http://127.0.0.1:8090";)
while 1:
        ## Get all the Call-IDs that match active_call=1
        inboundCalls = s.listByFilter(['active_call','1'])
        for cid in inboundCalls:
                ## Save all the monitoring exported variables for each Call-ID
                aux = s.get(cid)
                
                ## Save the 'response' variable values for all the call ids 
listed
                res = aux[0]['response']
                
                ## Save the 'dest' variable values for all the call ids listed
                dest = aux[0]['dest']
                
                ## Send out a statsd set named 'inboundCalls' and value=Call-ID
                ## StatsD will then check for unique values within its 
collecting window and save the count
                statsd.set('inboundCalls',cid)
                if res!='none':
                        ## Send out a StatsD set with name=connectedCalls.X.Y 
where X is the destination number and Y is the SIP Response (200_OK, 
404_Not_Found, etc).
                        ## To populate these values you do it within the DSM 
script, and the monitoring module exports it so you can access them with Python
                        statsd.set('connectedCalls.%s.%s' % (dest,res),cid)
        time.sleep(1)

6) This is an example UDP message that you get when using this script (I'm 
using kamailio-ers1 as the StatsD Prefix):

14:14:43.143192 IP 172.30.4.133.36778 > 172.30.24.20.8125: UDP, length 63
        0x0000:  4500 005b ed5c 4000 4011 d85f ac1e 0485  E..[.\@.@.._....
        0x0010:  ac1e 1814 8faa 1fbd 0047 752e 6b61 6d61  .........Gu.kama
        0x0020:  696c 696f 2d65 7273 312e 696e 626f 756e  ilio-ers1.inboun
        0x0030:  6443 616c 6c73 3a37 4638 4345 4433 422d  dCalls:7F8CED3B-
        0x0040:  3535 4136 3934 4639 3030 3037 3234 3046  55A694F90007240F
        0x0050:  2d32 4541 3242 3730 307c 73              -2EA2B700|s
14:14:43.143292 IP 172.30.4.133.36778 > 172.30.24.20.8125: UDP, length 84
        0x0000:  4500 0070 ed5d 4000 4011 d849 ac1e 0485  E..p.]@[email protected]....
        0x0010:  ac1e 1814 8faa 1fbd 005c 7543 6b61 6d61  .........\uCkama
        0x0020:  696c 696f 2d65 7273 312e 636f 6e6e 6563  ilio-ers1.connec
        0x0030:  7465 6443 616c 6c73 2e30 3830 3033 3333  tedCalls.0800333
        0x0040:  3132 3030 2e32 3030 5f4f 4b3a 3746 3843  1200.200_OK:7F8C
        0x0050:  4544 3342 2d35 3541 3639 3446 3930 3030  ED3B-55A694F9000
        0x0060:  3732 3430 462d 3245 4132 4237 3030 7c73  7240F-2EA2B700|s

6) You can also use the counter, timers or gauges from statsd with the 
variables you want (check statsd.readthedocs.org for syntax and use cases). 

Hope you find it useful, any doubts let me know.

Best regards,

Federico

----- Original Message ----- 
From: "Stefan Sayer" <[email protected]> 
To: "Federico San Martín" <[email protected]>, [email protected] 
Sent: Monday, July 6, 2015 4:42:08 PM 
Subject: Re: [Sems] sems-stats usage 

Hi Federico, 

Federico San Martín wrote on 07/06/2015 08:58 PM: 
> Hi all, I have installed a couple of services using SEMS and would 
> like to get some statistics from them. I noticed there is a tool 
> called "sems-stats" built-in but I can't figure how to use it, and for 
> example, on the help it says that if I send the command "version" it 
> should output the sems version but all I get is this: 
> 
> root@KAMAILIO-ERS1:/usr/local/sbin# sems-stats -c "version" 
> sending 'version\n' to 127.0.0.1:5040 
> read timeout! 
> 
> 
> root@KAMAILIO-ERS1:/usr/local/sbin# sems-stats 
> sending 'calls\n' to 127.0.0.1:5040 
> read timeout! 
> 
> 
> Can anybody help me with an example or something that can explain its 
> use? Or even better, can you figure out a way to use StatsD within a 
> DSM script? That would be awesome, since I already have a 
> Graphite/Grafana server that accepts StatsD messages... 

if you absolutely don't want to add C++ code I guess from DSM you 
could use sys.popen() from mod_sys and shell out to nc, or use mod_py 
and a little python script (e.g. with pystatsd). You can use 
mod_monitoring to get more information about running calls which are 
collected by the monitoring module. 

Best Regards 
Stefan
_______________________________________________
Sems mailing list
[email protected]
http://lists.iptel.org/mailman/listinfo/sems

Reply via email to