1. 程式人生 > >十六.監控系統cpu.內存,磁盤等,自動報警,發送郵件

十六.監控系統cpu.內存,磁盤等,自動報警,發送郵件

subject sub percent tmp 工具 exc sendmai join pri

發送郵箱小工具,將它放在#/usr/bin/mail chmod +x /usr/bin/mail

#!/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‘] = ‘監控

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)

監控腳本

#!/bin/bash

#/author/dengsiyuan

cpu_limit=0 #定義cpu報警線

memory_limit=0 #定義內存報警線

disk=‘/dev/sda1‘ # 定義要監控的磁盤

disk_inode_limit=0 #定義磁盤inode報警線

disk_space_limit=0 #定義磁盤使用空間報警線

function bc_install() #查看是否安裝了bc軟件包

{

rpm -qa |grep ‘^bc.*$‘ >> /dev/null

if [ $? -eq 0 ];then

echo ‘bc has been installed‘

else

yum install bc -y

fi

}

function nettools_install() #查看是否安裝了nettools 軟件包

{

rpm -qa |grep net-tools >> /dev/null

if [ $? -eq 0 ];then

echo ‘nettools has been installed‘

else

yum install net-tools -y

Fi

}

function monitor_cpu()

{

cpu_id_free= `vmstat 1 5 |awk ‘NR>=3{ x = x + $15 } END {print x/5}‘|awk -F. ‘{print $1}‘`

#提取CPU空余空間

cpu_used= $((100-cpu_id_free)) #提取CPU使用空間

if [ $cpu_used -gt $cpu_limit ]then; #cpu使用率報警線比較

msg="TIME:$(date +%F_%T)

HOSTNAME:$(hostname)

IPADDR:$(ifconfig |awk ‘NR==2{print $2}‘)

MSG:cpu usage exceeds the limit ,current value is ${cpu_used}%"

echo $msg

/usr/bin/mail $msg

fi

}

function monitor_mem()

{

mem_total=`free |awk ‘NR==2{print $2}‘` #提取內存總量

mem_used=`free |awk ‘NR==2{print $3}‘` #提取內存使用

mem_percent=`echo "scale=2;$mem_used/$mem_total" |bc -l |cut -d. -f2` #計算內存使用率

if [ $mem_percent -gt $mem_limit ];then #與內存使用率報警線比較

msg="TIME:$(date +%F_%T)

HOSTNAME:$(hostname)

-- IPADDR:$(ifconfig |awk ‘NR==2{print $2}‘)

MSG:mem usage exceeds the limit,current value is ${mem_percent}%"

echo $msg

/usr/bin/mail $msg # 調用郵件

fi

}

function monitor_disk_inode()

{

disk_inode_used=`df -i $disk |awk ‘NR==2{print $5}‘|cut -d% -f1` #提取磁盤Inode使用情況

if [ $disk_inode_used -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 vaule is ${disk_inode_used}%"

echo $msg

/usr/bin/mail $msg

fi

}

function monitor_disk_space()

{

disk_space_used=`df $disk |awk ‘NR==2{print $5}‘|cut -d% -f1` #提取磁盤使用情況

if [ $disk_space_used -gt $disk_space_limit ];then #比較

msg="TIME:$(date +%F_%T)

HOSTNAME:$(hostname)

--PADDR:$(ifconfig |awk ‘NR==2{print $2}‘)

MSG:disk_space_used usage exceeds the limit,current vaule is ${disk_space_used}%"

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

十六.監控系統cpu.內存,磁盤等,自動報警,發送郵件