您的位置:首页 > 运维架构 > Linux

linux 服务器之间无密码传送文件

2014-10-31 14:21 671 查看
#!/bin/ksh

portalMSPort="8881"

localhostIP="172.21.11.97"

monitorProgramName=portalMS

monitorpath="/usr/local/portal/jboss5/bin/run.jar"

sysMonitorDataFile="/usr/local/portalMS/jboss5/bin/sysMonitorData.txt"

###监控进程类型:0:java 1:c

monitorType=0

getCPUUseRatio()

{

    times=5

    cpuIdleInfo=`top -b -n $times | grep 'Cpu(s)' | awk -F' ' '{print $5}'`;

    

    i=0;

    total=0;

    for eachInfo in $cpuIdleInfo

    do

        i=`expr $i + 1`;

        

        #first rows not get

        if [ "X$i" == "X1" ]; then

            continue;

        fi;

        eachIdle=`echo $eachInfo | cut -d"%" -f1`;

        flag=`echo $eachInfo | cut -d"%" -f2`;

        if [ "X$flag" == "Xid," ]; then

            total=`echo $total + $eachIdle | bc`;

        fi;

        

    done

    ruleTimes=`expr $times - 1`;

    totalIdle=`echo $total / $ruleTimes | bc`;

    echo `echo 100.00 - $totalIdle | bc`;

}

getNetUseRatio()

{

    monitorEth=""

    monitorEths=`ifconfig  |grep 'Link encap:'|awk  '{print $1}'`

    for tmpMonitorEth in $monitorEths

    do

        tmpIP="`/sbin/ifconfig|sed -n 'N;/^'$tmpMonitorEth'/p' |grep 'inet addr:'|awk -F: '{print $2}'|awk  '{print $1}'`"

        if [ "X$localhostIP" == "X$tmpIP" ]

        then

            monitorEth=$tmpMonitorEth

        fi

    done

    ethBandWidth="`ethtool ${monitorEth}|grep Speed|awk '{print $2}'|sed 's/[^0-9]//g'`"

    RXpre=$(ifconfig ${monitorEth} | grep bytes | awk '{print $2}'| awk -F":" '{print $2}')

    TXpre=$(ifconfig ${monitorEth} | grep bytes | awk '{print $6}' | awk -F":" '{print $2}')

    #统计10秒总流量

    sleep 10

    RXnext=$(ifconfig ${monitorEth} | grep bytes | awk '{print $2}'| awk -F":" '{print $2}')

    TXnext=$(ifconfig ${monitorEth} | grep bytes | awk '{print $6}' | awk -F":" '{print $2}')

    Totaltrans=$(((${RXnext}-${RXpre}+${TXnext}-${TXpre})*8))

    

    NetUseRatio=`echo "scale=4;${Totaltrans}*10/1024/1024/${ethBandWidth}"|bc`

    echo $NetUseRatio

}

getIOUseRatio()

{

    #iosta取5次统计,去掉第一个

    #统计次数

    statCount=5

    v_ioutils=`iostat -d -x 1 $statCount|grep -v "Device:\|Linux"| awk '{print $NF}'`

    i=0;

    total=`echo $v_ioutils|wc -w `

    tmputil=0.0

    

    #去掉iostat命令第一次取的数据个数

    count=`echo $total/$statCount + 1|bc`

    

    for v_ioutil in $v_ioutils

        do

            i=`expr $i + 1`;

            if [ 1 == `echo "$i < $count"|bc` ];then

                continue;

            fi

            if [ 1 == `echo "$v_ioutil > $tmputil"|bc` ]; then

                tmputil=$v_ioutil

            fi

        done

    echo $tmputil

}

getMemUseRatio()

{

    memUseRatio=0

    if [ "$monitorType" == "1" ] ;then

        total=`free -m|grep "Mem:"|awk '{print $2}'`

        used=`free -m|grep "Mem:"|awk '{print $3}'`

        memUseRatio=`echo "scale=4;${used}*100/$total"|bc`

    else

        pid=`ps -ef |grep java|grep ${monitorpath}|awk '{print $2}'`

        maxMem=`jmap -heap ${pid}| grep   MaxHeapSize| awk '{print $3}'`    

        memUsedData=`jmap -heap ${pid}| grep "% used"`

        memUseRatio=`echo $memUsedData|awk -F"%" '{print $5}'|awk '{print $2}'`

        memUseRatio=`echo "scale=4;${memUseRatio}/1"|bc`

    fi

    echo $memUseRatio

}

cpu_useratio=`getCPUUseRatio`

net_useratio=`getNetUseRatio`

io_useratio=`getIOUseRatio`

mem_useratio=`getMemUseRatio`

echo "`hostname`|${monitorProgramName}|"`date "+%Y-%m-%d %T"`"|${cpu_useratio}|${mem_useratio}|${io_useratio}|${net_useratio}" >> ${sysMonitorDataFile}

#set connect timeout 5s

clearResult=`curl --connect-timeout 5 "http://127.0.0.1:"${portalMSPort}"/sys/sysMonitoerDataGratherAction"\!"saveSysMonitoerData.do?sysMonitorDataFile=${sysMonitorDataFile}" 2> /dev/null`

if [ "000000" == "${clearResult}" ]; then

    echo "[INFO]: save system monitoer data success."

else

    resultMsg=`echo ${clearResult} | awk -F= '{print $2}'`

    if [ -z "${resultMsg}" ]; then

        echo "[ERROR]: save system monitoer data fail."

    else

        echo "[ERROR]: ${resultMsg}"

    fi

fi
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: