#!/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. python 简介

    python简史 python的创始人:Guido van Rossum Guido 在1989年12月时,寻找一门“课余”编程项目来打发圣诞节前后的时间.Guido决定为当时正构思的一个新的脚本语言 ...

  2. 自动选中div中的文字

    <html> <head> <title></title> <script type="text/javascript" de ...

  3. 如何正确做 Web端压力测试?

    ​    ​一个完整的压力测试需要关注三个方面:如何正确产生压力.如何定位瓶颈.如何预估系统的承载能力. ​    ​(1) 如何产生压力:产生压力的方法有很多,通常可以写脚本产生压力机器人对服务器进 ...

  4. 【MySQL】探究之常用SQL

    一些SQL命令(不断更新,我总记不住,哭) List 建库建表 表的重命名(不区分大小写) 列的重命名 编码 修改结构 添加删除索引 大批量删除 binlog相关 select相关 数据库备份和恢复 ...

  5. Asp.net MVC 之过滤器

    整理一下MVC中的几种过滤器,以及每种过滤器是干什么用的 四种过滤器 1.AuthorizationFilter(授权过滤器) 2.ActionFilter(方法过滤器) 3.ResultFilter ...

  6. Navicat(连接) -1之HTTP 设置

    HTTP 设置 HTTP 通道是一种连接到服务器的方法,使用与网络服务器相同的通讯协定(http://)和相同的端口(端口 80).这是当你的互联网服务供应商不允许直接连接,但允许创建 HTTP 连接 ...

  7. 更改RAC日志组

    alter database add logfile thread 1 group 5 ('+DATA/idb/onlinelog/group5.log') size 256m;alter datab ...

  8. nginx/Windows-1.9.3启动脚本

    启动nginx.bat @echo off D: cd D:\Program Files\nginx-1.9.3 tasklist | findstr /i "nginx.exe" ...

  9. 项目管理软件之易度1.5,禅道2.0,redmine1.2(附redmine1.2的安装)

    http://www.cnblogs.com/ljzforever/archive/2011/06/29/2093786.html 公司最近准备分出一套人马去客户那里做驻场研发,这就涉及到代码库的统一 ...

  10. Android自动化初探:ADB

    Info:经过一段时间的准备,从今天开始自学Android之旅,初步学习会有疏漏,以后的每篇文章,我都会不断修改补全,直到完美. 2014-10-09:初版 --------------------- ...