美文网首页
脚本集锦-shell发送post请求

脚本集锦-shell发送post请求

作者: CarsonCao | 来源:发表于2019-03-28 10:10 被阅读0次

下面是一个监控shell中任务执行状态的脚本:

#!/bin/bash 

set -e
set -x

################################
# constants
################################

JOB_STATUS_UPLOAD_URL=127.0.0.1:7001/fool
HEADER_STR="-H Content-Type:application/json -H Charset:UTF-8 -H backdoor:sys"
JOB_ID=""
APPLICATION_ID=""
JOB_START_TIME=""
JOB_END_TIME=""
JOB_STATUS=""
JOB_DATE=""

################################
# functions
################################

## output error message and exit with code.
error() {
  local msg=$1
  local exit_code=$2

  echo "Error: $msg" >&2

  if [ -n "$exit_code" ] ; then
    exit $exit_code
  fi
}

## init job
init() {
  args=""

  while [ -n "$*" ] ; do
    arg=$1
    shift

    case "$arg" in
      --jobid|-i)
        [ -n "$1" ] || error "Option --jobid requires an argument" 1
        JOB_ID=$1
        shift
        ;;
      --appid|-a)
        [ -n "$1" ] || error "Option --appid requires an argument" 1
        APPLICATION_ID=$1
        shift
        ;;
      --starttime|-s)
        [ -n "$1" ] || error "Option --starttime requires an argument format: <timpstamp>" 1
        JOB_START_TIME=`date -d @"$1" +"%Y-%m-%d %H:%M:%S"`
        shift
        ;;
    --date|-d)
        [ -n "$1" ] || error "Option --date requires an argument" 1
        JOB_DATE=$1
        shift
        ;;
    esac
  done

  if [[ -z $JOB_ID || -z $APPLICATION_ID || -z $JOB_START_TIME || -z $JOB_DATE ]]; then
    display_help
    error "params :'jobid','appid','starttime','date' are all required !" 1
  fi

  local INIT_BODY=$(cat<< EOF
    { 
      "jobId": "$JOB_ID", 
      "applicationId": "${APPLICATION_ID}",
      "startTime": "$JOB_START_TIME",
      "status": "init",
      "date": "$JOB_DATE" 
    }
EOF
)

  response=$(curl -i -X POST $HEADER_STR -d "$INIT_BODY" $JOB_STATUS_UPLOAD_URL)

  if [[ $response =~ "status" && $response =~ "true" ]];then
    echo $response
    echo "====init success===="
  else
    echo $response
    echo "====init error=====" 
  fi

}

## upload job status{success, failed}
uploadStatus() {
  args=""

  while [ -n "$*" ] ; do
    arg=$1
    shift

    case "$arg" in
      --jobid|-i)
        [ -n "$1" ] || error "Option --jobid requires an argument" 1
        JOB_ID=$1
        shift
        ;;
      --appid|-a)
        [ -n "$1" ] || error "Option --appid requires an argument" 1
        APPLICATION_ID=$1
        shift
        ;;
      --endtime|-e)
        [ -n "$1" ] || error "Option --endtime requires an argument format: <timpstamp>" 1
        JOB_END_TIME=`date -d @"$1" +"%Y-%m-%d %H:%M:%S"`
        shift
        ;;
    --status|-S)
        [ -n "$1" ] || error "Option --status requires an argument" 1
        JOB_STATUS=$1
        if [[ $1 != 'success' ]];then
          JOB_STATUS='failed'
        fi
        shift
        ;;
    esac
  done

  if [[ -z $JOB_ID || -z $APPLICATION_ID || -z $JOB_END_TIME || -z $JOB_STATUS ]]; then
    display_help
    error "params :'jobid','appid','endtime','status' are all required !" 1
  fi

  local STATUS_BODY=$(cat<< EOF
    { 
      "jobId": "$JOB_ID", 
      "applicationId": "${APPLICATION_ID}",
      "completeTime": "${JOB_END_TIME}",
      "status": "${JOB_STATUS}"
    }
EOF
)

  response=$(curl -i -X POST $HEADER_STR -d "$STATUS_BODY" $JOB_STATUS_UPLOAD_URL)

  if [[ $response =~ "status" && $response =~ "true" ]];then
    echo $response
    echo "====update status success===="
  else
    echo $response
    echo "====update status error====="
  fi

}

## display help
display_help() {
  cat <<EOF
Usage: $0 <command> [options]...

commands:
  help              display this help text
  init              init one job
  status            upload status to server


init options:
  --jobid,-i <jobId>              job id
  --appid,-a  <applicationId>     The application-id of spark job,ETL task could be set to 'ETL_' + <timestamp>
  --starttime,-s <timestamp>      job start time, format:<timestamp>
  --date,-d <yyyy-mm-dd>          job running day
 
status options:
  --jobid,-i <jobId>              job id
  --appid,-a  <applicationId>     The application-id of spark job,ETL task could be set to 'ETL_' + <timestamp>
                                  This appid should be the same as init's.
  --endtime,-e <timestamp>        job end time, format :<timestamp>
  --status,-S <success/failed>    job status
                 

EOF
}

################################
# main
################################

module=$1
shift

case "$module" in
  help)
    display_help
    exit 0
    ;;
  init)
    init $*
    exit 0
    ;;
  status)
    uploadStatus $*
    exit 0
    ;;
  *)
    error "Unknown or unspecified command '$module'"
    echo
    display_help
    exit 1
    ;;
esac

调用demo:

#!/bin/bash

BASEDIR=$(dirname $(readlink -f $0))
echo -e "Current dir: ${BASEDIR}"

JOB_ID=121
APPLICATION_ID=etl_${JOB_ID}_$(date +'%Y%m%d')

## Initializing job
bash $BASEDIR/supervisor.sh init -a $APPLICATION_ID  -i $JOB_ID -s $(date +'%s') -d $(date +'%Y-%m-%d')
## job
sleep 10s 
echo "==================================="

##Uploading status
if [[ $? -eq 0 ]];then
  STATUS='success'
else 
  STATUS='failed'
fi
bash $BASEDIR/supervisor.sh status -a $APPLICATION_ID  -i $JOB_ID -e $(date +'%s') -S $STATUS

相关文章

网友评论

      本文标题:脚本集锦-shell发送post请求

      本文链接:https://www.haomeiwen.com/subject/xtvfbqtx.html