集群监控

#! /bin/bash
# _*_coding:utf-8 _*_
# __author__ = Lex

zombie_limit=0  #僵尸进程数值
mem_limit=0    #内存监控阈值
disk='/dev/sda3'        #需要监控的磁盘
disk_inode_limit=0      #磁盘inode阈值
disk_space_limit=0      #磁盘空间阈值

#监控僵尸进程
function monitor_zombie(){
    zombie_num=`ps -ef|grep defunct|grep -v grep|wc -l`        #获取僵尸进程数目
    #echo 'zombie number is $zombie_num'
    if [ $zombie_num -gt $zombie_limit ]        #比较是否产生僵尸进程
        then
          # echo 'your server has zombie'
            msg="TIME:$(date +%F-%T)            
                Hostname:$hostname
                IpAddr:$(ifconfig|awk 'NR==2{print $2}')
                MSG:Your server has zombie,current value is ${zombie_num}"      #获取当前时间、主机名、IP地址
            echo $msg
            /usr/bin/mail $msg          #发送邮件
    fi
}

#监控内存
function monitor_mem(){
    mem_use=`free|awk 'NR==2{print $3}'`        #已使用内存
    mem_total=`free|awk 'NR==2{print $2}'`      #总内存
    mem_per=`echo "scale=2;$mem_use/$mem_total"|bc -l|cut -d. -f2`      #已用内存百分比
    #echo $mem_per
    if [ $mem_per -gt $mem_limit ]
        then
            msg="TIME:$(date +%F-%T)            
                Hostname:$hostname
                IpAddr:$(ifconfig|awk 'NR==2{print $2}')
                MSG:Memory usage exceeds the limit,current value is ${mem_per}%"      #获取当前时间、主机名、IP地址
            echo $msg
            /usr/bin/mail $msg          #发送邮件
    fi
}

#监控磁盘inode
function monitor_disk_inode(){
    inode_use=`df -i $disk|awk 'NR==2{print $3}'`      #已使用inode
    inode_total=`df -i $disk|awk 'NR==2{print $2}'`    #总inode
    inode_per=`echo "scale=2;$inode_use/$inode_total"|bc -l|cut -d. -f2`        #已用inode百分比
    #echo $inode_per
    if [ $inode_per -gt $disk_inode_limit ]
        then
            msg="TIME:$(date +%F-%T)            
                Hostname:$hostname
                IpAddr:$(ifconfig|awk 'NR==2{print $2}')
                MSG:Disk space usage exceeds the limit,current value is ${inode_per}%"      #获取当前时间、主机名、IP地址
            echo $msg
            /usr/bin/mail $msg          #发送邮件

    fi
}

#监控磁盘空间
function monitor_disk_space(){
    space_use=`df $disk|awk 'NR==2{print $3}'`      #已使用space
    space_total=`df $disk|awk 'NR==2{print $2}'`    #总space
    space_per=`echo "scale=2;$space_use/$space_total"|bc -l|cut -d. -f2`        #已用space百分比
  # echo $space_per
    if [ $space_per -gt $disk_space_limit ]
        then
            msg="TIME:$(date +%F-%T)            
                Hostname:$hostname
                IpAddr:$(ifconfig|awk 'NR==2{print $2}')
                MSG:Disk space usage exceeds the limit,current value is ${space_per}%"      #获取当前时间、主机名、IP地址
            echo $msg
            /usr/bin/mail $msg          #发送邮件

    fi

}

monitor_zombie $>> /test/monitor_server.log
monitor_mem $>> /test/monitor_server.log
monitor_disk_inode $>> /test/monitor_server.log
monitor_disk_space $>> /test/monitor_server.log
原文地址:https://www.cnblogs.com/sama/p/7604592.html