老男孩教育每日一题-2017-04-17:使用Shell或Python写一个脚本,CPU使用率超过80%或硬盘超过85%邮件报警

简介:

老男孩教育每日一题-2017-04-17:

使用Shell或Python写一个脚本,CPU使用率超过80%或硬盘超过85%邮件报警。

  • Shell

知识点1

CPU监控:top -n 1 查看1次就退出

1
Cpu(s): 0.3%us,  0.3%sy,  0.0%ni, 99.3% id ,  0.0%wa, 0.0%hi,  0.0%si,  0.0%st

 99.3%id  是未使用的CPU,剩余的都是使用的。

获取使用率

1
top  -n 1| awk  -F  '[, %]+'  'NR==3 {print 100-$11}'

知识点2

磁盘监控先监控/

1
df  -h| awk  -F  '[ %]+'   '/\/$/{print $5}'

知识点3:使用bc进行含有小数的大小判断

1
2
3
4
5
6
[root@oldboy ~] # echo "0.1>0.01"|bc
1
[root@oldboy ~] # echo "0.1>0.2"|bc
0
[root@oldboy ~] # echo "5.6>10.3"|bc
0

具体步骤:

1. 配置/etc/mail.rc支持发邮件

1
http: //oldboy .blog.51cto.com /2561410/1706911

2. 脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@oldboy scripts] # cat check.sh
#!/bin/bash
LANG=en_US.UTF-8
cpuUsed=` top  -n 1| awk  -F  '[, %]+'  'NR==3 {print100-$11}' `
diskUsed=$( df  -h| awk  -F  '[ %]+'   '/\/$/{print $5}' )
logFile= /tmp/jiankong .log
  
function  Sendmail(){
     mail -s "监控报警"  user@oldboyedu.com <$logFile
}
  
function  check(){
     if  [ ` echo "$cpuUsed>80" | bc ` - eq  1 -o $diskUsed - ge  85 ]; then
        echo "CPU使用率:${cpuUsed}%,磁盘使用率:${diskUsed}%" >$logFile
       Sendmail
     fi
}
  
function  main(){
     check
}
 
main

 

3. 加入定时任务,每5分钟执行一次。

 

                           

wKioL1j0iluQc7qwAADXQEDkpAs974.png


  • Python3

声明:已在python3测试,需要安装pip3 install psutil

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
[root@linux-node1 ~] # cat check.py 
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import  psutil    # python获取系统信息模块,需要额外安装
import  smtplib   # 发送邮件
from email.mime.text  import  MIMEText   # 构造纯文本邮件
from email.utils  import  formataddr  # 格式化邮件地址
 
cpuUsed=psutil.cpu_percent(interval=1)
diskUsed=psutil.disk_usage( '/' ).percent
 
def structural_mail(text, recipient):
     msg = MIMEText(text,  'plain' 'utf-8' )
     msg[ 'From' ] = formataddr([ "张耀" 'user@oldboyedu.com' ])   # 发件人
     msg[ 'To' ] = formataddr([recipient, recipient])   # recipient收件人
     msg[ 'Subject' ] =  "监控报警"   # 主题
     return  msg
 
 
def send_mail(text, recipient):
     from_addr =  '发送邮箱账号'
     password =  '密码'
     smtp_server =  'smtp.exmail.qq.com'
     smtp_port = 25
     to_addr = []   # 可以一次发给多个人,因此传入一个列表
     to_addr.append(recipient)
 
     msg = structural_mail(text, recipient)
 
     server = smtplib.SMTP(smtp_server, smtp_port)
     server.login(from_addr, password) 
     server.sendmail(from_addr, to_addr, msg.as_string())
 
 
def check():
     if  cpuUsed <= 80 or diskUsed >= 85:
        send_mail( 'CPU使用率:{}%,磁盘使用率:{}%' . format (cpuUsed, diskUsed), '12345678@qq.com' )
 
if  __name__ ==  '__main__' :
     check()

 

wKiom1j0ilzSL2A1AACHewlNqLM092.png


今天是老男孩教育每日一题陪伴大家的第29天。

往期题目索引

http://lidao.blog.51cto.com/3388056/1914205


本文转自 李导 51CTO博客,原文链接:http://blog.51cto.com/lidao/1916530


相关文章
|
4天前
|
存储 Shell Linux
【Shell 命令集合 磁盘维护 】Linux 管理硬盘分区 mpartition命令使用教程
【Shell 命令集合 磁盘维护 】Linux 管理硬盘分区 mpartition命令使用教程
46 1
|
4天前
|
数据安全/隐私保护 Python
Python实现邮件发送(含详细设置方法),并总结自己遇到的错误
Python实现邮件发送(含详细设置方法),并总结自己遇到的错误
|
4天前
|
存储 缓存 Linux
【Shell 命令集合 磁盘维护 】Linux 设置和查看硬盘驱动器参数 hdparm命令使用教程
【Shell 命令集合 磁盘维护 】Linux 设置和查看硬盘驱动器参数 hdparm命令使用教程
43 0
|
4天前
|
Python
Python自动化办公实战案例:文件整理与邮件发送
Python自动化办公实战案例:文件整理与邮件发送
8 0
|
4天前
|
数据可视化 Python
Python的分子模拟动态促进DF Theory理论对二进制硬盘系统的适用性
Python的分子模拟动态促进DF Theory理论对二进制硬盘系统的适用性
|
4天前
|
运维 Shell Linux
第十四章 Python发送邮件(常见四种邮件内容)
第十四章 Python发送邮件(常见四种邮件内容)
|
4天前
|
监控 Python
Python监控主机是否存活,并发报警邮件
Python监控主机是否存活,并发报警邮件
|
4天前
|
监控 Shell
Shell脚本监控CPU、内存和硬盘利用率
Shell脚本监控CPU、内存和硬盘利用率
|
4天前
|
数据安全/隐私保护 Python
Python Flask-Mail实现邮件发送
Python Flask-Mail实现邮件发送
|
4天前
|
存储 Python
终于,手把手教会 HR 实现 Python + Excel 「邮件自动化」发工资条了
终于,手把手教会 HR 实现 Python + Excel 「邮件自动化」发工资条了
31 0