How To Get Number of SIP Peers registered

Apr 24, 2014 at 7:42 AM
Please help in getting the number of SIP peers registered to the server. Following is the code but it returns nothing.

manager.PeerlistComplete += new PeerlistCompleteEventHandler(manager_PeerlistComplete);

void manager_PeerlistComplete(object sender, PeerlistCompleteEvent e)
    {
        try
        {
            MessageBox.Show(Convert.ToString(e.ListItems));
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error: " + ex.Message.ToString());
        }
    }
Coordinator
Apr 24, 2014 at 7:48 AM
PeerListComplete doesn't give you the number, it gets fired at the end of number of PeerStatus events.

Each PeerStatus event informs you about a single peer visible to Asterisk. The state of the peer etc. You need to use those events to build up an in-memory list of the peers, which you can then count.

AMI and AsterNET depend heavily on you being able to build your own representation of Asterisk's internal state.
Apr 24, 2014 at 8:48 AM
Edited Apr 24, 2014 at 8:49 AM
Okay, how about this:
manager.PeerStatus += new PeerStatusEventHandler(manager_PeerStatus);

void manager_PeerStatus(object sender, PeerStatusEvent e)
        {
            MessageBox.Show(e.Peer.ToString());
            count++;
        }
At the end I can show the value of count so that number of action fired by PeerStatus event it gets increamented.

Right?
Coordinator
Apr 24, 2014 at 10:52 AM
Yes!

I often find it's better to have some sort of in memory model, a "List<MyPeer>" if you will, and update the list when you see a PeerStatus event. That way you can always know how many peers, but also how many peer that are registered etc.
Apr 24, 2014 at 11:08 AM
u could use the command action and send "sip show peers"
in the results array, the last line contains a summary of peers. (total, online, etc)
Apr 24, 2014 at 11:14 AM
skrusty,

Can you please give me some hint or some part of code that I can implement as I'm not well versed in C #

Thanks in Anticipation
Apr 25, 2014 at 10:40 AM
garysprice wrote:
u could use the command action and send "sip show peers"
in the results array, the last line contains a summary of peers. (total, online, etc)
through originate Action command ?
Apr 25, 2014 at 11:23 AM
When I issue command "sip show peers" as
CommandAction ca = new CommandAction("sip show peers");
            CommandResponse cr = new CommandResponse();
            MessageBox.Show(Convert.ToString(cr.Result));
It is showing null :/ I'm Stuck :(
  • cr {CommandResponse {DateReceived:4:18:39 PM}} AsterNET.Manager.Response.CommandResponse
  • base {CommandResponse {DateReceived:4:18:39 PM}} AsterNET.Manager.Response.ManagerResponse {AsterNET.Manager.Response.CommandResponse}
  • Result null System.Collections.Generic.List<string>
  • result null System.Collections.Generic.List<string>
Coordinator
Apr 27, 2014 at 8:05 PM
What permissions does your manager account have? Also, what's the output in the Asterisk console, often errors will be output there if the verbose level is set correctly.
Apr 29, 2014 at 2:58 PM
OKay let me check about errors I'm using verbose level 3. What level should I try to debug?
Coordinator
Apr 30, 2014 at 7:25 AM
As high as you can tolerate :) Check the manager account permissions first! You may simply not have read/write access to the functionality you want.
May 2, 2014 at 5:55 AM
Following are the permissions,

[admin]
server=localhost
secret = *****
deny=0.0.0.0/0.0.0.0
permit=127.0.0.1/255.255.255.0
permit=172.16.0.30/255.255.255.0
read = system,call,log,verbose,command,agent,user,config,command,dtmf,reporting,cdr,dialplan,originate
write = system,call,log,verbose,command,agent,user,config,command,dtmf,reporting,cdr,dialplan,originate

Read and Write both access available.
Coordinator
May 2, 2014 at 11:25 AM
And nothing in the asterisk console?
May 2, 2014 at 11:28 AM
Somehow I managed to get the desired results. Thanks to you both skrusty and garysprice :)

Following is the working code:
    public void Run()
    {
        CommandAction ca;
        CommandResponse cr;
        //List<String> list = new List<String> [];

        ca = new CommandAction();
        ca.Command = "sip show peers";
        cr = (CommandResponse)manager.SendAction(ca);

        MessageBox.Show(Convert.ToString(cr.Result[6]));
        label14.Text = Convert.ToString(cr.Result[6]);

        //while (manager.IsConnected())
        //{
            ca.Command = "core show channels";
            cr = (CommandResponse)manager.SendAction(ca);
            label16.Text = Convert.ToString(cr.Result[0] + "\n" + cr.Result[1] + "\n" + cr.Result[2]);
        //}
    }