#!/bin/bash
sys:centos6.x/7.x
[ $(id -u) -ne 0 ] && echo "请用root用户执行此脚本!" && exit 1
sysversion=$(rpm -q centos-release|cut -d- -f3)
line="-------------------------------------------------"
[ -d logs ] || mkdir logs
#sys_check_file="logs/$(ip a show dev eth0|grep -w inet|awk '{print $2}'|awk -F '/' '{print $1}')-date +%Y%m%d
.txt"
sys_check_file="logs/$(ifconfig |awk 'NR==2{print $2}')-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)
#echo -e '\033[32m CPU信息:\033[0m'
echo -e '\033[05;32m CPU信息:\033[0m'
cat <<eof 2 | column -t 物理cpu个数: $physical_cpus 逻辑cpu个数: $virt_cpus 每cpu核心数: $cpu_kernels cpu型号: $cpu_type cpu架构: $cpu_arch eof } # 获取系统内存信息 function get_mem_info() { total="$(free" -m sed -n '2p' awk '{print $2"m"}') used="$(free" $3"m"}') rate="$(free" '{print""($3 $2)*100"%"}') echo -e '\033[05;31m 内存信息:\033[0m' cat <<eof 内存总容量:$total 内存已使用:$used 内存使用率:$rate 获取系统网络信息 get_net_info() pri_ipadd="$(ifconfig" |awk 'nr="=2{print" $2}') #pub_ipadd="$(curl" ip.sb>&1)
pub_ipadd=$(curl -s http://ddns.oray.com/checkip | awk -F ":" '{print $2}' | awk -F "<" 2 '{print $1}'|awk $1}') gateway="$(ip" route | grep default awk $3}') mac_info="$(ip" link| egrep -v "lo"|grep link|awk $2}') dns_config="$(egrep" 'nameserver' etc resolv.conf) route_info="$(route" -n) echo -e '\033[05;33m ip信息:\033[0m' cat <<eof column -t 系统公网地址: ${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" '\033[05;34m 磁盘信息:\033[0m' ${disk_info} 磁盘使用: ${disk_use} inode信息: ${disk_inode} 获取系统信息 get_systatus_info() sys_os="$(uname" -o) sys_release="$(cat" redhat-release) sys_kernel="$(uname" -r) sys_hostname="$(hostname)" sys_selinux="$(getenforce)" sys_lang="$(echo" $lang) sys_lastreboot="$(who" -b $3,$4}') sys_runtime="$(uptime" |awk $3,$4}'|cut sys_time="$(date)" sys_load="$(uptime" |cut -d: -f5) '\033[05;35m 系统信息:\033[0m' 系统: ${sys_os} 发行版本: ${sys_release} 系统内核: ${sys_kernel} 主机名: ${sys_hostname} selinux状态: ${sys_selinux} 系统语言: ${sys_lang} 系统当前时间: ${sys_time} 系统最后重启时间: ${sys_lastreboot} 系统运行时间: ${sys_runtime} 系统负载: ${sys_load} 获取服务信息 get_service_info() port_listen="$(netstat" -lntup|grep "active internet") kernel_config="$(sysctl" -p>/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")
fi
echo -e '\033[05;36m 服务启动配置:\033[0m'
cat <<eof 2 ${service_config} ${line} 运行的服务: ${run_service} 监听端口: ${port_listen} 内核参考配置: ${kernel_config} eof } function get_sys_user() { login_user="$(awk" -f: '{if ($nf="="/bin/bash")" print $0}' etc passwd) ssh_config="$(egrep" -v "^#|^$" ssh sshd_config) sudo_config="$(egrep" sudoers |grep "^defaults") host_config="$(egrep" hosts) crond_config="$(for" cronuser in var spool cron * ;do ls ${cronuser}>/dev/null|cut -d/ -f5;egrep -v "^$|^#" ${cronuser} 2>/dev/null;echo "";done)
echo -e '\033[05;37m 系统登录用户:\033[0m'
cat <<eof ${login_user} ${line} ssh 配置信息: ${ssh_config} sudo 配置用户: ${sudo_config} 定时任务配置: ${crond_config} 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" -17|tail -10|sort -k10 -r) echo -e '\033[05;38m cpu占用top10:\033[0m' cat <<eof ${top_title} ${cpu_top10} '\033[05;39m 内存占用top10:\033[0m' ${mem_top10} sys_check() get_cpu_info get_mem_info get_net_info get_disk_info get_systatus_info get_service_info get_sys_user process_top_info sys_check> ${sys_check_file}</eof></eof></"></eof>
执行成功之后会在当前目录下创建一个logs/目录,进入目录查看文件信息
Original: https://www.cnblogs.com/Jqazc/p/16669269.html
Author: 我真的兔了
Title: Linux巡检脚本
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/611770/
转载文章受原作者版权保护。转载请注明原作者出处!