Command to print debugging from a running server.
[freeradius.git] / scripts / raddebug
1 #!/bin/sh
2 ######################################################################
3 #
4 #    This program is free software; you can redistribute it and/or modify
5 #    it under the terms of the GNU General Public License as published by
6 #    the Free Software Foundation; either version 2 of the License, or
7 #    (at your option) any later version.
8 #
9 #    This program is distributed in the hope that it will be useful,
10 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
11 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 #    GNU General Public License for more details.
13 #
14 #    You should have received a copy of the GNU General Public License
15 #    along with this program; if not, write to the Free Software
16 #    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17 #
18 #    Copyright (C) 2008 Alan DeKok <aland@deployingradius.com>
19 #
20 ######################################################################
21 #
22 #  This script assumes that "radmin" is in PATH, and that the user
23 #  running this script has permission to connect to the radmin socket,
24 #  and to read/write files in the "logdir" directory.  If none of this is
25 #  true, then it won't work.
26 #
27 #   Usage: raddebug [-c condition] [-i client-ip-address] [-I client-ipv6-address] [-f socket_file] [-t timeout]  [-u username]
28 #
29 #
30
31 extra=
32 filename=
33 condition=1
34 timeout=10
35 while getopts 'c:i:I:f:t:u:' OPTION
36 do
37   case $OPTION in
38   c)    condition="$OPTARG"
39         ;;
40   i)    condition="(Packet-Src-IP-Address == $OPTARG)"
41         ;;
42   I)    condition="(Packet-Src-IPv6-Address == $OPTARG)"
43         ;;
44   f)    filename="$OPTARG"
45         extra="-f $filename"
46         ;;
47   t)    timeout="$OPTARG"
48         ;;
49   u)    condition="(User-Name == $OPTARG)"
50         ;;
51   ?)    printf "Usage: %s: [-c condition] [-i client-ip-address] [-I client-ipv6-address] [-f socket_file] [-t timeout] [-u user]\n" $(basename $0) >&2
52         exit 2
53         ;;
54   esac
55 done
56 shift $(($OPTIND - 1))
57
58 radmin="/Users/alandekok/git/2_1_x.git.freeradius.org/src/main/radmin $extra"
59
60 #
61 #  Start off by turning off debugging.
62 #  If there's a problem, die.
63 #
64 $radmin -e "debug condition"
65 if [ "$?" != "0" ]; then
66   exit 1
67 fi
68
69 #
70 #  Debug to a file, and then tell us where the file is.
71 #
72 outfile=`$radmin -e "debug file radmin.debug" -e "show debug file"`
73
74 #
75 #  If there was an error setting the debug output, re-set the
76 #  debug condition, echo the error, and exit.
77 #
78 echo $outfile | grep 'ERROR' >/dev/null 2>&1
79 if [ "$?" = "0" ]; then
80   $radmin -e "debug condition"
81   echo $outfile
82   exit 1
83 fi
84
85 #
86 #  Truncate the file, and ensure it's writable by radiusd
87 #
88 cp /dev/null $outfile
89 chmod g+w $outfile
90
91 #
92 #  Set the trap to clean up on exit and any interrupts.
93 #
94 trap '$radmin -e "debug condition" -e "debug file"; rm -f $outfile; exit 0' 0 1 2 15
95
96 #
97 #  Set the debug condition
98 #
99 $radmin -e "debug condition \"$condition\""
100
101 #
102 #  Print the output, and wait for "timeout".  Then, stop printing.
103 #
104 (tail -f $outfile | sed 's/^.*Debug: //') &
105 sleep $timeout
106 kill -INT %1