Shell脚本监控Centos 7系统运行状态

#!/usr/bin/bash
## @date: 2021-08-17
## This is a script for security operation indicator monitoring!

export LANG="zh_CN.UTF-8"

##当前系统时间

DATE_time=$(date -d "0 day" +%Y年%m月%d日%H时%M分%S秒)

##服务器IP地址

IP_addr=$(ifconfig eth0 | grep inet | grep -v inet6 | awk '{print $2}')

##CPU占用率

CPU=100.0
CPU_free=$(top -b -n 1 | grep Cpu | awk '{print $8}')
CPU_used=$(awk 'BEGIN{printf "%.1f%\n",'$CPU' - '$CPU_free'}')
sleep 2
##内存使用率

RAM_total=$(free -m | grep Mem | awk '{print $2}')
RAM_used=$(free -m | grep Mem | awk '{print $3}')
RAM_percent=$(awk 'BEGIN{printf "%.1f%\n",('$RAM_used'/'$RAM_total')*100}')
sleep 2

##磁盘使用率

Disk_totalSpace=$(df -m | sed '1d;/ /!N;s/\n//;s/ \+/ /;' | awk '{totalSpace+=$2} END{print totalSpace}')
Disk_used=$(df -m | sed '1d;/ /!N;s/\n//;s/ \+/ /;' | awk '{used+=$3} END{print used}')
Disk_percent=$(awk 'BEGIN{printf "%.1f%\n",('$Disk_used'/'$Disk_totalSpace')*100}')
sleep 2

##带宽使用率

##执行查看带宽命令,10秒内执行5次,取平均值

i=1
Receive_result=0.000
Transmit_result=0.000
while(($i<=5))
do
Receive_before=$(cat /proc/net/dev | grep 'eth' | tr : " " | awk '{print $2}')
Transmit_before=$(cat /proc/net/dev | grep 'eth' | tr : " " | awk '{print $10}')
sleep 2
Receive_after=$(cat /proc/net/dev | grep 'eth' | tr : " " | awk '{print $2}')
Transmit_after=$(cat /proc/net/dev | grep 'eth' | tr : " " | awk '{print $10}')

Receive_bandwidth=$(awk 'BEGIN{printf "%.3f\n",('$Receive_after'-'$Receive_before')/1024/1024/2*8}')
Transmit_bandwidth=$(awk 'BEGIN{printf "%.3f\n",('$Transmit_after'-'$Transmit_before')/1024/1024/2*8}')

Receive_result=$(awk 'BEGIN{printf "%.3f\n",('$Receive_result'+'$Receive_bandwidth')}')
Transmit_result=$(awk 'BEGIN{printf "%.3f\n",('$Transmit_result'+'$Transmit_bandwidth')}')

let "i++"
done

Receive_average=$(awk 'BEGIN{printf "%.3fMbps\n",('$Receive_result'/5)}')
Transmit_average=$(awk 'BEGIN{printf "%.3fMbps\n",('$Transmit_result'/5)}')
posted @ 2021-09-29 11:12  巴韭特  阅读(297)  评论(0编辑  收藏  举报