5b18473ec1ddd1bba61149f7a1fd6eb6f27eee6a
[sahlberg/ctdb.git] / tests / eventscripts / stubs / ctdb
1 #!/bin/sh
2
3 prog="ctdb"
4
5 not_implemented_exit_code=1
6
7 usage ()
8 {
9     cat >&2 <<EOF
10 Usage: $prog [-Y] cmd
11
12 A fake CTDB stub that prints items depending on the variables
13 FAKE_CTDB_PNN (default 0) depending on command-line options.
14 EOF
15     exit 1
16 }
17
18 not_implemented ()
19 {
20     echo "${prog}: command \"$1\" not implemented in stub" >&2
21     exit $not_implemented_exit_code
22 }
23
24 # Don't set $POSIXLY_CORRECT here.
25 _temp=$(getopt -n "$prog" -o "Yvh" -l help -- "$@") || \
26     usage
27
28 eval set -- "$_temp"
29
30 verbose=false
31 machine_readable=false
32
33 while true ; do
34     case "$1" in
35         -Y) machine_readable=true ; shift ;;
36         -v) verbose=true ; shift ;;
37         --) shift ; break ;;
38         -h|--help|*) usage ;; # * shouldn't happen, so this is reasonable.
39     esac
40 done
41
42 [ $# -ge 1 ] || usage
43
44 setup_tickles ()
45 {
46     # Make sure tickles file exists.
47     tickles_file="$CTDB_VARDIR/fake-ctdb/tickles"
48     mkdir -p $(dirname "$tickles_file")
49     touch "$tickles_file"
50 }
51
52 setup_pstore ()
53 {
54     pstore_dir="$CTDB_VARDIR/fake-ctdb/pstore/$1"
55     mkdir -p "$pstore_dir"
56 }
57
58 # For testing backward compatibility...
59 for i in $CTDB_NOT_IMPLEMENTED ; do
60     if [ "$i" = "$1" ] ; then
61         not_implemented "$i"
62     fi
63 done
64
65 case "$1" in
66     ip)
67         # NOTE: all nodes share the same public addresses file.
68
69         # This is completely stateless and IPs move unnecessarily.
70         _f="${CTDB_PUBLIC_ADDRESSES:-${CTDB_BASE}/public_addresses}"
71         if [ -f "$_f" ] ; then
72             if $verbose ; then
73                 echo ":Public IP:Node:ActiveInterface:AvailableInterfaces:ConfiguredInterfaces:"
74
75             else
76                 echo ":Public IP:Node:"
77             fi
78             # Here IPs are distributed across nodes in a stupid way...
79             _n=0
80             while read _ip _ifaces ; do
81                 case "_$ip" in
82                     \#*) : ;;
83                     *)
84                         # Find a node that is up... but don't loop forever.
85                         _orig=$_n
86                         while [ -f "${FAKE_CTDB_NODES_DOWN}/${_n}" ] ; do
87                             _n=$(($_n + 1))
88                             if [ _n -eq $FAKE_CTDB_NUMNODES ] ; then
89                                 _n=0
90                             fi
91                             if [ $_n -eq $_orig ] ; then
92                                 _n=-1 # Never down!  :-)
93                             fi
94                         done
95                         if $verbose ; then
96                             # If more than 1 interface, assume all
97                             # addresses are on the 1st.
98                             _first_iface="${_ifaces%%,*}"
99                             # Only show interface if address is on
100                             # this node.
101                             _my_iface=""
102                             if [ "PNN:$_n" = $(ctdb -Y pnn) ]; then
103                                 _my_iface="$_first_iface"
104                             fi
105                             echo ":${_ip}:${_n}:${_my_iface}:${_first_iface}:${_ifaces}:"
106                         else
107                             echo ":${_ip}:${_n}:"
108                         fi
109                 esac
110             done <"$_f"
111         fi
112         ;;
113     pnn|xpnn)
114         # Defaults to 0
115         echo "PNN:${FAKE_CTDB_PNN:-0}"
116         ;;
117     gettickles)
118         setup_tickles
119         echo ":source ip:port:destination ip:port:"
120         while read src dst ; do
121             echo ":${src}:${dst}:"
122         done <"$tickles_file"
123         ;;
124     addtickle)
125         setup_tickles
126         echo "$2 $3" >>"$tickles_file"
127         ;;
128     deltickle)
129         setup_tickles
130         _t=$(grep -F -v "$2 $3" "$tickles_file")
131         echo "$_t" >"$tickles_file"
132         ;;
133     pstore)
134         setup_pstore "$2"
135         cat "$4" >"${pstore_dir}/$3"
136         ;;
137     pfetch)
138         setup_pstore "$2"
139         cat "${pstore_dir}/$3" >"$4" 2>/dev/null
140         ;;
141     ifaces)
142         # Assume -Y.
143         echo ":Name:LinkStatus:References:"
144         _f="${CTDB_PUBLIC_ADDRESSES:-${CTDB_BASE}/public_addresses}"
145         if [ -r "$_f" ] ; then
146             while read _ip _iface ; do
147                 case "_$ip" in
148                     \#*) : ;;
149                     *)
150                         _status=1
151                         # For now assume _iface contains only 1.
152                         if [ -f "{FAKE_CTDB_IFACES_DOWN}/${_iface}" ] ; then
153                             _status=0
154                         fi
155                         # Nobody looks at references
156                         echo ":${_iface}:${_status}:0"
157                 esac
158             done <"$_f" |
159             sort -u
160         fi
161         ;;
162     setifacelink)
163         # Existence of file means CTDB thinks interface is down.
164         _f="${FAKE_CTDB_IFACES_DOWN}/$2"
165         case "$3" in
166             up)   rm -f "$_f" ;;
167             down) touch "$_f" ;;
168             *)
169                 echo "ctdb setifacelink: unsupported interface status $3"
170                 exit 1
171         esac
172         ;;
173     getdebug)
174         case "${CTDB_DEBUGLEVEL:-0}" in
175             -3) _t="EMERG"  ;;
176             -2) _t="ALERT"  ;;
177             -1) _t="CRIT"   ;;
178             0) _t="ERR"     ;;
179             1) _t="WARNING" ;;
180             2) _t="NOTICE"  ;;
181             3) _t="INFO"    ;;
182             4) _t="DEBUG"   ;;
183             *) _t="ERR"     ;;
184         esac
185
186         cat<<EOF
187 :Name:Level:
188 :${_t}:${CTDB_DEBUGLEVEL}:
189 EOF
190         ;;
191     checktcpport)
192         for _i in $FAKE_TCP_LISTEN ; do
193             if [ "$2" = "${_i##*:}" ] ; then
194                 exit 98
195             fi
196         done
197
198         exit 0
199         ;;
200     scriptstatus)
201         $machine_readable || not_implemented "$1, without -Y"
202         [ "$2" != "all" ] || not_implemented "scriptstatus all"
203         # For now just assume everything is good.
204         _d1=$(date '+%s.%N')
205         _d2=$(date '+%s.%N')
206         echo ":Type:Name:Code:Status:Start:End:Error Output...:"
207         for _i in "$CTDB_BASE/events.d/"*.* ; do
208             _b="${_i##*/}" # basename
209             _status="OK"
210             _code="0"
211             if [ ! -x "$_i" ] ; then
212                 _status="DISABLED"
213                 _code="-8"
214             fi
215             echo ":${2:-monitor}:${_b}:${_code}:${_status}:${_d1}:${_d2}::"
216         done
217         ;;
218     *)
219         not_implemented "$1"
220 esac