码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-04-24 00:57:13      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:异常   ipa   class   exce   argv   vmstat   multipart   服务   amp   

发送邮件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‘] = ‘python4_mail@163.com‘
    msg[‘To‘] = ‘2764312406@qq.com‘
    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

  

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

标签:异常   ipa   class   exce   argv   vmstat   multipart   服务   amp   

原文地址:http://www.cnblogs.com/freelandun/p/6754814.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!