File: kamctl.fifo

package info (click to toggle)
kamailio 4.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 56,100 kB
  • sloc: ansic: 552,832; xml: 166,484; sh: 8,659; makefile: 7,676; sql: 6,235; perl: 3,487; yacc: 3,428; python: 1,457; cpp: 1,219; php: 1,047; java: 449; pascal: 194; cs: 40; awk: 27
file content (204 lines) | stat: -rw-r--r-- 3,991 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#
# $Id$
#
# 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 "$FIFOPATH" ]; then
	if [ -z "$OSER_FIFO" ]; then
		FIFOPATH=/var/run/kamailio/kamailio_fifo
	else
		FIFOPATH=$OSER_FIFO
	fi
fi

#
##### ----------------------------------------------- #####
### functions
#
usage_fifo() {
	echo
	mecho " -- command 'mi' - send raw MI commands"
	echo
cat <<EOF
 mi ................................. send raw MI command
 fifo ............................... send raw FIFO (MI) command
EOF
}
USAGE_FUNCTIONS="$USAGE_FUNCTIONS usage_fifo"


fifo_cmd()
{
	mdbg "entering fifo_cmd $*"
	
	if [ "$#" -lt 1 ]; then
		merr "fifo_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 $FIFOPATH ]; then
		merr "Error opening Kamailio's FIFO $FIFOPATH"
		merr "Make sure you have the line 'modparam(\"mi_fifo\", \"fifo_name\", \"$FIFOPATH\")' in your config"
		merr "and also have loaded the mi_fifo module."
		if [ ! -z $CHROOT_DIR ]; then
			merr "[chrooted environment] Check that $FIFOPATH is symlinked to ${CHROOT_DIR}${FIFOPATH}"
		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=":$1:$name\n";
	shift
	while [ -n "$1" ] ; do
		CMD="${CMD}${1}\n"
		shift
	done
	CMD="${CMD}\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" > $FIFOPATH

	# wait for the reader to complete
	wait
	rm $path

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


CTLCMD=fifo_cmd

fifo_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 $FIFOPATH ]; then
		merr "Error opening Kamailio's FIFO $FIFOPATH"
		merr "Make sure you have the line 'modparam(\"mi_fifo\", \"fifo_name\", \"$FIFOPATH\")' in your config"
		merr "and also have loaded the mi_fifo module."
		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 > $FIFOPATH <<EOF
:version:$name

EOF
		wait

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

EOF
		wait
		echo

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

EOF
		wait
		echo

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

EOF
		wait
		echo

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

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

KAMAILIO_MONITOR=fifo_kamailio_monitor