ast_logescalator 11 KB

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