nginx Python google 开源 Firefox linux mysql Android apache Windows HTML5 centos wordpress php 微软 Ubuntu 程序员 linux命令 java shell

linux下使用shell脚本监控服务器服务器 内存、磁盘、cpu、swap

直接Shell代码

#!/bin/bash

MAILLIST="foyon0806@gmail.com"    #emailist

MEM_CORDON=100   #内存使用大于这个值报警
SWAP_CORDON=50  #交换区使用值大于这个报警  
CPU_CORDON=5    #cpu空闲小于这个值报警
DISK_CORDON=85  #磁盘占用大于这个值报警
hostname=`hostname`
DATA=`date`

send_warning()
{
    echo $MESSAGE | /bin/mail -s "$TITLE" "$MAILLIST" 
}

if [ $# -ne 0 ];then
    DISK_DIR=$1
else
    DISK_DIR="/dev/sdb1"
fi

#MEM|SWAP check
MEMSTATUS=`free | grep "Mem" | awk '{printf("%d", $3*100/$2)}'`
SWAPSTATUS=`free | grep "Swap" | awk '{printf("%d", $3*100/$2)}'`

if [ $MEMSTATUS -ge $MEM_CORDON ];then
    TITLE="[bad_girl]:$HOSTNAME mem usage"
    MESSAGE="time:${DATA},Mem_used:${MEMSTATUS}%,Swap_used:${SWAPSTATUS}%"
    send_warning
fi

if [ $SWAPSTATUS -ge $SWAP_CORDON ];then
    TITLE="[bad_girl]:$HOSTNAME Swap usage"
    MESSAGE="Time:${DATA},Mem_used:${MEMSTATUS}%,Swap_used:${SWAPSTATUS}%"
    send_warning
fi    

#cpu

CPUSTATUS=`vmstat | awk '{print $15}' | tail -1`

if [ $CPUSTATUS -le $CPU_CORDON ];then
    TITLE="[bad_girl]:$HOSTNAME cpu usage"
    MESSAGE="Time:${DATA},MCpu_free:${CPUSTATUS}%"
fi

#disk use n%

DISKSTATUS=`df -h $DISK_DIR | awk '{print $5}' | tail -1 | tr -d %`

if [ $DISKSTATUS -ge $DISK_CORDON ];then
    TITLE="[bad_girl]:$HOSTNAME disk usage"
    MESSAGE="Time:${DATA},Disk_used:${DISKSTATUS}%"
    send_warning
fi

延伸阅读

评论

暂无评论

写评论