1. #!/usr/bin/env bash
    2. ##############################################################################
    3. # console color
    4. C_RESET="$(tput sgr0)"
    5. C_BLACK="\033[1;30m"
    6. C_RED="\033[1;31m"
    7. C_GREEN="\033[1;32m"
    8. C_YELLOW="\033[1;33m"
    9. C_BLUE="\033[1;34m"
    10. C_PURPLE="\033[1;35m"
    11. C_CYAN="\033[1;36m"
    12. C_WHITE="\033[1;37m"
    13. ##############################################################################
    14. printf "${C_PURPLE}"
    15. cat << EOF
    16. ###################################################################################
    17. # 系统信息检查脚本
    18. ###################################################################################
    19. EOF
    20. printf "${C_RESET}"
    21. [[ $(id -u) -gt 0 ]] && echo "请用root用户执行此脚本!" && exit 1
    22. sysversion=$(rpm -q centos-release | cut -d- -f3)
    23. double_line="==============================================================="
    24. line="----------------------------------------------"
    25. # 打印头部信息
    26. printHeadInfo() {
    27. cat << EOF
    28. +---------------------------------------------------------------------------------+
    29. | 欢迎使用 【系统信息检查脚本】 |
    30. +---------------------------------------------------------------------------------+
    31. EOF
    32. }
    33. # 打印尾部信息
    34. printFootInfo() {
    35. cat << EOF
    36. +---------------------------------------------------------------------------------+
    37. | 脚本执行结束,感谢使用!|
    38. +---------------------------------------------------------------------------------+
    39. EOF
    40. }
    41. options=( "获取系统信息" "获取服务信息" "获取CPU信息" "获取系统网络信息" "获取系统内存信息" "获取系统磁盘信息" "获取CPU/内存占用TOP10" "获取系统用户信息" "输出所有信息" "退出" )
    42. printMenu() {
    43. printf "${C_BLUE}"
    44. printf "主菜单:\n"
    45. for i in "${!options[@]}"; do
    46. index=`expr ${i} + 1`
    47. val=`expr ${index} % 2`
    48. printf "\t(%02d) %-30s" "${index}" "${options[$i]}"
    49. if [[ ${val} -eq 0 ]]; then
    50. printf "\n"
    51. fi
    52. done
    53. printf "${C_BLUE}请输入需要执行的指令:\n"
    54. printf "${C_RESET}"
    55. }
    56. # 获取系统信息
    57. get_systatus_info() {
    58. sys_os=$(uname -o)
    59. sys_release=$(cat /etc/redhat-release)
    60. sys_kernel=$(uname -r)
    61. sys_hostname=$(hostname)
    62. sys_selinux=$(getenforce)
    63. sys_lang=$(echo $LANG)
    64. sys_lastreboot=$(who -b | awk '{print $3,$4}')
    65. sys_runtime=$(uptime | awk '{print $3,$4}' | cut -d, -f1)
    66. sys_time=$(date)
    67. sys_load=$(uptime | cut -d: -f5)
    68. cat << EOF
    69. 【系统信息】
    70. 系统: ${sys_os}
    71. 发行版本: ${sys_release}
    72. 系统内核: ${sys_kernel}
    73. 主机名: ${sys_hostname}
    74. selinux状态: ${sys_selinux}
    75. 系统语言: ${sys_lang}
    76. 系统当前时间: ${sys_time}
    77. 系统最后重启时间: ${sys_lastreboot}
    78. 系统运行时间: ${sys_runtime}
    79. 系统负载: ${sys_load}
    80. EOF
    81. }
    82. # 获取CPU信息
    83. get_cpu_info() {
    84. Physical_CPUs=$(grep "physical id" /proc/cpuinfo | sort | uniq | wc -l)
    85. Virt_CPUs=$(grep "processor" /proc/cpuinfo | wc -l)
    86. CPU_Kernels=$(grep "cores" /proc/cpuinfo | uniq | awk -F ': ' '{print $2}')
    87. CPU_Type=$(grep "model name" /proc/cpuinfo | awk -F ': ' '{print $2}' | sort | uniq)
    88. CPU_Arch=$(uname -m)
    89. cat << EOF
    90. CPU信息】
    91. 物理CPU个数:$Physical_CPUs
    92. 逻辑CPU个数:$Virt_CPUs
    93. CPU核心数:$CPU_Kernels
    94. CPU型号:$CPU_Type
    95. CPU架构:$CPU_Arch
    96. EOF
    97. }
    98. # 获取服务信息
    99. get_service_info() {
    100. port_listen=$(netstat -lntup | grep -v "Active Internet")
    101. kernel_config=$(sysctl -p 2> /dev/null)
    102. if [[ ${sysversion} -gt 6 ]]; then
    103. service_config=$(systemctl list-unit-files --type=service --state=enabled | grep "enabled")
    104. run_service=$(systemctl list-units --type=service --state=running | grep ".service")
    105. else
    106. service_config=$(/sbin/chkconfig | grep -E ":on|:启用" | column -t)
    107. run_service=$(/sbin/service --status-all | grep -E "running")
    108. fi
    109. cat << EOF
    110. 【服务信息】
    111. ${service_config}
    112. ${line}
    113. 运行的服务:
    114. ${run_service}
    115. ${line}
    116. 监听端口:
    117. ${port_listen}
    118. ${line}
    119. 内核参考配置:
    120. ${kernel_config}
    121. EOF
    122. }
    123. # 获取系统内存信息
    124. get_mem_info() {
    125. check_mem=$(free -m)
    126. MemTotal=$(grep MemTotal /proc/meminfo | awk '{print $2}') #KB
    127. MemFree=$(grep MemFree /proc/meminfo | awk '{print $2}') #KB
    128. let MemUsed=MemTotal-MemFree
    129. MemPercent=$(awk "BEGIN {if($MemTotal==0){printf 100}else{printf \"%.2f\",$MemUsed*100/$MemTotal}}")
    130. report_MemTotal="$((MemTotal/1024))" "MB" #内存总容量(MB)
    131. report_MemFree="$((MemFree/1024))" "MB" #内存剩余(MB)
    132. report_MemUsedPercent=$(free | sed -n '2p' | gawk 'x = int(( $3 / $2 ) * 100) {print x}' | sed 's/$/%/')
    133. cat << EOF
    134. 【内存信息】
    135. 内存总容量(MB): ${report_MemTotal}
    136. 内存剩余量(MB):${report_MemFree}
    137. 内存使用率: ${report_MemUsedPercent}
    138. EOF
    139. }
    140. # 获取系统网络信息
    141. get_net_info() {
    142. pri_ipadd=$(ip addr | awk '/^[0-9]+: / {}; /inet.*global/ {print gensub(/(.*)\/(.*)/, "\\1", "g", $2)}')
    143. pub_ipadd=$(curl ifconfig.me -s)
    144. gateway=$(ip route | grep default | awk '{print $3}')
    145. mac_info=$(ip link | egrep -v "lo" | grep link | awk '{print $2}')
    146. dns_config=$(egrep -v "^$|^#" /etc/resolv.conf)
    147. route_info=$(route -n)
    148. cat << EOF
    149. 【网络信息】
    150. 系统公网地址:${pub_ipadd}
    151. 系统私网地址:${pri_ipadd}
    152. 网关地址:${gateway}
    153. MAC地址:${mac_info}
    154. 路由信息:
    155. ${route_info}
    156. DNS 信息:
    157. ${dns_config}
    158. EOF
    159. }
    160. # 获取系统磁盘信息
    161. get_disk_info() {
    162. disk_info=$(fdisk -l | grep "Disk /dev" | cut -d, -f1)
    163. disk_use=$(df -hTP | awk '$2!="tmpfs"{print}')
    164. disk_percent=$(free | sed -n '2p' | gawk 'x = int(( $3 / $2 ) * 100) {print x}' | sed 's/$/%/')
    165. disk_inode=$(df -hiP | awk '$1!="tmpfs"{print}')
    166. cat << EOF
    167. 【磁盘信息】
    168. ${disk_info}
    169. 磁盘使用: ${disk_use}
    170. 磁盘使用百分比: ${disk_percent}
    171. inode信息: ${disk_inode}
    172. EOF
    173. }
    174. # 获取系统用户信息
    175. get_sys_user() {
    176. login_user=$(awk -F: '{if ($NF=="/bin/bash") print $0}' /etc/passwd)
    177. ssh_config=$(egrep -v "^#|^$" /etc/ssh/sshd_config)
    178. sudo_config=$(egrep -v "^#|^$" /etc/sudoers | grep -v "^Defaults")
    179. host_config=$(egrep -v "^#|^$" /etc/hosts)
    180. crond_config=$(for cronuser in /var/spool/cron/*; do
    181. ls ${cronuser} 2> /dev/null | cut -d/ -f5; egrep -v "^$|^#" ${cronuser} 2> /dev/null;
    182. echo "";
    183. done)
    184. cat << EOF
    185. 【用户信息】
    186. 系统登录用户:
    187. ${login_user}
    188. ${line}
    189. ssh 配置信息:
    190. ${ssh_config}
    191. ${line}
    192. sudo 配置用户:
    193. ${sudo_config}
    194. ${line}
    195. 定时任务配置:
    196. ${crond_config}
    197. ${line}
    198. hosts 信息:
    199. ${host_config}
    200. EOF
    201. }
    202. # 获取CPU/内存占用TOP10
    203. get_process_top_info() {
    204. top_title=$(top -b n1 | head -7 | tail -1)
    205. cpu_top10=$(top -b n1 | head -17 | tail -11)
    206. mem_top10=$(top -b n1 | head -17 | tail -10 | sort -k10 -r)
    207. cat << EOF
    208. 【TOP10】
    209. CPU占用TOP10:
    210. ${cpu_top10}
    211. 内存占用TOP10:
    212. ${top_title}
    213. ${mem_top10}
    214. EOF
    215. }
    216. show_dead_process() {
    217. printf "僵尸进程:\n"
    218. ps -al | gawk '{print $2,$4}' | grep Z
    219. }
    220. get_all_info() {
    221. get_systatus_info
    222. echo ${double_line}
    223. get_service_info
    224. echo ${double_line}
    225. get_cpu_info
    226. echo ${double_line}
    227. get_net_info
    228. echo ${double_line}
    229. get_mem_info
    230. echo ${double_line}
    231. get_disk_info
    232. echo ${double_line}
    233. get_process_top_info
    234. echo ${double_line}
    235. get_sys_user
    236. }
    237. main() {
    238. while [[ 1 ]]
    239. do
    240. printMenu
    241. read option
    242. local index=$[ ${option} - 1 ]
    243. case ${options[${index}]} in
    244. "获取系统信息")
    245. get_systatus_info ;;
    246. "获取服务信息")
    247. get_service_info ;;
    248. "获取CPU信息")
    249. get_cpu_info ;;
    250. "获取系统网络信息")
    251. get_net_info ;;
    252. "获取系统内存信息")
    253. get_mem_info ;;
    254. "获取系统磁盘信息")
    255. get_disk_info ;;
    256. "获取CPU/内存占用TOP10")
    257. get_process_top_info ;;
    258. "获取系统用户信息")
    259. get_sys_user ;;
    260. "输出所有信息")
    261. get_all_info > sys.log
    262. printf "${C_GREEN}信息已经输出到 sys.log 中。${C_RESET}\n\n"
    263. ;;
    264. "退出")
    265. exit ;;
    266. *)
    267. clear
    268. echo "抱歉,不支持此选项" ;;
    269. esac
    270. done
    271. }
    272. ######################################## MAIN ########################################
    273. printHeadInfo
    274. main
    275. printFootInfo
    276. printf "${C_RESET}"