Linux 系统日常巡检脚本


Linux 系统日常巡检脚本

文章插图
 
linux 系统日常巡检脚本 , 巡检内容包含了 , 磁盘 , 内存 cpu 进程 文件更改 用户登录等一系列的操作 直接用就行了 。
报告以邮件发送到邮箱 在log下生成巡检报告 。
#!/bin/bash# @Author: HanWei# @Date:2020-03-16 09:56:57# @Last Modified by:HanWei# @Last Modified time: 2020-03-16 11:06:31# @E-mail: han_wei_95@163.com#!/bin/bash#主机信息每日巡检IPADDR=$(ifconfig eth0|grep 'inet addr'|awk -F '[ :]' '{print $13}')#环境变量PATH没设好 , 在cron里执行时有很多命令会找不到export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/binsource /etc/profile[ $(id -u) -gt 0 ] && echo "请用root用户执行此脚本!" && exit 1centosVersion=$(awk '{print $(NF-1)}' /etc/redhat-release)VERSION="2020-03-16"#日志相关PROGPATH=`echo $0 | sed -e 's,[\/][^\/][^\/]*$,,'`[ -f $PROGPATH ] && PROGPATH="."LOGPATH="$PROGPATH/log"[ -e $LOGPATH ] || mkdir $LOGPATHRESULTFILE="$LOGPATH/HostDailyCheck-$IPADDR-`date +%Y%m%d`.txt"#定义报表的全局变量report_DateTime="" #日期 okreport_Hostname="" #主机名 okreport_OSRelease="" #发行版本 okreport_Kernel="" #内核 okreport_Language="" #语言/编码 okreport_LastReboot="" #最近启动时间 okreport_Uptime="" #运行时间(天) okreport_CPUs="" #CPU数量 okreport_CPUType="" #CPU类型 okreport_Arch="" #CPU架构 okreport_MemTotal="" #内存总容量(MB) okreport_MemFree="" #内存剩余(MB) okreport_MemUsedPercent="" #内存使用率% okreport_DiskTotal="" #硬盘总容量(GB) okreport_DiskFree="" #硬盘剩余(GB) okreport_DiskUsedPercent="" #硬盘使用率% okreport_InodeTotal="" #Inode总量 okreport_InodeFree="" #Inode剩余 okreport_InodeUsedPercent="" #Inode使用率 okreport_IP="" #IP地址 okreport_mac="" #MAC地址 okreport_Gateway="" #默认网关 okreport_DNS="" #DNS okreport_Listen="" #监听 okreport_Selinux="" #Selinux okreport_Firewall="" #防火墙 okreport_USERs="" #用户 okreport_USEREmptyPassword="" #空密码用户 okreport_USERTheSameUID="" #相同ID的用户 ok report_PasswordExpiry="" #密码过期(天) okreport_RootUser="" #root用户 okreport_Sudoers="" #sudo授权 okreport_SSHAuthorized="" #SSH信任主机 okreport_SSHDProtocolVersion="" #SSH协议版本 okreport_SSHDPermitRootLogin="" #允许root远程登录 okreport_DefunctProsess="" #僵尸进程数量 okreport_SelfInitiatedService="" #自启动服务数量 okreport_SelfInitiatedProgram="" #自启动程序数量 okreport_RuningService="" #运行中服务数 okreport_Crontab="" #计划任务数 okreport_Syslog="" #日志服务 okreport_SNMP="" #SNMP OKreport_NTP="" #NTP okreport_JDK="" #JDK版本 okfunction version(){echo ""echo ""echo "系统巡检脚本:Version $VERSION"}function getCpuStatus(){echo ""echo ""echo "############################ CPU检查 #############################"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 "物理CPU个数:$Physical_CPUs"echo "逻辑CPU个数:$Virt_CPUs"echo "每CPU核心数:$CPU_Kernels"echo " CPU型号:$CPU_Type"echo " CPU架构:$CPU_Arch"#报表信息report_CPUs=$Virt_CPUs #CPU数量report_CPUType=$CPU_Type #CPU类型report_Arch=$CPU_Arch #CPU架构}function getMemStatus(){echo ""echo ""echo "############################ 内存检查 ############################"if [[ $centosVersion < 7 ]];thenfree -moelsefree -hfi#报表信息MemTotal=$(grep MemTotal /proc/meminfo| awk '{print $2}') #KBMemFree=$(grep MemFree /proc/meminfo| awk '{print $2}') #KBlet MemUsed=MemTotal-MemFreeMemPercent=$(awk "BEGIN {if($MemTotal==0){printf 100}else{printf "%.2f",$MemUsed*100/$MemTotal}}")report_MemTotal="$((MemTotal/1024))""MB" #内存总容量(MB)report_MemFree="$((MemFree/1024))""MB" #内存剩余(MB)report_MemUsedPercent="$(awk "BEGIN {if($MemTotal==0){printf 100}else{printf "%.2f",$MemUsed*100/$MemTotal}}")""%" #内存使用率%}function getDiskStatus(){echo ""echo ""echo "############################ 磁盘检查 ############################"df -hiP | sed 's/Mounted on/Mounted/'> /tmp/inodedf -hTP | sed 's/Mounted on/Mounted/'> /tmp/disk join /tmp/disk /tmp/inode | awk '{print $1,$2,"|",$3,$4,$5,$6,"|",$8,$9,$10,$11,"|",$12}'| column -t#报表信息diskdata=https://www.isolves.com/it/rj/czxt/linux/2022-07-14/$(df -TP | sed '1d' | awk '$2!="tmpfs"{print}') #KBdisktotal=$(echo "$diskdata" | awk '{total+=$3}END{print total}') #KBdiskused=$(echo "$diskdata" | awk '{total+=$4}END{print total}') #KBdiskfree=$((disktotal-diskused)) #KBdiskusedpercent=$(echo $disktotal $diskused | awk '{if($1==0){printf 100}else{printf "%.2f",$2*100/$1}}') inodedata=$(df -iTP | sed '1d' | awk '$2!="tmpfs"{print}')inodetotal=$(echo "$inodedata" | awk '{total+=$3}END{print total}')inodeused=$(echo "$inodedata" | awk '{total+=$4}END{print total}')inodefree=$((inodetotal-inodeused))inodeusedpercent=$(echo $inodetotal $inodeused | awk '{if($1==0){printf 100}else{printf "%.2f",$2*100/$1}}')report_DiskTotal=$((disktotal/1024/1024))"GB" #硬盘总容量(GB)report_DiskFree=$((diskfree/1024/1024))"GB" #硬盘剩余(GB)report_DiskUsedPercent="$diskusedpercent""%" #硬盘使用率%report_InodeTotal=$((inodetotal/1000))"K" #Inode总量report_InodeFree=$((inodefree/1000))"K" #Inode剩余report_InodeUsedPercent="$inodeusedpercent""%" #Inode使用率%}function getSystemStatus(){echo ""echo ""echo "############################ 系统检查 ############################"if [ -e /etc/sysconfig/i18n ];thendefault_LANG="$(grep "LANG=" /etc/sysconfig/i18n | grep -v "^#" | awk -F '"' '{print $2}')"elsedefault_LANG=$LANGfiexport LANG="en_US.UTF-8"Release=$(cat /etc/redhat-release 2>/dev/null)Kernel=$(uname -r)OS=$(uname -o)Hostname=$(uname -n)SELinux=$(/usr/sbin/sestatus | grep "SELinux status: " | awk '{print $3}')LastReboot=$(who -b | awk '{print $3,$4}')uptime=$(uptime | sed 's/.*up ([^,]*), .*/1/')echo " 系统:$OS"echo " 发行版本:$Release"echo " 内核:$Kernel"echo " 主机名:$Hostname"echo " SELinux:$SELinux"echo "语言/编码:$default_LANG"echo " 当前时间:$(date +'%F %T')"echo " 最后启动:$LastReboot"echo " 运行时间:$uptime"#报表信息report_DateTime=$(date +"%F %T") #日期report_Hostname="$Hostname" #主机名report_OSRelease="$Release" #发行版本report_Kernel="$Kernel" #内核report_Language="$default_LANG" #语言/编码report_LastReboot="$LastReboot" #最近启动时间report_Uptime="$uptime" #运行时间(天)report_Selinux="$SELinux"export LANG="$default_LANG"}function getServiceStatus(){echo ""echo ""echo "############################ 服务检查 ############################"echo ""if [[ $centosVersion > 7 ]];thenconf=$(systemctl list-unit-files --type=service --state=enabled --no-pager | grep "enabled")process=$(systemctl list-units --type=service --state=running --no-pager | grep ".service")#报表信息report_SelfInitiatedService="$(echo "$conf" | wc -l)" #自启动服务数量report_RuningService="$(echo "$process" | wc -l)" #运行中服务数量elseconf=$(/sbin/chkconfig | grep -E ":on|:启用")process=$(/sbin/service --status-all 2>/dev/null | grep -E "is running|正在运行")#报表信息report_SelfInitiatedService="$(echo "$conf" | wc -l)" #自启动服务数量report_RuningService="$(echo "$process" | wc -l)" #运行中服务数量fiecho "服务配置"echo "--------"echo "$conf" | column -techo ""echo "正在运行的服务"echo "--------------"echo "$process"}function getAutoStartStatus(){echo ""echo ""echo "############################ 自启动检查 ##########################"conf=$(grep -v "^#" /etc/rc.d/rc.local| sed '/^$/d')echo "$conf"#报表信息report_SelfInitiatedProgram="$(echo $conf | wc -l)" #自启动程序数量}function getLoginStatus(){echo ""echo ""echo "############################ 登录检查 ############################"last | head}function getNetworkStatus(){echo ""echo ""echo "############################ 网络检查 ############################"if [[ $centosVersion


推荐阅读