日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

十六.监控系统cpu.内存,磁盘等,自动报警,发送邮件

發布時間:2023/11/27 生活经验 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 十六.监控系统cpu.内存,磁盘等,自动报警,发送邮件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

發送郵箱小工具,將它放在#/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'] = 'python4_mail@163.com'

????msg['To'] = 'python4_recvmail@163.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)

?

?

監控腳本

#!/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 ??????????????

轉載于:https://www.cnblogs.com/njzy-yuan/p/6813722.html

總結

以上是生活随笔為你收集整理的十六.监控系统cpu.内存,磁盘等,自动报警,发送邮件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。