Skip to content

Commit

Permalink
kamctl: added rpcfifo control engine
Browse files Browse the repository at this point in the history
- interact with kamailio via a fifo file using jsonrpc-s module
- CTLENGINE has to be set to RPCFIFO
- RPCFIFOPATH has to be set to the fifo file created by jsonrpc-s module
- kamctl rpc command is available for sending raw jsonrpc commands
- it forwards number cli parameters as number type in jsonrpc commands
- the prefix s: or i: can be used to enforce type string or int for
  parameters

kamctl rpc jsonrpc.eco abc 123
kamctl rpc jsonrpc.eco s:abc i:123
kamctl rpc jsonrpc.eco s:888 i:123
  • Loading branch information
miconda committed Dec 2, 2016
1 parent f2181d7 commit 3b8d795
Show file tree
Hide file tree
Showing 3 changed files with 276 additions and 5 deletions.
10 changes: 8 additions & 2 deletions utils/kamctl/kamctl
Expand Up @@ -180,6 +180,12 @@ case $CTLENGINE in
CTLENGINELOADED=1
fi
;;
RPCFIFO|rpcfifo)
if [ -f "$MYLIBDIR/kamctl.rpcfifo" ]; then
. "$MYLIBDIR/kamctl.rpcfifo"
CTLENGINELOADED=1
fi
;;
UNIXSOCK|unixsock)
if [ -f "$MYLIBDIR/kamctl.unixsock" ]; then
. "$MYLIBDIR/kamctl.unixsock"
Expand Down Expand Up @@ -2806,13 +2812,13 @@ case $1 in
permissions_address "$@"
;;

fifo|mi|unixsock|ser_mi|sercmd_mi|sercmdmi|kamcmd_mi|kamcmdmi)
fifo|mi|rpc|unixsock|ser_mi|sercmd_mi|sercmdmi|kamcmd_mi|kamcmdmi)
require_ctlengine
shift
$CTLCMD "$@"
;;

fifoprint|miprint)
fifoprint|miprint|rpcprint)
require_ctlengine
shift
$CTLCMDPRINT "$@"
Expand Down
262 changes: 262 additions & 0 deletions utils/kamctl/kamctl.rpcfifo
@@ -0,0 +1,262 @@
#
#
# control tool for maintaining Kamailio
#
#===================================================================

##### ----------------------------------------------- #####
### FIFO specific variables and functions
#

##### ----------------------------------------------- #####
### load CTL base
#
if [ -f "$MYLIBDIR/kamctl.ctlbase" ]; then
. "$MYLIBDIR/kamctl.ctlbase"
else
mwarn "Cannot load CTL core functions '$MYLIBDIR/kamctl.ctlbase' ..."
# exit -1
fi

#
##### ----------------------------------------------- #####
### parameters
#
if [ -z "$RPCFIFOPATH" ]; then
RPCFIFOPATH=/var/run/kamailio/kamailio_rpc.fifo
fi

#
##### ----------------------------------------------- #####
### functions
#
usage_rpc() {
echo
mecho " -- command 'rpc' - send raw RPC commands"
echo
cat <<EOF
rpc ................................ send raw RPC command
EOF
}
USAGE_FUNCTIONS="$USAGE_FUNCTIONS usage_rpc"

isnum() {
printf "%f" $1 >/dev/null 2>&1
}

rpcparamval() {
RPCVAL = "${1}"

case "${1}" in
s:*)
prefix="s:"
RPCVAL="\"${1#${prefix}}\""
;;
i:*)
prefix="i:"
RPCVAL="${1#${prefix}}"
;;
*)
if isnum ${1} ; then
RPCVAL="${1}"
else
RPCVAL="\"${1}\""
fi
;;
esac
}

rpc_cmd()
{
mdbg "entering rpc_cmd $*"

if [ "$#" -lt 1 ]; then
merr "rpc_cmd must take at least command name as parameter"
exit 1
fi
name=kamailio_receiver_$$
path=$CHROOT_DIR/tmp/$name
# delete existing fifo file with same name
if test -p $path; then
rm -f $path
fi
if [ ! -w $RPCFIFOPATH ]; then
merr "Error opening Kamailio's FIFO $RPCFIFOPATH"
merr "Make sure you have loaded the jsonrpc-s module and set FIFO transport parameters"
if [ ! -z $CHROOT_DIR ]; then
merr "[chrooted environment] Check that $RPCFIFOPATH is symlinked to ${CHROOT_DIR}${RPCFIFOPATH}"
fi
exit 2
fi
if ! test -p $path; then
mkfifo $path
if [ $? -ne 0 ] ; then
merr "error opening read fifo $path"
exit 3
fi
chmod a+w $path
fi

# construct the command now
CMD="{\"jsonrpc\": \"2.0\", \"method\": \"$1\"";
shift
RPCPARAMS="no"
if [ -n "$1" ] ; then
rpcparamval "${1}"
CMD="${CMD}, \"params\": [${RPCVAL}"
RPCPARAMS="yes"
fi
shift
while [ -n "$1" ] ; do
rpcparamval "${1}"
CMD="${CMD}, ${RPCVAL}"
shift
done
if [ "$RPCPARAMS" = "yes" ]; then
CMD="${CMD}]"
fi
CMD="${CMD}, \"reply_name\": \"${name}\", \"id\": $$}\n"

trap "rm -f $path; kill 0" 2

# start reader now so that it is ready for replies
# immediately after a request was sent out
cat < $path | filter_fl &

# issue FIFO request (printf taken to deal with \n)
printf "$CMD" > $RPCFIFOPATH

# wait for the reader to complete
wait
rm $path

mdbg "FIFO command was:\n$CMD"
}


CTLCMD=rpc_cmd

rpc_cmd_print()
{
name=kamailio_receiver_$$
# construct the command now
CMD="{\"jsonrpc\": \"2.0\", \"method\": \"$1\"";
shift
RPCPARAMS="no"
if [ -n "$1" ] ; then
rpcparamval "${1}"
CMD="${CMD}, \"params\": [${RPCVAL}"
RPCPARAMS="yes"
shift
fi
while [ -n "$1" ] ; do
rpcparamval "${1}"
CMD="${CMD}, ${RPCVAL}"
shift
done
if [ "$RPCPARAMS" = "yes" ]; then
CMD="${CMD}]"
fi
CMD="${CMD}, \"reply_name\": \"${name}\", \"id\": $$}\n"

minfo "The command is:\n"

mecho "$CMD"
}

CTLCMDPRINT=rpc_cmd_print

rpc_kamailio_monitor() {
name=kamailio_receiver_$$
path=$CHROOT_DIR/tmp/$name
# delete existing fifo file with same name
if test -p $path; then
rm -f $path
fi
if [ ! -w $RPCFIFOPATH ]; then
merr "Error opening Kamailio's FIFO $RPCFIFOPATH"
merr "Make sure you have loaded the jsonrpc-s module and set FIFO transport parameters"
exit 1
fi
if ! test -p $path; then
mkfifo $path
if [ $? -ne 0 ] ; then
merr "monitor - error opening read fifo $path"
exit 1
fi
chmod a+w $path
fi
trap "rm $path; clear; echo monitor ^C-ed; exit 1" 2
attempt=0
if [ "$2" = "" ]; then
loops=-1;
else
loops=$2;
fi
clear
while [ $loops -ne $attempt ] ; do
attempt=`$EXPR $attempt + 1`
#clear
tput clear

# print_stats $name $path $attempt
mecho "[cycle #: $attempt; if constant make sure server lives]"

cat < $path | filter_fl &
cat > $RPCFIFOPATH <<EOF
:version:$name

EOF
wait

cat < $path | filter_fl &
cat > $RPCFIFOPATH << EOF
:uptime:$name

EOF
wait
echo

mecho "Transaction Statistics: "
cat < $path | filter_fl &
cat > $RPCFIFOPATH <<EOF
:get_statistics:$name
UAS_transactions
UAC_transactions
inuse_transactions

EOF
wait
echo

mecho "Stateless Server Statistics: "
cat < $path | filter_fl &
cat > $RPCFIFOPATH <<EOF
:get_statistics:$name
sent_replies
sent_err_replies
received_ACKs

EOF
wait
echo

mecho "UsrLoc Stats: "
cat < $path | filter_fl &
cat > $RPCFIFOPATH <<EOF
:get_statistics:$name
usrloc:

EOF
wait

if [ $loops -ne $attempt ] ; then
sleep $WATCH_PERIOD
fi
done
rm $path
exit 0
}

KAMAILIO_MONITOR=rpc_kamailio_monitor

9 changes: 6 additions & 3 deletions utils/kamctl/kamctlrc
Expand Up @@ -49,15 +49,15 @@


# SQL definitions
# If you change this definitions here, then you must change them
# If you change this definitions here, then you must change them
# in db/schema/entities.xml too.
# FIXME

# FOREVER="2030-05-28 21:32:15"
# DEFAULT_Q="1.0"


# Program to calculate a message-digest fingerprint
# Program to calculate a message-digest fingerprint
# MD5="md5sum"

# awk tool
Expand Down Expand Up @@ -117,13 +117,16 @@
## - default: none
# ALIASES_TYPE="DB"

## control engine: FIFO or UNIXSOCK
## control engine: FIFO, RPCFIFO or UNIXSOCK
## - default FIFO
# CTLENGINE="FIFO"

## path to FIFO file
# FIFOPATH="/var/run/kamailio/kamailio_fifo"

## path to RPCFIFO file
# RPCFIFOPATH="/var/run/kamailio/kamailio_rpc_fifo"

## check ACL names; default on (1); off (0)
# VERIFY_ACL=1

Expand Down

0 comments on commit 3b8d795

Please sign in to comment.