Linux下万能巡检脚本 获取远程主机内存 cpu磁盘等信息

Posted 河静CTO

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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" 网卡名称是:$i ip4地址是:$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" 网卡名称是:$i ip6地址是:$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" 网卡名称是:$i mac地址是:$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下万能巡检脚本



以上是关于Linux下万能巡检脚本 获取远程主机内存 cpu磁盘等信息的主要内容,如果未能解决你的问题,请参考以下文章

日常巡检脚本

java能远程监控linux主机的cpu和内存使用情况么

windows上配置rsync服务器收集linux主机巡检报告

linux服务器巡检脚本

Java 连接远程Linux 服务器执行 shell 脚本查看 CPU内存硬盘信息

Java 连接远程Linux 服务器执行 shell 脚本查看 CPU内存硬盘信息