[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 | |
---|
[963] | 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 |
---|
[871] | 25 | g__stackfile= |
---|
| 26 | g__scenario_dir=scenario |
---|
[866] | 27 | g__mode="scenario" |
---|
[871] | 28 | g__scenariofile= |
---|
| 29 | g__dryrun=0 |
---|
| 30 | g__confirm=0 |
---|
| 31 | g__delay=1 # delay between message |
---|
[842] | 32 | |
---|
[866] | 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: |
---|
[875] | 55 | -c ask for confirmation |
---|
[963] | 56 | -d dry-run |
---|
[871] | 57 | -h this help |
---|
[866] | 58 | -f set stack file |
---|
| 59 | -l print scenarios list |
---|
[871] | 60 | -m set MODE |
---|
[875] | 61 | MODE may be "scenario", "stackfile" or "cleanup" |
---|
[871] | 62 | default mode is "scenario" |
---|
| 63 | -s set scenario file |
---|
[866] | 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 | |
---|
[878] | 71 | |
---|
[866] | 72 | To list scenarios, do: |
---|
| 73 | |
---|
| 74 | $0 -l |
---|
| 75 | |
---|
| 76 | |
---|
[878] | 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 |
---|
[866] | 85 | EOF |
---|
| 86 | exit 2 |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | list_scenarios () |
---|
| 90 | { |
---|
| 91 | echo "" |
---|
| 92 | echo "Scenarios list:" |
---|
| 93 | echo "" |
---|
[871] | 94 | ls -1 $g__scenario_dir |
---|
[866] | 95 | echo "" |
---|
| 96 | exit 2 |
---|
| 97 | } |
---|
| 98 | |
---|
[875] | 99 | send_cleanup_msg() |
---|
| 100 | { |
---|
[963] | 101 | $send_msg_cmd -h $g__broker_host -p $g__broker_port -b "$( echo {\"code\":\"8888\"} | base64 -w 0 )" |
---|
[875] | 102 | } |
---|
| 103 | |
---|
[866] | 104 | # check |
---|
| 105 | |
---|
| 106 | if [ $# -eq 0 ]; then |
---|
| 107 | usage |
---|
| 108 | fi |
---|
| 109 | |
---|
| 110 | # parse args |
---|
| 111 | |
---|
[871] | 112 | while getopts 'cdf:hlm:s:' OPTION |
---|
[866] | 113 | do |
---|
| 114 | case $OPTION in |
---|
[871] | 115 | c) g__confirm="1" |
---|
| 116 | ;; |
---|
| 117 | d) g__dryrun="1" |
---|
| 118 | ;; |
---|
[866] | 119 | f) g__stackfile="$OPTARG" |
---|
| 120 | ;; |
---|
| 121 | h) usage |
---|
| 122 | ;; |
---|
| 123 | l) list_scenarios |
---|
| 124 | ;; |
---|
| 125 | m) g__mode="$OPTARG" |
---|
| 126 | ;; |
---|
[871] | 127 | s) l__scenariofile="$OPTARG" |
---|
[866] | 128 | |
---|
[878] | 129 | if [[ "$l__scenariofile" =~ "/" ]]; then |
---|
[866] | 130 | # full/relative path was given with the filename |
---|
| 131 | |
---|
[871] | 132 | g__scenariofile="$l__scenariofile" |
---|
[866] | 133 | else |
---|
| 134 | # only the filename was given |
---|
| 135 | |
---|
[871] | 136 | g__scenariofile="$g__scenario_dir/$l__scenariofile" |
---|
[866] | 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 |
---|
[871] | 149 | if [ ! -f "$g__scenariofile" ]; then |
---|
[875] | 150 | msg "LIBIGCM-MOCK-ERR003" "scenario file not found ($g__scenariofile)" |
---|
[866] | 151 | exit 1 |
---|
| 152 | fi |
---|
| 153 | |
---|
[878] | 154 | #send_cleanup_msg # cleanup any previous tests on the backend side |
---|
| 155 | |
---|
[871] | 156 | while read LINE <&3; do |
---|
[866] | 157 | |
---|
[871] | 158 | # debug |
---|
| 159 | #echo $LINE |
---|
[866] | 160 | |
---|
| 161 | l__JSON_msg_buf= |
---|
[871] | 162 | N=1 |
---|
| 163 | fields_arr=(${LINE// / }) # process fields (split on " " delimiter) |
---|
| 164 | for FIELD in "${fields_arr[@]}"; do |
---|
[866] | 165 | |
---|
[871] | 166 | # debug |
---|
| 167 | #echo $FIELD |
---|
[866] | 168 | |
---|
| 169 | |
---|
[871] | 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 | |
---|
[963] | 178 | l__configcard_file="$l__configcard_file_sample_dir/${field_arr[1]}" |
---|
[871] | 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 | |
---|
[866] | 193 | # append to JSON message buffer |
---|
[871] | 194 | if [ "$N" -gt "1" ]; then |
---|
[866] | 195 | |
---|
[871] | 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)) |
---|
[866] | 205 | done |
---|
| 206 | |
---|
[871] | 207 | # enclose |
---|
| 208 | l__JSON_msg_buf="{""$l__JSON_msg_buf""}" |
---|
| 209 | |
---|
| 210 | # debug |
---|
| 211 | #echo $l__JSON_msg_buf |
---|
| 212 | |
---|
[866] | 213 | # message base64 encoding |
---|
[871] | 214 | l__JSON_msg_buf_encoded=$( echo $l__JSON_msg_buf | base64 -w 0 ) |
---|
[866] | 215 | |
---|
[871] | 216 | # debug |
---|
| 217 | #echo $l__JSON_msg_buf_encoded |
---|
[963] | 218 | #echo $send_msg_cmd -h $g__broker_host -p $g__broker_port -b "$l__JSON_msg_buf_encoded" |
---|
[866] | 219 | |
---|
| 220 | # send AMQP message |
---|
[871] | 221 | if [ "$g__dryrun" = "1" ]; then |
---|
[866] | 222 | |
---|
[963] | 223 | echo $send_msg_cmd -h $g__broker_host -p $g__broker_port -b "$l__JSON_msg_buf" |
---|
[866] | 224 | |
---|
[871] | 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 |
---|
[963] | 237 | $send_msg_cmd -h $g__broker_host -p $g__broker_port -b "$l__JSON_msg_buf_encoded" |
---|
[871] | 238 | fi |
---|
| 239 | |
---|
| 240 | |
---|
| 241 | |
---|
| 242 | |
---|
| 243 | if [ "$g__confirm" = "1" ]; then |
---|
[878] | 244 | read -p "<<< $( echo $l__JSON_msg_buf | cut -c 0-70 ) >>>> sent. Press enter for next message" bla |
---|
[871] | 245 | else |
---|
[878] | 246 | echo "<<< $( echo $l__JSON_msg_buf | cut -c 0-70 ) >>>> sent." |
---|
[871] | 247 | sleep $g__delay |
---|
| 248 | fi |
---|
| 249 | |
---|
| 250 | |
---|
| 251 | |
---|
| 252 | # debug |
---|
| 253 | #break |
---|
| 254 | |
---|
| 255 | done 3<$g__scenariofile |
---|
| 256 | |
---|
[875] | 257 | elif [ "$g__mode" = "cleanup" ]; then |
---|
[871] | 258 | |
---|
[875] | 259 | send_cleanup_msg |
---|
[871] | 260 | |
---|
[866] | 261 | elif [ "$g__mode" = "stackfile" ]; then |
---|
| 262 | |
---|
| 263 | # check |
---|
[871] | 264 | if [ ! -f $g__stackfile ]; then |
---|
[866] | 265 | msg "LIBIGCM-MOCK-ERR001" "file not found" |
---|
| 266 | exit 1 |
---|
| 267 | fi |
---|
| 268 | |
---|
| 269 | IFS=$'\n' |
---|
[871] | 270 | for line in $(cat $g__stackfile); do |
---|
[866] | 271 | #echo $line | awk -F" " '{print $4}' |
---|
| 272 | callname=$(echo $line | awk -F" " '{print $4}' ) |
---|
[963] | 273 | $send_msg_cmd $g__broker_host $g__broker_port string "$callname" |
---|
[866] | 274 | done |
---|
| 275 | else |
---|
| 276 | msg "LIBIGCM-MOCK-ERR002" "incorrect mode" |
---|
[854] | 277 | exit 1 |
---|
| 278 | fi |
---|
| 279 | |
---|
[866] | 280 | exit 0 |
---|