当前位置:   article > 正文

Linux常用监控脚本_linux服务器监控脚本

linux服务器监控脚本

1、查看主机网卡流量

#!/bin/bash
#network
#Mike.Xu
while : ; do
time=`date +%m"-"%d" "%k":"%M`
day=`date +%m"-"%d`
rx_before=`ifconfig ens33|sed -n "8"p|awk '{print $2}'|cut -c7-`
tx_before=`ifconfig ens33|sed -n "8"p|awk '{print $6}'|cut -c7-`
sleep 2
rx_after=`ifconfig ens33|sed -n "8"p|awk '{print $2}'|cut -c7-`
tx_after=`ifconfig ens33|sed -n "8"p|awk '{print $6}'|cut -c7-`
rx_result=$[(rx_after-rx_before)/256]
tx_result=$[(tx_after-tx_before)/256]
echo "$time Now_In_Speed: "$rx_result"kbps             Now_OUt_Speed: "$tx_result"kbps"
sleep 2
done

2、 监控CPU和内存的使用情况

#!/bin/bash
#script to capture system statistics
OUTFILE=/home/xu/capstats.csv
DATE='date +%m/%d/%Y'
TIME='date +%k:%m:%s'
TIMEOUT='uptime'
VMOUT='vmstat 1 2'
USERS='echo $TIMEOUT | gawk '{print $4}' '
LOAD='echo $TIMEOUT | gawk '{print $9}' | sed "s/,//' '
FREE='echo $VMOUT | sed -n '/[0-9]/p' | sed -n '2p' | gawk '{print $4} ' '
IDLE='echo $VMOUT | sed -n '/[0-9]/p' | sed -n '2p' |gawk '{print $15}' '
echo "$DATE,$TIME,$USERS,$LOAD,$FREE,$IDLE" >> $OUTFILE

3、巡检磁盘使用率并达到阈值则报警

(1)先做免密互信免交互

#!/bin/bash
mkdir /hosts
cat >> /hosts/hosts.txt << EOF
192.168.1.6 root xyq745
192.168.1.7 root xyq745
EOF
cat >> /hosts/ipall.txt << EOF
192.168.1.6
192.168.1.7
EOF
if [ ! -f ~/.ssh/id_rsa ];then
    ssh-keygen -P "" -f ~/.ssh/id_rsa && mv /root/.ssh/id_rsa.pub /root/.ssh/authorized_keys
    echo -e "id_rsa已创建"
fi

#检测expect是否安装
rpm -q expect
if [ $? == 0 ];then
    echo -e "expect已安装"
else
    echo -e "expect未安装,开始安装"
    rm -rf /var/run/yum.pid
    yum -y install expect
fi

#导入IPlist文件循环主机免密
while read line
do
    ip=`echo $line | cut -d " " -f 1`
    user=`echo $line | cut -d " " -f 2`
    passwd=`echo $line | cut -d " " -f 3`
expect <<EOF
    set timeout 30
    spawn scp -r /root/.ssh $ip:/root
    expect {
        "yes/no" { send "yes\n";exp_continue }
        "password" { send "$passwd\n" }
    }
    expect "password" {send "$passwd\n"}
EOF
done < /hosts/hosts.txt

#判断命令是否成功
if [ $? == 0 ];then
    echo -e "\033[32m ALL互信成功 \033[0m"
else
    echo -e "\033[31m ALL互信失败 \033[0m"
fi

(2)本地写好监测磁盘和报警脚本

#添加邮件报警
from="2658130675@qq.com"
smtp="smtp.qq.com"
smtpuser="2658130675@qq.com"
smtppassword="niobmpftlfqjdhjd"

sed -i '$aset from='"${from}"'' /etc/mail.rc
sed -i '$aset smtp='"${smtp}"'' /etc/mail.rc
sed -i '$aset smtp-auth-user='"${smtpuser}"'' /etc/mail.rc
sed -i '$aset smtp-auth-password='"${smtppassword}"'' /etc/mail.rc
sed -i '$aset smtp-auth=login' /etc/mail.rc

ip=`ip a|grep ens33 |tail -1 |awk '{print $2}'|cut -c 1-11`
a=`df -h |grep sda1 |awk '{print $5}'`
echo "当前磁盘使用率:$a"
b=`df -h |grep sda1 |awk '{print $5}'|cut -c 1-2`
if [ $b -gt 50 ];then
        echo "磁盘空间不足百分之50"
    mail -s "磁盘空间不足百分之50" $from
elif [ $b -gt 60 ];then
        echo "磁盘空间不足百分之40"
        mail -s "磁盘空间不足百分之40" $from
elif [ $b -gt 70 ];then
        echo "磁盘空间不足百分之30"
        mail -s "磁盘空间不足百分之30" $from
elif [ $b -gt 80 ];then
        echo "磁盘空间不足百分之20"
        mail -s "磁盘空间不足百分之20" $from
elif [ $b -gt 90 ];then
        echo "磁盘空间不足百分之10"
        mail -s "磁盘空间不足百分之10" $from
fi

(3)发送本地写好的脚本,并且远程执行

for i in `cat /hosts/ipall.txt`
do
echo $i
scp -r /root/sh.sh root@$i:/
ssh $i "
sh /sh.sh
"
done

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/710822
推荐阅读
相关标签
  

闽ICP备14008679号