1 #!/bin/bash -
2 # 设置检测环境变量。
3 source /etc/profile
4 export LC_ALL=C
5 TMP_FILE=/tmp/check_tmp_file
6 CHECK_ID=$(id|sed -e 's/(.*$//' -e 's/^uid=//')
7 if [ $CHECK_ID -ne 0 ]
8 then
9 echo -e "\t你不是root用户!!"
10 exit 0
11 fi
12 # 检测信息
13 cat << EOF
14 +-------------------------------------------------------------------+
15 | 检测并收集操作系统信息 |
16 | |
17 | 脚本完成时间:`date +'%Y%m%d'` |
18 +-------------------------------------------------------------------+
19 EOF
20 echo "开始检测时间:$(date|awk '{ print $4}')"
21 echo "主机名:$(hostname)"
22 echo "系统连续运行时间:$(uptime|awk -F, '{ print $1,$2 }')"
23 echo "最后启动时间:$(who -b|awk '{ print $3,$4}')"
24 echo ''
25 echo "操作系统信息"
26 echo "操作系统版本:"
27 /usr/bin/which lsb_release 2>&1> /dev/null
28 if [ $? -eq 0 ]
29 then
30 echo "$(lsb_release -d|awk -F '\t' '{ print $2 }' 2> /dev/null)"
31 else
32 echo `cat /etc/redhat-release`
33 echo "未安装 lsb 相关 rpm 包"
34 fi
35 echo "当前启动内核信息:"
36 echo "$(uname -rm)"
37 echo "已经安装的内核包信息:"
38 echo "$(rpm -qa|grep -i ^kernel-[1-9])"
39 echo "已经存在的启动文件信息:"
40 echo "$(ls -l /boot/|egrep 'init|vmlin'|awk '{ print $9}')"
41 echo ""
42 echo "网络信息"
43 echo "网络地址:"
44 echo "$(ip addr|grep inet|egrep -v 'inet6|127.0.0.1'|awk '{ print $2 }'|awk -F/ '{ print $1 }')"
45 cat << EOF
46 网络地址信息:
47 $(ifconfig -a)
48 EOF
49 echo "网络适配器驱动模块信息:"
50 lspci|egrep 'Ethernet controller|Network controller'|awk '{ print $1}' > $TMP_FILE
51 while read line1
52 do
53 echo "$(lsmod|grep $(lspci -s $line1 -k|grep 'Kernel driver in use'|awk -F: '{ print $2 }'))"
54 done < $TMP_FILE
55 rm -f $TMP_FILE
56 echo ""
57 echo "网络适配器绑定信息:"
58 grep -i bond /etc/modprobe* 2>&1> /dev/null
59 if [ $? -eq 0 ]
60 then
61 lsmod|grep bonding > /dev/null && echo '网络适配器绑定配置正常!'
62 else
63 echo '网络适配器没有绑定配置!'
64 fi
65 echo ""
66 echo '网络连通性测试:'
67 DROP_NU=$(ping -c 100 $(route|grep UG|grep -i default|awk '{print $2}') -i 0.01|grep 'Destination Host Unreachable'|wc -l)
68 if [ $DROP_NU -eq 0 ]
69 then
70 echo "网络没有丢包!"
71 else
72 echo "连接错误: $DROP_NU !"
73 fi
74 echo ""
75 #echo 'RHN 注册信息:'
76 #RHN_INFO=$(rhn-channel -l 2>&1> /dev/null)
77 #if [ ${RHN_INFO} -eq 0 ]
78 #then
79 # echo "系统注册到 RHN"
80 #else
81 # echo "系统未注册到 RHN"
82 #fi
83 echo ""
84 echo "系统磁盘信息:"
85 echo "$(fdisk -l 2> /dev/null|grep '^Disk /dev/'|awk -F, '{ print $1 }')"
86 echo ""
87 echo "分区空间信息:"
88 echo "$(df -h|grep -vE 'tmpfs|none')"
89 echo ""
90 echo "分区 inode 号信息:"
91 echo "$(df -hi|grep -vE 'tmpfs|none')"
92 echo ""
93 echo '逻辑卷信息:'
94 echo "$(uname -r|grep 2.4.9 > /dev/null || lvscan 2> /dev/null)"
95 echo ''
96 echo 'UID 是 0 的用户:'
97 echo "$(awk -F: '$3==0 {print $1}' /etc/passwd)"
98 echo ''
99 echo '普通用户列表:'
100 echo "$(grep -v nobody /etc/passwd|awk -F: '$3>=500 {print $1}')"
101 echo ''
102 echo '未设置密码及未锁定用户列表:'
103 grep -v nobody /etc/passwd|awk -F: '$3>=500 {print $1}' > $TMP_FILE
104 while read line1
105 do
106 echo "$(grep $line1 /etc/shadow|grep :!)"
107 done < $TMP_FILE
108 rm -f $TMP_FILE
109 echo ''
110 echo "最后登录的 10 个用户:"
111 echo "$(last -R|head -n 10)"
112 echo ''
113 ROOT_MX=$(ls -l ~/Mail 2> /dev/null|wc -l)
114 if [ $ROOT_MX -eq 0 ]
115 then
116 echo 'root 用户没有告警邮件!'
117 else
118 echo "root 用户有 $(expr $ROOT_MX - 1) 封告警邮件!"
119 echo "$(ls -l ~/Mail)"
120 fi
121 echo ''
122 grep -v nobody /etc/passwd|awk -F: '$3>=500 {print $1}' > $TMP_FILE
123 while read line1
124 do
125 echo "用户 $line1 告警邮件:"
126 echo "$(su - $line1 -c 'ls -l ~/Mail' 2> /dev/null|grep -v 'total')"
127 done < $TMP_FILE
128 rm -f $TMP_FILE
129 echo ''
130 echo '系统内存/交换空间检测(间隔每3秒)'
131 echo "$(free -m -s 30 -c2)"
132 echo ''
133 echo "CPU使用率信息:"
134 /usr/bin/which lsb_release 2>&1> /dev/null
135 if [ $? -eq 0 ]
136 then
137 OS_ID=$(lsb_release -r|awk -F '\t' '{ print $2 }'|awk -F. '{ print $1 }' 2> /dev/null)
138 if [ $OS_ID -ne 9 ]
139 then
140 CPU_IDLE=$(top -b -n1|grep -i '^cpu'|awk -F, '{ print $4 }'|awk '{ print $1 }'|awk -F. '{ print $1 }')
141 if [[ $CPU_IDLE -ne 0 ]]
142 then
143 echo "CPU 未使用率 $CPU_IDLE%"
144 else
145 echo "CPU 未使用率 $(top -b -n1|grep 'total'|awk '{ print $8 }'|awk -F. '{ print $1 }')%"
146 fi
147 else
148 echo "CPU 未使用率 $(top -b -n1|grep -i '^cpu'|awk '{ print $11 }'|awk -F. '{ print $1 }')%"
149 fi
150 else
151 echo `cat /etc/redhat-release`
152 echo "未安装 lsb 相关 rpm 包"
153 fi
154 echo ""
155 if [[ $CPU_IDLE < 20 ]]
156 then
157 echo "CPU 未使用率 $($CPU_IDLE)% ,使用率 80%+"
158 fi
159 echo ''
160 echo "物理CPU个数: $(cat /proc/cpuinfo|grep "physical id"|sort|uniq|wc -l)"
161 echo "物理CPU核数: $(cat /proc/cpuinfo|grep "cores"|uniq|awk '{print $4}')"
162 echo "逻辑CPU个数: $(cat /proc/cpuinfo|grep "processor"|wc -l)"
163 echo "当前运行模式: $(getconf LONG_BIT)"
164 CPU_BIT=$(cat /proc/cpuinfo|grep flags|grep ' lm '|wc -l)
165 if [[ $CPU_BIT > 0 ]]
166 then
167 echo "支持 64 位运算模式"
168 else
169 echo "不支持 64 位运算模式"
170 fi
171 echo ''
172 echo 'CPU 负载信息:'
173 echo "$(top -b -n2|grep '^Cpu(s):')"
174 echo ''
175 Z_PID=$(ps aux|awk '{print $8,$2,$11}'|sed -n '/^Z/p')
176 IFS=${IFS:3:1}
177 for pid in $Z_PID
178 do
179 echo "系统中的僵尸进程: $(echo $pid|awk '{print $2,$3}')"
180 done
181 echo ''
182 echo '不可结束进程:'
183 echo "$(ps -eo pid,stat|grep -i 'stat=d')"
184 echo ''
185 echo '占用 CPU 最高的 10 个进程:'
186 echo "$(ps aux|head -1;ps aux|sort -k3nr|head -10)"
187 echo ''
188 echo '占用内存最高的 10 个进程:'
189 echo "$(ps aux|head -1;ps aux|sort -k4nr|head -10)"
190 echo ''
191 cat /boot/grub/grub.conf|grep 'crashkernel=' > /dev/null && echo "$(service kdump status)" || echo '未配置 Kdump 服务!'
192 echo "$(ls -l /var/crash/dump* 2> /dev/null)"
193 echo "$(ls -l /root/core.* 2> /dev/null)"
194 echo ''
195 echo "当前运行级别:$(runlevel|awk '{ print $2 }')"
196 echo ''
197 echo '在 $(runlevel|awk '{ print $2 }') 级别下开机启动服务信息:'
198 echo "$(chkconfig --list|grep $(runlevel|awk '{ print $2 }'):on)"
199 echo ''
200 echo '系统日志信息: /var/log/messages'
201 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)"
202 echo '系统日志信息: /var/log/secure'
203 echo "$(egrep -i "error|fail" /var/log/secure 2> /dev/null)"
204 echo '系统日志信息: /var/log/boot.log'
205 echo "$(egrep -i "error|fail" /var/log/boot.log 2> /dev/null)"
206 echo '系统日志信息: /var/log/dmesg'
207 echo "$(egrep -i "error|fail" /var/log/dmesg 2> /dev/null)"
208 echo ''
209 echo "系统级别计划任务:"
210 echo "$(cat /etc/crontab)"
211 echo ''
212 echo "root 用户计划任务:"
213 echo "$(crontab -l 2> /dev/null)"
214 echo ''
215 grep -v nobody /etc/passwd|awk -F: '$3>=500 {print $1}' > $TMP_FILE
216 while read line1
217 do
218 echo "$line1 用户计划任务:"
219 echo "$(su - $line1 -c 'crontab -l' 2> /dev/null)"
220 done < $TMP_FILE
221 rm -f $TMP_FILE
222 echo ''
223 echo "$(iostat -x 2> /dev/null || echo 'Sysstat 包没有安装!')"
224 echo "$(sar -u 3 10 2> /dev/null || echo 'Sysstat 包没有安装!')"
225 echo "$(sar -w 2> /dev/null || echo 'Sysstat 包没有安装!')"
226 echo '执行频率最高的 10 个历史命令:'
227 echo "$(sed -e 's/|/\n/g' ~/.bash_history|cut -d '' -f 1|sort|uniq -c|sort -nr|head)"
228 echo ''
229 # RHCS 检测脚本(RHEL4,RHEL5,RHEL6;kernel 2.6.+):
230 echo '--------------------------RHCS 检测脚本(RHEL4,RHEL5,RHEL6)-----------------'
231 echo "$(chkconfig --list|egrep "cman|ccsd|fenced|qdiskd|rgmanager" || echo '没有检测到集群相关服务!')"
232 echo "$(rpm -qa|egrep 'cman|ccsd|fenced|qdiskd|rgmanager' || echo '未安装集群套件相关 rpm 包!')"
233 echo '/etc/rc.local 文件内容:'
234 echo "$(egrep -v '^#|^$' /etc/rc.local)"
235 echo '/etc/hosts file contents:'
236 echo "$(egrep -v '^#|^:|^$' /etc/hosts)"
237 echo '集群当前状态:'
238 echo "$(clustat 2> /dev/null || echo '没有检测到集群信息!')"
239 echo "$(mkqdisk -L 2> /dev/null || echo '没有检测到 qdisk 信息!')"
240 echo "$(service cman status 2>&1)"
241 echo "$(service ccsd status 2>&1)"
242 echo "$(service fenced status 2>&1)"
243 echo "$(service qdiskd status 2>&1)"
244 echo "$(service rgmanager status 2>&1)"
245 echo '集群配置文件内容:'
246 echo "$(cat /etc/cluster/cluster.conf 2> /dev/null || echo '没有找到集群配置文件!')"
247 echo ''
248 #openssl 检测脚本 (RHEL4,RHEL5,RHEL6)
249 echo "search openssl verion:"
250 rpm -qa | grep openssl
251 echo "lsof openssl:"
252 lsof | grep libssl.so
253
254 echo "完成检测时间: $(date|awk '{ print $4}')!"