当前位置 : 主页 > 操作系统 > centos >

使用Shell脚本获取CentOS 7服务器软硬件信息

来源:互联网 收集:自由互联 发布时间:2022-06-20
1、自己编写的Shell脚本中会用到的一些命令: # which ifconfig # rpm -qf /usr/sbin/ifconfig # which wget # rpm -qf /usr/bin/wget # which iostat # rpm -qf /usr/bin/iostat 说明: (1)安装sysstat软件包过程中需要安

1、自己编写的Shell脚本中会用到的一些命令:

# which ifconfig

使用Shell脚本获取CentOS 7服务器软硬件信息_grep

# rpm -qf /usr/sbin/ifconfig

使用Shell脚本获取CentOS 7服务器软硬件信息_shell_02

# which wget

使用Shell脚本获取CentOS 7服务器软硬件信息_awk_03

# rpm -qf /usr/bin/wget

使用Shell脚本获取CentOS 7服务器软硬件信息_grep_04

# which iostat

使用Shell脚本获取CentOS 7服务器软硬件信息_sed_05

# rpm -qf /usr/bin/iostat

使用Shell脚本获取CentOS 7服务器软硬件信息_grep_06

说明:

(1)安装sysstat软件包过程中需要安装依赖软件包lm_sensors-libs;

(2)某些特定环境下可能无法直接联网使用yum或挂盘搭建yum仓库安装软件包,提前准备好上述net-tools-2.0-0.25.20131004git.el7.x86_64.rpm、wget-1.14-18.el7_6.1.x86_64.rpm、lm_sensors-libs-3.4.0-8.20160601gitf9185e5.el7.x86_64.rpm、sysstat-10.1.5-19.el7.x86_64.rpm软件包,并上传至服务器的/tmp目录下;

(3)为了演示自动安装软件包的效果,提前先将这些软件包删除:# yum -y remove net-tools wget sysstat。

 

2、编写Shell脚本:

# mkdir -pv /scripts
# cd /scripts
# vim qiuyue.sh


#!/bin/bash
#
# Author:QiuYue
# Blog: https://blog.51cto.com/qiuyue
# Date: 2022-06-02

export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

# 定义echo命令输出后的字体颜色
colour_title='\033[33;1m'
colour_info_key='\033[36;1m'
colour_info_value='\033[0m'
colour_warning='\033[31;1m'

# 定义函数
# 打印脚本标题
function print_title(){
echo -e "==============================================================================="
echo -e " Tool From QiuYue "
echo -e " Blog:https://blog.51cto.com/qiuyue "
echo -e "==============================================================================="
}

# 判断软件包是否安装,软件包名称由脚本传入,如果软件包没有安装,则执行安装操作,软件包提前上传至服务器的/tmp目录下
install_packages(){
if ! rpm -q $1 &> /dev/null; then
echo -e "${colour_warning}Warning :$1 is not installed, installing now."
yum -y localinstall /tmp/$1*.rpm &> /dev/null && echo -e "${colour_info_key}Message :${colour_info_value}$1 is installed successfully."
else
echo -e "${colour_info_key}Message :${colour_info_value}$1 has been installed."
fi
}

# 检查防火墙状态
check_firewall(){
firewall-cmd --state &> /dev/null
if [ $? -ne 0 ]; then
echo -e "${colour_warning}Firewall :disabled"
else
echo -e "${colour_info_key}Firewall :${colour_info_value}enabled"
fi
}

# 检查SELinux状态
check_selinux(){
selinux_status=`getenforce`
if [ ${selinux_status} == "Disabled" ]; then
echo -e "${colour_warning}SELinux :disabled"
elif [ ${selinux_status} == "Permissive" ]; then
echo -e "${colour_info_key}SELinux :${colour_info_value}permissive"
else
echo -e "${colour_info_key}SELinux :${colour_info_value}enforcing"
fi
}

# 检查路由转发是否启用
check_ip_forward(){
res=`cat /proc/sys/net/ipv4/ip_forward`
if [ $res -eq 1 ]; then
echo -e "${colour_info_key}IP Forward :${colour_info_value}enabled"
else
echo -e "${colour_info_key}IP Forward :${colour_warning}disabled"
fi
}

# 显示能够登录服务器的用户、UID和它的计划任务数量
list_users(){
for shell in `grep -v "/sbin/nologin" /etc/shells`; do
for user in $(grep "$shell" /etc/passwd | awk -F: '{print $1}'); do
uid=`id -u "$user"`
crontabnum=0
crontab -u $user -l &> /dev/null
if [ $? -eq 0 ]; then
let crontabnum+=`crontab -u $user -l | wc -l`
fi
echo "$user:$uid:$crontabnum"
done
done
}

# 显示服务器所有网卡名称、内网IP、MAC地址和对应网关
list_interfaces(){
# 获取类似eth0、ens33网卡名称,获取到的网卡名称可能不止一个
interface=`ifconfig | grep "^e" | awk -F: '{print $1}'`
# 采用遍历方式获取网卡对应IP
for i in $interface; do
private_ip=`ifconfig $i | grep -w inet | awk '{print $2}'`
mac=`ifconfig $i | grep -w ether | awk '{print $2}'`
gateway=`ip route | grep default | grep $i | awk '{print $3}'`
echo "$i#${private_ip}#$mac#$gateway"
done
}

# 检查服务器是否能通公网
check_internet(){
if wget -q --spider https://blog.51cto.com/; then
echo -e "${colour_info_key}Internet :${colour_info_value}connected"
else
echo -e "${colour_info_key}Internet :${colour_warning}disconnected"
fi
}

# 如果服务器能通公网,显示公网IP,也可使用curl -s https://api.myip.la实现类似功能
list_publicip(){
publicip=`wget -qO- -t2 -T2 ipinfo.io/ip`
[ -z $publicip ] && publicip=$(wget -qO- -t2 -T2 ipv4.icanhazip.com)
[ -n $publicip ] && echo -e "${colour_info_key}Public IP :${colour_info_value}$publicip" || echo -e "${colour_info_key}Public IP :${colour_warning}unknown"
}

# 定义变量
operation_system=`cat /etc/centos-release | tr 'A-Z' 'a-z'`
architecture=`uname -m`
kernel_version=`uname -r`
srvname=`hostname`
srvtime=`date +'%F %T'`
uptime=`uptime -p | cut -d' ' -f 2-`
# 最近一次系统引导时间
last_system_boot=`who -b | awk '{print $3,$4}'`
one_minute_load_average=`cat /proc/loadavg | awk '{print $1}'`
five_minutes_load_average=`cat /proc/loadavg | awk '{print $2}'`
fifteen_minutes_load_average=`cat /proc/loadavg | awk '{print $3}'`
# ulimit -n表示控制进程级别能够打开的文件句柄数,提供对Shell及其启动进程的可用文件句柄数的控制
process_open_files=`ulimit -n`
# file-max表示系统级别的能够打开的文件句柄数,是对整个系统的限制,并不针对用户
system_open_files=`cat /proc/sys/fs/file-max`
# 开机自启的服务数量
service_enabled_number=`systemctl list-unit-files -t service --state=enabled | grep -c ".service"`
# 正在运行的服务数量
service_running_number=`systemctl list-units -t service --state=running | grep -c ".service"`
# w命令显示已登录用户以及这些用户正在执行的操作,who命令也能实现,last命令显示上次登录用户的列表
users=`w | grep -v '^ ' | column -t`
# /etc/sudoers配置文件内容
sudoers_content=`grep -v "^#" /etc/sudoers | grep -v "Defaults" | sed '/^$/d' | column -t`
# /etc/rc.d/rc.local文件中自定义的开机启动项
rclocal_content=`grep -v '^#' /etc/rc.d/rc.local | sed '/^$/d'`
# CPU型号
cpu_model_name=$(lscpu | grep "^Model name" | cut -d: -f2 | sed 's/^ *//')
# 总核数=物理CPU个数*每个物理CPU的核心数
# 总逻辑CPU数=物理CPU个数*每个物理CPU的核心数*超线程数
# 物理CPU个数
physical_cpu_number=$(grep 'physical id' /proc/cpuinfo | sort -u | wc -l)
# 每个物理CPU的核心数
cpu_cores_number=$(grep 'cpu cores' /proc/cpuinfo | uniq | awk '{print $4}')
# 逻辑CPU数
cpu_processores=$(grep -c 'processor' /proc/cpuinfo)
# 物理内存使用情况
memory_usage=$(free -h)
# 磁盘使用情况(sd:SCSI设备、vd:virtio虚拟设备、hd:IDE设备、mapper:LVM、md:软Raid设备)
disk_usage=$(df -Th | grep 'Filesystem\|^/dev/sd*\|^/dev/vd*\|^/dev/hd*\|^/dev/mapper*\|^/dev/md*')
nameservers=$(grep ^nameserver /etc/resolv.conf | awk '{ORS=" "; print $2}')

# 输出结果前先清屏
clear
print_title
echo -e "\n${colour_title}Software Information"
install_packages net-tools
install_packages wget
install_packages sysstat
# %iowait:CPU等待磁盘完成I/O的时间百分比,如果%iowait的值过高,表示磁盘存在I/O瓶颈,使用vmstat命令也能收集系统相关信息,但是vmstat命令依赖于procps-ng软件包
avg_cpu_iowait=`iostat -c | awk '/^ /{print $4}'`
# %idle:CPU处于空闲的时间百分比,如果%idle的值高,表示CPU较空闲,如果%idle的值高但系统响应慢,有可能是CPU等待分配内存,此时应加大内存容量
avg_cpu_idle=`iostat -c | awk '/^ /{print $NF}'`
echo -e "\n${colour_title}System Information"
echo -e "${colour_info_key}Operation System :${colour_info_value}${operation_system}"
echo -e "${colour_info_key}Architecture :${colour_info_value}$architecture"
echo -e "${colour_info_key}Kernel Version :${colour_info_value}${kernel_version}"
echo -e "${colour_info_key}Hostname :${colour_info_value}$srvname"
check_firewall
check_selinux
echo -e "${colour_info_key}Time :${colour_info_value}$srvtime"
echo -e "${colour_info_key}Uptime :${colour_info_value}$uptime"
echo -e "${colour_info_key}Last System Boot :${colour_info_value}${last_system_boot}"
echo -e "${colour_info_key}Load Average :${colour_info_value}${one_minute_load_average} ${five_minutes_load_average} ${fifteen_minutes_load_average}"
echo -e "${colour_info_key}Avg CPU Iowait :${colour_info_value}${avg_cpu_iowait}"
echo -e "${colour_info_key}Avg CPU Idle :${colour_info_value}${avg_cpu_idle}"
echo -e "${colour_info_key}Process Open Files :${colour_info_value}${process_open_files}"
echo -e "${colour_info_key}System Open Files :${colour_info_value}${system_open_files}"
check_ip_forward
echo -e "${colour_info_key}Service Enabled :${colour_info_value}${service_enabled_number}"
echo -e "${colour_info_key}Service Running :${colour_info_value}${service_running_number}"
echo -e "${colour_info_key}User List:${colour_info_value}"
list_users | sort -t: -nk2 | awk -F: 'BEGIN{printf "%-10s %-8s %s\n", "User", "UID", "CrontabNumber"}{printf "%-10s %-8s %s\n", $1, $2, $3}END{print "Total", NR, "users."}'
echo -e "${colour_info_key}Logged In Users:${colour_info_value}"
echo "$users"
echo -e "${colour_info_key}/etc/sudoers:${colour_info_value}"
echo "${sudoers_content}"
echo -e "${colour_info_key}/etc/rc.d/rc.local:${colour_info_value}"
echo "${rclocal_content}"
echo -e "\n${colour_title}Hardware Information"
echo -e "${colour_info_key}CPU Model Name :${colour_info_value}${cpu_model_name}"
echo -e "${colour_info_key}Physical CPU Number :${colour_info_value}${physical_cpu_number}"
echo -e "${colour_info_key}Per CPU Cores :${colour_info_value}${cpu_cores_number}"
echo -e "${colour_info_key}CPU Processores :${colour_info_value}${cpu_processores}"
echo -e "${colour_info_key}Memory Usage:${colour_info_value}"
echo "$memory_usage"
echo -e "${colour_info_key}Disk Usage:${colour_info_value}"
echo "$disk_usage"
echo -e "\n${colour_title}Network Information"
echo -e "${colour_info_key}Interfaces:${colour_info_value}"
list_interfaces | awk -F# 'BEGIN{printf "%-12s %-17s %-21s %s\n", "Interface", "IP", "MAC", "Gateway"}{printf "%-12s %-17s %-21s %s\n", $1, $2, $3, $4}END{print "Total", NR, "interfaces."}'
check_internet
list_publicip
echo -e "${colour_info_key}Nameserver :${colour_info_value}$nameservers"

3、  执行Shell脚本,查看效果:

# chmod +x qiuyue.sh
# ./qiuyue.sh

 使用Shell脚本获取CentOS 7服务器软硬件信息_sed_07

使用Shell脚本获取CentOS 7服务器软硬件信息_grep_08

上一篇:CDN的功能及原理概述
下一篇:没有了
网友评论