install_prereq: Update FreeBSD libraries.
[asterisk/asterisk.git] / contrib / scripts / ast_logescalator
1 #!/usr/bin/env bash
2 # Turn on extended globbing
3 shopt -s extglob
4 # Bail on any error
5 set -e
6
7 prog=$(basename $0)
8
9 print_help() {
10 cat <<EOF
11 NAME
12         $prog - Escalate Asterisk logging levels
13
14 SYNOPSIS
15         $prog [ --help ] | [ [ --reset ] | [
16                 [ --uniqueid="<uniqueid>" ]
17
18                 [ --pjsip-debug=<on|off> ] [ --sip-debug=<on|off> ]
19                 [ --iax2-debug=<on|off> ]
20
21                 [ --agi-debug=<on|off> ] [ --ami-debug=<on|off> ]
22                 [ --ari-debug=<on|off> ] [ --cdr-debug=<on|off> ]
23                 [ --channel-debug=<on|off> ] [ --rtp-debug=<on|off> ]
24                 [ --rtcp-debug=<on|off> ]
25
26                 [ --dtmf-debug=<on|off> ] [ --fax-debug=<on|off> ]
27                 [ --security-debug=<on|off> ]
28
29                 [ --pjsip-history=<on|off> ] [ --sip-history=<on|off> ]
30
31                 [ --verbose=<level> ] [ --debug=<level> ]
32                 ] ]
33
34 DESCRIPTION
35
36         Escalates log and/or debug levels on Asterisk subsystems.
37
38         Options:
39
40         --help
41                 Print this help.
42
43         --reset
44                 Resets logging to the pre-escalation state.
45
46         --uniqueid="<uniqueid>"
47                 Normally DATEFORMAT from ast_debug_tools.conf is used to make
48                 the log files unique but you can set the unique id to
49                 something else such as the Jira issue.  Once any logging
50                 is enabled, the uniqueid is stored in cli.conf so any future
51                 on/off commands will use the same uniqueid.  Use the --reset
52                 option to reset it (and everything else).
53
54         --pjsip-debug  --sip-debug --iax2-debug --agi-debug --ami-debug
55         --ari-debug --cdr-debug --channel-debug --rtp-debug --rtcp-debug
56                 Issues the subsystem appropriate command to turn on
57                 or off debugging.  These are usually functional debug messages
58                 such as packet dumps as opposed to code level messages and usually
59                 go to the VERBOSE log channel.
60
61         --dtmf-debug --fax-debug --security-debug
62                 These subsystems set up their own log channels so if turned
63                 on, log files will be created in \$astlogdir for them.
64
65         --pjsip-history  --sip-history
66                 The pjsip and sip channels have the ability to output an
67                 abbreviated, one-line, packet summary.  If enabled, the summaries
68                 will be written to \$astlogdir/pjsip_history.\$UNIQUEID and
69                 \$astlogdir/sip_history.\$UNIQUEID.
70
71         --verbose-level --debug-level
72                 Sets the levels for their respective messages.
73
74 NOTES
75
76         The escalator works by creating a set of startup commands in cli.conf
77         that set up logger channels and issue the debug commands.  If asterisk
78         is running when $prog is executed, the same commands will be issued
79         to the running instance.  The original cli.conf is saved before any
80         changes are made and can be restored by executing '$prog --reset'.
81
82         The log output will be stored in...
83         \$astlogdir/message.\$uniqueid
84         \$astlogdir/debug.\$uniqueid
85         \$astlogdir/dtmf.\$uniqueid
86         \$astlogdir/fax.\$uniqueid
87         \$astlogdir/security.\$uniqueid
88         \$astlogdir/pjsip_history.\$uniqueid
89         \$astlogdir/sip_history.\$uniqueid
90
91 EOF
92         exit 1
93 }
94
95 PJSIP_DEBUG_SPECIFIED=false
96 PJSIP_HISTORY_SPECIFIED=false
97 SIP_DEBUG_SPECIFIED=false
98 SIP_HISTORY_SPECIFIED=false
99 IAX2_DEBUG_SPECIFIED=false
100 ARI_DEBUG_SPECIFIED=false
101 AMI_DEBUG_SPECIFIED=false
102 AGI_DEBUG_SPECIFIED=false
103 CDR_DEBUG_SPECIFIED=false
104 CHANNEL_DEBUG_SPECIFIED=false
105 RTP_DEBUG_SPECIFIED=false
106 RTCP_DEBUG_SPECIFIED=false
107 DTMF_DEBUG_SPECIFIED=false
108 FAX_DEBUG_SPECIFIED=false
109 SECURITY_DEBUG_SPECIFIED=false
110 DEBUG_LEVEL_SPECIFIED=false
111 VERBOSE_LEVEL_SPECIFIED=false
112 DEBUGS=false
113 RESET=false
114
115 declare -A DEBUG_COMMANDS=(
116 [PJSIP,on]="pjsip set logger on" [PJSIP,off]="pjsip set logger off"
117 [SIP,on]="sip set debug on" [SIP,off]="sip set debug off"
118 [IAX2,on]="iax2 set debug on" [IAX2,off]="iax2 set debug off"
119 [ARI,on]="ari set debug all on" [ARI,off]="ari set debug all off"
120 [AMI,on]="manager set debug on" [AMI,off]="manager set debug off"
121 [AGI,on]="agi set debug on" [AGI,off]="agi set debug off"
122 [CDR,on]="cdr set debug on" [CDR,off]="cdr set debug off"
123 [CHANNEL,on]="core set debug channel all" [CHANNEL,off]="core set debug channel all off"
124 [RTP,on]="rtp set debug on" [RTP,on]="rtp set debug off"
125 [RTCP,on]="rtcp set debug on" [RTCP,off]="rtcp set debug off"
126 )
127
128 VERBOSE_LEVELS="NOTICE,WARNING,ERROR,VERBOSE"
129 DEBUG_LEVELS="DEBUG"
130
131 # Read config files from least important to most important
132 [ -f /etc/asterisk/ast_debug_tools.conf ] && source /etc/asterisk/ast_debug_tools.conf
133 [ -f ~/ast_debug_tools.conf ] && source ~/ast_debug_tools.conf
134 [ -f ./ast_debug_tools.conf ] && source ./ast_debug_tools.conf
135
136 DATEFORMAT=${DATEFORMAT:-'date +%FT%H-%M-%S%z'}
137 UNIQUEID=$($DATEFORMAT)
138 UNIQUEID_SPECIFIED=false
139
140 for a in "$@" ; do
141         case "$a" in
142         --*-debug=*)
143                 subsystem=${a%-debug=*}
144                 subsystem=${subsystem#--}
145                 flag=${a#*=}
146                 if [[ ${flag,,} =~ ^y(es)?|on ]] ; then
147                         eval ${subsystem^^}_DEBUG=true
148                 else
149                         eval ${subsystem^^}_DEBUG=false
150                 fi
151                 eval ${subsystem^^}_DEBUG_SPECIFIED=true
152                 DEBUGS=true
153                 ;;
154         --pjsip-history=*)
155                 ;&
156         --sip-history=*)
157                 subsystem=${a%-history=*}
158                 subsystem=${subsystem#--}
159                 if [[ ${a#*=} =~ ^[Yy].* ]] ; then
160                         eval ${subsystem^^}_HISTORY=true
161                 else
162                         eval ${subsystem^^}_HISTORY=false
163                 fi
164                 eval ${subsystem^^}_HISTORY_SPECIFIED=true
165                 DEBUGS=true
166                 ;;
167         --verbose=*)
168                 VERBOSE_LEVEL=${a#*=}
169                 VERBOSE_LEVEL_SPECIFIED=true
170                 DEBUGS=true
171                 ;;
172         --debug=*)
173                 DEBUG_LEVEL=${a#*=}
174                 DEBUG_LEVEL_SPECIFIED=true
175                 DEBUGS=true
176                 ;;
177         --reset)
178                 RESET=true
179                 ;;
180         --uniqueid=*)
181                 UNIQUEID=${a#*=}
182                 UNIQUEID_SPECIFIED=true
183                 DEBUGS=true
184                 ;;
185         --help|*)
186                 print_help
187                 ;;
188         esac
189 done
190
191 if $DEBUGS && $RESET ; then
192         echo "--reset must be specified by itself"
193         print_help
194 fi
195
196 if ! $DEBUGS && ! $RESET ; then
197         echo "No options specified."
198         print_help
199 fi
200
201 ASTERISK_IS_RUNNING=false
202 CONFIG_DIR=/etc/asterisk
203 LOG_DIR=/var/log/asterisk
204
205 if [ "$(pidof asterisk)" != "" ] ; then
206         CONFIG_DIR=`asterisk -rx "core show settings" | sed -n -r -e "s/^\s*Configuration\s+directory:\s+(.*)$/\1/gp"`
207         LOG_DIR=`asterisk -rx "core show settings" | sed -n -r -e "s/^\s*Log\s+directory:\s+(.*)$/\1/gp"`
208         ASTERISK_IS_RUNNING=true
209 fi
210 CLI_CONF="$CONFIG_DIR/cli.conf"
211
212 if [ ! -f "$CLI_CONF" ] ; then
213         echo "The location of cli.conf could not be determined."
214         exit 1
215 fi
216
217 if $RESET ; then
218         if [ -f "$CLI_CONF.unescalated" ] ; then
219                 mv "$CLI_CONF.unescalated" "$CLI_CONF"
220         fi
221         if $ASTERISK_IS_RUNNING ; then
222                 (
223                 asterisk -rx "core set verbose 0"
224                 asterisk -rx "core set debug 0"
225                 asterisk -rx "pjsip set logger off"
226                 asterisk -rx "pjsip set history off"
227                 asterisk -rx "sip set debug off"
228                 asterisk -rx "sip set history off"
229                 asterisk -rx "iax2 set debug off"
230                 asterisk -rx "manager set debug off"
231                 asterisk -rx "ari set debug all off"
232                 asterisk -rx "agi set debug off"
233                 asterisk -rx "rtp set debug off"
234                 asterisk -rx "rtcp set debug off"
235                 asterisk -rx "cdr set debug off"
236                 asterisk -rx "core set debug channel all off"
237                 asterisk -rx "logger reload"
238                 )  >/dev/null 2>&1 || :
239         fi
240         exit 1
241 fi
242
243 if ! grep -q "; --START DEBUG_LOGGING-- ;" $CLI_CONF ; then
244         VERBOSE_LOG="$LOG_DIR/message.${UNIQUEID}"
245         DEBUG_LOG="$LOG_DIR/debug.${UNIQUEID}"
246         PJSIP_HISTORY_LOG="$LOG_DIR/pjsip_history.${UNIQUEID}"
247         SIP_HISTORY_LOG="$LOG_DIR/sip_history.${UNIQUEID}"
248         DTMF_LOG="$LOG_DIR/dtmf.${UNIQUEID}"
249         FAX_LOG="$LOG_DIR/fax.${UNIQUEID}"
250         SECURITY_LOG="$LOG_DIR/security.${UNIQUEID}"
251
252         cp "$CLI_CONF" "$CLI_CONF.unescalated"
253
254         sed -i -r -e "s/\[startup_commands\]/[startup_commands_original](!)/g" "$CLI_CONF"
255
256         cat >> "$CLI_CONF" <<-EOF
257         ; --START DEBUG_LOGGING-- ;
258
259         [pjsip_debug](!)
260         pjsip set logger on = yes
261
262         [sip_debug](!)
263         sip set debug on = yes
264
265         [iax2_debug](!)
266         iax2 set debug on = yes
267
268         [ari_debug](!)
269         ari set debug all on = yes
270
271         [ami_debug](!)
272         manager set debug on = yes
273
274         [agi_debug](!)
275         agi set debug on = yes
276
277         [cdr_debug](!)
278         cdr set debug on = yes
279
280         [channel_debug](!)
281         core set debug channel all = yes
282
283         [rtp_debug](!)
284         rtp set debug on = yes
285
286         [rtcp_debug](!)
287         rtcp set debug on = yes
288
289         [dtmf_debug](!)
290         logger add channel $DTMF_LOG DTMF = yes
291
292         [fax_debug](!)
293         logger add channel $FAX_LOG FAX = yes
294
295         [security_debug](!)
296         logger add channel $SECURITY_LOG SECURITY = yes
297
298         [pjsip_history](!)
299         logger add channel $PJSIP_HISTORY_LOG PJSIP_HISTORY = yes
300         pjsip set history on = yes
301
302         [sip_history](!)
303         logger add channel $SIP_HISTORY_LOG SIP_HISTORY = yes
304         sip set history on = yes
305
306         [verbose_level](!)
307         core set verbose 3 = yes
308
309         [debug_level](!)
310         core set debug 3 = yes
311
312         [log_channels](!)
313         logger add channel $VERBOSE_LOG NOTICE,WARNING,ERROR,VERBOSE = yes
314         logger add channel $DEBUG_LOG DEBUG = yes
315
316         [startup_commands](startup_commands_original,log_channels)
317
318         ; --END DEBUG_LOGGING-- ;
319         EOF
320
321 else
322         if $UNIQUEID_SPECIFIED ; then
323                 echo "Debug logging is already active.  Either rerun $prog without --uniqueid or with --reset to start over."
324                 exit 1
325         fi
326
327         VERBOSE_LOG=$(sed -n -r -e "s@logger add channel ($LOG_DIR/message\..+)\s+NOTICE.*@\1@p" "$CLI_CONF")
328         DEBUG_LOG=$(sed -n -r -e "s@logger add channel ($LOG_DIR/debug\..+)\s+DEBUG.*@\1@p" "$CLI_CONF")
329         PJSIP_HISTORY_LOG=$(sed -n -r -e "s@logger add channel ($LOG_DIR/pjsip_history\..+)\s+PJSIP.*@\1@p" "$CLI_CONF")
330         SIP_HISTORY_LOG=$(sed -n -r -e "s@logger add channel ($LOG_DIR/sip_history\..+)\s+SIP.*@\1@p" "$CLI_CONF")
331         DTMF_LOG=$(sed -n -r -e "s@logger add channel ($LOG_DIR/dtmf\..+)\s+DTMF.*@\1@p" "$CLI_CONF")
332         FAX_LOG=$(sed -n -r -e "s@logger add channel ($LOG_DIR/fax\..+)\s+FAX.*@\1@p" "$CLI_CONF")
333         SECURITY_LOG=$(sed -n -r -e "s@logger add channel ($LOG_DIR/security\..+)\s+SECURITY.*@\1@p" "$CLI_CONF")
334 fi
335
336 for x in PJSIP SIP ARI AMI AGI ARI IAX2 CDR RTP RTCP ; do
337         if eval \$${x}_DEBUG_SPECIFIED ; then
338                 if eval \$${x}_DEBUG ; then
339                         if $ASTERISK_IS_RUNNING ; then
340                                 asterisk -rx "${DEBUG_COMMANDS[$x,on]}"
341                         fi
342                         egrep -q "^\[startup_commands\].*${x,,}_debug.*" "$CLI_CONF" ||
343                                 sed -i -r -e "/\[startup_commands\]/ s/\((.*)\)/(\1,${x,,}_debug)/g" "$CLI_CONF"
344                 else
345                         if $ASTERISK_IS_RUNNING ; then
346                                 asterisk -rx "${DEBUG_COMMANDS[$x,off]}"
347                         fi
348                         sed -i -r -e "/\[startup_commands\].*${x,,}_debug.*/ s/,${x,,}_debug//g" "$CLI_CONF"
349                 fi
350         fi
351 done
352
353 for x in DTMF FAX SECURITY ; do
354         if eval \$${x}_DEBUG_SPECIFIED ; then
355                 if eval \$${x}_DEBUG ; then
356                         if $ASTERISK_IS_RUNNING ; then
357                                 asterisk -rx "$(eval "echo logger add channel \$${x}_LOG ${x}")" >/dev/null 2>&1
358                         fi
359                         egrep -q "^\[startup_commands\].*${x,,}_debug.*" "$CLI_CONF" ||
360                                 sed -i -r -e "/\[startup_commands\]/ s/\((.*)\)/(\1,${x,,}_debug)/g" "$CLI_CONF"
361                 else
362                         if $ASTERISK_IS_RUNNING ; then
363                                 asterisk -rx "$(eval "echo logger remove channel \$${x}_LOG")"
364                         fi
365                         sed -i -r -e "/\[startup_commands\].*${x,,}_debug.*/ s/,${x,,}_debug//g" "$CLI_CONF"
366                 fi
367         fi
368 done
369
370 for x in PJSIP SIP ; do
371         if eval \$${x}_HISTORY_SPECIFIED ; then
372                 if eval \$${x}_HISTORY ; then
373                         if $ASTERISK_IS_RUNNING ; then
374                                 asterisk -rx "$(eval "echo logger add channel \$${x}_HISTORY_LOG ${x}_HISTORY")"
375                                 asterisk -rx "${x,,} set history on"
376                         fi
377                         egrep -q "^\[startup_commands\].*${x,,}_history.*" "$CLI_CONF" ||
378                                 sed -i -r -e "/\[startup_commands\]/ s/\((.*)\)/(\1,${x,,}_history)/g" "$CLI_CONF"
379                 else
380                         if $ASTERISK_IS_RUNNING ; then
381                                 asterisk -rx "$(eval "echo logger remove channel \$${x}_HISTORY_LOG")"
382                                 asterisk -rx "${x,,} set history off"
383                         fi
384                         sed -i -r -e "/\[startup_commands\].*${x,,}_history.*/ s/,${x,,}_history//g" "$CLI_CONF"
385                 fi
386         fi
387 done
388
389 for x in VERBOSE DEBUG ; do
390         if eval \$${x}_LEVEL_SPECIFIED ; then
391                 if $ASTERISK_IS_RUNNING ; then
392                         asterisk -rx "$(eval "echo logger add channel \$${x}_LOG \$${x}_LEVELS")"
393                         asterisk -rx "$(eval "echo core set ${x,,} \$${x}_LEVEL")"
394                 fi
395                 sed -i -r -e "$(eval "echo s/core set ${x,,} .*/core set ${x,,} \$${x}_LEVEL/g")" "$CLI_CONF"
396                 egrep -q "^\[startup_commands\].*${x,,}_level.*" "$CLI_CONF" ||
397                         sed -i -r -e "/\[startup_commands\]/ s/\((.*)\)/(\1,${x,,}_level)/g" "$CLI_CONF"
398         fi
399 done