| 1 |
#!/bin/sh |
| 2 |
# |
| 3 |
# examples for gaining some interesting values from SIP DB |
| 4 |
# |
| 5 |
|
| 6 |
SERUN=ser |
| 7 |
SERDB=ser |
| 8 |
DBHOST=localhost |
| 9 |
|
| 10 |
# ------------ |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
usage() { |
| 15 |
COMMAND=`basename $0` |
| 16 |
cat <<EOF |
| 17 |
usage: $COMMAND logged_cnt # number of logged-in users |
| 18 |
$COMMAND natted_cnt # number of natted users |
| 19 |
$COMMAND contact_cnt # number of registered contacts |
| 20 |
$COMMAND calls_lh # number of calls in last hour |
| 21 |
$COMMAND calls_cnt # number of calls |
| 22 |
$COMMAND subs_1d # new subscribers in last day |
| 23 |
$COMMAND subs_cnt # number of subscriber |
| 24 |
$COMMAND minutes_cnt # number of minutes |
| 25 |
$COMMAND minutes_1d # number of minutes in last days |
| 26 |
$COMMAND top_calls [dst] [ago] # longest calls |
| 27 |
$COMMAND top_callers [dst] [ago] # most active callers |
| 28 |
|
| 29 |
EOF |
| 30 |
} #usage |
| 31 |
|
| 32 |
query() { |
| 33 |
mysql -h $DBHOST -u$SERUN -p -e "$1" $SERDB |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
case $1 in |
| 38 |
|
| 39 |
logged_cnt) |
| 40 |
query "select count(distinct username,domain) from location;" |
| 41 |
;; |
| 42 |
natted_cnt) |
| 43 |
query "select count(distinct username,domain) from location |
| 44 |
where flags>0;" |
| 45 |
;; |
| 46 |
contact_cnt) |
| 47 |
query "select count(*) from location;" |
| 48 |
;; |
| 49 |
calls_lh) |
| 50 |
query "select count(*) from acc where sip_method='INVITE' and |
| 51 |
sip_status='200' and |
| 52 |
(DATE_SUB(CURDATE(), INTERVAL 1 hour) <= timestamp);" |
| 53 |
;; |
| 54 |
calls_cnt) |
| 55 |
query "select count(*) from acc where sip_method='INVITE' |
| 56 |
and sip_status='200';" |
| 57 |
;; |
| 58 |
subs_1d) |
| 59 |
query "select count(*) from subscriber where |
| 60 |
(DATE_SUB(CURDATE(), INTERVAL 1 day) <= datetime_created);" |
| 61 |
;; |
| 62 |
subs_cnt) |
| 63 |
query "select count(*) from subscriber;" |
| 64 |
;; |
| 65 |
minutes_cnt) |
| 66 |
query "select sum(unix_timestamp(t2.time)-unix_timestamp(t1.time))/60 |
| 67 |
as length from acc t1, acc t2 |
| 68 |
where t1.sip_method='INVITE' and t1.sip_status='200' |
| 69 |
and t2.sip_method='BYE' and t1.sip_callid=t2.sip_callid |
| 70 |
and ((t1.totag=t2.totag and t1.fromtag=t2.fromtag) |
| 71 |
or (t1.totag=t2.fromtag and t1.fromtag=t2.totag));" |
| 72 |
;; |
| 73 |
minutes_1d) |
| 74 |
query "select |
| 75 |
sum(unix_timestamp(t2.time)-unix_timestamp(t1.time))/60 as length |
| 76 |
from acc t1, acc t2 |
| 77 |
where t1.sip_method='INVITE' and t1.sip_status='200' |
| 78 |
and (DATE_SUB(CURDATE(), INTERVAL 1 hour) <= t1.timestamp) |
| 79 |
and t2.sip_method='BYE' and t1.sip_callid=t2.sip_callid |
| 80 |
and ((t1.totag=t2.totag and t1.fromtag=t2.fromtag) |
| 81 |
or (t1.totag=t2.fromtag and t1.fromtag=t2.totag));" |
| 82 |
;; |
| 83 |
top_calls) |
| 84 |
if [ -n "$2" ] ; then |
| 85 |
LIKE="and t1.i_uri like '%$2%'" |
| 86 |
fi |
| 87 |
if [ -n "$3" ] ; then |
| 88 |
AGO="and (DATE_SUB(CURDATE(), INTERVAL $3 day) <= t1.timestamp)" |
| 89 |
fi |
| 90 |
query "select t1.time, |
| 91 |
((unix_timestamp(t2.time)-unix_timestamp(t1.time))/60) as min, |
| 92 |
t1.username, t1.domain, t1.i_uri |
| 93 |
from acc t1, acc t2 |
| 94 |
where t1.sip_method='INVITE' and t1.sip_status='200' |
| 95 |
and t2.sip_method='BYE' and t1.sip_callid=t2.sip_callid |
| 96 |
and ((t1.totag=t2.totag and t1.fromtag=t2.fromtag) |
| 97 |
or (t1.totag=t2.fromtag and t1.fromtag=t2.totag)) |
| 98 |
$LIKE |
| 99 |
$AGO |
| 100 |
ORDER by min desc limit 20;" |
| 101 |
;; |
| 102 |
top_callers) |
| 103 |
if [ -n "$2" ] ; then |
| 104 |
LIKE="and t1.i_uri like '%$2%'" |
| 105 |
fi |
| 106 |
if [ -n "$3" ] ; then |
| 107 |
AGO="and (DATE_SUB(CURDATE(), INTERVAL $3 day) <= t1.timestamp)" |
| 108 |
fi |
| 109 |
query "select |
| 110 |
sum((unix_timestamp(t2.time)-unix_timestamp(t1.time))/60) as min, |
| 111 |
t1.username, t1.domain |
| 112 |
from acc t1, acc t2 |
| 113 |
where t1.sip_method='INVITE' and t1.sip_status='200' |
| 114 |
and t2.sip_method='BYE' and t1.sip_callid=t2.sip_callid |
| 115 |
and ((t1.totag=t2.totag and t1.fromtag=t2.fromtag) |
| 116 |
or (t1.totag=t2.fromtag and t1.fromtag=t2.totag)) |
| 117 |
$LIKE |
| 118 |
$AGO |
| 119 |
GROUP by t1.username,t1.domain |
| 120 |
ORDER by min desc limit 20;" |
| 121 |
|
| 122 |
;; |
| 123 |
*) |
| 124 |
usage |
| 125 |
exit 1 |
| 126 |
;; |
| 127 |
|
| 128 |
esac |
| 129 |
|
| 130 |
|
| 131 |
|