kamctl.rpcfifo 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. #
  2. #
  3. # control tool for maintaining Kamailio
  4. #
  5. #===================================================================
  6. ##### ----------------------------------------------- #####
  7. ### RPC FIFO specific variables and functions
  8. #
  9. ##### ----------------------------------------------- #####
  10. ### load CTL base
  11. #
  12. if [ -f "$MYLIBDIR/kamctl.ctlbase" ]; then
  13. . "$MYLIBDIR/kamctl.ctlbase"
  14. else
  15. mwarn "Cannot load CTL core functions '$MYLIBDIR/kamctl.ctlbase' ..."
  16. # exit -1
  17. fi
  18. #
  19. ##### ----------------------------------------------- #####
  20. ### parameters
  21. #
  22. if [ -z "$RPCFIFOPATH" ]; then
  23. RPCFIFOPATH=/var/run/kamailio/kamailio_rpc.fifo
  24. fi
  25. #
  26. ##### ----------------------------------------------- #####
  27. ### functions
  28. #
  29. usage_rpc() {
  30. echo
  31. mecho " -- command 'rpc' - send raw RPC commands"
  32. echo
  33. cat <<EOF
  34. rpc ................................ send raw RPC command
  35. EOF
  36. }
  37. USAGE_FUNCTIONS="$USAGE_FUNCTIONS usage_rpc"
  38. isnum() {
  39. printf "%f" $1 >/dev/null 2>&1
  40. }
  41. rpcparamval() {
  42. RPCVAL="${1}"
  43. case "${1}" in
  44. s:*)
  45. prefix="s:"
  46. RPCVAL="\"${1#${prefix}}\""
  47. ;;
  48. i:*)
  49. prefix="i:"
  50. RPCVAL="${1#${prefix}}"
  51. ;;
  52. *)
  53. if isnum ${1} ; then
  54. RPCVAL="${1}"
  55. else
  56. RPCVAL="\"${1}\""
  57. fi
  58. ;;
  59. esac
  60. }
  61. rpc_cmd()
  62. {
  63. mdbg "entering rpc_cmd $*"
  64. if [ "$#" -lt 1 ]; then
  65. merr "rpc_cmd must take at least command name as parameter"
  66. exit 1
  67. fi
  68. name=kamailio_receiver_$$
  69. path=$CHROOT_DIR/tmp/$name
  70. # delete existing fifo file with same name
  71. if test -p $path; then
  72. rm -f $path
  73. fi
  74. if [ ! -w $RPCFIFOPATH ]; then
  75. merr "Error opening Kamailio's FIFO $RPCFIFOPATH"
  76. merr "Make sure you have loaded the jsonrpcs module and set FIFO transport parameters"
  77. if [ ! -z $CHROOT_DIR ]; then
  78. merr "[chrooted environment] Check that $RPCFIFOPATH is symlinked to ${CHROOT_DIR}${RPCFIFOPATH}"
  79. fi
  80. exit 2
  81. fi
  82. if ! test -p $path; then
  83. mkfifo $path
  84. if [ $? -ne 0 ] ; then
  85. merr "error opening read fifo $path"
  86. exit 3
  87. fi
  88. chmod a+w $path
  89. fi
  90. # construct the command now
  91. CMD="{\"jsonrpc\": \"2.0\", \"method\": \"$1\"";
  92. shift
  93. RPCPARAMS="no"
  94. if [ -n "$1" ] ; then
  95. rpcparamval "${1}"
  96. CMD="${CMD}, \"params\": [${RPCVAL}"
  97. RPCPARAMS="yes"
  98. fi
  99. if [ "$RPCPARAMS" = "yes" ]; then
  100. shift
  101. while [ -n "$1" ] ; do
  102. rpcparamval "${1}"
  103. CMD="${CMD}, ${RPCVAL}"
  104. shift
  105. done
  106. CMD="${CMD}]"
  107. fi
  108. CMD="${CMD}, \"reply_name\": \"${name}\", \"id\": $$}\n"
  109. trap "rm -f $path; kill 0" 2
  110. # start reader now so that it is ready for replies
  111. # immediately after a request was sent out
  112. cat < $path | filter_json | $EXPAND &
  113. # issue FIFO request (printf taken to deal with \n)
  114. printf "$CMD" > $RPCFIFOPATH
  115. # wait for the reader to complete
  116. wait
  117. rm $path
  118. mdbg "FIFO command was:\n$CMD"
  119. }
  120. CTLCMD=rpc_cmd
  121. rpc_cmd_print()
  122. {
  123. name=kamailio_receiver_$$
  124. # construct the command now
  125. CMD="{\"jsonrpc\": \"2.0\", \"method\": \"$1\"";
  126. shift
  127. RPCPARAMS="no"
  128. if [ -n "$1" ] ; then
  129. rpcparamval "${1}"
  130. CMD="${CMD}, \"params\": [${RPCVAL}"
  131. RPCPARAMS="yes"
  132. shift
  133. fi
  134. if [ "$RPCPARAMS" = "yes" ]; then
  135. while [ -n "$1" ] ; do
  136. rpcparamval "${1}"
  137. CMD="${CMD}, ${RPCVAL}"
  138. shift
  139. done
  140. CMD="${CMD}]"
  141. fi
  142. CMD="${CMD}, \"reply_name\": \"${name}\", \"id\": $$}\n"
  143. minfo "The command is:\n"
  144. mecho "$CMD"
  145. }
  146. CTLCMDPRINT=rpc_cmd_print
  147. rpc_kamailio_monitor() {
  148. name=kamailio_receiver_$$
  149. path=$CHROOT_DIR/tmp/$name
  150. # delete existing fifo file with same name
  151. if test -p $path; then
  152. rm -f $path
  153. fi
  154. if [ ! -w $RPCFIFOPATH ]; then
  155. merr "Error opening Kamailio's FIFO $RPCFIFOPATH"
  156. merr "Make sure you have loaded the jsonrpcs module and set FIFO transport parameters"
  157. exit 1
  158. fi
  159. if ! test -p $path; then
  160. mkfifo $path
  161. if [ $? -ne 0 ] ; then
  162. merr "monitor - error opening read fifo $path"
  163. exit 1
  164. fi
  165. chmod a+w $path
  166. fi
  167. trap "rm $path; clear; echo monitor ^C-ed; exit 1" 2
  168. attempt=0
  169. if [ "$2" = "" ]; then
  170. loops=-1;
  171. else
  172. loops=$2;
  173. fi
  174. clear
  175. while [ $loops -ne $attempt ] ; do
  176. attempt=`$EXPR $attempt + 1`
  177. #clear
  178. tput clear
  179. # print_stats $name $path $attempt
  180. mecho "[cycle #: $attempt; if constant make sure server lives]"
  181. mecho "Kamailio Runtime Details: "
  182. cat < $path | filter_json | $EXPAND | grep "result" | awk -F'"' '{ print $4 }' &
  183. cat > $RPCFIFOPATH <<EOF
  184. {
  185. "jsonrpc": "2.0",
  186. "method": "core.version",
  187. "reply_name": "${name}",
  188. "id": $$
  189. }
  190. EOF
  191. wait
  192. cat < $path | filter_json | $EXPAND | egrep "now|up_since|uptime" | sed 's/[",]//g' &
  193. cat > $RPCFIFOPATH << EOF
  194. {
  195. "jsonrpc": "2.0",
  196. "method": "core.uptime",
  197. "reply_name": "${name}",
  198. "id": $$
  199. }
  200. EOF
  201. wait
  202. echo
  203. mecho "Transaction Statistics: "
  204. cat < $path | filter_json | $EXPAND | grep "=" | sed 's/[",]//g' | column &
  205. cat > $RPCFIFOPATH <<EOF
  206. {
  207. "jsonrpc": "2.0",
  208. "method": "stats.get_statistics",
  209. "params": [ "UAS_transactions", "UAC_transactions", "active_transactions", "inuse_transactions"],
  210. "reply_name": "${name}",
  211. "id": $$
  212. }
  213. EOF
  214. wait
  215. echo
  216. mecho "Stateless Server Statistics: "
  217. cat < $path | filter_json | $EXPAND | grep "=" | sed 's/[",]//g' | column &
  218. cat > $RPCFIFOPATH <<EOF
  219. {
  220. "jsonrpc": "2.0",
  221. "method": "stats.get_statistics",
  222. "params": [ "sent_replies", "sent_err_replies"],
  223. "reply_name": "${name}",
  224. "id": $$
  225. }
  226. EOF
  227. wait
  228. echo
  229. mecho "UsrLoc Statistics: "
  230. cat < $path | filter_json | $EXPAND | grep "=" | sed 's/[",]//g' | column &
  231. cat > $RPCFIFOPATH <<EOF
  232. {
  233. "jsonrpc": "2.0",
  234. "method": "stats.get_statistics",
  235. "params": [ "usrloc:"],
  236. "reply_name": "${name}",
  237. "id": $$
  238. }
  239. EOF
  240. wait
  241. echo
  242. mecho "Core Statistics: "
  243. cat < $path | filter_json | $EXPAND | grep "=" | sed 's/[",]//g' | column &
  244. cat > $RPCFIFOPATH <<EOF
  245. {
  246. "jsonrpc": "2.0",
  247. "method": "stats.get_statistics",
  248. "params": [ "rcv_requests", "fwd_requests", "rcv_replies", "fwd_replies"],
  249. "reply_name": "${name}",
  250. "id": $$
  251. }
  252. EOF
  253. wait
  254. echo
  255. mecho "Shared Memory Statistics: "
  256. cat < $path | filter_json | $EXPAND | grep "=" | sed 's/[",]//g' | column &
  257. cat > $RPCFIFOPATH <<EOF
  258. {
  259. "jsonrpc": "2.0",
  260. "method": "stats.get_statistics",
  261. "params": [ "shmem:"],
  262. "reply_name": "${name}",
  263. "id": $$
  264. }
  265. EOF
  266. wait
  267. if [ $loops -ne $attempt ] ; then
  268. sleep $WATCH_PERIOD
  269. fi
  270. done
  271. rm $path
  272. exit 0
  273. }
  274. KAMAILIO_MONITOR=rpc_kamailio_monitor