常用故障排查监控shell脚本

#!/bin/bash

#ping_monitor.sh


IP_ADDRESS=$1
    if [  -n "$IP_ADDRESS" ] ; then

        while :
        do
                PING_OK=`ping -c 1 -W 2  $IP_ADDRESS  | grep "time=" `
                if [ 0 -eq $? ]; then
                        echo "$PING_OK $TIME_ `date`" >> PING_${IP_ADDRESS}_OK.log
                else
                        echo "PING $IP_ADDRESS 2seconds TMOUT...  `date`"  >> PING_${IP_ADDRESS}_TMOUT.log
                fi

                sleep 1
        done
    else
        echo "用法:$0 <IP or 域名>"
        echo "监控日志请到当前目录下获取" 
    fi

脚本执行花费时间毫秒级别

#!/bin/bash
#计算脚本执行时间

#开始 纳秒时间戳
start=$(date +%s%N)
echo 'start nanosecond:' $start

#脚本内容
sleep 0.001;

#结束 纳秒时间戳
end=$(date +%s%N)
echo 'end   nanosecond:' $end

#计算时间差毫秒 需要除以1000000
take=$(( (end - start) /1000000 ))

#输出计算结果
echo Time taken to execute commands is ${take} millisecond .

 tcp端口持续监测

#!/bin/bash
# 持续监测端口监听状态
#tcping 二进制文件:https://ks3-cn-beijing.ksyun.com/zhangmingda/tcping
GUEST_IP=$1
GUEST_PORT=$2
LOGFILE=$1_TcpPort$2_monitor.log

if [  -n "$GUEST_IP" ] && [  -n "$GUEST_PORT" ]; then
    while :
        do
        RESO=`tcping -t 2  $GUEST_IP $GUEST_PORT`
        # -t 超时时间S
        if [ $? -eq 0 ]; then
            sleep 1
        fi
        echo `date` $RESO     >> $LOGFILE
    done
else
    echo "用法:$0 <IP or 域名> <端口>"
    echo "监控日志请到当前目录下获取" 
fi

curl 持续监控返回值

#!/bin/bash 
#
#Author:zhangmingda
#date:20191021
#use:持续监控https/http连接请求状态
#########################################################
logfile='curl_monitor.log'
if [ ! -f ${logfile}  ];then
    touch $logfile
fi  #日志文件
echo;echo "curl_log  result from  $1 " |tee -a ${logfile}
#########################################################
echo '                              DNS_OK: TCP_OK: DATA_START: TOTAL_TIME: http_code:'  | tee -a ${logfile}

while true  ;
do 
    tid="$(date '+%F %H:%M:%S')" ; 
    url=$1 ;
    curl -m 3 -4 -o /dev/null -s -w "curl_tid:${tid}   %{time_namelookup}    %{time_connect}    %{time_starttransfer}     %{time_total}     code:%{h
ttp_code} 
"     ${url}   | tee -a  $logfile   ;   
    sleep 1;  
done

#!/bin/bash
#
#Author:zhangmingda
#date:2019-12-30
#use:持续监控https/http连接请求状态
#########################################################
logfile='curl_monitor.log'
if [ ! -f ${logfile} ];then
touch $logfile
fi #日志文件
echo;echo "curl_log result from $1 " |tee -a ${logfile}
#########################################################
echo ' DNS_OK: TCP_OK: DATA_START: TOTAL_TIME: http_code:' | tee -a ${logfile}

while true ;
do
tid="$(date '+%F %H:%M:%S')" ;
url=$1 ;
curl -m 3 -4 -o /dev/null -s -w "curl_tid:${tid} %{time_namelookup} %{time_connect} %{time_starttransfer} %{time_total}
code:%{http_code} http_connect:%{http_connect} remote_tcp/ip:%{remote_ip}:%{remote_port} url_effective:%{url_effective}
speed_download:%{speed_download} time_redirect:%{time_redirect} "
${url} | tee -a $logfile ;
sleep 1;
done

原文地址:https://www.cnblogs.com/zhangmingda/p/12020914.html