Linux 系统检查脚本
发布于: 2020 年 08 月 22 日
一、背景
对登录一个系统,快速查看其系统信息,检查系统各项指标及参数,编写系统快速检查脚本,输出系统信息到脚本运行的logs目录下。
二、脚本
#!/bin/bash# auth:kaliarch# func:sys info check# version:v1.0# sys:centos6.x/7.x[ $(id -u) -gt 0 ] && echo "请用root用户执行此脚本!" && exit 1sysversion=$(rpm -q centos-release|cut -d- -f3)line="-------------------------------------------------"[ -d logs ] || mkdir logssys_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_KernelsCPU型号: $CPU_TypeCPU架构: $CPU_ArchEOF}# 获取系统内存信息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") ficat <<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 <<EOFCPU占用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}
三、测试
检查的信息如下
CPU信息:物理CPU个数: 1逻辑CPU个数: 2每CPU核心数: 2CPU型号: QEMU Virtual CPU version 2.3.0CPU架构: x86_64-------------------------------------------------内存信息: total used free shared buff/cache availableMem: 1839 117 1292 8 428 1526Swap: 2047 0 2047-------------------------------------------------IP信息:系统公网地址: 103.21.119.220系统私网地址: 10.234.1.160网关地址: 10.234.1.254MAC地址: fa:de:19:ea:54:00路由信息:Kernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface0.0.0.0 10.234.1.254 0.0.0.0 UG 100 0 0 eth010.234.1.0 0.0.0.0 255.255.255.0 U 100 0 0 eth0DNS 信息:nameserver 114.114.114.114-------------------------------------------------磁盘信息:Disk /dev/vda: 21.5 GBDisk /dev/mapper/cl-root: 18.2 GBDisk /dev/mapper/cl-swap: 2147 MB磁盘使用:Filesystem Type Size Used Avail Use% Mounted on/dev/mapper/cl-root xfs 17G 1.2G 16G 7% /devtmpfs devtmpfs 910M 0 910M 0% /dev/dev/vda1 xfs 1014M 138M 877M 14% /bootinode信息:Filesystem Inodes IUsed IFree IUse% Mounted on/dev/mapper/cl-root 8.5M 32K 8.5M 1% /devtmpfs 228K 403 227K 1% /dev/dev/vda1 512K 330 512K 1% /boot-------------------------------------------------系统信息:系统: GNU/Linux发行版本: CentOS Linux release 7.3.1611 (Core)系统内核: 3.10.0-514.el7.x86_64主机名: 10-234-1-160selinux状态: Permissive系统语言: en_US.UTF-8系统当前时间: Wed Oct 24 10:30:59 CST 2018系统最后重启时间: 2018-10-23 12:07系统运行时间: 22:23系统负载: 0.00, 0.01, 0.05-------------------------------------------------服务启动配置:auditd.service enabledautovt@.service enabledcrond.service enableddbus-org.fedoraproject.FirewallD1.service enableddbus-org.freedesktop.NetworkManager.service enableddbus-org.freedesktop.nm-dispatcher.service enabledfirewalld.service enabledgetty@.service enabledirqbalance.service enabledkdump.service enabledlvm2-monitor.service enabledmicrocode.service enabledNetworkManager-dispatcher.service enabledNetworkManager.service enabledpostfix.service enabledqemu-guest-agent.service enabledrsyslog.service enabledsshd.service enabledsystemd-readahead-collect.service enabledsystemd-readahead-drop.service enabledsystemd-readahead-replay.service enabledtuned.service enabled-------------------------------------------------运行的服务:auditd.service loaded active running Security Auditing Servicecrond.service loaded active running Command Schedulerdbus.service loaded active running D-Bus System Message Busfirewalld.service loaded active running firewalld - dynamic firewall daemongetty@tty1.service loaded active running Getty on tty1irqbalance.service loaded active running irqbalance daemonlvm2-lvmetad.service loaded active running LVM2 metadata daemonNetworkManager.service loaded active running Network Managerpolkit.service loaded active running Authorization Managerpostfix.service loaded active running Postfix Mail Transport Agentqemu-guest-agent.service loaded active running QEMU Guest Agentrsyslog.service loaded active running System Logging Servicesshd.service loaded active running OpenSSH server daemonsystemd-journald.service loaded active running Journal Servicesystemd-logind.service loaded active running Login Servicesystemd-udevd.service loaded active running udev Kernel Device Managertuned.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 1217/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1998/master tcp6 0 0 :::22 :::* LISTEN 1217/sshd tcp6 0 0 ::1:25 :::* LISTEN 1998/master udp 0 0 0.0.0.0:8900 0.0.0.0:* 739/dhclient udp 0 0 0.0.0.0:68 0.0.0.0:* 739/dhclient udp6 0 0 :::60097 :::* 739/dhclient -------------------------------------------------内核参考配置:kernel.msgmnb = 65536kernel.msgmax = 65536kernel.shmmax = 68719476736kernel.shmall = 4294967296net.ipv4.tcp_syncookies = 1net.ipv4.tcp_syn_retries = 1net.ipv4.tcp_tw_recycle = 1net.ipv4.tcp_tw_reuse = 1net.ipv4.tcp_fin_timeout = 1net.ipv4.tcp_keepalive_time = 1200net.ipv4.ip_local_port_range = 1024 65535net.ipv6.conf.all.disable_ipv6 = 1-------------------------------------------------系统登录用户:root:x:0:0:root:/root:/bin/bash-------------------------------------------------ssh 配置信息:HostKey /etc/ssh/ssh_host_rsa_keyHostKey /etc/ssh/ssh_host_ecdsa_keyHostKey /etc/ssh/ssh_host_ed25519_keySyslogFacility AUTHPRIVAuthorizedKeysFile .ssh/authorized_keysPasswordAuthentication yesChallengeResponseAuthentication noGSSAPIAuthentication yesGSSAPICleanupCredentials noUsePAM yesX11Forwarding yesUsePrivilegeSeparation sandbox # Default for new installations.AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGESAcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENTAcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGEAcceptEnv XMODIFIERSSubsystem sftp /usr/libexec/openssh/sftp-server-------------------------------------------------sudo 配置用户:root ALL=(ALL) ALL%wheel ALL=(ALL) ALL-------------------------------------------------定时任务配置:root1 1 * * * /bin/bash /usr/scripts/IP_iptables.sh1 1 * * 0 /usr/sbin/ntpdate time1.aliyun.com30 8 * * 1 /bin/bash /data/blsexcle/rds_bak.sh-------------------------------------------------hosts 信息:127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4::1 localhost localhost.localdomain localhost6 localhost6.localdomain610.234.1.150 10-234-1-150-------------------------------------------------CPU占用top10: PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 1 root 20 0 128092 6644 3888 S 0.0 0.4 0:05.30 systemd 2 root 20 0 0 0 0 S 0.0 0.0 0:00.04 kthreadd 3 root 20 0 0 0 0 S 0.0 0.0 0:00.01 ksoftirqd/0 6 root 20 0 0 0 0 S 0.0 0.0 0:00.37 kworker/u4:0 7 root rt 0 0 0 0 S 0.0 0.0 0:00.02 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:01.97 rcu_sched 10 root rt 0 0 0 0 S 0.0 0.0 0:00.57 watchdog/0 11 root rt 0 0 0 0 S 0.0 0.0 0:00.45 watchdog/1 12 root rt 0 0 0 0 S 0.0 0.0 0:00.03 migration/1内存占用top10: PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 1 root 20 0 128092 6644 3888 S 0.0 0.4 0:05.30 systemd 9 root 20 0 0 0 0 S 0.0 0.0 0:01.97 rcu_sched 10 root rt 0 0 0 0 S 0.0 0.0 0:00.57 watchdog/0 11 root rt 0 0 0 0 S 0.0 0.0 0:00.45 watchdog/1 6 root 20 0 0 0 0 S 0.0 0.0 0:00.37 kworker/u4:0 2 root 20 0 0 0 0 S 0.0 0.0 0:00.04 kthreadd 12 root rt 0 0 0 0 S 0.0 0.0 0:00.03 migration/1 7 root rt 0 0 0 0 S 0.0 0.0 0:00.02 migration/0 3 root 20 0 0 0 0 S 0.0 0.0 0:00.01 ksoftirqd/0 8 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcu_bh
划线
评论
复制
发布于: 2020 年 08 月 22 日阅读数: 55
版权声明: 本文为 InfoQ 作者【雪雷】的原创文章。
原文链接:【http://xie.infoq.cn/article/450b84a1ca0edc13ece1c6d17】。文章转载请联系作者。
雪雷
关注
stay hungry stay foolish 2019.08.16 加入
Devops,python,shell,云原生,云架构,kubernetes https://github.com/redhatxl
评论