Dialplan state of IAX trunk

I have two Asterisk servers with an IAX trunk between them. They are working fine but I would like to improve some of the methodology when the trunk is down and/or the remote extension is busy.

What can I put in the dialplan which will give me the current state of the trunk. I’ve been looking at a number of things but they either give me nothing useful or in the case of CHANNEL STATUS it gave me a v ery large number.

The other thing is how to find out if the remote extension is busy, before trying to dial, if that is possible and worthwhile.

Any hints would be appreciated.

Busy or not ? -> Use functions GROUP and GROUP_COUNT
Trunk connected/available? -> Use the DEVICE_STATE function