How To Get Number of SIP Peers registered

Apr 24, 2014 at 8: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)
        catch (Exception ex)
            MessageBox.Show("Error: " + ex.Message.ToString());
Apr 24, 2014 at 8: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 9:48 AM
Edited Apr 24, 2014 at 9:49 AM
Okay, how about this:
manager.PeerStatus += new PeerStatusEventHandler(manager_PeerStatus);

void manager_PeerStatus(object sender, PeerStatusEvent e)
At the end I can show the value of count so that number of action fired by PeerStatus event it gets increamented.

Apr 24, 2014 at 11:52 AM

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 12:08 PM
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 12:14 PM

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 11: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 12:23 PM
When I issue command "sip show peers" as
CommandAction ca = new CommandAction("sip show peers");
            CommandResponse cr = new CommandResponse();
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>
Apr 27, 2014 at 9: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 3:58 PM
OKay let me check about errors I'm using verbose level 3. What level should I try to debug?
Apr 30, 2014 at 8: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 6:55 AM
Following are the permissions,

secret = *****
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.
May 2, 2014 at 12:25 PM
And nothing in the asterisk console?
May 2, 2014 at 12:28 PM
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);

        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]);