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

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

Improve "config-card" file handling.
Use explicite function for "repo_io" module initialization

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