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)}')
相关推荐
DianSan_ERP3 小时前
电商架构演进:如何在高并发场景下实现多平台API的标准化履约?
运维·前端·网络·安全·架构·自动化
恒5393 小时前
Linux文件系统I
linux·运维·服务器
阿成学长_Cain3 小时前
Linux ipcs 命令超全详解:查看共享内存 / 消息队列 / 信号量,IPC 运维必备
linux·运维·服务器·网络·数据库
黯然神伤8883 小时前
AlmaLinux设置软件下载源
linux·alma
青瓦梦滋4 小时前
协议定制/序列化-反序列化(Linux视角)
linux·服务器·网络·c++
运维老郭6 小时前
【K8s运维实战】Kubernetes临时存储卷实战:emptyDir核心用法与gitRepo弃用迁移指南
运维·云原生·kubernetes
珠海西格电力6 小时前
云边端协同架构:零碳园区管理系统的技术底座
大数据·运维·人工智能·算法·架构·能源
阿成学长_Cain7 小时前
Linux dirs命令详解|Bash目录堆栈管理快速切换目录实战教程
linux·运维·前端·数据库
小羊Yveesss7 小时前
2026年微信小程序后端怎么搭建?后台、数据、接口和运维怎么选
运维·微信小程序·小程序
ICECREAM8 小时前
Linux 进程间通讯(IPC)——总结
linux