#化鲲为鹏,我有话说# 鲲鹏服务器信息巡检脚本

举报
kaliarch 发表于 2019/11/13 18:24:55 2019/11/13
【摘要】 一、背景目前使用华为云鲲鹏服务器,对登录一个系统,快速查看其系统信息,检查系统各项指标及参数,编写系统快速检查脚本,输出系统信息到脚本运行的logs目录下。二、脚本git地址#!/bin/bash# auth:kaliarch# func:sys info check# version:v1.0# sys:centos6.x/7.x[ $(id -u) -gt 0 ] && echo "请用...

一、背景

目前使用华为云鲲鹏服务器,对登录一个系统,快速查看其系统信息,检查系统各项指标及参数,编写系统快速检查脚本,输出系统信息到脚本运行的logs目录下。

二、脚本

git地址

#!/bin/bash
# auth:kaliarch
# 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)
    MemTotal=$(grep MemTotal /proc/meminfo| awk '{print $2}')  #KB
    MemFree=$(grep MemFree /proc/meminfo| awk '{print $2}')    #KB
    let MemUsed=MemTotal-MemFree
    MemPercent=$(awk "BEGIN {if($MemTotal==0){printf 100}else{printf \"%.2f\",$MemUsed*100/$MemTotal}}")
    report_MemTotal="$((MemTotal/1024))""MB"        #内存总容量(MB)
    report_MemFree="$((MemFree/1024))""MB"          #内存剩余(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}
磁盘使用:

${disk_use}
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
}


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 -7|tail -1)
    cpu_top10=$(top b -n1 | head -17 | tail -10)
    mem_top10=$(top -b n1|head -17|tail -10|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}

三、测试

鲲鹏服务器测试

image.png

采集鲲鹏服务器信息

CPU信息:
物理CPU个数:  0
逻辑CPU个数:  2
每CPU核心数:
CPU型号:
CPU架构:      aarch64
-------------------------------------------------
内存信息:

              total        used        free      shared  buff/cache   available
Mem:           3482         393        2081          20        1007        2514
Swap:             0           0           0
-------------------------------------------------
IP信息:
系统公网地址:      121.36.1.227
系统私网地址:      192.168.0.17
网关地址:          192.168.0.1
MAC地址:           fa:16:3e:50:d8:50
02:42:9b:e4:84:bd
d6:77:f9:49:14:8f
路由信息:
Kernel             IP                     routing          table
Destination        Gateway                Genmask          Flags  Metric  Ref  Use  Iface
0.0.0.0            192.168.0.1            0.0.0.0          UG     100     0    0    eth0
169.254.169.254    192.168.0.1            255.255.255.255  UGH    100     0    0    eth0
172.17.0.0         0.0.0.0                255.255.0.0      U      0       0    0    docker0
192.168.0.0        0.0.0.0                255.255.255.0    U      100     0    0    eth0
DNS                信息:
search             openstacklocal
nameserver         100.125.1.250
nameserver         100.125.129.250
options            single-request-reopen
-------------------------------------------------
磁盘信息:

Disk /dev/vda: 42.9 GB
Disk /dev/vdb: 107.4 GB
磁盘使用:

Filesystem     Type      Size  Used Avail Use% Mounted on
devtmpfs       devtmpfs  1.7G     0  1.7G   0% /dev
/dev/vda2      ext4       39G  3.4G   33G  10% /
/dev/vda1      vfat     1022M  7.7M 1015M   1% /boot/efi
overlay        overlay    39G  3.4G   33G  10% /var/lib/docker/overlay2/6fde1851846f50e525be6b6fc104715c604c66b38f5d9e640f403dbc95f54def/merged
inode信息:

Filesystem     Inodes IUsed IFree IUse% Mounted on
devtmpfs          28K   303   27K    2% /dev
/dev/vda2        2.5M   78K  2.4M    4% /
/dev/vda1           0     0     0     - /boot/efi
overlay          2.5M   78K  2.4M    4% /var/lib/docker/overlay2/6fde1851846f50e525be6b6fc104715c604c66b38f5d9e640f403dbc95f54def/merged
-------------------------------------------------
系统信息:
系统:              GNU/Linux
发行版本:          CentOS                                Linux  release  7.4.1708  (AltArch)
系统内核:          4.14.0-115.5.1.el7a.aarch64
主机名:            ecs-kc1-large-2-linux-20191111131638
selinux状态:       Disabled
系统语言:          en_US.UTF-8
系统当前时间:      Wed                                   Nov    13       17:53:35  CST        2019
系统最后重启时间:  2019-11-11                            13:18
系统运行时间:      2                                     days
系统负载:          0.00,                                 0.00,  0.00
-------------------------------------------------
服务启动配置:

atd.service                                 enabled
auditd.service                              enabled
autovt@.service                             enabled
cloud-config.service                        enabled
cloud-final.service                         enabled
cloud-init-local.service                    enabled
cloud-init.service                          enabled
crond.service                               enabled
dbus-org.freedesktop.NetworkManager.service enabled
dbus-org.freedesktop.nm-dispatcher.service  enabled
docker.service                              enabled
getty@.service                              enabled
irqbalance.service                          enabled
kdump.service                               enabled
lvm2-monitor.service                        enabled
NetworkManager-dispatcher.service           enabled
NetworkManager.service                      enabled
ntpd.service                                enabled
postfix.service                             enabled
rsyslog.service                             enabled
sshd.service                                enabled
systemd-readahead-collect.service           enabled
systemd-readahead-drop.service              enabled
systemd-readahead-replay.service            enabled
tuned.service                               enabled
-------------------------------------------------
运行的服务:

atd.service                      loaded active running Job spooling tools
auditd.service                   loaded active running Security Auditing Service
cloudResetPwdUpdateAgent.service loaded active running LSB: @app.long.name@
containerd.service               loaded active running containerd container runtime
crond.service                    loaded active running Command Scheduler
dbus.service                     loaded active running D-Bus System Message Bus
denyhosts.service                loaded active running LSB: denyhosts
docker.service                   loaded active running Docker Application Container Engine
getty@tty1.service               loaded active running Getty on tty1
irqbalance.service               loaded active running irqbalance daemon
lvm2-lvmetad.service             loaded active running LVM2 metadata daemon
NetworkManager.service           loaded active running Network Manager
ntpd.service                     loaded active running Network Time Service
polkit.service                   loaded active running Authorization Manager
postfix.service                  loaded active running Postfix Mail Transport Agent
rsyslog.service                  loaded active running System Logging Service
serial-getty@ttyAMA0.service     loaded active running Serial Getty on ttyAMA0
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      1293/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      990/master          
tcp        0      0 0.0.0.0:6379            0.0.0.0:*               LISTEN      7695/./src/redis-se 
tcp6       0      0 :::80                   :::*                    LISTEN      9992/docker-proxy   
tcp6       0      0 :::22                   :::*                    LISTEN      1293/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      990/master          
tcp6       0      0 :::6379                 :::*                    LISTEN      7695/./src/redis-se 
udp        0      0 0.0.0.0:7945            0.0.0.0:*                           621/dhclient        
udp        0      0 0.0.0.0:68              0.0.0.0:*                           621/dhclient        
udp        0      0 172.17.0.1:123          0.0.0.0:*                           548/ntpd            
udp        0      0 192.168.0.17:123        0.0.0.0:*                           548/ntpd            
udp        0      0 127.0.0.1:123           0.0.0.0:*                           548/ntpd            
udp        0      0 0.0.0.0:123             0.0.0.0:*                           548/ntpd            
udp6       0      0 fe80::d477:f9ff:fe4:123 :::*                                548/ntpd            
udp6       0      0 fe80::42:9bff:fee4::123 :::*                                548/ntpd            
udp6       0      0 fe80::f816:3eff:fe5:123 :::*                                548/ntpd            
udp6       0      0 ::1:123                 :::*                                548/ntpd            
udp6       0      0 :::123                  :::*                                548/ntpd            
udp6       0      0 :::58458                :::*                                621/dhclient        
-------------------------------------------------
内核参考配置:


-------------------------------------------------
系统登录用户:

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
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
PermitRootLogin yes
PasswordAuthentication yes
UseDNS no
-------------------------------------------------
sudo 配置用户:

root    ALL=(ALL)       ALL
%wheel  ALL=(ALL)       ALL
-------------------------------------------------
定时任务配置:


-------------------------------------------------
hosts 信息:

::1     localhost       localhost.localdomain   localhost6      localhost6.localdomain6
127.0.0.1       localhost       localhost.localdomain   localhost4      localhost4.localdomain4
127.0.0.1       localhost       localhost
127.0.0.1       ecs-kc1-large-2-linux-20191111131638    ecs-kc1-large-2-linux-20191111131638
-------------------------------------------------
CPU占用top10:

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
    1 root      20   0    9536   8192   4224 S   0.0  0.2   0:02.49 systemd
    2 root      20   0       0      0      0 S   0.0  0.0   0:00.00 kthreadd
    4 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 kworker/0:0H
    6 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 mm_percpu_wq
    7 root      20   0       0      0      0 S   0.0  0.0   0:00.12 ksoftirqd/0
    8 root      20   0       0      0      0 I   0.0  0.0   0:19.98 rcu_sched
    9 root      20   0       0      0      0 I   0.0  0.0   0:00.00 rcu_bh
   10 root      rt   0       0      0      0 S   0.0  0.0   0:00.02 migration/0
   11 root      rt   0       0      0      0 S   0.0  0.0   0:00.14 watchdog/0   12 root      20   0       0      0      0 S   0.0  0.0   0:00.00 cpuhp/0

内存占用top10:

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
    1 root      20   0    9536   8192   4224 S   0.0  0.2   0:02.49 systemd
    8 root      20   0       0      0      0 I   0.0  0.0   0:19.98 rcu_sched
   11 root      rt   0       0      0      0 S   0.0  0.0   0:00.14 watchdog/0
    7 root      20   0       0      0      0 S   0.0  0.0   0:00.12 ksoftirqd/0
   10 root      rt   0       0      0      0 S   0.0  0.0   0:00.02 migration/0
    9 root      20   0       0      0      0 I   0.0  0.0   0:00.00 rcu_bh
    6 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 mm_percpu_wq
    4 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 kworker/0:0H
    2 root      20   0       0      0      0 S   0.0  0.0   0:00.00 kthreadd
   12 root      20   0       0      0      0 S   0.0  0.0   0:00.00 cpuhp/0


【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。