后台状态监测shell脚本

运行环境:ubuntu

运行目的:定时获取同ubuntu连接的网络设备的连接状态,我的需求是每分钟统计一次,这个时间段可在脚本中调整。然后每日上午10:00出过去24小时的统计结果。

#!/bin/bash

TMP_FILE=$PWD/tmp
LOG_DIR=/export/tftp
STAT_FILE=$PWD/stat_now
STAT_CACHE=$PWD/stat_cache
BASE_ONLINE_COUNT=0
REAL_ONLINE_COUNT=0
COUNT_SUM=0
DIFF=$PWD/diff
CHANGE_CACHE=$PWD/change_cache
CHANGE_TMP=$PWD/change_tmp
RESULT_TEMP=$PWD/result
REBOOT_COUNTS=0

function clear_state()
{
    COUNT_SUM=0
    BASE_ONLINE_COUNT=0
    REAL_ONLINE_COUNT=0

    if [ -f $CHANGE_CACHE ]; then
        rm $CHANGE_CACHE
    fi
}

function vm_state_handler()
{
#    echo "vm_state_handler"
    date=`date +%Y%m%d%H%M%S`
    echo "$date" >> $RESULT_TEMP
    echo "COUNT_SUM: $COUNT_SUM" >> $RESULT_TEMP
    echo "BASE_ONLINE_COUNT: $BASE_ONLINE_COUNT" >> $RESULT_TEMP
    echo "REAL_ONLINE_COUNT: $REAL_ONLINE_COUNT" >> $RESULT_TEMP

    if [ -f $CHANGE_CACHE ]; then
        awk '{a[$1]++}END{for(i in a){print i,a[i] | "sort -r -k 2"}}' $CHANGE_CACHE >> $RESULT_TEMP
    fi

    ONLINE_PERCENT=`echo "scale=4;$REAL_ONLINE_COUNT/$BASE_ONLINE_COUNT" | bc`
    SRC1=`echo $ONLINE_PERCENT | cut -b2-3`
    SRC2=`echo $ONLINE_PERCENT | cut -b4-5`
    echo "ONLINE_PERCENT: $SRC1.$SRC2%" >> $RESULT_TEMP

    REBOOT_COUNTS=`find /export/tftp -name '*.log' -type f -mtime -1 | wc -l`
    echo "REBOOT_COUNTS: $REBOOT_COUNTS" >> $RESULT_TEMP

    mv $RESULT_TEMP $date.result

    clear_state
}

function state_change_handler()
{
    if [ -f $STAT_CACHE ]; then
        grep -vwf $STAT_CACHE $STAT_FILE > $DIFF
        if [ -s "$DIFF" ]; then
            for line in `cat $DIFF`
            do
                mac=`echo $line | cut -b24-35`
                echo "$mac" >> $CHANGE_CACHE
            done
            cat $CHANGE_CACHE | tr -s '\n' > $CHANGE_TMP
            mv $CHANGE_TMP $CHANGE_CACHE
            mv $STAT_FILE $STAT_CACHE
        fi
    else
        mv $STAT_FILE $STAT_CACHE
    fi
}

function cache_vm_state()
{
#    echo "cache_vn_state"
    timeout 2 mosquitto_sub -u php -P phpmq@123! -h localhost -p 1883 -t '$SYS/broker/connection/#' -v > $TMP_FILE

    if [ -f $TMP_FILE ]; then
        awk '!a[$1]++' $TMP_FILE > $STAT_FILE
    fi

    if [ -f $STAT_FILE ]; then
        COUNT_SUM=$(($COUNT_SUM+1))
        BASE_ONLINE_COUNT=$[BASE_ONLINE_COUNT+`cat $STAT_FILE | cut -f4 -d "/" | grep "000000000" | wc -l`]
        REAL_ONLINE_COUNT=$[REAL_ONLINE_COUNT+`cat $STAT_FILE | cut -f2 -d " " | grep "1" | wc -l`]
        state_change_handler
    fi
    
}

function timer_tick()
{
    date=`date`
    time=`echo $date | cut -d " " -f4`
    hour=`echo $time | cut -d ":" -f1`
    min=`echo $time | cut -d ":" -f2`
    day=`date +%Y%m%d`

    day_result_file=`ls | grep $day`

#    if [ "$hour" = "10" ] && [ "$min" = "53" ]; then
    if [ "$hour" = "02" ] && [ "$min" -lt "02" ] && [ -z "$day_result_file" ]; then
        vm_state_handler
    fi
}

while true
do
    cache_vm_state

    sleep 60

    timer_tick
done

颜色标注行说明:

红色标注:统计$CHANGE_CACHE 文件中重复行的出现次数。

橙色标注:统计某目录近24小时的.log文件个数(这个尾缀的文件是内核异常重启日志文件,因此其个数标志了重启次数)。

绿色标注:去除$CHANGE_CACHE文件中,第一列相同的行,去重后只留下一行。

蓝色标注:与我司的具体业务相关的获取ubuntu挂接的网络设备状态online/offline。







你可能感兴趣的:(脚本)