Sip friend host question


Hi everyone,

I’m new to asterisk, and I’m looking for an answer to the following question:

Let’s say I set the host=dynamic option to a sip friend. How can I save to a database (mysql) the ip he last registered from?

If I enter the “sip show peers” command in the CLI, I get all the info I need. So I’m assuming the asterisk keeps this info
in the memory (quite evident I guess). If he does it is just a problem of updateing the corresponding values (in the
database) every time these values change.

Is this option available somehow?

Any comments, links, docs regarding my problem are greatly apreciated.

Gaspar Zoltan


You may be able to accomplish something like this using the Manager API, periodically sending “sip show peers” or something like that.

I don’t know if there’s an exposed event hook, but I’ve read in the code that res_odbc and res_mysql do provide more-or-less exactly what you’re asking for; when a peer (re)registers via sip, realtime_update_peer() calls into ast_update_realtime(), which in turn (if configured) calls into update_odbc() or update_mysql(). One of the fields included in this update is the client IP address. You could set an update trigger (does MySQL have triggers?) on the table (probably a bad idea, because it might hang the PBX), or you could periodically poll that table via an external app.

Hope that helps.


Polling the “sip show peers” using Manager API, or by any other way, isn’t an acceptable solution, because it uses a lot of resource and has a delay of polling period in the worst case.

I need this thing because I have to set up a server loadbalance system. In this case, the server, on witch the sip friend has registers (host= dynamic), doesn’t has to be the server that receives the call to this friend.

The answer should be to set up a external database server (Mysql let’s say) and all the asterisk servers to take/save/update all there data from here.

Realtime gives me this facility to configure friends, voicemail, extensions and so on. There is cdr_mysql to keep the cdr’s.

So the only problem I have is to keep the registration (host=dynamic for sip friend) info in this external database.

I think some of you solved the loadbalanceing issue, and it would be nice, to help me (and quite possibly others) get on the right track.