脚本信息
#!/bin/bash
# auth:lsr_zds
# func:sys info check
# version:v1.0
# sys:centos6.x/7.x
[ $(id -u) -gt 0 ] && echo "请用root用户执行此脚本!" && exit 1
sysversion=$(rpm -q centos-release | cut -d- -f3)
line="-------------------------------------------------"
[ -d logs ] || mkdir logs
# 记录输出位置
sys_check_file="logs/$(ip a show dev eth0 | grep -w inet | awk '{print $2}' | awk -F '/' '{print $1}')-`date +%Y%m%d`.txt"
# 获取系统cpu信息
function get_cpu_info() {
Physical_CPUs=$(grep "physical id" /proc/cpuinfo | sort | uniq | wc -l)
Virt_CPUs=$(grep "processor" /proc/cpuinfo | wc -l)
CPU_Kernels=$(grep "cores" /proc/cpuinfo | uniq | awk -F ': ' '{print $2}')
CPU_Type=$(grep "model name" /proc/cpuinfo | awk -F ': ' '{print $2}' | sort | uniq)
CPU_Arch=$(uname -m)
cat <<EOF | column -t
CPU信息:
物理CPU个数: $Physical_CPUs
逻辑CPU个数: $Virt_CPUs
每CPU核心数: $CPU_Kernels
CPU型号: $CPU_Type
CPU架构: $CPU_Arch
EOF
}
# 获取系统内存信息
function get_mem_info() {
check_mem=$(free -m)
# KB
MemTotal=$(grep MemTotal /proc/meminfo | awk '{print $2}')
# KB
MemFree=$(grep MemFree /proc/meminfo | awk '{print $2}')
let MemUsed=MemTotal-MemFree
MemPercent=$(awk "BEGIN {if($MemTotal==0){printf 100}else{printf \"%.2f\",$MemUsed*100/$MemTotal}}")
# 内存总容量(MB)
report_MemTotal="$((MemTotal/1024))""MB"
# 内存剩余(MB)
report_MemFree="$((MemFree/1024))""MB"
report_MemUsedPercent="$(awk "BEGIN {if($MemTotal==0){printf 100}else{printf \"%.2f\",$MemUsed*100/$MemTotal}}")""%" #内存使用率%
cat <<EOF
内存信息:
${check_mem}
EOF
}
# 获取系统网络信息
function get_net_info() {
pri_ipadd=$(ip a show dev eth0 | grep -w inet | awk '{print $2}' | awk -F '/' '{print $1}')
pub_ipadd=$(curl ifconfig.me -s)
gateway=$(ip route | grep default | awk '{print $3}')
mac_info=$(ip link | egrep -v "lo" | grep link | awk '{print $2}')
dns_config=$(egrep -v "^$|^#" /etc/resolv.conf)
route_info=$(route -n)
cat <<EOF | column -t
IP信息:
系统公网地址: ${pub_ipadd}
系统私网地址: ${pri_ipadd}
网关地址: ${gateway}
MAC地址: ${mac_info}
路由信息:
${route_info}
DNS 信息:
${dns_config}
EOF
}
# 获取系统磁盘信息
function get_disk_info() {
disk_info=$(fdisk -l | grep "Disk /dev" | cut -d, -f1)
disk_use=$(df -hTP | awk '$2!="tmpfs"{print}')
disk_inode=$(df -hiP | awk '$1!="tmpfs"{print}')
cat <<EOF
磁盘信息:
${disk_info}
${line}
磁盘使用:
${disk_use}
${line}
inode信息:
${disk_inode}
EOF
}
# 获取系统信息
function get_systatus_info() {
sys_os=$(uname -o)
sys_release=$(cat /etc/redhat-release)
sys_kernel=$(uname -r)
sys_hostname=$(hostname)
sys_selinux=$(getenforce)
sys_lang=$(echo $LANG)
sys_lastreboot=$(who -b | awk '{print $3,$4}')
sys_runtime=$(uptime | awk '{print $3,$4}' | cut -d, -f1)
sys_time=$(date)
sys_load=$(uptime | cut -d: -f5)
cat <<EOF | column -t
系统信息:
系统: ${sys_os}
发行版本: ${sys_release}
系统内核: ${sys_kernel}
主机名: ${sys_hostname}
selinux状态: ${sys_selinux}
系统语言: ${sys_lang}
系统当前时间: ${sys_time}
系统最后重启时间: ${sys_lastreboot}
系统运行时间: ${sys_runtime}
系统负载: ${sys_load}
EOF
}
# 获取服务信息
function get_service_info() {
port_listen=$(netstat -lntup | grep -v "Active Internet")
kernel_config=$(sysctl -p 2>/dev/null)
if [ ${sysversion} -gt 6 ];then
service_config=$(systemctl list-unit-files --type=service --state=enabled | grep "enabled")
run_service=$(systemctl list-units --type=service --state=running | grep ".service")
else
service_config=$(/sbin/chkconfig | grep -E ":on|:启用" | column -t)
run_service=$(/sbin/service --status-all | grep -E "running")
fi
cat <<EOF
服务启动配置:
${service_config}
${line}
运行的服务:
${run_service}
${line}
监听端口:
${port_listen}
${line}
内核参考配置:
${kernel_config}
EOF
}
# 获取 sys && user 信息
function get_sys_user() {
login_user=$(awk -F: '{if ($NF=="/bin/bash") print $0}' /etc/passwd)
ssh_config=$(egrep -v "^#|^$" /etc/ssh/sshd_config)
sudo_config=$(egrep -v "^#|^$" /etc/sudoers | grep -v "^Defaults")
host_config=$(egrep -v "^#|^$" /etc/hosts)
crond_config=$(for cronuser in /var/spool/cron/*; do ls ${cronuser} 2>/dev/null | cut -d/ -f5; egrep -v "^$|^#" ${cronuser} 2>/dev/null; echo "" ;done)
cat <<EOF
系统登录用户:
${login_user}
${line}
ssh 配置信息:
${ssh_config}
${line}
sudo 配置用户:
${sudo_config}
${line}
定时任务配置:
${crond_config}
${line}
hosts 信息:
${host_config}
EOF
}
# 获取进程信息
function process_top_info() {
top_title=$(top -b -n1 | head -n7 | tail -n1)
cpu_top10=$(top -b -n1 | head -n17 | tail -n10)
mem_top10=$(top -b -n1 | head -n17 | tail -n10 | sort -k10 -r)
cat <<EOF
CPU占用top10:
${top_title}
${cpu_top10}
内存占用top10:
${top_title}
${mem_top10}
EOF
}
function sys_check() {
get_cpu_info
echo ${line}
get_mem_info
echo ${line}
get_net_info
echo ${line}
get_disk_info
echo ${line}
get_systatus_info
echo ${line}
get_service_info
echo ${line}
get_sys_user
echo ${line}
process_top_info
}
sys_check > ${sys_check_file}
执行结果
[root@demo ~]# cat logs/10.10.200.108-20200923.txt
CPU信息:
物理CPU个数: 1
逻辑CPU个数: 2
每CPU核心数: 1
CPU型号: Intel(R) Core(TM) i5-8250U CPU @ 1.60GHz
CPU架构: x86_64
-------------------------------------------------
内存信息:
total used free shared buff/cache available
Mem: 3790 3062 291 8 435 463
Swap: 8191 0 8191
-------------------------------------------------
IP信息:
系统公网地址: 182.50.120.71
系统私网地址: 10.10.200.108
网关地址: 10.10.200.1
MAC地址: 00:15:5d:88:6a:00
路由信息:
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 10.10.200.1 0.0.0.0 UG 100 0 0 eth0
10.10.200.0 0.0.0.0 255.255.255.0 U 100 0 0 eth0
DNS 信息:
nameserver 202.96.199.133
-------------------------------------------------
磁盘信息:
Disk /dev/sda: 32.2 GB
Disk /dev/mapper/centos-root: 23.4 GB
Disk /dev/mapper/centos-swap: 8589 MB
-------------------------------------------------
磁盘使用:
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/centos-root xfs 22G 1.7G 21G 8% /
devtmpfs devtmpfs 1.9G 0 1.9G 0% /dev
/dev/sda2 xfs 194M 150M 44M 78% /boot
-------------------------------------------------
inode信息:
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/mapper/centos-root 11M 59K 11M 1% /
devtmpfs 471K 358 471K 1% /dev
/dev/sda2 89K 333 89K 1% /boot
-------------------------------------------------
系统信息:
系统: GNU/Linux
发行版本: CentOS Linux release 7.5.1804 (Core)
系统内核: 3.10.0-862.el7.x86_64
主机名: demo
selinux状态: Enforcing
系统语言: en_US.UTF-8
系统当前时间: Wed Sep 23 17:22:47 CST 2020
系统最后重启时间: 2020-09-16 22:51
系统运行时间: 1 day
系统负载: 0.00, 0.01, 0.05
-------------------------------------------------
服务启动配置:
auditd.service enabled
autovt@.service enabled
crond.service enabled
dbus-org.fedoraproject.FirewallD1.service enabled
dbus-org.freedesktop.NetworkManager.service enabled
dbus-org.freedesktop.nm-dispatcher.service enabled
firewalld.service enabled
getty@.service enabled
irqbalance.service enabled
kdump.service enabled
lvm2-monitor.service enabled
microcode.service enabled
NetworkManager-dispatcher.service enabled
NetworkManager-wait-online.service enabled
NetworkManager.service enabled
postfix.service enabled
rhel-autorelabel.service enabled
rhel-configure.service enabled
rhel-dmesg.service enabled
rhel-domainname.service enabled
rhel-import-state.service enabled
rhel-loadmodules.service enabled
rhel-readonly.service enabled
rsyslog.service enabled
sshd.service enabled
sysstat.service enabled
systemd-readahead-collect.service enabled
systemd-readahead-drop.service enabled
systemd-readahead-replay.service enabled
tuned.service enabled
-------------------------------------------------
运行的服务:
auditd.service loaded active running Security Auditing Service
crond.service loaded active running Command Scheduler
dbus.service loaded active running D-Bus System Message Bus
firewalld.service loaded active running firewalld - dynamic firewall daemon
getty@tty1.service loaded active running Getty on tty1
hypervkvpd.service loaded active running Hyper-V KVP daemon
hypervvssd.service loaded active running Hyper-V VSS daemon
irqbalance.service loaded active running irqbalance daemon
lvm2-lvmetad.service loaded active running LVM2 metadata daemon
NetworkManager.service loaded active running Network Manager
polkit.service loaded active running Authorization Manager
postfix.service loaded active running Postfix Mail Transport Agent
rsyslog.service loaded active running System Logging Service
sshd.service loaded active running OpenSSH server daemon
systemd-journald.service loaded active running Journal Service
systemd-logind.service loaded active running Login Service
systemd-udevd.service loaded active running udev Kernel Device Manager
tuned.service loaded active running Dynamic System Tuning Daemon
-------------------------------------------------
监听端口:
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 955/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1273/master
tcp6 0 0 :::22 :::* LISTEN 955/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1273/master
-------------------------------------------------
内核参考配置:
-------------------------------------------------
系统登录用户:
root:x:0:0:root:/root:/bin/bash
-------------------------------------------------
ssh 配置信息:
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key
SyslogFacility AUTHPRIV
AuthorizedKeysFile .ssh/authorized_keys
PasswordAuthentication yes
ChallengeResponseAuthentication no
GSSAPIAuthentication yes
GSSAPICleanupCredentials no
UsePAM yes
X11Forwarding yes
AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE
AcceptEnv XMODIFIERS
Subsystem sftp /usr/libexec/openssh/sftp-server
-------------------------------------------------
sudo 配置用户:
root ALL=(ALL) ALL
%wheel ALL=(ALL) ALL
-------------------------------------------------
定时任务配置:
-------------------------------------------------
hosts 信息:
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
-------------------------------------------------
CPU占用top10:
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 46108 6404 3944 S 0.0 0.2 0:05.15 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.06 kthreadd
3 root 20 0 0 0 0 S 0.0 0.0 0:00.46 ksoftirqd/0
5 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0:0H
6 root 20 0 0 0 0 S 0.0 0.0 1:45.76 kworker/u4:0
7 root rt 0 0 0 0 S 0.0 0.0 0:01.43 migration/0
8 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcu_bh
9 root 20 0 0 0 0 S 0.0 0.0 0:13.61 rcu_sched
10 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 lru-add-drain
11 root rt 0 0 0 0 S 0.0 0.0 0:00.61 watchdog/0
内存占用top10:
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 46108 6404 3944 S 0.0 0.2 0:05.15 systemd
6 root 20 0 0 0 0 S 0.0 0.0 1:45.76 kworker/u4:0
9 root 20 0 0 0 0 S 0.0 0.0 0:13.61 rcu_sched
7 root rt 0 0 0 0 S 0.0 0.0 0:01.43 migration/0
11 root rt 0 0 0 0 S 0.0 0.0 0:00.61 watchdog/0
3 root 20 0 0 0 0 S 0.0 0.0 0:00.46 ksoftirqd/0
2 root 20 0 0 0 0 S 0.0 0.0 0:00.06 kthreadd
8 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcu_bh
10 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 lru-add-drain
5 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0:0H