请启用 Javascript 以查看内容

系统巡检脚本合集

 ·   ·  ☕ 11 分钟  ·  ✍ CNSRE

Linux系统巡检脚本

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#!/bin/bash -

# 设置检测环境变量。
source /etc/profile
export LC_ALL=C
TMP_FILE=/tmp/check_tmp_file

CHECK_ID=$(id|sed -e 's/(.*$//' -e 's/^uid=//')
if [ $CHECK_ID -ne 0 ]
then
    echo -e "\t你不是root用户!!"
exit 0
fi

# 检测信息

cat << EOF
    +-------------------------------------------------------------------+
    |                   检测并收集操作系统信息                          |
    |                                  |
    |                   脚本完成时间:`date +'%Y%m%d'`                          |
    +-------------------------------------------------------------------+
EOF

echo "开始检测时间:$(date|awk '{ print $4}')"
echo "主机名:$(hostname)"
echo "系统连续运行时间:$(uptime|awk -F, '{ print $1,$2 }')"
echo "最后启动时间:$(who -b|awk '{ print $3,$4}')"
echo ''

echo "操作系统信息"
echo "操作系统版本:"
/usr/bin/which lsb_release 2>&1> /dev/null
if [ $? -eq 0 ]
then
    echo "$(lsb_release -d|awk -F '\t' '{ print $2 }' 2> /dev/null)"
else
    echo `cat /etc/redhat-release`
    echo "未安装 lsb 相关 rpm 包"
fi

echo "当前启动内核信息:"
echo "$(uname -rm)"

echo "已经安装的内核包信息:"
echo "$(rpm -qa|grep -i ^kernel-[1-9])"

echo "已经存在的启动文件信息:"
echo "$(ls -l /boot/|egrep 'init|vmlin'|awk '{ print $9}')"

echo ""

echo "网络信息"
echo "网络地址:"
echo "$(ip addr|grep inet|egrep -v 'inet6|127.0.0.1'|awk '{ print $2 }'|awk -F/ '{ print $1 }')"

cat << EOF
网络地址信息:
$(ifconfig -a)
EOF

echo "网络适配器驱动模块信息:"
lspci|egrep 'Ethernet controller|Network controller'|awk '{ print $1}' > $TMP_FILE
while read line1
do
    echo "$(lsmod|grep $(lspci -s $line1 -k|grep 'Kernel driver in use'|awk -F: '{ print $2 }'))"
done < $TMP_FILE
rm -f $TMP_FILE

echo ""

echo "网络适配器绑定信息:"
grep -i bond /etc/modprobe* 2>&1> /dev/null
if [ $? -eq 0 ]
then
    lsmod|grep bonding > /dev/null && echo '网络适配器绑定配置正常!'
else
    echo '网络适配器没有绑定配置!'
fi

echo ""

echo '网络连通性测试:'
DROP_NU=$(ping -c 100 $(route|grep UG|grep -i default|awk '{print $2}') -i 0.01|grep 'Destination Host Unreachable'|wc -l)
if [ $DROP_NU -eq 0 ]
then
    echo "网络没有丢包!"
else
    echo "连接错误: $DROP_NU !"
fi

echo ""

#echo 'RHN 注册信息:'
#RHN_INFO=$(rhn-channel -l 2>&1> /dev/null)
#if [ ${RHN_INFO} -eq 0 ]
#then
#    echo "系统注册到 RHN"
#else
#    echo "系统未注册到 RHN"
#fi

echo ""

echo "系统磁盘信息:"
echo "$(fdisk -l 2> /dev/null|grep '^Disk /dev/'|awk -F, '{ print $1 }')"
echo ""

echo "分区空间信息:"
echo "$(df -h|grep -vE 'tmpfs|none')"
echo ""

echo "分区 inode 号信息:"
echo "$(df -hi|grep -vE 'tmpfs|none')"
echo ""

echo '逻辑卷信息:'
echo "$(uname -r|grep 2.4.9 > /dev/null || lvscan 2> /dev/null)"
echo ''

echo 'UID 是 0 的用户:'
echo "$(awk -F: '$3==0 {print $1}' /etc/passwd)"
echo ''

echo '普通用户列表:'
echo "$(grep -v nobody /etc/passwd|awk -F: '$3>=500 {print $1}')"
echo ''

echo '未设置密码及未锁定用户列表:'
grep -v nobody /etc/passwd|awk -F: '$3>=500 {print $1}' > $TMP_FILE 
while read line1
do
    echo "$(grep $line1 /etc/shadow|grep :!)"
done < $TMP_FILE
rm -f $TMP_FILE
echo ''

echo "最后登录的 10 个用户:"
echo "$(last -R|head -n 10)"
echo ''

ROOT_MX=$(ls -l ~/Mail 2> /dev/null|wc -l)
if [ $ROOT_MX -eq 0 ]
then
    echo 'root 用户没有告警邮件!'
else
    echo "root 用户有 $(expr $ROOT_MX - 1) 封告警邮件!"
    echo "$(ls -l ~/Mail)"
fi
echo ''

grep -v nobody /etc/passwd|awk -F: '$3>=500 {print $1}' > $TMP_FILE 
while read line1
do
    echo "用户 $line1 告警邮件:"
    echo "$(su - $line1 -c 'ls -l ~/Mail' 2> /dev/null|grep -v 'total')"
done < $TMP_FILE
rm -f $TMP_FILE
echo ''

echo '系统内存/交换空间检测(间隔每3秒)'
echo "$(free -m -s 30 -c2)"
echo ''

echo "CPU使用率信息:"
/usr/bin/which lsb_release 2>&1> /dev/null
if [ $? -eq 0 ]
then
    OS_ID=$(lsb_release -r|awk -F '\t' '{ print $2 }'|awk -F. '{ print $1 }' 2> /dev/null)
    if [ $OS_ID -ne 9 ]
    then
        CPU_IDLE=$(top -b -n1|grep -i '^cpu'|awk -F, '{ print $4 }'|awk '{ print $1 }'|awk -F. '{ print $1 }')
        if [[ $CPU_IDLE -ne 0 ]]
        then
            echo "CPU 未使用率 $CPU_IDLE%"
        else
            echo "CPU 未使用率 $(top -b -n1|grep 'total'|awk '{ print $8 }'|awk -F. '{ print $1 }')%"
        fi

    else
        echo "CPU 未使用率 $(top -b -n1|grep -i '^cpu'|awk '{ print $11 }'|awk -F. '{ print $1 }')%"
    fi
else
    echo `cat /etc/redhat-release`
    echo "未安装 lsb 相关 rpm 包"
fi

echo ""

if [[ $CPU_IDLE < 20 ]]
then
    echo "CPU 未使用率 $($CPU_IDLE)% ,使用率 80%+"
fi
echo ''

echo "物理CPU个数: $(cat /proc/cpuinfo|grep "physical id"|sort|uniq|wc -l)"
echo "物理CPU核数: $(cat /proc/cpuinfo|grep "cores"|uniq|awk '{print $4}')"
echo "逻辑CPU个数: $(cat /proc/cpuinfo|grep "processor"|wc -l)"
echo "当前运行模式: $(getconf LONG_BIT)"
CPU_BIT=$(cat /proc/cpuinfo|grep flags|grep ' lm '|wc -l)
if [[ $CPU_BIT > 0 ]]
then
    echo "支持 64 位运算模式"
else
    echo "不支持 64 位运算模式"
fi

echo ''
echo 'CPU 负载信息:'
echo "$(top -b -n2|grep '^Cpu(s):')"
echo ''

Z_PID=$(ps aux|awk '{print $8,$2,$11}'|sed -n '/^Z/p')
IFS=${IFS:3:1}
for pid in $Z_PID
do
    echo "系统中的僵尸进程: $(echo $pid|awk '{print $2,$3}')"
done
echo ''

echo '不可结束进程:'
echo "$(ps -eo pid,stat|grep -i 'stat=d')"
echo ''

echo '占用 CPU 最高的 10 个进程:'
echo "$(ps aux|head -1;ps aux|sort -k3nr|head -10)"
echo ''

echo '占用内存最高的 10 个进程:'
echo "$(ps aux|head -1;ps aux|sort -k4nr|head -10)"
echo ''

cat /boot/grub/grub.conf|grep 'crashkernel=' > /dev/null && echo "$(service kdump status)" || echo '未配置 Kdump 服务!'
echo "$(ls -l /var/crash/dump* 2> /dev/null)"
echo "$(ls -l /root/core.* 2> /dev/null)"
echo ''

echo "当前运行级别:$(runlevel|awk '{ print $2 }')"
echo ''
echo '在 $(runlevel|awk '{ print $2 }') 级别下开机启动服务信息:'
echo "$(chkconfig --list|grep $(runlevel|awk '{ print $2 }'):on)"
echo ''

echo '系统日志信息: /var/log/messages'
echo "$(egrep -i "error|fail|scsi reset|file system full|Warning|token was lost|fencing|rejecting I/O to offline device|segfault|CPU#|Call Trace" /var/log/messages 2> /dev/null)"
echo '系统日志信息: /var/log/secure'
echo "$(egrep -i "error|fail" /var/log/secure 2> /dev/null)"
echo '系统日志信息: /var/log/boot.log'
echo "$(egrep -i "error|fail" /var/log/boot.log 2> /dev/null)"
echo '系统日志信息: /var/log/dmesg'
echo "$(egrep -i "error|fail" /var/log/dmesg 2> /dev/null)"
echo ''

echo "系统级别计划任务:"
echo "$(cat /etc/crontab)"
echo ''

echo "root 用户计划任务:"
echo "$(crontab -l 2> /dev/null)"
echo ''

grep -v nobody /etc/passwd|awk -F: '$3>=500 {print $1}' > $TMP_FILE 
while read line1
do
    echo "$line1 用户计划任务:"
    echo "$(su - $line1 -c 'crontab -l' 2> /dev/null)"
done < $TMP_FILE
rm -f $TMP_FILE
echo ''

echo "$(iostat -x 2> /dev/null || echo 'Sysstat 包没有安装!')"
echo "$(sar -u 3 10 2> /dev/null || echo 'Sysstat 包没有安装!')"
echo "$(sar -w 2> /dev/null || echo 'Sysstat 包没有安装!')"

echo '执行频率最高的 10 个历史命令:'
echo "$(sed -e 's/|/\n/g' ~/.bash_history|cut -d '' -f 1|sort|uniq -c|sort -nr|head)"
echo ''

# RHCS 检测脚本(RHEL4,RHEL5,RHEL6;kernel 2.6.+):
echo '--------------------------RHCS 检测脚本(RHEL4,RHEL5,RHEL6)-----------------'
echo "$(chkconfig --list|egrep "cman|ccsd|fenced|qdiskd|rgmanager" || echo '没有检测到集群相关服务!')"
echo "$(rpm -qa|egrep 'cman|ccsd|fenced|qdiskd|rgmanager' || echo '未安装集群套件相关 rpm 包!')"
echo '/etc/rc.local 文件内容:'
echo "$(egrep -v '^#|^$' /etc/rc.local)"
echo '/etc/hosts file contents:'
echo "$(egrep -v '^#|^:|^$' /etc/hosts)"
echo '集群当前状态:'
echo "$(clustat 2> /dev/null || echo '没有检测到集群信息!')"
echo "$(mkqdisk -L 2> /dev/null || echo '没有检测到 qdisk 信息!')"
echo "$(service cman status 2>&1)"
echo "$(service ccsd status 2>&1)"
echo "$(service fenced status 2>&1)"
echo "$(service qdiskd status 2>&1)"
echo "$(service rgmanager status 2>&1)"
echo '集群配置文件内容:'
echo "$(cat /etc/cluster/cluster.conf 2> /dev/null || echo '没有找到集群配置文件!')"
echo ''

#openssl 检测脚本 (RHEL4,RHEL5,RHEL6)

echo "search openssl verion:"
rpm -qa | grep openssl
echo "lsof openssl:"
lsof | grep libssl.so 


echo "完成检测时间: $(date|awk '{ print $4}')!"

根据指令展示不同的系统数据

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#!/bin/bash

os_check() {
        if [ -e /etc/redhat-release ]; then
                REDHAT=`cat /etc/redhat-release |cut -d' '  -f1`
        else
                DEBIAN=`cat /etc/issue |cut -d' ' -f1`
        fi
        if [ "$REDHAT" == "CentOS" -o "$REDHAT" == "Red" ]; then
                P_M=yum
        elif [ "$DEBIAN" == "Ubuntu" -o "$DEBIAN" == "ubutnu" ]; then
                P_M=apt-get
        else
                Operating system does not support.
                exit 1
        fi
}
if [ $LOGNAME != root ]; then
    echo "Please use the root account operation."
    exit 1
fi
if ! which vmstat &>/dev/null; then
        echo "vmstat command not found, now the install."
        sleep 1
        os_check
        $P_M install procps -y
        echo "-----------------------------------------------------------------------"
fi
if ! which iostat &>/dev/null; then
        echo "iostat command not found, now the install."
        sleep 1
        os_check
        $P_M install sysstat -y
        echo "-----------------------------------------------------------------------"
fi
 
while true; do
    select input in cpu_load disk_load disk_use disk_inode mem_use tcp_status cpu_top10 mem_top10 traffic quit; do
        case $input in
            cpu_load)
                #CPU利用率与负载
                echo "---------------------------------------"
                i=1
                while [[ $i -le 3 ]]; do
                    echo -e "\033[32m  参考值${i}\033[0m"
                    UTIL=`vmstat |awk '{if(NR==3)print 100-$15"%"}'`
                    USER=`vmstat |awk '{if(NR==3)print $13"%"}'`
                    SYS=`vmstat |awk '{if(NR==3)print $14"%"}'`
                    IOWAIT=`vmstat |awk '{if(NR==3)print $16"%"}'`
                    echo "Util: $UTIL"
                    echo "User use: $USER"
                    echo "System use: $SYS"
                    echo "I/O wait: $IOWAIT"
                    i=$(($i+1))
                    sleep 1
                done
                echo "---------------------------------------"
                break
                ;;
            disk_load)
                #硬盘I/O负载
                echo "---------------------------------------"
                i=1
                while [[ $i -le 3 ]]; do
                    echo -e "\033[32m  参考值${i}\033[0m"
                    UTIL=`iostat -x -k |awk '/^[v|s]/{OFS=": ";print $1,$NF"%"}'`
                    READ=`iostat -x -k |awk '/^[v|s]/{OFS=": ";print $1,$6"KB"}'`
                    WRITE=`iostat -x -k |awk '/^[v|s]/{OFS=": ";print $1,$7"KB"}'`
                    IOWAIT=`vmstat |awk '{if(NR==3)print $16"%"}'`
                    echo -e "Util:"
                    echo -e "${UTIL}"
                    echo -e "I/O Wait: $IOWAIT"
                    echo -e "Read/s:\n$READ"
                    echo -e "Write/s:\n$WRITE"
                    i=$(($i+1))
                    sleep 1
                done
                echo "---------------------------------------"
                break
                ;;
            disk_use)
                #硬盘利用率
                DISK_LOG=/tmp/disk_use.tmp
                DISK_TOTAL=`fdisk -l |awk '/^Disk.*bytes/&&/\/dev/{printf $2" ";printf "%d",$3;print "GB"}'`
                USE_RATE=`df -h |awk '/^\/dev/{print int($5)}'`
                for i in $USE_RATE; do
                    if [ $i -gt 90 ];then
                        PART=`df -h |awk '{if(int($5)=='''$i''') print $6}'`
                        echo "$PART = ${i}%" >> $DISK_LOG
                    fi
                done
                echo "---------------------------------------"
                echo -e "Disk total:\n${DISK_TOTAL}"
                if [ -f $DISK_LOG ]; then
                    echo "---------------------------------------"
                    cat $DISK_LOG
                    echo "---------------------------------------"
                    rm -f $DISK_LOG
                else
                    echo "---------------------------------------"
                    echo "Disk use rate no than 90% of the partition."
                    echo "---------------------------------------"
                fi
                break
                ;;
            disk_inode)
                #硬盘inode利用率
                INODE_LOG=/tmp/inode_use.tmp
                INODE_USE=`df -i |awk '/^\/dev/{print int($5)}'`
                for i in $INODE_USE; do
                    if [ $i -gt 90 ]; then
                        PART=`df -h |awk '{if(int($5)=='''$i''') print $6}'`
                        echo "$PART = ${i}%" >> $INODE_LOG
                    fi
                done
                if [ -f $INODE_LOG ]; then
                    echo "---------------------------------------"
                    rm -f $INODE_LOG
                else
                    echo "---------------------------------------"
                    echo "Inode use rate no than 90% of the partition."
                    echo "---------------------------------------"
                fi
                break
                ;;
            mem_use)
                #内存利用率
                echo "---------------------------------------"
                MEM_TOTAL=`free -m |awk '{if(NR==2)printf "%.1f",$2/1024}END{print "G"}'`
                USE=`free -m |awk '{if(NR==3) printf "%.1f",$3/1024}END{print "G"}'`
                FREE=`free -m |awk '{if(NR==3) printf "%.1f",$4/1024}END{print "G"}'`
                CACHE=`free -m |awk '{if(NR==2) printf "%.1f",($6+$7)/1024}END{print "G"}'`
                echo -e "Total: $MEM_TOTAL"
                echo -e "Use: $USE"
                echo -e "Free: $FREE"
                echo -e "Cache: $CACHE"
                echo "---------------------------------------"
                break
                ;;
            tcp_status)
                #网络连接状态
                echo "---------------------------------------"
                COUNT=`netstat -antp |awk '{status[$6]++}END{for(i in status) print i,status[i]}'`
                echo -e "TCP connection status:\n$COUNT"
                echo "---------------------------------------"
                ;;
            cpu_top10)
                #占用CPU高的前10个进程
                echo "---------------------------------------"
                CPU_LOG=/tmp/cpu_top.tmp
                i=1
                while [[ $i -le 3 ]]; do
                    #ps aux |awk '{if($3>0.1)print "CPU: "$3"% -->",$11,$12,$13,$14,$15,$16,"(PID:"$2")" |"sort -k2 -nr |head -n 10"}' > $CPU_LOG
                    ps aux |awk '{if($3>0.1){{printf "PID: "$2" CPU: "$3"% --> "}for(i=11;i<=NF;i++)if(i==NF)printf $i"\n";else printf $i}}' |sort -k4 -nr |head -10 > $CPU_LOG
                    #循环从11列(进程名)开始打印,如果i等于最后一行,就打印i的列并换行,否则就打印i的列
                    if [[ -n `cat $CPU_LOG` ]]; then
                       echo -e "\033[32m  参考值${i}\033[0m"
                       cat $CPU_LOG
                       > $CPU_LOG
                    else
                        echo "No process using the CPU." 
                        break
                    fi
                    i=$(($i+1))
                    sleep 1
                done
                echo "---------------------------------------"
                break
                ;;
            mem_top10)
                #占用内存高的前10个进程
                echo "---------------------------------------"
                MEM_LOG=/tmp/mem_top.tmp
                i=1
                while [[ $i -le 3 ]]; do
                    #ps aux |awk '{if($4>0.1)print "Memory: "$4"% -->",$11,$12,$13,$14,$15,$16,"(PID:"$2")" |"sort -k2 -nr |head -n 10"}' > $MEM_LOG
                    ps aux |awk '{if($4>0.1){{printf "PID: "$2" Memory: "$3"% --> "}for(i=11;i<=NF;i++)if(i==NF)printf $i"\n";else printf $i}}' |sort -k4 -nr |head -10 > $MEM_LOG
                    if [[ -n `cat $MEM_LOG` ]]; then
                        echo -e "\033[32m  参考值${i}\033[0m"
                        cat $MEM_LOG
                        > $MEM_LOG
                    else
                        echo "No process using the Memory."
                        break
                    fi
                    i=$(($i+1))
                    sleep 1
                done
                echo "---------------------------------------"
                break
                ;;
            traffic)
                #查看网络流量
                while true; do
                    read -p "Please enter the network card name(eth[0-9] or em[0-9]): " eth
                    #if [[ $eth =~ ^eth[0-9]$ ]] || [[ $eth =~ ^em[0-9]$ ]] && [[ `ifconfig |grep -c "\<$eth\>"` -eq 1 ]]; then
                    if [ `ifconfig |grep -c "\<$eth\>"` -eq 1 ]; then
                            break
                    else
                        echo "Input format error or Don't have the card name, please input again."
                    fi
                done
                echo "---------------------------------------"
                echo -e " In ------ Out"
                i=1
                while [[ $i -le 3 ]]; do
                    #OLD_IN=`ifconfig $eth |awk '/RX bytes/{print $2}' |cut -d: -f2`
                    #OLD_OUT=`ifconfig $eth |awk '/RX bytes/{print $6}' |cut -d: -f2`
                    OLD_IN=`ifconfig $eth |awk -F'[: ]+' '/bytes/{if(NR==8)print $4;else if(NR==5)print $6}'`
                    #CentOS6和CentOS7 ifconfig输出进出流量信息位置不同,CentOS6中RX与TX行号等于8,CentOS7中RX行号是5,TX行号是5,所以就做了个判断.       
                    OLD_OUT=`ifconfig $eth |awk -F'[: ]+' '/bytes/{if(NR==8)print $9;else if(NR==7)print $6}'`
                    sleep 1
                    NEW_IN=`ifconfig $eth |awk -F'[: ]+' '/bytes/{if(NR==8)print $4;else if(NR==5)print $6}'`
                    NEW_OUT=`ifconfig $eth |awk -F'[: ]+' '/bytes/{if(NR==8)print $9;else if(NR==7)print $6}'`
                    IN=`awk 'BEGIN{printf "%.1f\n",'$((${NEW_IN}-${OLD_IN}))'/1024/128}'`
                    OUT=`awk 'BEGIN{printf "%.1f\n",'$((${NEW_OUT}-${OLD_OUT}))'/1024/128}'`
                    echo "${IN}MB/s ${OUT}MB/s"
                    i=$(($i+1))
                    sleep 1
                done
                echo "---------------------------------------"
                break
                ;;
                        quit)
                                exit 0
                                ;;
               *)
                    echo "---------------------------------------"
                    echo "Please enter the number." 
                    echo "---------------------------------------"
                    break
                    ;;
        esac
    done
done

显示简单的系统信息

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/bin/bash

#[网络部分]
net_work=`[[ $(curl -o /dev/null --connect-timeout 3 -s -w "%{http_code}" www.baidu.com) -eq 200 ]] && echo yes || echo no`
ip_local=`ip addr | egrep -o '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | egrep -v  "^127" | head -n 1`

#[cpu部分]
cpu_info=`awk -F: '/model name/ {name=$2} END {print name}' /proc/cpuinfo | sed 's/^[ \t]*//;s/[ \t]*$//'`
cpu_pinlv=`awk -F: '/cpu MHz/ {freq=$2} END {print freq}' /proc/cpuinfo | sed 's/^[ \t]*//;s/[ \t]*$//'`
cpu_hexin=`awk -F: '/model name/ {core++} END {print core}' /proc/cpuinfo`

#[内存部分]
mem_zong=`free -m | awk '/Mem/ {print $2}'`
men_sheng=`free -m | awk '/Mem/ {print $4}'`
swa_zong=`free -m | awk '/Swap/ {print $2}'`
swa_sheng=`free -m | awk '/Swap/ {print $4}'`

#[硬盘部分]
disk_zong=`df -Th | grep '/dev/' | awk '{print $3}' | head -n 1`
disk_sheng=`df -Th | grep '/dev/' | awk '{print $5}' | head -n 1`

#[其它]
time_local=`awk '{a=$1/86400;b=($1%86400)/3600;c=($1%3600)/60;d=$1%60} {printf("%ddays, %d:%d:%d\n",a,b,c,d)}' /proc/uptime`
jiagou=`uname -m`
hostname=`hostname`

#判断是否centos或ubuntu
if cat /proc/version | grep -Eqi "ubuntu"; then
        banben=`lsb_release -a`
elif cat /proc/version | grep -Eqi "centos|red hat|redhat"; then
        banben=`awk '{print ($1,$3~/^[0-9]/?$3:$4)}' /etc/redhat-release`
fi

cn_info() { #全部信息
    clear
    echo "主机名:  $hostname"
    echo "版本:    $banben"
    echo "架构:    $jiagou"
    echo
    echo "cpu信息: $cpu_info"
    echo "cpu频率: $cpu_pinlv"
    echo "cpu核心: $cpu_hexin"
    echo
    echo "总内存:  $mem_zong"
    echo "剩内存:  $men_sheng"
    echo "总swap:  $swa_zong"
    echo "剩swap:  $swa_sheng"
    echo
    echo "根目录:  $disk_zong"
    echo "根剩余:  $disk_sheng"
    echo
    echo "是否联网:$net_work"
    echo "本地ip:  $ip_local"
    echo
    echo "开机:    $time_local"
}

cn_info

查看CPU内存磁盘信息

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/bin/bash
#输入不同字符完成不同巡检内容
#打印提示符

HINT(){
        read -p "请按回车继续:"
}

#查看CPU信息

CPU_INFO(){

        echo

        echo -e "\033[4;31mPrint the CPU info:\033[0m"

        cat /proc/cpuinfo | awk 'BEGIN {FS=":"} /model name/{print "CPU Model:" $2 }'

        cat /proc/cpuinfo | awk 'BEGIN {FS=":"} /cpu MHz/{print "CPU Speed:" $2"MHz"}'

        grep -Eq 'svm|vmx' /proc/cpuinfo && echo "Virtualization: Support" || \

        echo "Virtualization: No Support"

        echo

}

#查看系统负载

LOAD_INFO(){

        echo -e "\033[4;31mPrint the system load:\033[0m"

        uptime | awk 'BEGIN{FS=":"} {print $5}' | awk 'BEGIN{FS=","}\
        {print "Last 1 minutes system load:"$1"\n""Last 5 minutes system load:"$2"\n"\
        "Last 15 minutes system load:"$3}'

echo

}

#查看内存与交换分区信息

MEM_INFO(){

        echo

        echo -e "\033[4;31mPrint the Memory and Swap info:\033[0m"

        free | grep Mem | awk '{print "Mem free: "$5" Bytes"}'

        free | grep Swap | awk '{print "Swap free: "$4" Bytes"}'

        echo

}

#查看磁盘挂载信息

DISK_INFO(){

        echo

        echo -e "\033[4;31mPrint system disk space usage:\033[0m"

        df -h

        echo

}

while true
do
        clear
        echo "------------------------------------------------------"

        echo "1. 查看CPU信息"

        echo "2. 查看系统负载"

        echo "3. 查看内存与交换分区信息"

        echo "4. 查看磁盘挂载信息"

        echo "5. 退出系统"
        echo "-------------------------------------------------------"
        read -p "请选择1-4选项:" U_SELECT
        #通过调用函数名称调用函数
        case $U_SELECT in
                1)
                CPU_INFO
                HINT
                ;;
                2)
                LOAD_INFO
                HINT
                ;;
                3)
                MEM_INFO
                HINT
                ;;
                4)
                DISK_INFO
                HINT
                ;;
                5)
                exit
                ;;
                *)
                read -p "请选择1-4选项,输入回车继续:"
                ;;
        esac
done


文章链接
https://www.cnsre.cn/posts/210317132104/

分享

CNSRE
作者
CNSRE
一个📚学习中的👨‍💻SRE运维工程师🚀🚀🚀


目录