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

发送邮件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'] = 'xxxxxx@xxx.xxx'
    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

  

转载于:https://www.cnblogs.com/freelandun/p/6754814.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值