日常巡檢

2020-08-13 00:13:11

#!/bin/bash
###系統資訊#########
os_system(){
os_type=$(uname)
echo 「操作系統的型別: ostype"osver={os_type}" os_ver=(cat /etc/redhat-release)
echo 「操作系統的版本號:osver"osker={os_ver}" os_ker=(uname -r)
echo 「系統內核:osker"ostime={os_ker}" os_time=(date +%F_%T)
echo 「伺服器當前執行時間:ostime"oslastreboot={os_time}" os_last_reboot=(uptime |awk ‘{print $3}’|awk -F ‘,’ '{print KaTeX parse error: Expected 'EOF', got '}' at position 2: 1}̲') echo "服務…{os_last_reboot}」
os_hostname=(hostname)echo":(hostname) echo "伺服器主機名稱:{os_hostname}」
}
####網路資訊#################
os_network(){
ip_addr=$(ifconfig |awk '/broadcast/{print KaTeX parse error: Expected 'EOF', got '}' at position 2: 2}̲') echo "服務…{ip_addr}」
ping -c3 www.baidu.com >/dev/null
if [ KaTeX parse error: Expected 'EOF', got '#' at position 91: …網路" fi #̲流入的量 RX=(ifconfig ens33|grep RX|head -n1|awk '{print KaTeX parse error: Expected 'EOF', got '}' at position 12: 5/1024/1024}̲') #流出的量 …(ifconfig ens33|grep TX|head -n1|awk '{print KaTeX parse error: Expected 'EOF', got '}' at position 12: 5/1024/1024}̲') } ########硬體…(cat /proc/cpuinfo |grep 「physical id」|sort |uniq |wc -l)
echo 「cpu個數:cpunum"cpucore={cpu_num}" cpu_core=(cat /proc/cpuinfo |grep 「cores」|sort |uniq |awk -F ‘:’ '{print KaTeX parse error: Expected 'EOF', got '}' at position 2: 2}̲') echo "cp…{cpu_core}」
cpu_model=$(cat /proc/cpuinfo |grep 「model name」|sort |uniq |awk -F ‘:’ '{print KaTeX parse error: Expected 'EOF', got '}' at position 2: 2}̲') echo "cp…{cpu_model}」
}
mem_info(){
#總記憶體大小
mem_total=free -m | sed -n '2p' |awk '{print $2}'
#已使用記憶體
mem_used=free -m | sed -n '2p' |awk '{print $3}'
#剩餘記憶體
mem_free=free -m |sed -n '2p' |awk '{print $4}'

#使用記憶體百分比
Percent_mem_used=`echo "scale=2; $mem_used / $mem_total *100" | bc`
#剩餘記憶體百分比
Percent_mem_free=`echo "scale=2; $mem_free / $mem_total *100" | bc`

echo  "目前記憶體使用率達到:${Percent_mem_used}%"

}
disk_info(){
#磁碟
##統計磁碟IO 效能指標(tps,每秒讀取的量,每秒寫入的量)
yum -y install sysstat
iostat
#-F ‘[ |%]+’:+正則表達式 表示一個或多個,這裏就表示一個或多個空格或%
##顯示已用超過80%的檔案系統
#方法一:
df -Th|awk -F ‘[ |%]+’ ‘NR>1 {if($6>80) print $1}’
#方法二:
df -Th|awk ‘NR>1 {if ($6>80) print $1}’

#磁碟總量
disktotal=$(lsblk |awk '/disk/{print $4}')
echo "磁碟總量爲:${disktotal}"
 
#剩餘磁碟總量
sum=0
    a=($(df -T |grep -v tmpfs|awk -F' ' '{print $5}' |sed '1d'))
    for i in ${a[@]}
    do
    let  sum=sum+$i
    done
    diskfree=$(($sum/1024/1024))
    echo "磁碟剩餘總量爲:${diskfree}GB"

}

##############安全資訊########################
sec_info(){
##統計登錄使用者資訊
count_user=$(last|grep 「still logged in」|awk '{print KaTeX parse error: Expected 'EOF', got '}' at position 2: 1}̲'|sort |uniq) …{count_user}"
##md5校驗/etc/passwd
[ -f /opt/pwd ] || md5sum /etc/passwd > /opt/pwd
md5sum -c /opt/pwd >/dev/null
if [ $? -eq 0 ]; then
echo 「file is ok」
else
echo 「檔案被篡改,please check !」
fi
}
while :
do
os_system
os_network
cpu_info
mem_info
disk_info
sec_info
done