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

复制代码
#!/usr/bin/bash
## @date: 2024-09-09
## 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)}')
相关推荐
两点王爷3 小时前
docker 运行自定义化的服务-后端
运维·docker·容器
邪恶的贝利亚4 小时前
FFMEPG常见命令查询
linux·运维·网络·ffmpeg
搜搜秀4 小时前
find指令中使用正则表达式
linux·运维·服务器·正则表达式·bash
弧襪5 小时前
Ubuntu vs CentOS:Shell 环境加载机制差异分析
linux·ubuntu·centos
七七powerful5 小时前
使用opentelemetry 可观测监控springboot应用的指标、链路实践,使用zipkin展示链路追踪数据,使用grafana展示指标
运维
Archie_IT6 小时前
修图自由!自建IOPaint服务器,手机平板随时随地远程调用在线P图
运维·服务器·前端·git·深度学习·npm·conda
行思理6 小时前
centos crontab 设置定时任务访问链接
linux·运维·centos
阳光明媚大男孩6 小时前
24.0.2 双系统ubuntu 安装显卡驱动黑屏,系统启动界面键盘失灵
linux·ubuntu·计算机外设
再玩一会儿看代码6 小时前
[特殊字符] 深入理解 WSL2:在 Windows 上运行 Linux 的极致方案
linux·运维·windows·经验分享·笔记·学习方法
有谁看见我的剑了?7 小时前
centos7.9 升级 gcc
linux