#!/bin/bash
function cpu() {
printf "%-13s %-10s\n" "使用率" "等待磁盘IO的时间百分比"
printf "%-10s %-10s\n" "------" "----------------------"
util=`vmstat |awk '{if(NR==3)print 100-$15}'`
#user=`vmstat |awk '{if(NR==3)print $13"%"}'`
#sys=`vmstat |awk '{if(NR==3)print $14"%"}'`
iowait=`vmstat |awk '{if(NR==3)print $16}'`
printf "%-10s %-10s\n" "${util}%" "${iowait}%"
}
function memory() {
printf "%-13s %-12s %-10s\n" "总大小" "使用" "剩余"
printf "%-10s %-10s %-10s\n" "------" "------" "------"
total=`free -m |awk '{if(NR==2)printf "%.1f",$2/1024}'`
used=`free -m |awk '{if(NR==2) printf "%.1f",($2-$NF)/1024}'`
available=`free -m |awk '{if(NR==2) printf "%.1f",$NF/1024}'`
printf "%-10s %-10s %-10s\n" "${total}G" "${used}G" "${available}G"
}
function disk() {
printf "%-13s %-13s %-12s %-10s\n" "挂载点" "总大小" "使用" "使用率"
printf "%-10s %-10s %-10s %-10s\n" "------" "------" "------" "------"
fs=`df -h |awk '/^\/dev/{print $1}'`
for p in $fs; do
mounted=`df -h |awk '$1=="'$p'"{print $NF}'`
size=`df -h |awk '$1=="'$p'"{print $2}'`
used=`df -h |awk '$1=="'$p'"{print $3}'`
used_percent=`df -h |awk '$1=="'$p'"{print $5}'`
printf "%-10s %-10s %-10s %-10s\n" "$mounted" "$size" "$used" "$used_percent"
done
}
function process_cpu_top() {
printf "%-10s %-10s %-12s %-10s\n" "PID" "CPU" "内存" "命令"
printf "%-10s %-10s %-10s %-10s\n" "------" "------" "------" "------"
tmp_file=/tmp/cputop
ps -eo pid,pcpu,pmem,command --sort=-pcpu |awk 'NR>1&&NR<=4' > $tmp_file
while read line; do
pid=`echo $line |awk '{print $1}'`
cpu=`echo $line |awk '{print $2}'`
mem=`echo $line |awk '{print $3}'`
com=`echo $line |awk '{print $4}'`
printf "%-10s %-10s %-10s %-10s\n" "$pid" "${cpu}%" "${mem}%" "$com"
done < $tmp_file
}
function process_memory_top() {
printf "%-10s %-12s %-10s %-10s\n" "PID" "内存" "CPU" "命令"
printf "%-10s %-10s %-10s %-10s\n" "------" "------" "------" "------"
tmp_file=/tmp/cputop
ps -eo pid,pcpu,pmem,command --sort=-pmem |awk 'NR>1&&NR<=4' > $tmp_file
while read line; do
pid=`echo $line |awk '{print $1}'`
cpu=`echo $line |awk '{print $2}'`
mem=`echo $line |awk '{print $3}'`
com=`echo $line |awk '{print $4}'`
printf "%-10s %-10s %-10s %-10s\n" "$pid" "${mem}%" "${cpu}%" "$com"
done < $tmp_file
}
echo "################# CPU #################"
cpu
echo "################# 内存 #################"
memory
echo "################# 硬盘 #################"
disk
echo "################# 占用CPU最高的前三个进程 #################"
process_cpu_top
echo "################# 占用内存最高的前三个进程 #################"
process_memory_top
linux主机巡检shell脚本
原创
©著作权归作者所有:来自51CTO博客作者肖咏卓的原创作品,请联系作者获取转载授权,否则将追究法律责任
上一篇:pyhon3-psutil模块
下一篇:python3巡检系统生成表格
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
shell巡检脚本
shell巡检脚本
shell巡检脚本 -
nacos巡检shell脚本
nacos健康巡检shell脚本
nacos nacos巡检 nacos检查 -
Linux巡检脚本
做运维的朋友都知道,大家经常要对一些系统做月度维护或者自查,在这个过程中,如果只是小工作量的查询,进入系统直接输入命令即可。但是如果说要
linux 服务器 运维 sed Ethernet -
Linux—编写shell脚本监控主机Linux自带邮件使用等 Linux脚本监控主机 Linux Shell主机监控 LINUX主机日常监控 linux shell脚本
-
linux系统巡检脚本
巡检脚本
巡检脚本