Debian Codegraph @

utils/kamctl/kamctl.rpcfifo

raw
1 #
2 #
3 # control tool for maintaining Kamailio
4 #
5 # This file is part of Kamailio, a free SIP server.
6 #
7 # SPDX-License-Identifier: GPL-2.0-or-later
8 #
9 # Kamailio is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version
13 #
14 # Kamailio is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the Free Software
21 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #
23 #===================================================================
24
25 ##### ----------------------------------------------- #####
26 ### RPC FIFO specific variables and functions
27 #
28
29 ##### ----------------------------------------------- #####
30 ### load CTL base
31 #
32 if [ -f "$MYLIBDIR/kamctl.ctlbase" ]; then
33 . "$MYLIBDIR/kamctl.ctlbase"
34 else
35 mwarn "Cannot load CTL core functions '$MYLIBDIR/kamctl.ctlbase' ..."
36 # exit -1
37 fi
38
39 #
40 ##### ----------------------------------------------- #####
41 ### parameters
42 #
43 if [ -z "$RPCFIFOPATH" ]; then
44 if [ -p "/var/run/kamailio/kamailio_rpc.fifo" ]; then
45 RPCFIFOPATH=/var/run/kamailio/kamailio_rpc.fifo
46 else
47 RPCFIFOPATH=/run/kamailio/kamailio_rpc.fifo
48 fi
49 fi
50
51 #
52 ##### ----------------------------------------------- #####
53 ### functions
54 #
55 usage_rpc() {
56 echo
57 mecho " -- command 'rpc' - send raw RPC commands"
58 echo
59 cat <<EOF
60 rpc ................................ send raw RPC command
61 EOF
62 }
63 USAGE_FUNCTIONS="$USAGE_FUNCTIONS usage_rpc"
64
65 isnum() {
66 printf "%f" $1 >/dev/null 2>&1
67 }
68
69 rpcparamval() {
70 RPCVAL="${1}"
71
72 case "${1}" in
73 s:*)
74 prefix="s:"
75 RPCVAL="\"${1#${prefix}}\""
76 ;;
77 i:*)
78 prefix="i:"
79 RPCVAL="${1#${prefix}}"
80 ;;
81 *)
82 if isnum ${1} ; then
83 RPCVAL="${1}"
84 else
85 RPCVAL="\"${1}\""
86 fi
87 ;;
88 esac
89 }
90
91 rpc_cmd()
92 {
93 mdbg "entering rpc_cmd $*"
94
95 if [ "$#" -lt 1 ]; then
96 merr "rpc_cmd must take at least command name as parameter"
97 exit 1
98 fi
99 name=kamailio_receiver_$$
100 path=$CHROOT_DIR/tmp/$name
101 # delete existing fifo file with same name
102 if test -p $path; then
103 rm -f $path
104 fi
105 if [ ! -w $RPCFIFOPATH ]; then
106 merr "Error opening Kamailio's FIFO $RPCFIFOPATH"
107 merr "Make sure you have loaded the jsonrpcs module and set FIFO transport parameters"
108 if [ ! -z $CHROOT_DIR ]; then
109 merr "[chrooted environment] Check that $RPCFIFOPATH is symlinked to ${CHROOT_DIR}${RPCFIFOPATH}"
110 fi
111 exit 2
112 fi
113 if ! test -p $path; then
114 mkfifo $path
115 if [ $? -ne 0 ] ; then
116 merr "error opening read fifo $path"
117 exit 3
118 fi
119 chmod a+w $path
120 fi
121
122 PARAMEVAL="no"
123 if [ "$1" = "=" ]; then
124 PARAMEVAL="yes"
125 shift
126 fi
127
128 # construct the command now
129 CMD="{\"jsonrpc\": \"2.0\", \"method\": \"$1\"";
130 shift
131 RPCPARAMS="no"
132 if [ -n "$1" ] ; then
133 rpcparamval "${1}"
134 CMD="${CMD}, \"params\": [${RPCVAL}"
135 RPCPARAMS="yes"
136 fi
137 if [ "$RPCPARAMS" = "yes" ]; then
138 shift
139 while [ -n "$1" ] ; do
140 rpcparamval "${1}"
141 if [ "$PARAMEVAL" = "yes" ]; then
142 CMDPARAM=`echo "$RPCVAL" | awk -F'=CRLF=' '{$1=$1}1' OFS='\r\n'`
143 CMD="${CMD}, ${CMDPARAM}"
144 else
145 CMD="${CMD}, ${RPCVAL}"
146 fi
147 shift
148 done
149 CMD="${CMD}]"
150 fi
151 if [ -n "$RPCSTOREPATH" ]; then
152 CMD="${CMD}, \"store_path\": \"${RPCSTOREPATH}\""
153 fi
154 CMD="${CMD}, \"reply_name\": \"${name}\", \"id\": $$}\n"
155
156 trap "rm -f $path; kill 0" 2
157
158 # start reader now so that it is ready for replies
159 # immediately after a request was sent out
160 cat < $path | filter_json | $EXPAND &
161
162 # issue FIFO request (printf taken to deal with \n)
163 printf "$CMD" > $RPCFIFOPATH
164
165 # wait for the reader to complete
166 wait
167 rm $path
168
169 mdbg "FIFO command was:\n$CMD"
170 }
171
172
173 CTLCMD=rpc_cmd
174
175 rpc_cmd_print()
176 {
177 name=kamailio_receiver_$$
178 # construct the command now
179 CMD="{\"jsonrpc\": \"2.0\", \"method\": \"$1\"";
180 shift
181 RPCPARAMS="no"
182 if [ -n "$1" ] ; then
183 rpcparamval "${1}"
184 CMD="${CMD}, \"params\": [${RPCVAL}"
185 RPCPARAMS="yes"
186 shift
187 fi
188 if [ "$RPCPARAMS" = "yes" ]; then
189 while [ -n "$1" ] ; do
190 rpcparamval "${1}"
191 CMD="${CMD}, ${RPCVAL}"
192 shift
193 done
194 CMD="${CMD}]"
195 fi
196 if [ -n "$RPCSTOREPATH" ]; then
197 CMD="${CMD}, \"store_path\": \"${RPCSTOREPATH}\""
198 fi
199 CMD="${CMD}, \"reply_name\": \"${name}\", \"id\": $$}\n"
200
201 minfo "The command is:\n"
202
203 mecho "$CMD"
204 }
205
206 CTLCMDPRINT=rpc_cmd_print
207
208 rpc_kamailio_monitor() {
209 name=kamailio_receiver_$$
210 path=$CHROOT_DIR/tmp/$name
211 # delete existing fifo file with same name
212 if test -p $path; then
213 rm -f $path
214 fi
215 if [ ! -w $RPCFIFOPATH ]; then
216 merr "Error opening Kamailio's FIFO $RPCFIFOPATH"
217 merr "Make sure you have loaded the jsonrpcs module and set FIFO transport parameters"
218 exit 1
219 fi
220 if ! test -p $path; then
221 mkfifo $path
222 if [ $? -ne 0 ] ; then
223 merr "monitor - error opening read fifo $path"
224 exit 1
225 fi
226 chmod a+w $path
227 fi
228 trap "rm $path; clear; echo monitor ^C-ed; exit 1" 2
229 attempt=0
230 if [ "$2" = "" ]; then
231 loops=-1;
232 else
233 loops=$2;
234 fi
235 clear
236 while [ $loops -ne $attempt ] ; do
237 attempt=`$EXPR $attempt + 1`
238 #clear
239 tput clear
240
241 # print_stats $name $path $attempt
242 mecho "[cycle #: $attempt; if constant make sure server lives]"
243 mecho "Kamailio Runtime Details: "
244
245 cat < $path | filter_json | $EXPAND | grep "result" | awk -F'"' '{ print $4 }' &
246 cat > $RPCFIFOPATH <<EOF
247 {
248 "jsonrpc": "2.0",
249 "method": "core.version",
250 "reply_name": "${name}",
251 "id": $$
252 }
253 EOF
254 wait
255
256 cat < $path | filter_json | $EXPAND | egrep "now|up_since|uptime" | sed 's/[",]//g' &
257 cat > $RPCFIFOPATH << EOF
258 {
259 "jsonrpc": "2.0",
260 "method": "core.uptime",
261 "reply_name": "${name}",
262 "id": $$
263 }
264 EOF
265 wait
266 echo
267
268 mecho "Transaction Statistics: "
269 cat < $path | filter_json | $EXPAND | grep "=" | sed 's/[",]//g' | column &
270 cat > $RPCFIFOPATH <<EOF
271 {
272 "jsonrpc": "2.0",
273 "method": "stats.get_statistics",
274 "params": [ "UAS_transactions", "UAC_transactions", "active_transactions", "inuse_transactions"],
275 "reply_name": "${name}",
276 "id": $$
277 }
278 EOF
279 wait
280 echo
281
282 mecho "Stateless Server Statistics: "
283 cat < $path | filter_json | $EXPAND | grep "=" | sed 's/[",]//g' | column &
284 cat > $RPCFIFOPATH <<EOF
285 {
286 "jsonrpc": "2.0",
287 "method": "stats.get_statistics",
288 "params": [ "sent_replies", "sent_err_replies"],
289 "reply_name": "${name}",
290 "id": $$
291 }
292 EOF
293 wait
294 echo
295
296 mecho "UsrLoc Statistics: "
297 cat < $path | filter_json | $EXPAND | grep "=" | sed 's/[",]//g' | column &
298 cat > $RPCFIFOPATH <<EOF
299 {
300 "jsonrpc": "2.0",
301 "method": "stats.get_statistics",
302 "params": [ "usrloc:"],
303 "reply_name": "${name}",
304 "id": $$
305 }
306 EOF
307 wait
308 echo
309
310 mecho "Core Statistics: "
311 cat < $path | filter_json | $EXPAND | grep "=" | sed 's/[",]//g' | column &
312 cat > $RPCFIFOPATH <<EOF
313 {
314 "jsonrpc": "2.0",
315 "method": "stats.get_statistics",
316 "params": [ "rcv_requests", "fwd_requests", "rcv_replies", "fwd_replies"],
317 "reply_name": "${name}",
318 "id": $$
319 }
320 EOF
321 wait
322 echo
323
324 mecho "Shared Memory Statistics: "
325 cat < $path | filter_json | $EXPAND | grep "=" | sed 's/[",]//g' | column &
326 cat > $RPCFIFOPATH <<EOF
327 {
328 "jsonrpc": "2.0",
329 "method": "stats.get_statistics",
330 "params": [ "shmem:"],
331 "reply_name": "${name}",
332 "id": $$
333 }
334 EOF
335 wait
336
337 if [ $loops -ne $attempt ] ; then
338 sleep $WATCH_PERIOD
339 fi
340 done
341 rm $path
342 exit 0
343 }
344
345 KAMAILIO_MONITOR=rpc_kamailio_monitor