监测阿里云CentOS服务器的shell脚本

#!/bin/bash
function cpu() {
    NUM=1
    while [ $NUM -le 3 ]; do
        util=`vmstat |awk '{if(NR==3)print 100-$15"%"}'`
        user=`vmstat |awk '{if(NR==3)print $13"%"}'`
        sys=`vmstat |awk '{if(NR==3)print $14"%"}'`
        iowait=`vmstat |awk '{if(NR==3)print $16"%"}'`
        echo -e "CPU - 使用率:"$util",等待磁盘IO响应使用率:"$iowait
        let NUM++
        sleep 1
    done
}

function memory() {
    total=`free -m |awk '{if(NR==2)printf "%.1f",$2/1024}'`
    used=`free -m |awk '{if(NR==2) printf "%.1f",($2-$NF)/1024}'`
    available=`free -m |awk '{if(NR==2) printf "%.1f",$NF/1024}'`
    echo "内存 - 总大小: ${total}G , 使用: ${used}G , 剩余: ${available}G"
}

function disk() {
    fs=$(df -h |awk '/^\/dev/{print $1}')
    for p in $fs; do
        mounted=$(df -h |awk '$1=="'$p'"{print $NF}')
        size=$(df -h |awk '$1=="'$p'"{print $2}')
        used=$(df -h |awk '$1=="'$p'"{print $3}')
        used_percent=$(df -h |awk '$1=="'$p'"{print $5}')
        echo -e "硬盘 - 挂载点:"$mounted",总大小:"$size",使用:"$used",使用率:"$used_percent
    done
}

function tcp_status() {
    summary=$(ss -antp |awk '{status[$1]++}END{for(i in status) printf i":"status[i]" "}')
    echo "TCP连接状态 - $summary"
}

function 80_port(){
    apa=`netstat -tanpl | grep 80 | awk '{print $4}' | sed -n '1p'`
    if [ $apa != "0.0.0.0:80" ];then
        echo "80端口已经停止运行"
    else
        echo "80端口运行正常"
    fi
}

echo -e $(cpu)"\n"$(memory)"\n"$(disk)"\n"$(tcp_status)"\n"$(80_port) | mail -s "huacai服务器状态" 1324928751@qq.com

发表评论?

1 条评论。

  1. crontab -e
    写入:
    #每天15:00执行”/home/www/check.sh”,监测服务器状态
    00 15 * * * /home/www/check.sh

发表评论


注意 - 你可以用以下 HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This site uses Akismet to reduce spam. Learn how your comment data is processed.