老男孩教育每日一题-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


相关文章
|
2天前
|
分布式计算 Hadoop Shell
使用shell脚本实现自动SSH互信功能
使用shell脚本实现自动SSH互信功能
11 1
|
2天前
|
Unix Shell Linux
轻松编写 AIX Shell 脚本
轻松编写 AIX Shell 脚本
14 1
|
2天前
|
监控 关系型数据库 Shell
Shell脚本入门:从基础到实践,轻松掌握Shell编程
Shell脚本入门:从基础到实践,轻松掌握Shell编程
|
2天前
|
关系型数据库 MySQL Shell
在Centos7中利用Shell脚本:实现MySQL的数据备份
在Centos7中利用Shell脚本:实现MySQL的数据备份
|
2天前
|
Shell Linux 编译器
C语言,Linux,静态库编写方法,makefile与shell脚本的关系。
总结:C语言在Linux上编写静态库时,通常会使用Makefile来管理编译和链接过程,以及Shell脚本来自动化构建任务。Makefile包含了编译规则和链接信息,而Shell脚本可以调用Makefile以及其他构建工具来构建项目。这种组合可以大大简化编译和构建过程,使代码更易于维护和分发。
30 5
|
2天前
|
存储 人工智能 数据处理
Python:编程的艺术与科学的完美交融
Python:编程的艺术与科学的完美交融
19 1
|
1天前
|
网络协议 Unix Python
Python编程-----网络通信
Python编程-----网络通信
8 1
|
2天前
|
JSON 数据格式 开发者
pip和requests在Python编程中各自扮演着不同的角色
【5月更文挑战第9天】`pip`是Python的包管理器,用于安装、升级和管理PyPI上的包;`requests`是一个HTTP库,简化了HTTP通信,支持各种HTTP请求类型及数据交互。两者在Python环境中分别负责包管理和网络请求。
31 5
|
2天前
|
存储 Python 容器
Python高级编程
Python集合包括可变的set和不可变的frozenset,用于存储无序、不重复的哈希元素。创建集合可使用{}或set(),如`my_set = {1, 2, 3, 4, 5}`。通过add()添加元素,remove()或discard()删除元素,如`my_set.remove(3)`。
13 0
|
2天前
|
测试技术 Python
Python模块化方式编程实践
【5月更文挑战第5天】Python模块化编程提升代码质量,包括:定义专注单一任务的模块;使用`import`导入模块;封装函数和类,明确命名便于重用;避免全局变量降低耦合;使用文档字符串增强可读性;为每个模块写单元测试确保正确性;重用模块作为库;定期维护更新以适应Python新版本。遵循这些实践,可提高代码可读性、重用性和可维护性。
43 2