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