#!/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