#!/bin/bash ################################## # @program smon # @description simulation monitor # @copyright Copyright “(c)2009 Centre National de la Recherche Scientifique CNRS. # All Rights Reserved” # @svn_file $Id: failover 2545 2013-02-01 09:58:10Z jripsl $ # @version $Rev: 2545 $ # @lastrevision $Date: 2013-02-01 10:58:10 +0100 (Fri, 01 Feb 2013) $ # @license CeCILL (http://dods.ipsl.jussieu.fr/jripsl/smon/LICENSE) ################################## # Notes # - bash required version: 3+ # - "=" and " " are used as delimiter in scenario file (be sure note to use it in fields data) # init stack_file=$1 send_msg_cmd="/home/jripsl/snapshot/Monitoring/CNClient/sendAMQPMsg" scenario_dir=scenario g__mode="scenario" g__stackfile= g__scenario_file= # func curdate () { date '+%F %T' } msg () { l__code="$1" l__msg="$2" echo "$(curdate) - $l__code - $l__msg" } usage () { cat >&1 << EOF USAGE: $(basename $0) [-m mode] [-s scenario] [-l] [-t file] OPTIONS: -f set stack file -l print scenarios list -m set mode (if missing, "scenario" mode is used) -s set scenario EXAMPLES: To parse a stack file and send corressponding messages, do: $0 -m stackfile -f ../sample/stack_light To list scenarios, do: $0 -l To run scenario , do: $0 -s EOF exit 2 } list_scenarios () { echo "" echo "Scenarios list:" echo "" ls -1 $scenario_dir echo "" exit 2 } # check if [ $# -eq 0 ]; then usage fi # parse args while getopts 'f:hlm:s:' OPTION do case $OPTION in f) g__stackfile="$OPTARG" ;; h) usage ;; l) list_scenarios ;; m) g__mode="$OPTARG" ;; s) l__scenario_file="$OPTARG" if [[ "$l__scenario_file" =~ "*/*" ]]; then # full/relative path was given with the filename g__scenario_file="$l__scenario_file" else # only the filename was given g__scenario_file="$scenario_dir/$l__scenario_file" fi ;; ?) exit 1 # we come here when a required option argument is missing (bash getopts mecanism) ;; esac done # mode switch if [ "$g__mode" = "scenario" ]; then # check if [ ! -f "$g__scenario_file" ]; then msg "LIBIGCM-MOCK-ERR003" "scenario file not found" exit 1 fi N=0 while read LINE ; do N=$((N+1)) l__JSON_msg_buf= # process fields (split on " " delimiter) for field in "$LINE"; do arr=(${LINE//=/ }) # process key/value (split on "=" delimiter) key=${arr[0]} val=${arr[1]} # base64 encoding # append to JSON message buffer l__JSON_msg_buf=$l__JSON_msg_buf done # message base64 encoding #callname=$(echo $line | awk -F" " '{print $4}' ) # send AMQP message #$send_msg_cmd localhost 5672 string "$callname" echo done < $g__scenario_file elif [ "$g__mode" = "stackfile" ]; then # check if [ ! -f $stack_file ]; then msg "LIBIGCM-MOCK-ERR001" "file not found" exit 1 fi IFS=$'\n' for line in $(cat $stack_file); do #echo $line | awk -F" " '{print $4}' callname=$(echo $line | awk -F" " '{print $4}' ) $send_msg_cmd localhost 5672 string "$callname" done else msg "LIBIGCM-MOCK-ERR002" "incorrect mode" exit 1 fi exit 0