source: trunk/Monitoring/libIGCM_mock/libIGCM_mock.sh

Last change on this file was 963, checked in by jripsl, 10 years ago
  • add host/port variables.
  • Property svn:executable set to *
File size: 5.6 KB
Line 
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
20l__configcard_file_sample_dir="/home/cscompute/CNClient/sample"
21send_msg_cmd="/home/cscompute/CNClient/sendAMQPMsg"
22
23g__broker_host=cstest-broker.ipsl.jussieu.fr
24g__broker_port=5672
25g__stackfile=
26g__scenario_dir=scenario
27g__mode="scenario"
28g__scenariofile=
29g__dryrun=0
30g__confirm=0
31g__delay=1 # delay between message
32
33# func
34
35curdate ()
36{
37    date '+%F %T'
38}
39
40msg ()
41{
42    l__code="$1"
43    l__msg="$2"
44
45    echo "$(curdate) - $l__code - $l__msg"
46}
47
48usage ()
49{
50        cat >&1 << EOF
51
52USAGE: $(basename $0) [-m mode] [-s scenario] [-l] [-t file]
53
54OPTIONS:
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
65EXAMPLES:
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
85EOF
86        exit 2
87}
88
89list_scenarios ()
90{
91        echo ""
92        echo "Scenarios list:"
93        echo ""
94        ls -1 $g__scenario_dir
95        echo ""
96        exit 2
97}
98
99send_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
106if [ $# -eq 0 ]; then
107        usage
108fi
109
110# parse args
111
112while getopts 'cdf:hlm:s:' OPTION
113do
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
143done
144
145# mode switch
146if [ "$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
257elif [ "$g__mode" = "cleanup" ]; then
258
259        send_cleanup_msg
260
261elif [ "$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
275else
276        msg "LIBIGCM-MOCK-ERR002" "incorrect mode"
277        exit 1
278fi
279
280exit 0
Note: See TracBrowser for help on using the repository browser.