编写监控脚本,监控集群内所有服务存活状态,内存磁盘剩余率检测,异常则发送报警邮件

Posted 云中王

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了编写监控脚本,监控集群内所有服务存活状态,内存磁盘剩余率检测,异常则发送报警邮件相关的知识,希望对你有一定的参考价值。

发送邮件Python程序

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import sys
import smtplib
import email.mime.multipart
import email.mime.text

server = ‘smtp.163.com‘
port = ‘25‘

def sendmail(server,port,user,pwd,msg):
    smtp = smtplib.SMTP()
    smtp.connect(server,port)
    smtp.login(user, pwd)
    smtp.sendmail(msg[‘from‘], msg[‘to‘], msg.as_string())
    smtp.quit()
    print(‘Email has send out !‘)


if __name__ == ‘__main__‘:
    msg = email.mime.multipart.MIMEMultipart()
    msg[‘Subject‘] = ‘WARNNING‘
    msg[‘From‘] = ‘[email protected]‘
    msg[‘To‘] = ‘[email protected]‘
    user = ‘python4_mail‘
    pwd = ‘sbalex3714‘
    content=‘%s\n%s‘ %(‘\n‘.join(sys.argv[1:4]),‘ ‘.join(sys.argv[4:]))

    txt = email.mime.text.MIMEText(content, _charset=‘utf-8‘)
    msg.attach(txt)

    sendmail(server,port,user,pwd,msg)

  编辑为/usr/bin/mail,添加可执行权限

监控脚本

#!/bin/bash

cpu_limit=90
mem_limit=90
disk_inode_limit=90
disk_space_limit=90
disk=‘/dev/sda1‘

function bc_install(){
    rpm -qa |grep ‘^bc.*$‘ >> /dev/null
    if [[ $? -ne 0 ]]; then
        yum install bc -y
    fi
}

function nettools_install(){
    rpm -qa |grep net-tools >> /dev/null
    if [[ $? -ne 0 ]]; then
        yum install net-tools -y
    fi
}

function monitor_cpu(){
    cpu_free=`vmstat 1 5 |awk ‘NR>=3{x = x + $15} END {print x/5}‘ |awk -F. ‘{print $1}‘`
    cpu_use=$((100-cpu_free))
    if [[ $cpu_use -gt $cpu_limit ]]; then
        msg="TIME:$(date +%F_%T)
             HOSTNAME:$(hostname)
             IPADDR:$(ifconfig |awk ‘NR==2{print $2}‘)
             MSG:CPU usage exceeds the limit,current value is ${cpu_use}%"
        echo $msg
        /usr/bin/mail $msg
    fi
}

function monitor_mem(){
    mem_total=`free |awk ‘NR==2{print $2}‘`
    mem_use=`free |awk ‘NR==2{print $3}‘`
    mem_per=`echo "scale=2;$mem_use/$mem_total" |bc -l|cut -d. -f2`
    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}%"
            echo $msg
            /usr/bin/mail $msg
    fi
}

function monitor_disk_inode(){
    inode_use=`df -i $disk |awk ‘NR==2{print $5}‘ |cut -d% -f1`
    if [[ $inode_use -gt $disk_inode_limit ]]; then
            msg="TIME:$(date +%F_%T)
                 HOSTNAME:$(hostname)
                 IPADDR:$(ifconfig |awk ‘NR==2{print $2}‘)
                 MSG:Disk inode usage exceeds the limit,current value is ${inode_use}%"
            echo $msg
            /usr/bin/mail $msg
    fi
}

function monitor_disk_space(){
    space_use=`df $disk |awk ‘NR==2{print $5}‘|cut -d% -f1`
    if [[ $space_use -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_use}%"
            echo $msg
            /usr/bin/mail $msg
    fi
}

bc_install
nettools_install
monitor_cpu &>> /tmp/monitor.log
monitor_mem &>> /tmp/monitor.log
monitor_disk_inode &>> /tmp/monitor.log
monitor_disk_space &>> /tmp/monitor.log

  

以上是关于编写监控脚本,监控集群内所有服务存活状态,内存磁盘剩余率检测,异常则发送报警邮件的主要内容,如果未能解决你的问题,请参考以下文章

Shell脚本监控LVS后台服务器存活状态

Day11.开发脚本自动部署及监控

Linux(三十二) 针对LINUX服务器的指标监控脚本编写

运维监控基础

linux下使用smtp+shell脚本实现服务器存活状态监控

脚本自动部署构架集群和监控状态