加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

shell脚本监控内嵌linux系统业务、CPU、内存等

发布时间:2020-12-15 07:06:50 所属栏目:安全 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 定制板卡中bash无法使用,使用标准sh完成对系统及业务监控 #! /bin/sh #Process_Name="/guard /aps /sa /zyguard"Process_Name="/mp /mcu"#获取主机IP

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

定制板卡中bash无法使用,使用标准sh完成对系统及业务监控
#! /bin/sh
 
#Process_Name="/guard /aps /sa /zyguard"
Process_Name="/mp /mcu"
#获取主机IP地址
ip=`ifconfig eth0 |grep "inet addr"| cut -f 2 -d ":"|cut -f 1 -d " "`
gstr="/guard"
space=" "
First_Process=""
 
#获取初始业务进程号
for i in $Process_Name
do
    if [[ $i == $gstr ]]
    then
        Bf_Process=`ps |grep $i|grep -v grep|grep -v '/bin/bash'|awk '{print $1}'`
        First_Process=$First_Process$i":"$Bf_Process$space
    else
        Bf_Process=`ps |grep $i|grep -v grep|awk '{print $1}'`
        First_Process=$First_Process$i":"$Bf_Process$space
    fi
done
 
while true
do
    For_num=1
    sleep 3
    Second_Process=""
    #第二次获取业务进程号
    for i in $Process_Name
    do
        if [[ $i == $gstr ]]
        then
            Bs_Process=`ps |grep $i|grep -v grep|grep -v '/bin/bash'|awk '{print $1}'`
            Second_Process=$Second_Process$i":"$Bs_Process$space
        else
            Bs_Process=`ps |grep $i|grep -v grep|awk '{print $1}'`
            Second_Process=$Second_Process$i":"$Bs_Process$space
        fi
    done
    echo "First_Process:"$First_Process
    echo "Second_Process:"$Second_Process
    #查看所有业务进程有无变化,有变化输出到日志文件
    for j in $Process_Name
    do
        echo $j
        One_f_Process=`echo $First_Process|awk -v t="${For_num}" '{print $t}'`
        One_s_Process=`echo $Second_Process|awk -v t="${For_num}" '{print $t}'`
        For_num=$((For_num+1))
        echo "One_f_Process:"$One_f_Process
        echo "One_s_Process:"$One_s_Process
        if [[ "$One_f_Process" != "$One_s_Process" ]]
        then
            Time_Now=`date`
            echo $Time_Now
            echo $j
            echo $One_s_Process
            echo $One_f_Process
            echo -e $Time_Now"t"$j"tCollapsetThe new process:"$One_s_Process"ttThe old process:"$One_f_Process >> /usr/tmp/$ip'_collapse'.txt
        fi
    done
    #获取业务进程号
    First_Process=""
    for i in $Process_Name
    do
        if [[ $i == $gstr ]]
        then
            Bf_Process=`ps |grep $i|grep -v grep|grep -v '/bin/bash'|awk '{print $1}'`
            First_Process=$First_Process$i":"$Bf_Process$space
        else
            Bf_Process=`ps |grep $i|grep -v grep|awk '{print $1}'`
            First_Process=$First_Process$i":"$Bf_Process$space
        fi
    done
     
    #系统内存使用率监控
    Memory_Use_Rate=`free | grep Mem | awk '{printf"%d",$3/$2*100}'`
    if [ 80 -le $Memory_Use_Rate ]
    then
        Time_Now=`date`
        echo -e $Time_Now"tSystm MemorytMemory_Use_RatetAt present the use value:"$Memory_Use_Rate"%tThreshold:80%" >> /usr/tmp/$ip'_alarm'.txt
    fi
     
    #系统CPU剩余率监控
    Cpu_Residual_Rate=`mpstat|grep all|awk '{printf"%d",$11}'`
    echo $Cpu_Residual_Rate
    if [ $Cpu_Residual_Rate -le 20 ]
    then
        Time_Now=`date`
        echo $Time_Now
        echo -e $Time_Now"tSystm CputCpu_Residual_Rate(%idle)tAt present the use value:"$Cpu_Residual_Rate"%tThreshold:20%" >> /usr/tmp/$ip'_alarm'.txt
    fi 
     
    #业务内存使用率监控
    System_Memory=`free | grep Mem | awk '{printf"%d",$2/1024}'`
    for k in $Process_Name
    do
        if [[ $k == $gstr ]]
        then
            Process_Memory_Use=`ps |grep $k|grep -v grep|grep -v '/bin/bash'|awk '{print $3}'`
            var_length=${#Process_Memory_Use}
            var_Position=`expr $var_length - 1`
            m=${Process_Memory_Use:$var_Position:1}
            if [[ $m == "m" ]]
            then
                Pro_Mem_Use=${Process_Memory_Use:0:$var_Position}   
            else
                Pro_Mem_Use=`expr $Process_Memory_Use / 1024`
            fi
            System_Memory_Threshold=`free | grep Mem | awk '{printf"%d",$2/1024*0.8}'`
            if [[ $Pro_Mem_Use  -ge $System_Memory_Threshold ]]
            then
                Time_Now=`date`
                echo -e $Time_Now"t"$k"tProcess_Memory_UsetAt present the use value:"$Pro_Mem_Use"MbtThreshold:"$System_Memory_Threshold"Mb" >> /usr/tmp/$ip'_alarm'.txt
            fi
        else
            Process_Memory_Use=`ps |grep $k|grep -v grep|awk '{print $3}'`
            var_length=${#Process_Memory_Use}
            var_Position=`expr $var_length - 1`
            m=${Process_Memory_Use:$var_Position:1}
            if [[ $m == "m" ]]
            then
                Pro_Mem_Use=${Process_Memory_Use:0:$var_Position}   
            else
                Pro_Mem_Use=`expr $Process_Memory_Use / 1024`
            fi
            System_Memory_Threshold=`free | grep Mem | awk '{printf"%d",$2/1024*0.8}'`
            if [[ $Pro_Mem_Use  -ge $System_Memory_Threshold ]]
            then
                Time_Now=`date`
                echo -e $Time_Now"t"$k"tProcess_Memory_UsetAt present the use value:"$Pro_Mem_Use"MbtThreshold:"$System_Memory_Threshold"Mb" >> /usr/tmp/$ip'_alarm'.txt
            fi
        fi
    done
done

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读