source: trunk/Monitoring/libIGCM_mock/libIGCM_mock.sh @ 878

Last change on this file since 878 was 878, checked in by jripsl, 11 years ago

Remove blank line from scenarios.

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