首頁>技術>

背景說明:我們在日常運維過程中,為了避免生產事件的發生,經常會去自定義一些指令碼來批次對伺服器的健康狀態進行檢查,今天就給大家分享一個bash指令碼,主要用來檢查OS的一些基礎資訊。

執行指令碼,如下圖所示:

指令碼詳細內容:

#!/bin/sh"echo "#######################################「OS系統資訊」##########################################"OS_TYPE=`uname`OS_Number=`dmidecode -t system |grep 'Serial Number'|awk '{print $3}'|awk -F, '{print $1}'`OS_VERSION=`cat /etc/redhat-release`OS_IPADDR=`ifconfig ens160|grep "inet" |awk '{print $2}' | sed -n '1p'`OS_KERNER=`uname -a|awk '{print $3}'`OS_NOWTIME=`date +%F_%T`OS_RUN_TIME=`uptime |awk '{print $3,$4}'|awk -F, '{print $1}'`OS_LASTREBOOT_TIME=`who -b|awk '{print $2,$3}'`OS_HOSTNAME=`hostname`echo " 主機型別: $OS_TYPE"echo " 主機序列號: $OS_Number"echo " 系統版本: $OS_VERSION"echo " 系統IP地址: $OS_IPADDR"echo " 核心版本: $OS_KERNER"echo " 系統時間: $OS_NOWTIME"echo " 執行時間: $OS_RUN_TIME"echo " 最後重啟時間: $OS_LASTREBOOT_TIME"echo " 主機名稱: $OS_HOSTNAME"echo " SELinux:` /usr/sbin/sestatus | grep 'SELinux status:' | awk '{print $3}'`"echo " 語言環境:`echo $LANG`"echo "#######################################「OS資源資訊」##########################################"OS_CPU_PRO=`cat /proc/cpuinfo |grep "processor" | wc -l`OS_CPU_COR=`cat /proc/cpuinfo| grep "cpu cores"| uniq |awk {'print $4'}`OS_CPU_TYPE=`grep "model name" /proc/cpuinfo | awk -F ': ' '{print $2}' | sort | uniq`echo " CPU總個數: $OS_CPU_PRO"echo " CPU總核數: $OS_CPU_COR"echo " CPU型 號: $OS_CPU_TYPE"OS_SWAP_S=`free|grep Swap|awk {'print $2'}`OS_PARTS=(`df -T|sed 1d|egrep -v "tmpfs|sr0"|awk {'print $3'}`)OS_MEM_TAL=`free -m|grep Mem|awk '{print $2}'`OS_MEM_FREE=`free -m|grep Mem|awk '{print $7}'`echo " 記憶體總量: ${OS_MEM_TAL}MB"echo " 記憶體餘量: ${OS_MEM_FREE}MB"OS_DISKS=0OS_SWAP=`free|grep Swap|awk {'print $2'}`OS_PARTS=(`df -T|sed 1d|egrep -v "tmpfs|sr0"|awk {'print $3'}`)for ((i=0;i<`echo ${#OS_PARTS[*]}`;i++))doOS_DISKS=`expr $OS_DISKS + ${OS_PARTS[$i]}`done((OS_DISKS=\($OS_DISKS+$OS_SWAP\)/1024/1024))echo " 磁碟總量: ${OS_DISKS}GB"OS_DISKS=0OS_SWAP=`free|grep Swap|awk '{print $4}'`OS_PARTS=(`df -T|sed 1d|egrep -v "tmpfs|sr0"|awk '{print $5}'`)for ((i=0;i<`echo ${#OS_PARTS[*]}`;i++))doOS_DISKS=`expr $OS_DISKS + ${OS_PARTS[$i]}`done((freetotal=\($OS_DISKS+$OS_SWAP\)/1024/1024))echo " 磁碟餘量: ${freetotal}GB"echo "#######################################「OS網路監測」##########################################"echo `ip a | grep eno | awk "NR==2" | awk '{print $NF,":",$2}'`echo "閘道器:`ip route | awk 'NR==1'| awk '{print $3}'`"echo "DNS: `cat /etc/resolv.conf | grep "nameserver" | awk '{print $2}'`"ping -c 4 www.baidu.com > /dev/nullif [ $? -eq 0 ];thenecho "網路連線狀態:正常"elseecho "網路連線狀態:失敗"fiechoecho "#######################################「OS安全檢查」##########################################"echo "使用者登陸資訊:`last | grep "still logged in" | awk '{print $1}'| sort | uniq`"md5sum -c --quiet /etc/passwd > /dev/null 2&>1if [ $? -eq 0 ];thenecho "檔案未被篡改"elseecho "檔案被篡改"fi

11
最新評論
  • BSA-TRITC(10mg/ml) TRITC-BSA 牛血清白蛋白改性標記羅丹明
  • 一文告訴你大廠億級併發下高效能伺服器是如何實現的