欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

获取服务器信息的Shell脚本分享(ubuntu、centos测试通过)

程序员文章站 2022-06-07 22:51:52
此脚本已经在ubuntu以及centos版本上测试通过,脚本如下: 复制代码 代码如下: #!/bin/bash line='===========' #linux...

此脚本已经在ubuntu以及centos版本上测试通过,脚本如下:

复制代码 代码如下:

#!/bin/bash

line='==========='

#linux发行版名称
if [[ -f /usr/bin/lsb_release ]]; then
os=$(/usr/bin/lsb_release -a |grep description |awk -f : '{print $2}' |sed 's/^[ \t]*//g')
else
os=$(cat /etc/issue |sed -n '1p')
fi

echo -e "${line}\nos:\n${os}\n${line}"

######################################################################################################

#查看系统是否为64位:uname -m,若出现x86_64,则为64位
os_version=$(uname -m)
echo -e "os_version:\n${os_version}\n${line}"


#系统内核版本
kernel_version=$(uname -r)
echo -e "kernel_version:\n${kernel_version}\n${line}"

#cpu型号
cpu=$(grep 'model name' /proc/cpuinfo |uniq |awk -f : '{print $2}' |sed 's/^[ \t]*//g' |sed 's/ \+/ /g')
echo -e "cpu model:\n${cpu}\n${line}"

#物理cpu个数
counts=$(grep 'physical id' /proc/cpuinfo |sort |uniq |wc -l)
echo -e "total of physical cpu:\n${counts}\n${line}"

#物理cpu内核数
cores=$(grep 'cpu cores' /proc/cpuinfo |uniq |awk -f : '{print $2}' |sed 's/^[ \t]*//g')
echo -e "number of cpu cores\n${cores}\n${line}"

#逻辑cpu个数
processor=$(grep 'processor' /proc/cpuinfo |sort |uniq |wc -l)
echo -e "number of logical cpus:\n${processor}\n${line}"

#查看cpu当前运行模式是64位还是32位
mode=$(getconf long_bit)
echo -e "present mode of cpu:\n${mode}\n${line}"


#查看cpu是否支持64位技术:grep 'flags' /proc/cpuinfo,若flags信息中包含lm字段,则支持64位
numbers=$(grep 'lm' /proc/cpuinfo |wc -l)
if (( ${numbers} > 0)); then lm=64
else lm=32
fi
echo -e "support mode of cpu:\n${lm}\n${line}"
######################################################################

#memtotal 内存总大小
total=$(cat /proc/meminfo |grep 'memtotal' |awk -f : '{print $2}' |sed 's/^[ \t]*//g')
echo -e "total memory:\n${total}\n${line}"

#系统支持最大内存
max_capacity=$(dmidecode -t memory -q |grep 'maximum capacity' |awk -f : '{print $2}' |sed 's/^[ \t]*//g')
echo -e "maxinum memory capacity:\n${max_capacity}\n${line}"

#查看内存类型、频率、条数、最大支持内存等信息:dmidecode -t memory,或dmidecode | grep -a16 "memory device$"
#下面为统计内存条数
number=$(dmidecode | grep -a16 "memory device$" |grep size|sort |sed 's/^[ \t]*//g'| grep -v 'no module installed' | wc -l)
echo -e "number of physical memory:\n${number}\n${line}"


#swaptotal swap分区总大小
swaptotal=$(cat /proc/meminfo |grep 'swaptotal' |awk -f : '{print $2}' |sed 's/^[ \t]*//g')
echo -e "total swap:\n${swaptotal}\n${line}"

#buffers size
buffers=$(cat /proc/meminfo |grep 'buffers' |awk -f : '{print $2}' |sed 's/^[ \t]*//g')
echo -e "buffers:\n${buffers}\n${line}"

#cached size
cached=$(cat /proc/meminfo |grep '\<cached\>' |awk -f : '{print $2}' |sed 's/^[ \t]*//g')
echo -e "cached:\n${cached}\n${line}"

#空闲内存 + buffers/cache
available=$(free -m |grep - |awk -f : '{print $2}' |awk '{print $2}')
echo -e "available memory:\n${available} mb\n${line}"

#显示硬盘,以及大小
disk=$(fdisk -l |grep 'disk' |awk -f , '{print $1}' | sed 's/disk identifier.*//g' | sed '/^$/d')
echo -e "amount of disks:\n${disk}\n${line}"

#各挂载分区使用情况
partion=$(df -hlp |sed -n '2,$p')
echo -e "usage of partions:\n${partion}\n${line}"


----------------------------------------------------------------------------------------
测试结果展示:
复制代码 代码如下:

===========
os:
red hat enterprise linux server release 5.6 (tikanga)
===========
os_version:
i686
===========
kernel_version:
2.6.18-238.el5
===========
cpu model:
amd a6-3400m apu with radeon(tm) hd graphics
===========
total of physical cpu:
2
===========
number of cpu cores
2
===========
number of logical cpus:
4
===========
present mode of cpu:
32
===========
support mode of cpu:
64
===========
total memory:
514744 kb
===========
maxinum memory capacity:
1024 gb
===========
number of physical memory:
1
===========
total swap:
1044216 kb
===========
buffers:
101936 kb
===========
cached:
267356 kb
===========
available memory:
369 mb
===========
amount of disks:
disk /dev/sda: 107.3 gb
===========
usage of partions:
/dev/sda3 96g 4.6g 87g 6% /
/dev/sda1 99m 12m 83m 13% /boot
tmpfs 252m 0 252m 0% /dev/shm
/dev/hdc 3.0g 3.0g 0 100% /media/rhel_5.6 i386 dvd
===========