Ignore:
Timestamp:
06/11/13 09:33:58 (11 years ago)
Author:
jripsl
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Monitoring/libIGCM_mock/libIGCM_mock.sh

    r866 r871  
    1818# init 
    1919 
    20 stack_file=$1 
    2120send_msg_cmd="/home/jripsl/snapshot/Monitoring/CNClient/sendAMQPMsg" 
    22 scenario_dir=scenario 
     21g__stackfile= 
     22g__scenario_dir=scenario 
    2323g__mode="scenario" 
    24 g__stackfile= 
    25 g__scenario_file= 
     24g__scenariofile= 
     25g__dryrun=0 
     26g__confirm=0 
     27g__delay=1 # delay between message 
    2628 
    2729# func 
     
    4749 
    4850OPTIONS: 
     51   -h              this help 
    4952   -f              set stack file 
    5053   -l              print scenarios list  
    51    -m              set mode (if missing, "scenario" mode is used) 
    52    -s              set scenario 
     54   -m              set MODE 
     55                   MODE may be "scenario", "stackfile" or "purge" 
     56                   default mode is "scenario" 
     57   -s              set scenario file 
    5358 
    5459EXAMPLES: 
     
    7479        echo "Scenarios list:" 
    7580        echo "" 
    76         ls -1 $scenario_dir 
     81        ls -1 $g__scenario_dir 
    7782        echo "" 
    7883        exit 2 
     
    8792# parse args 
    8893 
    89 while getopts 'f:hlm:s:' OPTION 
     94while getopts 'cdf:hlm:s:' OPTION 
    9095do 
    9196  case $OPTION in 
     97  c)    g__confirm="1" 
     98        ;; 
     99  d)    g__dryrun="1" 
     100        ;; 
    92101  f)    g__stackfile="$OPTARG" 
    93102        ;; 
     
    98107  m)    g__mode="$OPTARG" 
    99108        ;; 
    100   s)    l__scenario_file="$OPTARG" 
    101  
    102                 if [[ "$l__scenario_file" =~ "*/*" ]]; then 
     109  s)    l__scenariofile="$OPTARG" 
     110 
     111                if [[ "$l__scenariofile" =~ "*/*" ]]; then 
    103112                        # full/relative path was given with the filename 
    104113 
    105                         g__scenario_file="$l__scenario_file" 
     114                        g__scenariofile="$l__scenariofile" 
    106115                else 
    107116                        # only the filename was given 
    108117 
    109                         g__scenario_file="$scenario_dir/$l__scenario_file" 
     118                        g__scenariofile="$g__scenario_dir/$l__scenariofile" 
    110119                fi 
    111120 
     
    120129 
    121130        # check 
    122         if [ ! -f "$g__scenario_file" ]; then 
     131        if [ ! -f "$g__scenariofile" ]; then 
    123132                msg "LIBIGCM-MOCK-ERR003" "scenario file not found" 
    124133                exit 1 
    125134        fi 
    126135 
    127         N=0 
    128         while read LINE ; do 
    129  
    130                 N=$((N+1)) 
     136        while read LINE <&3; do 
     137 
     138                # debug 
     139                #echo $LINE 
    131140 
    132141                l__JSON_msg_buf= 
    133  
    134                 # process fields (split on " " delimiter) 
    135                 for field in "$LINE"; do 
    136                         arr=(${LINE//=/ }) # process key/value (split on "=" delimiter) 
    137                         key=${arr[0]} 
    138                         val=${arr[1]} 
    139  
    140                         # base64 encoding 
     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 
    141172 
    142173                        # append to JSON message buffer 
    143                         l__JSON_msg_buf=$l__JSON_msg_buf 
    144  
     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)) 
    145185                done 
    146186 
     187                # enclose 
     188                l__JSON_msg_buf="{""$l__JSON_msg_buf""}" 
     189 
     190                # debug 
     191                #echo $l__JSON_msg_buf 
     192 
    147193                # message base64 encoding 
    148  
    149  
    150                 #callname=$(echo $line | awk -F" " '{print $4}' ) 
     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" 
    151199 
    152200                # send AMQP message 
    153                 #$send_msg_cmd localhost 5672 string "$callname" 
    154                 echo 
    155  
    156         done < $g__scenario_file 
     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        : 
    157239 
    158240elif [ "$g__mode" = "stackfile" ]; then 
    159241 
    160242        # check 
    161         if [ ! -f $stack_file ]; then 
     243        if [ ! -f $g__stackfile ]; then 
    162244                msg "LIBIGCM-MOCK-ERR001" "file not found" 
    163245                exit 1 
     
    165247 
    166248        IFS=$'\n' 
    167         for line in $(cat $stack_file); do 
     249        for line in $(cat $g__stackfile); do 
    168250                #echo $line | awk -F" " '{print $4}' 
    169251                callname=$(echo $line | awk -F" " '{print $4}' ) 
Note: See TracChangeset for help on using the changeset viewer.