1 | #!/bin/bash |
---|
2 | |
---|
3 | ################################## |
---|
4 | # @program smon |
---|
5 | # @description simulation monitor |
---|
6 | # @copyright Copyright â(c)2009 Centre National de la Recherche Scientifique CNRS. |
---|
7 | # All Rights Reservedâ |
---|
8 | # @svn_file $Id: failover 2545 2013-02-01 09:58:10Z jripsl $ |
---|
9 | # @version $Rev: 2545 $ |
---|
10 | # @lastrevision $Date: 2013-02-01 10:58:10 +0100 (Fri, 01 Feb 2013) $ |
---|
11 | # @license CeCILL (http://dods.ipsl.jussieu.fr/jripsl/smon/LICENSE) |
---|
12 | ################################## |
---|
13 | |
---|
14 | # Notes |
---|
15 | # - bash required version: 3+ |
---|
16 | # - "=" and " " are used as delimiter in scenario file (be sure note to use it in fields data) |
---|
17 | |
---|
18 | # init |
---|
19 | |
---|
20 | l__configcard_file_sample_dir="/home/cscompute/CNClient/sample" |
---|
21 | send_msg_cmd="/home/cscompute/CNClient/sendAMQPMsg" |
---|
22 | |
---|
23 | g__broker_host=cstest-broker.ipsl.jussieu.fr |
---|
24 | g__broker_port=5672 |
---|
25 | g__stackfile= |
---|
26 | g__scenario_dir=scenario |
---|
27 | g__mode="scenario" |
---|
28 | g__scenariofile= |
---|
29 | g__dryrun=0 |
---|
30 | g__confirm=0 |
---|
31 | g__delay=1 # delay between message |
---|
32 | |
---|
33 | # func |
---|
34 | |
---|
35 | curdate () |
---|
36 | { |
---|
37 | date '+%F %T' |
---|
38 | } |
---|
39 | |
---|
40 | msg () |
---|
41 | { |
---|
42 | l__code="$1" |
---|
43 | l__msg="$2" |
---|
44 | |
---|
45 | echo "$(curdate) - $l__code - $l__msg" |
---|
46 | } |
---|
47 | |
---|
48 | usage () |
---|
49 | { |
---|
50 | cat >&1 << EOF |
---|
51 | |
---|
52 | USAGE: $(basename $0) [-m mode] [-s scenario] [-l] [-t file] |
---|
53 | |
---|
54 | OPTIONS: |
---|
55 | -c ask for confirmation |
---|
56 | -d dry-run |
---|
57 | -h this help |
---|
58 | -f set stack file |
---|
59 | -l print scenarios list |
---|
60 | -m set MODE |
---|
61 | MODE may be "scenario", "stackfile" or "cleanup" |
---|
62 | default mode is "scenario" |
---|
63 | -s set scenario file |
---|
64 | |
---|
65 | EXAMPLES: |
---|
66 | |
---|
67 | To parse a stack file and send corressponding messages, do: |
---|
68 | |
---|
69 | $0 -m stackfile -f ../sample/stack_light |
---|
70 | |
---|
71 | |
---|
72 | To list scenarios, do: |
---|
73 | |
---|
74 | $0 -l |
---|
75 | |
---|
76 | |
---|
77 | To run a scenario, do: |
---|
78 | |
---|
79 | $0 -s <scenario_filename> |
---|
80 | |
---|
81 | |
---|
82 | To remove tests data from the backend, do: |
---|
83 | |
---|
84 | $0 -m cleanup |
---|
85 | EOF |
---|
86 | exit 2 |
---|
87 | } |
---|
88 | |
---|
89 | list_scenarios () |
---|
90 | { |
---|
91 | echo "" |
---|
92 | echo "Scenarios list:" |
---|
93 | echo "" |
---|
94 | ls -1 $g__scenario_dir |
---|
95 | echo "" |
---|
96 | exit 2 |
---|
97 | } |
---|
98 | |
---|
99 | send_cleanup_msg() |
---|
100 | { |
---|
101 | $send_msg_cmd -h $g__broker_host -p $g__broker_port -b "$( echo {\"code\":\"8888\"} | base64 -w 0 )" |
---|
102 | } |
---|
103 | |
---|
104 | # check |
---|
105 | |
---|
106 | if [ $# -eq 0 ]; then |
---|
107 | usage |
---|
108 | fi |
---|
109 | |
---|
110 | # parse args |
---|
111 | |
---|
112 | while getopts 'cdf:hlm:s:' OPTION |
---|
113 | do |
---|
114 | case $OPTION in |
---|
115 | c) g__confirm="1" |
---|
116 | ;; |
---|
117 | d) g__dryrun="1" |
---|
118 | ;; |
---|
119 | f) g__stackfile="$OPTARG" |
---|
120 | ;; |
---|
121 | h) usage |
---|
122 | ;; |
---|
123 | l) list_scenarios |
---|
124 | ;; |
---|
125 | m) g__mode="$OPTARG" |
---|
126 | ;; |
---|
127 | s) l__scenariofile="$OPTARG" |
---|
128 | |
---|
129 | if [[ "$l__scenariofile" =~ "/" ]]; then |
---|
130 | # full/relative path was given with the filename |
---|
131 | |
---|
132 | g__scenariofile="$l__scenariofile" |
---|
133 | else |
---|
134 | # only the filename was given |
---|
135 | |
---|
136 | g__scenariofile="$g__scenario_dir/$l__scenariofile" |
---|
137 | fi |
---|
138 | |
---|
139 | ;; |
---|
140 | ?) exit 1 # we come here when a required option argument is missing (bash getopts mecanism) |
---|
141 | ;; |
---|
142 | esac |
---|
143 | done |
---|
144 | |
---|
145 | # mode switch |
---|
146 | if [ "$g__mode" = "scenario" ]; then |
---|
147 | |
---|
148 | # check |
---|
149 | if [ ! -f "$g__scenariofile" ]; then |
---|
150 | msg "LIBIGCM-MOCK-ERR003" "scenario file not found ($g__scenariofile)" |
---|
151 | exit 1 |
---|
152 | fi |
---|
153 | |
---|
154 | #send_cleanup_msg # cleanup any previous tests on the backend side |
---|
155 | |
---|
156 | while read LINE <&3; do |
---|
157 | |
---|
158 | # debug |
---|
159 | #echo $LINE |
---|
160 | |
---|
161 | l__JSON_msg_buf= |
---|
162 | N=1 |
---|
163 | fields_arr=(${LINE// / }) # process fields (split on " " delimiter) |
---|
164 | for FIELD in "${fields_arr[@]}"; do |
---|
165 | |
---|
166 | # debug |
---|
167 | #echo $FIELD |
---|
168 | |
---|
169 | |
---|
170 | field_arr=(${FIELD//=/ }) # process key/value (split on "=" delimiter) |
---|
171 | key=${field_arr[0]} |
---|
172 | |
---|
173 | |
---|
174 | # HACK |
---|
175 | if [ "$key" = "file" ]; then |
---|
176 | # special processing for "file" key (base64 encoding) |
---|
177 | |
---|
178 | l__configcard_file="$l__configcard_file_sample_dir/${field_arr[1]}" |
---|
179 | |
---|
180 | # check |
---|
181 | if [ ! -f "$l__configcard_file" ]; then |
---|
182 | msg "LIBIGCM-MOCK-ERR004" "config-card file not found ($l__configcard_file)" |
---|
183 | exit 1 |
---|
184 | fi |
---|
185 | |
---|
186 | val=$( cat $l__configcard_file | base64 -w 0 ) |
---|
187 | |
---|
188 | else |
---|
189 | val=${field_arr[1]} |
---|
190 | fi |
---|
191 | |
---|
192 | |
---|
193 | # append to JSON message buffer |
---|
194 | if [ "$N" -gt "1" ]; then |
---|
195 | |
---|
196 | l__JSON_msg_buf="$l__JSON_msg_buf,\"$key\":\"$val\"" |
---|
197 | else |
---|
198 | # first field |
---|
199 | |
---|
200 | l__JSON_msg_buf="\"$key\":\"$val\"" |
---|
201 | fi |
---|
202 | |
---|
203 | |
---|
204 | N=$((N+1)) |
---|
205 | done |
---|
206 | |
---|
207 | # enclose |
---|
208 | l__JSON_msg_buf="{""$l__JSON_msg_buf""}" |
---|
209 | |
---|
210 | # debug |
---|
211 | #echo $l__JSON_msg_buf |
---|
212 | |
---|
213 | # message base64 encoding |
---|
214 | l__JSON_msg_buf_encoded=$( echo $l__JSON_msg_buf | base64 -w 0 ) |
---|
215 | |
---|
216 | # debug |
---|
217 | #echo $l__JSON_msg_buf_encoded |
---|
218 | #echo $send_msg_cmd -h $g__broker_host -p $g__broker_port -b "$l__JSON_msg_buf_encoded" |
---|
219 | |
---|
220 | # send AMQP message |
---|
221 | if [ "$g__dryrun" = "1" ]; then |
---|
222 | |
---|
223 | echo $send_msg_cmd -h $g__broker_host -p $g__broker_port -b "$l__JSON_msg_buf" |
---|
224 | |
---|
225 | |
---|
226 | # debug |
---|
227 | # |
---|
228 | # uncomment line below (and comment line above) to output only the encoded message |
---|
229 | # |
---|
230 | # it can then be unencoded for debug purpose (message encoding level, not config-card encoding) using command below |
---|
231 | # ./libIGCM_mock.sh -s start_simu__stop_simu -d | base64 -d | less |
---|
232 | # |
---|
233 | # |
---|
234 | #echo $l__JSON_msg_buf_encoded |
---|
235 | |
---|
236 | else |
---|
237 | $send_msg_cmd -h $g__broker_host -p $g__broker_port -b "$l__JSON_msg_buf_encoded" |
---|
238 | fi |
---|
239 | |
---|
240 | |
---|
241 | |
---|
242 | |
---|
243 | if [ "$g__confirm" = "1" ]; then |
---|
244 | read -p "<<< $( echo $l__JSON_msg_buf | cut -c 0-70 ) >>>> sent. Press enter for next message" bla |
---|
245 | else |
---|
246 | echo "<<< $( echo $l__JSON_msg_buf | cut -c 0-70 ) >>>> sent." |
---|
247 | sleep $g__delay |
---|
248 | fi |
---|
249 | |
---|
250 | |
---|
251 | |
---|
252 | # debug |
---|
253 | #break |
---|
254 | |
---|
255 | done 3<$g__scenariofile |
---|
256 | |
---|
257 | elif [ "$g__mode" = "cleanup" ]; then |
---|
258 | |
---|
259 | send_cleanup_msg |
---|
260 | |
---|
261 | elif [ "$g__mode" = "stackfile" ]; then |
---|
262 | |
---|
263 | # check |
---|
264 | if [ ! -f $g__stackfile ]; then |
---|
265 | msg "LIBIGCM-MOCK-ERR001" "file not found" |
---|
266 | exit 1 |
---|
267 | fi |
---|
268 | |
---|
269 | IFS=$'\n' |
---|
270 | for line in $(cat $g__stackfile); do |
---|
271 | #echo $line | awk -F" " '{print $4}' |
---|
272 | callname=$(echo $line | awk -F" " '{print $4}' ) |
---|
273 | $send_msg_cmd $g__broker_host $g__broker_port string "$callname" |
---|
274 | done |
---|
275 | else |
---|
276 | msg "LIBIGCM-MOCK-ERR002" "incorrect mode" |
---|
277 | exit 1 |
---|
278 | fi |
---|
279 | |
---|
280 | exit 0 |
---|