serstats 3.5 KB

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