运维日常巡检脚本
#!/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_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_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_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"
}
os_sys
os_network
cpu_info
mem_info
disk_info
Nginx日志每天切割压缩
#!/bin/bash
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
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免交互登陆
yum install expect tcl tclx tcl-devel -y
ssh-keygen
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!!!"