3.系统状态监控

时间:2021-09-06
本文章向大家介绍3.系统状态监控,主要包括3.系统状态监控使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
#!/bin/bash
#获取ip地址
#ip=` ifconfig | grep inet | grep -vE 'inet6|127.0.0.1' | awk '{print $2}'     
#获取系统总核数
#cpu_num=`grep -c 'model name' /proc/cpuinfo`
#cpu_num=grep -c 'cpu cores' /proc/cpuinfo 
#获取当前时间
now=`date -u -d"+8 hour" +'%Y-%m-%d %H:%M:%S'`
#cpt使用阈值
cpu_warn='75'
#mem空闲阈值
mem_warn='100'
#disk使用阈值
disk_warn='90'
#------cpu
function item_cpu(){
    cpu_idle=` top -b -n 1 | grep Cpu |awk {'print $8'}|cut -f 1 -d "."`
    #cpu使用率
    cpu_use=`expr 100 - $cpu_idle`
    echo "$now 当前的cpu使用率为 $cpu_use" >> /linuxTest/cpu_$(date +'%Y-%m-%d').log
    if [[ $cpu_use -gt $cpu_warn ]]; then
        echo "cpu报警" >> /linuxTest/cpu.log  #这里的文件类型要写成绝对路径,要不然定时任务会不生效
    else
        echo "cpu使用正常" >> /linuxTest/cpu.log
    fi
}

#----mem内存
function item_mem(){
    mem_free=`free -m |grep "Mem"| awk {'print $4+$6'}`
    echo "$now 当前内存剩余空间为 $mem_freeMB" >> /linuxTest/mem.log
    if [[ $mem_free -lt $mem_warn  ]]; then
        echo "mem报警" >> /linuxTest/mem.log
    else
        echo "mem使用正常" >> /linuxTest/mem.log
    fi

}


#----disk磁盘
function item_disk(){
    disk_use=`df -P | grep /dev | grep -v -E '(tmp|boot)' | awk '{print $5}' | cut -f 1 -d "%"`
    echo "$now 当前磁盘使用率为 $disk_use %" >> /linuxTest/disk.log
    if [[ $disk_use -gt $disk_warn ]]; then
        echo "disk报警" >> /linuxTest/disk.log
    else
        echo "disk使用正常" >> /linuxTest/disk.log
    
    fi

}

item_cpu
item_disk
item_mem

原文地址:https://www.cnblogs.com/zmc60/p/15232670.html