相逢意气为君饮,系马高楼垂柳边。这篇文章主要讲述Linux下万能巡检脚本 获取远程主机内存 cpu磁盘等信息相关的知识,希望能为你提供帮助。
??脚本内容如下,可无偿定制,留言即可?
?
#!/bin/bash
#DATE: 2022/3/11
#FUNC: collect remote machine info
#AUTHOR:hejing
#1.获取每张网卡ipv4/ipv6/mac地址
#2.获取内核版本/系统安装方式
#3.获取磁盘/内存/CPU使用率
TIME=$(date "+%Y-%m-%d %H:%M:%S")
TIME_INTERVAL=5
NIC_NAME=(`ip a s | grep -E ^\\< [0-9]\\> : | awk -F: print $2 | awk gsub(/^\\s+|s+$/, ""); print`)
function collect_IPV4
for i in $NIC_NAME[@]
do
IP4_ADD=(`ip a s | grep -i $i | grep -i inet | awk print $2`)
printf "%-10s%-10s %-10s %-10s\\n" 网卡名称是:$iip4地址是:$IP4_ADD
done
function collect_IPV6
for i in $NIC_NAME[@]
do
IP6_ADD=(`ip a s | grep -A2 $i | grep -i inet6 | awk print $2 `)
printf "%-10s%-10s %-10s %-10s\\n" 网卡名称是:$iip6地址是:$IP6_ADD
done
function collect_MAC
for i in $NIC_NAME[@]
do
MAC_ADD=(`ip a s | grep -A1 $i | grep -i "link/ether" | awk print $2`)
printf "%-10s%-10s %-10s %-10s\\n" 网卡名称是:$imac地址是:$MAC_ADD
done
function collect_kernel
printf "%-10s\\n" 内核版本是:$(uname -r)
function collect_os_install
printf "%-10s\\n" 当前系统安装方式是:$(if [ `rpm -qa | wc -l` -le 1000]; then echo "最小化安装mini"; else echo "图形化安装graph"; fi)
function collect_disk_root_use
TOTAL_SPACE=100
USED_SPACE=$(df -k | grep -vE "文件系统|Filesystem" | grep -w "/" | awk print int($5))
USED_PERCENT=$(($TOTAL_SPACE-$USED_SPACE)).00%
echo "当前系统根目录可用率:$USED_PERCENT"
function collect_cal_cpu
#关于/proc/stat参考文档链接:https://man7.org/linux/man-pages/man5/proc.5.html
#根据/proc/stat文件获取并计算CPU使用率
#CPU时间计算公式:CPU_TIME=user+nice+system+idle+irq+softirq+iowait
#CPU使用率计算公式:CPU_USAGE=(idle2-idle1)/(cpu2-cpu1)@100
#默认时间间隔
LAST_CPU_INFO=$(cat /proc/stat | grep -w cpu | awk print $2,$3,$4,$5,$6,$7,$8)
LAST_SYS_IDLE=$(echo $LAST_CPU_INFO | awk print $4 )
LAST_TOTAL_CPU_TIME=$(echo $LAST_CPU_INFO | awk print $1+$2+$3+$4+$5+$6+$7 )
sleep $TIME_INTERVAL
NEXT_CPU_INFO=$(cat /proc/stat | grep -w cpu | awk print $2,$3,$4,$5,$6,$7,$8)
NEXT_SYS_IDLE=$(echo $NEXT_CPU_INFO | awk print $4 )
NEXT_TOTAL_CPU_TIME=$(echo $NEXT_CPU_INFO | awk print $1+$2+$3+$4+$5+$6+$7 )
#计算系统空闲时间
SYSTEM_IDLE=`echo $NEXT_SYS_IDLE $LAST_SYS_IDLE | awk print $1-$2`
TOTAL_TIME=`echo $NEXT_TOTAL_CPU_TIME $LAST_TOTAL_CPU_TIME | awk print $1-$2`
CPU_USAGE=`echo $SYSTEM_IDLE $TOTAL_TIME | awk printf "%.2f", 100-$1/$2*100`
echo "CPU使用率是:$CPU_USAGE%"
function collect_mem_use
TOTAL_MEM=$(free -m | sed -n 2p | awk print $2)
USED_MEM=$(free -m | sed -n 2p | awk print $3)
USED_PERCENT=$(echo $USED_MEM $TOTAL_MEM | awk printf "%.2f", $1/$2*100)
echo"当前系统总内存是:$TOTAL_MEMM,已使用内存是:$USED_MEMM,使用率达:$USED_PERCENT%"
echo "------------------------------------------------------------------------"
echo "------------------当前主机名:$HOSTNAME当前时间:$TIME------------------"
echo "------------------------------------------------------------------------"
collect_IPV4
collect_IPV6
collect_MAC
collect_kernel
collect_os_install
collect_disk_root_use
collect_cal_cpu
collect_mem_use
【Linux下万能巡检脚本 获取远程主机内存 cpu磁盘等信息】?
?执行后结果示例:?
?文章图片
推荐阅读
- Linux小技巧之终端窗口screen
- CentOS 结束支持了新BP发行版又出现了AlmaLinux
- 为什么我的Linux ls命令不能用了()
- [Linux用户空间编程-5](用IPTable实现NAT功能)
- 读书分享《树莓派开始,玩转Linux》读书分享
- [Linux用户空间编程-4](Linux虚拟网络设备TUN/TAP的工作原理与代码示例)
- Linux(shell)遍历目录删除指定文件,解决文件夹名称带空格问题
- Linux下软连接(link)和硬链接(hard)的区别
- RabbitMQ教程-在Linux上安装RabbitMQ报错解决方案