日常巡检shell脚本

运维日常巡检脚本

#!/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!!!"
  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

611#

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值