使用shell脚本查询主机cpu、内存、硬盘使用情况(显示时间,IP,使用情况等)。
2020-08-24 21:41
1661 查看
cpu:
代码:
#!/bin/bash
if [
uname!= “Linux” ];then
echo “操作系统不是Linux.”
exit 1
fi
which vmstat &>/dev/null
if [ $? -ne 0 ];then
echo “vmstat没装”
exit 1
fi
cpu_us=
vmstat | awk '{print $13}' | sed -n '$p'
cpu_sy=
vmstat | awk '{print $14}' | sed -n '$p'
cpu_id=
vmstat | awk '{print $15}' | sed -n '$p'
cpu_wa=
vmstat | awk '{print $16}' | sed -n '$p'
cpu_sum=((((((cpu_us+$cpu_sy))
cpu_info()
{
echo “CPU_Sum : cpusumcpu_sum% ( CPU_Use:cpusum{cpu_us}% , CPU_System:${cpu_sy}% )”
echo “CPU_Idle : ${cpu_id}%”
echo “CPU_Wait : ${cpu_wa}”
}
if [ cpusum−ge0];thendate=cpu_sum -ge 0 ];then date=cpusum−ge0];thendate=(date)
ip=
ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "ad dr:"
echo “CPU占用率为:cpusum日期:cpu_sum 日期:cpusum日期:date 主机IP: $ip” >>c.txt
内存:
代码:
#!/bin/bash
total=
free -m | grep Mem | awk '{print $2}'
use=
free -m | grep Mem | awk '{print $3}'
free=
free -m | grep Mem | awk '{print $4}'
cache=
free -m | grep Mem | awk '{print $6}'
date=$(date)
ip=
ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "ad dr:"
if [ free−lt1024];thenecho"时间:free -lt 1024 ];then echo "时间:free−lt1024];thenecho"时间:date,主机IP:ip,内存使用情况:总内存:ip,内存使用情况:总内存:ip,内存使用情况:总内存:[total]m,使用内存:[use]m,缓冲区使用内存:[use]m,缓冲区使用内存:[use]m,缓冲区使用内存:[cache]m空闲内存:$[free]m" >>b.txt
fi
硬盘:
#!/bin/bash
Date=$(date)
ip=
ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "ad dr:"
Total=
fdisk -l | grep "Disk /dev/sd[a-z]" |awk '{print $2$3"GB"}' |sed 's/:/=/' |xargs echo -n |sed 's/[ ]/,/g'
Disk_Use=
df -h |awk '{print $1"="$5}' | sed '1d' | sed 's/%//g'
for i in $Disk_Use
do
A=
echo $i |awk -F'=' '{print $2}'
if [ $A -gt 8 ];then
echo -e “日期 : $Date \nIP : $ip \n总量 : $Total \n部分使用 : ${i}%” >> a.txt
fi
done
shell初学,费了大半天劲,百度了半天,再加上上课的那点笔记慢慢憋出来,哎 cpu和内存的熟悉了,硬盘的还是有些不懂,还得努力啊。
相关文章推荐
- shell脚本应用《十》查看多个系统CPU,指定的进程CPU,主备机,内存使用情况
- shell脚本监控系统负载、CPU和内存使用情况
- shell脚本监控系统负载、CPU和内存使用情况
- shell脚本监控系统负载、CPU和内存使用情况
- shell脚本监控系统负载、CPU和内存使用情况
- shell脚本监控系统负载、CPU和内存使用情况
- shell脚本监控系统负载、CPU和内存使用情况
- Shell脚本获取系统运行信息 (信息包括:内存M,硬盘%,CPU%,负载情况,进程数,等)
- 编写shell脚本监控系统负载、CPU和内存使用情况
- 获取磁盘,内存,CPU使用情况的shell脚本
- 利用shell脚本来监控linux系统的负载与CPU和内存使用情况
- shell脚本监控系统负载、CPU和内存使用情况
- shell脚本监控系统负载、CPU和内存使用情况
- shell脚本:统计磁盘使用情况/IP访问量/内存
- shell脚本监控系统负载、CPU和内存使用情况
- 一个用了统计CPU 内存 硬盘 使用率的shell脚本
- 测试显示内存使用情况的JSP脚本
- C/C++获取Linux系统CPU和内存及硬盘使用情况
- 检测网段IP使用情况的shell脚本
- cacti监控主机磁盘,cpu,内存的使用情况