#!/usr/local/bin/python3.5
#coding:utf-8
import mailll, linecache, re, socket, os, time hostname = socket.gethostname()
ip = socket.gethostbyname(hostname) def cpu_status():
with open("/proc/loadavg", 'rt') as f:
data = f.read().split()
if float(data[2]) > 0.7:
with open("/home/cpustatus.txt", 'wt') as f:
print("notice : \n Hostname : %s\n IP : %s\n\n The CPU loadavg will overload : %s" % (hostname, ip, data[2]), file = f)
mailll.send_mail("/home/cpustatus.txt") def memory_status():
theline = linecache.getline(r'/proc/meminfo', 2)
mem_free_num = "".join(re.findall(r"[0-9]", theline)) if (int(mem_free_num) / 1000) < 100:
with open("/home/memory.txt", 'wt') as f:
print("notice : \n Hostname : %s\n IP : %s\n\n Insufficient free memory : %s" % (hostname, ip, str(int(mem_free_num) / 1000) + "M"), file = f)
mailll.send_mail("/home/memory.txt") def disk_status():
os.system("df -hT |grep ext4 |awk -F '[ %]+' ' {if($6 >= 70) print $6}' |wc -l > /home/diskstatus.txt")
diskcount = int(linecache.getline(r'/home/diskstatus.txt', 1))
if diskcount >= 1 : with open("/home/diskstatus.txt", 'wt') as f:
print("notice : \n Hostname : %s\n IP : %s\n" % (hostname, ip), file = f)
os.system("echo 'Hard disk capacity is insufficient :' >> /home/diskstatus.txt")
os.system("df -hT |grep 'ext4\|Filesystem' >> /home/diskstatus.txt")
mailll.send_mail('/home/diskstatus.txt') try:
cpu_status()
time.sleep(2)
memory_status()
time.sleep(2)
disk_status()
except Exception as e:
print("program is error!!!")

新手,自我感觉就是功能实现了,代码实在不怎么样,但是还是记录下来,希望大家多多指出不足,以后随着学习的深入,希望能做的更好!

END!!!

用python监控Linux,CPU,内存,硬盘的更多相关文章

  1. jmeter监控linux cpu 内存 网络 IO

    下载地址:http://jmeter-plugins.org/downloads/all/ PerfMon: 用来监控Server的CPU.I/O.Memory等情况 ServerAgent-2.2. ...

  2. Linux 性能监控之CPU&内存&I/O监控Shell脚本2

    Linux 性能监控之CPU&内存&I/O监控Shell脚本2   by:授客 QQ:1033553122 思路: 捕获数据->停止捕获数据->提取数据 备注:一些命令的输 ...

  3. Linux 性能监控之CPU&内存&I/O监控Shell脚本1

    Linux 性能监控之CPU&内存&I/O监控Shell脚本1   by:授客 QQ:1033553122   #!/bin/bash # 获取要监控的本地服务器IP地址 IP=`if ...

  4. 第6章:使用Python监控Linux系统

    1.Python编写的监控工具 1).多功能系统资源统计工具dstat dstat是一个用Python编写的多功能系统资源统计工具,用来取代Linux下的vmstat,iostat,netstat和i ...

  5. VPS性能测试:CPU内存,硬盘IO读写,带宽速度,UnixBench和压力测试

    现在便宜的VPS主机越来越多了,一些美国的VPS主机甚至给出1美元一月的VPS,堪比虚拟主机还要便宜,巨大的价格优势吸引不少人购买和使用,而近些年来国内的主机商也开始意识到便宜的VPS对草根站长的诱惑 ...

  6. Windows 性能监视器的基本指标说明(CPU,内存,硬盘参数)

    [转]Windows 性能监视器的基本指标说明(CPU,内存,硬盘参数) 作为一个系统工程师来说,要看懂监控的数据至关重要,关系着优化和分析出现的问题.我是在运维过程中要用到的.因此,今天给出Wind ...

  7. Windows 性能监视器的基本指标(CPU,内存,硬盘参数)

    转载:http://kms.lenovots.com/kb/article.php?id=7045 Windows 性能监视器的基本指标(CPU,内存,硬盘参数) 作为一个系统工程师来说,要看懂监控的 ...

  8. 一个统计 CPU 内存 硬盘 使用率的shell脚本

    一个统计 CPU 内存 硬盘 使用率的shell脚本,供大家学习参考 #!/bin/bash #This script is use for describle CPU Hard Memery Uti ...

  9. 使用Python监控Linux系统

    一.Python编写的监控工具 一.多功能系统资源统计工具dstat 1.dstat介绍 dstat是一个用Python语言实现的多功能系统资源统计工具,用来取代Linux下的vmstat.iosta ...

  10. linux cpu内存利用率获取

    有了这么好的工具,我们还需要自己造轮子么? 两种情况,如果有复杂的监控需求,而且愿意花时间学习,我们可以使用nmon:但如果监控需求特殊比如说还要监控单个进程的情况,这时候就需要自己动手实现了.自己动 ...

随机推荐

  1. Java--RuntimeException(运行时异常)

    [转载自]:http://blog.csdn.net/qq7342272/article/details/7940741 java运行时异常是可能在java虚拟机正常工作时抛出的异常. java提供了 ...

  2. 全面理解Javascript中Function对象的属性和方法

    http://www.cnblogs.com/liontone/p/3970420.html 函数是 JavaScript 中的基本数据类型,在函数这个对象上定义了一些属性和方法,下面我们逐一来介绍这 ...

  3. WinForm中使用XML文件存储用户配置及操作本地Config配置文件

    大家都开发winform程序时候会大量用到配置App.config作为保持用户设置的基本信息,比如记住用户名,这样的弊端就是每个人一些个性化的设置每次更新程序的时候会被覆盖. 故将配置文件分两大类: ...

  4. FFmpeg.exe使用随笔

    一.将图片合成为MP4 1.将照片合成为h264 ffmpeg -framerate 12 -i %3d.png -c:v libx264 -pix_fmt yuv420p test.h2642.将h ...

  5. js call()和apply()

    一.call()和apply(),实例如下: function add(a,b) {     alert(a+b); } function sub(a,b) {     alert(a-b); } a ...

  6. PowerDesigner里数据表里AUTO_INCREMENT(自增)的设置

    使用流程: File->New Model->Model types->Physical Data Model->Physical Diagram 在下边DBMS里选择:MyS ...

  7. java 线程的让步

    //线程的让步 // //线程 class xc1 implements Runnable{ public void run(){ for(int i=1;i<=30;i++){ System. ...

  8. bootstrap-巨幕、缩略图、警告框

    巨幕: <div class="jumbotron"> <div class="container"> <h1>W3Scho ...

  9. InnoDB: Error number 24 means ‘Too many open files’.--转载

    一.问题的描述 备份程序 执行前滚的时候报错.(-apply-log) InnoDB: Errornumber 24 means 'Too many open files'. InnoDB: Some ...

  10. linux文件上传

    租了个服务器,要把自己打的war包打上去发布,服务器是linux的(本机是mac).以前都是用wget命令,把需要上传的东西先放到github上面再在服务器上用wget命令下载.最近不知道什么原因,w ...