日常巡檢腳本
- 運維日常巡檢腳本
- Nginx日志每天切割壓縮
- 回圈探測全網主機并記錄
- Expect實作SSH免互動登陸
運維日常巡檢腳本
#!/bin/bash
os_sys(){
#系統資訊
os_type=`uname`
echo "作業系統的型別: $os_type"
os_version=`cat /etc/redhat-release`
echo "作業系統的版本號:$os_version"
os_ker=`uname -r`
echo "作業系統的內核版本:$os_ker"
os_time=`date +%F_%T`
echo "服務器當前的運行時間:$os_time"
os_last_reboot=`uptime |awk '{print $3,$4}'| awk -F ',' '{print $1}'`
echo "服務器最后重啟時間: $os_last_reboot"
os_hostname=`hostname`
echo "服務器的主機名: $os_hostname"
echo " "
}
os_network(){
#網路資訊
which ifconfig &> /dev/null
if [ $? -gt 0 ];then
yum -y install net-tools &>/dev/null
fi
ip_addr=$(ifconfig ens32 |awk '/netmask/{print $2}')
echo "服務器的IP地址為:${ip_addr}"
ping -c1 www.baidu.com &> /dev/null
if [ $? -eq 0 ];then
echo -e "\033[5;33m服務器的網路正常\033[0m"
else
echo -e "\033[5;34m資料例外請檢查網路\033[0m"
fi
RX=$(ifconfig ens32 | grep RX | sed -n '1p' | awk '{print $(NF-1),$NF}')
echo "網卡流入的量為:${RX}"
TX=$(ifconfig ens32 | grep TX | sed -n '1p' | awk '{print $(NF-1),$NF}')
echo "網卡流出的量為:${TX}"
echo " "
}
#cpu
cpu_info(){
cpu_num=$(cat /proc/cpuinfo |grep "physical id" | sort |uniq |wc -l)
echo "cpu的物理個數為:${cpu_num}"
cpu_core=$(cat /proc/cpuinfo |grep "core id" |sort |uniq |wc -l)
echo "cpu的核心個數為:${cpu_core}"
cpu_model=$(cat /proc/cpuinfo |grep "model name" |uniq |awk '{print $4,$6,$7,$9}')
echo "cpu的型號:${cpu_model}"
echo " "
}
#mem
mem_info(){
#記憶體總大小
mem_total=`free | awk '/Mem/{print $2}'`
echo "記憶體總大小: $mem_total"
#已使用記憶體
mem_used=`free | awk '/Mem/{print $3}'`
echo "已用記憶體大小:$mem_used"
#剩余記憶體大小
mem_free=`free | awk '/Mem/{print $4}'`
echo "剩余記憶體大小:$mem_free"
#已使用記憶體百分比
p_bfb=$(free | grep Mem | awk '{print $3/$2 * 100.0}')
echo "已使用記憶體百分比:${p_bfb}%"
#剩余記憶體百分比
s_bfb=$(free | grep Mem | awk '{print $4/$2 * 100.0}')
echo "剩余記憶體百分比:${s_bfb}%"
echo " "
}
#disk
#磁盤總量
disk_info(){
disk_total=$(lsblk |awk '/disk/{print $4}')
echo "磁盤總量為:${disk_total}"
count=($(df -Tm |egrep -v tmpfs |sed '1d' |awk '{print $5}'))
sum=0
for i in ${count[@]}
do
let sum=sum+$i
done
sumb=($sum/1024)
echo "剩余磁盤總量為 $sum M"
}
#while :
#do
os_sys
os_network
cpu_info
mem_info
disk_info
#sleep 5
#done
Nginx日志每天切割壓縮
#!/bin/bash
#Nginx日志每天切割壓縮
#將當前的nginx日志先按照當天日期進行重命名接著進行壓縮,最后是新建空白的nginx日志檔案,并重新載入nginx
nginx_log="/usr/local/nginx_log"
if [[ ! -f $nginx_log ]];then
mkdir -p $nginx_log
fi
cd /var/log/nginx/
newAccessLog="access`date +%Y-%m-%d`.log"
newErrorLog="error`date +%Y-%m-%d`.log"
mv access.log $newAccessLog && mv error.log $newErrorLog
#創建日志檔案
touch access.log error.log
#reload Nginx
systemctl reload nginx
#壓縮日志檔案
tar -zcvf $newAccessLog.tar.gz $newAccessLog --remove-files &>/dev/null
tar -zcvf $newErrorLog.tar.gz $newErrorLog --remove-files &>/dev/null
mv *.tar.gz $nginx_log
回圈探測全網主機并記錄
#!/bin/bash
for i in {2..254}
do
ip=192.168.100.$i
ping -c1 -W1 $ip &>/dev/null
if [ $? -eq 0 ];then
echo "$ip" | tee -a ip.txt
fi
done
Expect實作SSH免互動登陸
#需提前安裝expect軟體包
yum install expect tcl tclx tcl-devel -y
#需提前生成公鑰
ssh-keygen
#!/bin/bash
for i in {2..254}
do
{
ip=192.168.100.$i
ping -c1 -w1 $ip &>/dev/null
if [ $? -eq 0 ];then
echo $ip
/usr/bin/expect <<-EOF
set timeout 10
spawn ssh-copy-id $ip
expect {
"yes/no" { send "yes\r";exp_continue }
"password:" { send "123456\r" } #密碼自定義
}
expect eof
EOF
fi
} &
done
wait
echo "Complete!!!"
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/293138.html
標籤:其他
