#!/bin/bash
#
# very crudely written script to show the top -20 unreachable phones.
#

INFILE=/var/log/asterisk/messages

if [ $# -gt 0 ]
then
  INFILE=$1
  shift
fi

fgrep UNREACHABLE $INFILE | awk '{ print $7; }' | cut -f2 "-d'" | sort | uniq -c | sort -r -n | head -20 | \
while read CNT NAME
do
    asterisk -rnx 'sip show peers' | grep $NAME/ > /tmp/ssp.$$
#example output
#sc7int207/sc7int207        67.215.23.186    D          2048     OK (84 ms)         "Steve - SNOM Murphy" <>
    IP=`cat /tmp/ssp.$$ | awk '{ print $2; }'`
    REALNAME=`cat /tmp/ssp.$$ | cut -c80-200`
    printf "%5d %-20.20s %-15.15s %s\n" $CNT $NAME $IP "$REALNAME"
done

