#!/usr/bin/perl

use IO::Socket::UNIX;
use Time::HiRes qw(time);
$sock = new IO::Socket::UNIX "/var/run/asterisk/asterisk.ctl" or die;

$greeting = <$sock>;
$sock->autoflush(1);

$| = 1;

for (;;) {
	$start = time;
	print $sock "sip show channels\0";
	$buf = '';
	while (<$sock>) {
		$buf .= $_;
		last if /active SIP dialog/;
	}
	$end = time;
	$delta = $end - $start;
	printf "%dms ", $delta / 1000;

	if ($buf =~ /deadlock/i) {
		print "\n";
		print $buf;
		sleep 1;
	}
}
