#! /bin/bash
#Server daily patrol inspection report
#Daily Patrolling Inspection Checklist
#Author: shuaili.gong
#Mail: 13526464656@163.com
##############################################################################
console color
C_RESET=“$(tput sgr0)”
C_BLACK=“\033[1;30m”
C_RED=“\033[1;31m”
C_GREEN=“\033[1;32m”
C_YELLOW=“\033[1;33m”
C_BLUE=“\033[1;34m”
C_PURPLE=“\033[1;35m”
C_CYAN=“\033[1;36m”
C_WHITE=“\033[1;37m”
##############################################################################
printf “${C_PURPLE}”
cat << EOF
###################################################################################
系统信息检查脚本
###################################################################################
EOF
printf “${C_RESET}”
[[ KaTeX parse error: Expected 'EOF', got '&' at position 18: …d -u) -gt 0 ]] &̲& echo "请用root用…(rpm -q centos-release | cut -d- -f3)
double_line=“===============================================================”
line=“----------------------------------------------”
打印头部信息
printHeadInfo() {
cat << EOF
±--------------------------------------------------------------------------------+
| 欢迎使用 【系统信息检查脚本】 |
±--------------------------------------------------------------------------------+
EOF
}
打印尾部信息
printFootInfo() {
cat << EOF
±--------------------------------------------------------------------------------+
| 脚本执行结束,感谢使用!|
±--------------------------------------------------------------------------------+
EOF
}
options=( “获取系统信息” “获取服务信息” “获取CPU信息” “获取系统网络信息” “获取系统内存信息” “获取系统磁盘信息” “获取CPU/内存占用TOP10” “获取系统用户信息” “输出所有信息” “退出” )
printMenu() {
printf “KaTeX parse error: Undefined control sequence: \n at position 25: … printf "主菜单:\̲n̲" for i in "{!options[@]}”; do
index=expr ${i} + 1
val=expr ${index} % 2
printf “\t(%02d) %-30s” “
i
n
d
e
x
"
"
{index}" "
index""{options[$i]}”
if [[ KaTeX parse error: Undefined control sequence: \n at position 36: … printf "\̲n̲" fi done…{C_BLUE}请输入需要执行的指令:\n"
printf “${C_RESET}”
}
获取系统信息
get_systatus_info() {
sys_os=
(
u
n
a
m
e
−
o
)
s
y
s
r
e
l
e
a
s
e
=
(uname -o) sys_release=
(uname−o)sysrelease=(cat /etc/redhat-release)
sys_kernel=
(
u
n
a
m
e
−
r
)
s
y
s
h
o
s
t
n
a
m
e
=
(uname -r) sys_hostname=
(uname−r)syshostname=(hostname)
sys_selinux=
(
g
e
t
e
n
f
o
r
c
e
)
s
y
s
l
a
n
g
=
(getenforce) sys_lang=
(getenforce)syslang=(echo
L
A
N
G
)
s
y
s
l
a
s
t
r
e
b
o
o
t
=
LANG) sys_lastreboot=
LANG)syslastreboot=(who -b | awk '{print $3,KaTeX parse error: Expected 'EOF', got '}' at position 2: 4}̲') sys_runtim…(uptime | awk '{print $3,KaTeX parse error: Expected 'EOF', got '}' at position 2: 4}̲' | cut -d, -f1…(date)
sys_load=$(uptime | cut -d: -f5)
cat << EOF
【系统信息】
系统: ${sys_os}
发行版本: ${sys_release}
系统内核: ${sys_kernel}
主机名: ${sys_hostname}
selinux状态: ${sys_selinux}
系统语言: ${sys_lang}
系统当前时间: ${sys_time}
系统最后重启时间: ${sys_lastreboot}
系统运行时间: ${sys_runtime}
系统负载: ${sys_load}
EOF
}
获取CPU信息
get_cpu_info() {
Physical_CPUs=
(
g
r
e
p
"
p
h
y
s
i
c
a
l
i
d
"
/
p
r
o
c
/
c
p
u
i
n
f
o
∣
s
o
r
t
∣
u
n
i
q
∣
w
c
−
l
)
V
i
r
t
C
P
U
s
=
(grep "physical id" /proc/cpuinfo | sort | uniq | wc -l) Virt_CPUs=
(grep"physicalid"/proc/cpuinfo∣sort∣uniq∣wc−l)VirtCPUs=(grep “processor” /proc/cpuinfo | wc -l)
CPU_Kernels=$(grep “cores” /proc/cpuinfo | uniq | awk -F ': ’ '{print KaTeX parse error: Expected 'EOF', got '}' at position 2: 2}̲') CPU_Type=(grep “model name” /proc/cpuinfo | awk -F ': ’ '{print KaTeX parse error: Expected 'EOF', got '}' at position 2: 2}̲' | sort | uniq…(uname -m)
cat << EOF
【CPU信息】
物理CPU个数:
P
h
y
s
i
c
a
l
C
P
U
s
逻辑
C
P
U
个数
:
Physical_CPUs 逻辑CPU个数:
PhysicalCPUs逻辑CPU个数:Virt_CPUs
每CPU核心数:
C
P
U
K
e
r
n
e
l
s
C
P
U
型号
:
CPU_Kernels CPU型号:
CPUKernelsCPU型号:CPU_Type
CPU架构:$CPU_Arch
EOF
}
获取服务信息
get_service_info() {
port_listen=
(
n
e
t
s
t
a
t
−
l
n
t
u
p
∣
g
r
e
p
−
v
"
A
c
t
i
v
e
I
n
t
e
r
n
e
t
"
)
k
e
r
n
e
l
c
o
n
f
i
g
=
(netstat -lntup | grep -v "Active Internet") kernel_config=
(netstat−lntup∣grep−v"ActiveInternet")kernelconfig=(sysctl -p 2> /dev/null)
if [[
s
y
s
v
e
r
s
i
o
n
−
g
t
6
]
]
;
t
h
e
n
s
e
r
v
i
c
e
c
o
n
f
i
g
=
{sysversion} -gt 6 ]]; then service_config=
sysversion−gt6]];thenserviceconfig=(systemctl list-unit-files --type=service --state=enabled | grep “enabled”)
run_service=
(
s
y
s
t
e
m
c
t
l
l
i
s
t
−
u
n
i
t
s
−
−
t
y
p
e
=
s
e
r
v
i
c
e
−
−
s
t
a
t
e
=
r
u
n
n
i
n
g
∣
g
r
e
p
"
.
s
e
r
v
i
c
e
"
)
e
l
s
e
s
e
r
v
i
c
e
c
o
n
f
i
g
=
(systemctl list-units --type=service --state=running | grep ".service") else service_config=
(systemctllist−units−−type=service−−state=running∣grep".service")elseserviceconfig=(/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
}
获取系统内存信息
get_mem_info() {
check_mem=
(
f
r
e
e
−
m
)
M
e
m
T
o
t
a
l
=
(free -m) MemTotal=
(free−m)MemTotal=(grep MemTotal /proc/meminfo | awk '{print KaTeX parse error: Expected 'EOF', got '}' at position 2: 2}̲') #KB MemFre…(grep MemFree /proc/meminfo | awk '{print KaTeX parse error: Expected 'EOF', got '}' at position 2: 2}̲') #KB let Me…(awk “BEGIN {if(KaTeX parse error: Expected group as argument to '\"' at end of input: …rintf \"%.2f\",MemUsed*100/KaTeX parse error: Expected 'EOF', got '}' at position 9: MemTotal}̲}") report_Me…((MemTotal/1024))”“MB” #内存总容量(MB)
report_MemFree="KaTeX parse error: Expected 'EOF', got '#' at position 23: …ee/1024))""MB" #̲内存剩余(MB) repo…(free | sed -n ‘2p’ | gawk ‘x = int(( $3 /
2
)
∗
100
)
p
r
i
n
t
x
′
∣
s
e
d
′
s
/
2 ) * 100) {print x}' | sed 's/
2)∗100)printx′∣sed′s//%/’)
cat << EOF
【内存信息】
内存总容量(MB):
r
e
p
o
r
t
M
e
m
T
o
t
a
l
内存剩余量
(
M
B
)
:
{report_MemTotal} 内存剩余量(MB):
reportMemTotal内存剩余量(MB):{report_MemFree}
内存使用率: ${report_MemUsedPercent}
EOF
}
获取系统网络信息
get_net_info() {
pri_ipadd=$(ip addr | awk '/1+: / {}; /inet.global/ {print gensub(/(.)/(.*)/, “\1”, “g”, KaTeX parse error: Expected 'EOF', got '}' at position 3: 2)}̲') pub_ipadd=(curl ifconfig.me -s)
gateway=$(ip route | grep default | awk '{print KaTeX parse error: Expected 'EOF', got '}' at position 2: 3}̲') mac_info=(ip link | egrep -v “lo” | grep link | awk '{print KaTeX parse error: Expected 'EOF', got '}' at position 2: 2}̲') dns_config…(egrep -v "^KaTeX parse error: Expected group after '^' at position 2: |^̲#" /etc/resolv.…(route -n)
cat << EOF
【网络信息】
系统公网地址:
p
u
b
i
p
a
d
d
系统私网地址
:
{pub_ipadd} 系统私网地址:
pubipadd系统私网地址:{pri_ipadd}
网关地址:
g
a
t
e
w
a
y
M
A
C
地址
:
{gateway} MAC地址:
gatewayMAC地址:{mac_info}
路由信息:
${route_info}
DNS 信息:
${dns_config}
EOF
}
获取系统磁盘信息
get_disk_info() {
disk_info=
(
f
d
i
s
k
−
l
∣
g
r
e
p
"
D
i
s
k
/
d
e
v
"
∣
c
u
t
−
d
,
−
f
1
)
d
i
s
k
u
s
e
=
(fdisk -l | grep "Disk /dev" | cut -d, -f1) disk_use=
(fdisk−l∣grep"Disk/dev"∣cut−d,−f1)diskuse=(df -hTP | awk '
2
!
=
"
t
m
p
f
s
"
p
r
i
n
t
′
)
d
i
s
k
p
e
r
c
e
n
t
=
2!="tmpfs"{print}') disk_percent=
2!="tmpfs"print′)diskpercent=(free | sed -n ‘2p’ | gawk ‘x = int(( $3 /
2
)
∗
100
)
p
r
i
n
t
x
′
∣
s
e
d
′
s
/
2 ) * 100) {print x}' | sed 's/
2)∗100)printx′∣sed′s//%/’)
disk_inode=$(df -hiP | awk ‘$1!=“tmpfs”{print}’)
cat << EOF
【磁盘信息】
${disk_info}
磁盘使用: ${disk_use}
磁盘使用百分比: ${disk_percent}
inode信息: ${disk_inode}
EOF
}
获取系统用户信息
get_sys_user() {
login_user=KaTeX parse error: Expected '}', got 'EOF' at end of input: (awk -F: '{if (NF==“/bin/bash”) print KaTeX parse error: Expected 'EOF', got '}' at position 2: 0}̲' /etc/passwd) …(egrep -v "#|
"
/
e
t
c
/
s
s
h
/
s
s
h
d
c
o
n
f
i
g
)
s
u
d
o
c
o
n
f
i
g
=
" /etc/ssh/sshd_config) sudo_config=
"/etc/ssh/sshdconfig)sudoconfig=(egrep -v "#|
"
/
e
t
c
/
s
u
d
o
e
r
s
∣
g
r
e
p
−
v
"
D
e
f
a
u
l
t
s
"
)
h
o
s
t
c
o
n
f
i
g
=
" /etc/sudoers | grep -v "^Defaults") host_config=
"/etc/sudoers∣grep−v"Defaults")hostconfig=(egrep -v “#|
"
/
e
t
c
/
h
o
s
t
s
)
c
r
o
n
d
c
o
n
f
i
g
=
" /etc/hosts) crond_config=
"/etc/hosts)crondconfig=(for cronuser in /var/spool/cron/*; do
ls KaTeX parse error: Expected group after '^' at position 50: …-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
}
获取CPU/内存占用TOP10
get_process_top_info() {
top_title=
(
t
o
p
−
b
n
1
∣
h
e
a
d
−
7
∣
t
a
i
l
−
1
)
c
p
u
t
o
p
10
=
(top -b n1 | head -7 | tail -1) cpu_top10=
(top−bn1∣head−7∣tail−1)cputop10=(top -b n1 | head -17 | tail -11)
mem_top10=$(top -b n1 | head -17 | tail -10 | sort -k10 -r)
cat << EOF
【TOP10】
CPU占用TOP10:
${cpu_top10}
内存占用TOP10:
${top_title}
${mem_top10}
EOF
}
show_dead_process() {
printf “僵尸进程:\n”
ps -al | gawk ‘{print $2,$4}’ | grep Z
}
get_all_info() {
get_systatus_info
echo ${double_line}
get_service_info
echo ${double_line}
get_cpu_info
echo ${double_line}
get_net_info
echo ${double_line}
get_mem_info
echo ${double_line}
get_disk_info
echo ${double_line}
get_process_top_info
echo ${double_line}
get_sys_user
}
main() {
while [[ 1 ]]
do
printMenu
read option
local index=$[ ${option} - 1 ]
case KaTeX parse error: Expected '}', got 'EOF' at end of input: {options[{index}]} in
“获取系统信息”)
get_systatus_info ;;
“获取服务信息”)
get_service_info ;;
“获取CPU信息”)
get_cpu_info ;;
“获取系统网络信息”)
get_net_info ;;
“获取系统内存信息”)
get_mem_info ;;
“获取系统磁盘信息”)
get_disk_info ;;
“获取CPU/内存占用TOP10”)
get_process_top_info ;;
“获取系统用户信息”)
get_sys_user ;;
“输出所有信息”)
get_all_info > sys.log
printf “
C
G
R
E
E
N
信息已经输出到
s
y
s
.
l
o
g
中。
{C_GREEN}信息已经输出到 sys.log 中。
CGREEN信息已经输出到sys.log中。{C_RESET}\n\n”
;;
“退出”)
exit ;;
*)
clear
echo “抱歉,不支持此选项” ;;
esac
done
}
######################################## MAIN ########################################
printHeadInfo
main
printFootInfo
printf “${C_RESET}”
0-9 ↩︎