Linux下脚本实战之系统监控

Linux下脚本实战之系统监控

  • 一、脚本要求
  • 二、脚本内容
  • 三、运行脚本

一、脚本要求

1.监控系统的CPU、内存、硬盘使用率

二、脚本内容

#!/bin/bash
##########################################################
#File Name:monitor.sh
#Version:V1.0
#Aurhor:
#Emali:
#Created Time:2021-05-03 12:45:32
#Description:
##########################################################


#! /bin/bash
DATETIME=$(date +%F" "%H%M)
IP=$(ifconfig bond0 |awk '/netmask/ {print $2}')
which vmstat &> /dev/null
if [ ! $? = 0 ];then
echo "vmstat command no found,please install the package!"
exit 10
elif [  $? = 0 ] ; then



## mem the state
TOTAL=$(free -m |awk '/Mem/ {print $2}')
USED=$(free -m |awk '/Mem/ {print $3}')
FREE=$(free -m |awk '/Mem/ {print $4+$6}')
if [ $FREE -lt 1024 ];then
echo "
       Date: $DATETIME
       Hosts: $IP
       Problem: Total=$TOTAL,USE=$USED,Free=$FREE"

fi

## the cpu state
US=$(vmstat |awk 'NR==3 {print $13}')
SY=$(vmstat |awk 'NR==3 {print $14}')
IDLE=$(vmstat |awk 'NR==3 {print $15}')
WAIT=$(vmstat |awk 'NR==3 {print $15}')
USE=$(($US+$SY))
if [ $USE -ge 50 ];then
    echo "
    Date: $DATETIME
    Host: $IP
    Problem: CPU utilization $USE"
fi

#disk the state
PART_USE=$(df -h |awk -F'[% ]+' 'BEGIN{OFS="="} /^\/dev/ {print $1,$2,$5,$6}')
for i in $PART_USE; do
    PART=$(echo $i |cut -d"=" -f1)
    TOTAL=$(echo $i |cut -d "=" -f2)
    USE=$(echo $i |cut -d"=" -f3)
    MOUNT=$(echo $i |cut -d"=" -f4)
    if [ $USE -gt 80 ]; then
        echo "
        Date: $DATETIME
        Host: $IP
        Total: $TOTAL
        Problem: $PART=$USE($MOUNT)"
    fi
done
else
                 echo "  the system is OK! "

fi

三、运行脚本

[root@control scripts]# ./monitor.sh 
  the system is OK! 

你可能感兴趣的:(Linux,shell,linux,bash,服务器)