feng1 各种运维脚本

1、access_analyse.sh

#!/bin/bash
# Created by tongkuizhuang@sf-express.com
#

option=$1
access_file=$2

usage(){
cat <<EOF
$0 <--help|access_num|ip_access_num|uri_access_num|uri_resp|err_status|no_resp> [access_filename]
Example:
    $0 --help
    $0 access_num access.2018-07-18.log
EOF
}

case ${option} in
access_num)
  printf "%10s  %10s\n" "uniq_by_ip" "access_count"
  printf "%s\n" "------------------------------------"
  cat ${access_file} | grep -v HEAD | awk '{sum_ip[$3]+=1;sum_a+=1}END{printf "%10d  %10d\n",length(sum_ip),sum_a}'
  ;;
ip_access_num)
  printf "%10s  %s\n" "access_num" "remote_ip"
  printf "%s\n" "-------------------------------------------------"
  cat ${access_file}|grep -v HEAD | awk '{print $3}'|sort|uniq -c|sort -k1nr
  ;;
uri_access_num)
  printf "%10s  %s\n" "access_num" "uri"
  printf "%s\n" "-------------------------------------------------"
  cat ${access_file}|grep -v HEAD | awk '{split($8,arr,"?");uri=arr[1];print uri}'|sort|uniq -c|sort -k1nr
  ;;
uri_resp)
  printf "%10s  %8s  %8s %11s  %-s\n" "count" "resp_min" "resp_max" "resp_avg" "uri"
  printf "%s\n" "-----------------------------------------------------------------------------"
  cat ${access_file}|grep -v HEAD |awk \
'{split($8,arr,"?");uri=arr[1];if(max[uri] + 0 < $12 +0 ){max[uri]=$12};if(min[uri] + 0 == 0 ||min[uri] + 0 > $12){min[uri] = $12} ;num[uri]+=1;sum[uri]+=$12}END{for(i in sum) printf "%10d  %8d  %8d %11.2f  %-s\n",num[i],min[i],max[i],sum[i]/num[i],i}'
  ;;
err_status)
  cat ${access_file} |grep -v HEAD|awk '$10>=400'|less
  ;;
no_resp)
  cat ${access_file} |grep -v HEAD|awk '$11=="-"'|less 
  ;;
--help)
  usage
  ;;
*)
  usage
  exit 1
  ;;
esac

  

2、fetchFile.sh

[appdeploy@DeployServer scripts]$ cat fetchFile.sh
#!/bin/bash
#
#copy filename from sit

usage(){
cat << EOF
Usage:
    $0 <file1 [file2 [ file3 ... ]]>
    $0 <list|help>

For example:
    $0 order-service-2.7.0.jar
EOF
}

re_cmd(){
    _host=$1
    _port=$2
    _cmd=$3
    #ssh -n -o StrictHostKeyChecking=no -p ${_port} feapp@${_host} "${_cmd}"
    ssh -n -o GSSAPIAuthentication=no -o StrictHostKeyChecking=no -p ${_port} fesit@${_host} "${_cmd}"
}

# config 
remote_dir=/app/project
local_dir=/app/deploy/tmp

if [ $# -lt 1 ] ; then
    usage
    exit 1
fi

if [ $1 = "list" ] ;then
   #re_cmd 10.200.130.17 2222 "cd /app/project; ls -l |grep -E '\.(jar|zip|war)$'" 
   re_cmd 10.200.130.17 52222 "cd /app/project; ls -l |grep -E '\.(jar|zip|war)$'" 
   exit 0
elif [ $1 = "help" ] ;then
   usage
   exit 0
fi

for fileName in $*
do
    #scp  feapp@202.104.112.157:/app/project/$fileName /app/backup/ 
    #scp -P 52222 fesit@10.200.130.17:${remote_dir}/$fileName ${local_dir}
    scp -o GSSAPIAuthentication=no -o StrictHostKeyChecking=no -P 52222 fesit@10.200.130.17:${remote_dir}/$fileName ${local_dir}
done

  

3、check_micro_ver.sh

[appdeploy@DeployServer scripts]$ cat check_micro_ver.sh
#!/bin/bash
#


scripts_dir=`dirname $0`
if ! [ -f ${scripts_dir}/distribute.cnf ] ;then
    echo "config file is not exist."
    exit 1
fi

source ${scripts_dir}/distribute.cnf

re_cmd(){
    _host=$1
    _cmd=$2
    ssh -n -o StrictHostKeyChecking=no appdeploy@${_host} "${_cmd}"
}

s_status(){
__host=$1
__service=$2
res=`re_cmd ${__host} "ps -ef|grep -E \"${__service}.*\.jar\"|grep -v grep"|awk '{print $2"\t"$5"\t"$NF}'`
if [ -n "${res}" ] ;then
    echo -e "${__host}  ${res}"
else
    echo -e "${__host}  ${__service} is not running."
fi
}

usage(){
cat <<EOF
$0 [gray|prod|all|test|--help]
Example:
    $0 gray
    $0 --help
EOF
}

# check command
if [ $# -eq 0 ] ;then
    env='undefinded'
elif [ $# -eq 1 ] ;then
    case $1 in
        gray )
            env="gray" ;;
        prod )
            env="prod" ;;
        all )
            env="all" ;;
        test )
            env="test" ;;
        --help )
            usage
            exit 0 ;;
        * )
            usage 
            exit 1  ;;
    esac
else
    usage
    exit 1
fi

if [ "${env}" = "gray" ] || [ "${env}" = "prod" ] ||[ "${env}" = "test" ] ; then
    services=`echo -e "$config"|grep -vE "^[ \t]*(#|$)"|awk -v env=${env} -F"|" '$2~env{print $5}'|sort -u|tr "\n" " "`
    for service in ${services}
    do
        echo -e "Checking running status for ${service}..."
        echo -e "$config"|grep -vE "^[ \t]*(#|$)"|awk -v var=${service} -v env=${env} -F"|" '$2~env && $5==var{print $2,$3}'|sort -k1|while read line
        do
            env=`echo -e "${line}" |awk '{print $1}'`
            host=`echo -e "${line}" |awk '{print $2}'`
            out=`s_status ${host} ${service}`
            echo -e "${env}\t${out}"
        done
    done
    exit
elif [ "${env}" = "all" ] ;then
    services=`echo -e "$config"|grep -vE "^[ \t]*(#|$)"|awk -F"|" '{print $5}'|sort -u|tr "\n" " "`
    for service in ${services}
    do
        echo -e "Checking running status for ${service}..."
        echo -e "$config"|grep -vE "^[ \t]*(#|$)"|awk -v var=${service} -F"|" '$5==var{print $2,$3}'|sort -k1|while read line
        do
            env=`echo -e "${line}" |awk '{print $1}'`
            host=`echo -e "${line}" |awk '{print $2}'`
            out=`s_status ${host} ${service}`
            echo -e "${env}\t${out}"
        done
    done
    exit
fi



services=`echo -e "$config"|grep -vE "^[ \t]*(#|$)"|awk -F"|" '{print $5}'|sort -u|tr "\n" " "`

while :
do
    select service in ${services}
    do
        echo -e "Checking running status for ${service}..."
        echo -e "$config"|grep -vE "^[ \t]*(#|$)"|awk -v var=${service} -F"|" '$5==var{print $2,$3}'|sort -k1|while read line
        do
            env=`echo -e "${line}" |awk '{print $1}'`
            host=`echo -e "${line}" |awk '{print $2}'`
            out=`s_status ${host} ${service}`
            echo -e "${env}\t${out}"
        done
    break
    done
        read -n1 -p "press Key \"q\" to exit or any other Key to continue:" k
        echo ""
        if [ -z "${k}" ] ; then
            continue
        elif [ "${k}" = "q" ] || [ "${k}" = "Q" ] ; then
            break
        else
            continue
        fi
done

  

 

posted @ 2019-02-28 14:03  Gringer  阅读(152)  评论(0)    收藏  举报