python监控系统资源最终版(CPU,内存,磁盘等)

#!/usr/bin/env python3
#-*- coding:utf-8 -*-
#create at 2018-12-07
‘this is a system monitor scripts‘
__author__="yjt"

import os
import time
import sys
import datetime
import socket
import psutil
import re
import json

#以下是变量值,自己定义
CPUT = 2      #计算CPU利用率的时间间隔
NETT = 2      #计算网卡流量的时间间隔"""
#LOOPT = 2     """#脚本循环时间间隔"""

#获取系统基本信息"""
def baseinfo():
    hostname = socket.gethostname()
    user_conn = len(psutil.users())
    start_time = datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S")
    now_time = time.strftime(‘%Y-%m-%d %H:%M:%S‘,time.localtime(time.time()))
#    sys_runtime = time.strftime(‘%Y-%m-%d %H:%M:%S‘,time.localtime(time.time() - psutil.boot_time()))
    sys_runtime = os.popen(‘w‘).readline().split(‘users‘)[0].split(‘up‘)[1].strip()[:-1].strip()[:-1]
    process = os.popen(‘ps -ef |wc -l‘).read().split()[0]
    value = {"hostname":hostname,"user_conn":user_conn,"sys_start_time":start_time,"now_time":now_time,"sys_runtime":sys_runtime,"process":process}
    value_json = json.dumps(value)
    return value_json
print(‘‘)
print(baseinfo())
#获取CPU信息
def cpuinfo():
    ave_load = os.popen(‘uptime‘).readline().split(":")[-1].split()
    ave_load = ‘ ‘.join(ave_load) #CPU平均负载
    #以下四项值都是获取的瞬时值
    user_time = psutil.cpu_times().user  #用户态使用CPU时间
    sys_time = psutil.cpu_times().system #系统态使用CPU时间
    idle_time = psutil.cpu_times().idle #CPU空闲时间
    iowait_time = psutil.cpu_times().iowait #IO等待时间
    total_cpu = 0
    for i in range(len(psutil.cpu_times())):
        total_cpu += psutil.cpu_times()[i]
    user_use = str(round(user_time / total_cpu * 100,2)) + ‘%‘
    sys_use = str(round(sys_time / total_cpu * 100,2)) + ‘%‘
    idle = str(round(idle_time / total_cpu * 100,2)) + ‘%‘
    iowait = str(round(iowait_time / total_cpu * 100,2)) + ‘%‘
    cpu_pre = str(psutil.cpu_percent(CPUT)) + "%"
    logical_cpu = psutil.cpu_count()
    pyhsical_cpu = psutil.cpu_count(logical=False)
    #获取CPU使用最高的前十行
    #top10 = len(os.popen(‘ps aux|grep -v PID|sort -rn -k +3|head -10‘).readlines())
#    dic1 = {"ave_load":ave_load,"user_use":user_use,"sys_use":sys_use,"idle":idle,"iowait":iowait,"cpu_pre":cpu_pre,"logical_cpu":logical_cpu,"pyhsical_cpu":pyhsical_cpu}
    l1,l2,l3,l4,l5,l6 = [],[],[],[],[],[]
    i = 0
    while i < 10:
        #user = os.popen(‘ps aux|grep -v PID|sort -rn -k +4|head‘).readlines().[info].split()[0]
        #pid = int(os.popen(‘ps aux|grep -v PID|sort -rn -k +3|head -10‘).readlines()[i].split()[1])
        #a = os.popen(‘ps aux |sort -k3 -nr‘).readlines()[i].split()
        try:
            info = ‘‘
            info = psutil.Process(int(os.popen("ps aux|grep -v PID|sort -rn -k +3").readlines()[i].split()[1]))
            #if bool(info):
            #pid = psutil.Process(int(os.popen(‘ps aux|grep -v PID|sort -rn -k +3‘).readlines()[i].split()[1])).pid
            pid = info.pid
            user = info.username()
            process_name = info.name()
            cpu_use = str(info.cpu_percent()) + ‘%‘
            status = info.status()
            l1.append(user)
            l2.append(pid)
            l3.append(cpu_use)
            l4.append(process_name)
            l5.append(status)
            i += 1
        except:
            pass
        continue
    c0 = []
    l = ["user","pid","cpu_use","process_name","status"]
    cpu_value = list(zip(l1,l2,l3,l4,l5))
    cpu_len = len(cpu_value)
    for i in range(cpu_len):
        c0.append(dict(zip(l,cpu_value[i])))
#def cpu():
#以下获取的是逻辑CPU的瞬时值
    with open(‘/proc/stat‘,‘r‘) as f:
        cpu_item = f.readlines()
    cpu_number,cpu_use = [],[]
    for i in cpu_item:
        if re.search("^cpu[0-9]{1,}",i):
            cpu_info = i
            cpu_info = cpu_info.split(‘ ‘)
            #cpu_number = cpu_info[0]
            cpu_number.append(cpu_info[0])
            cpu_total = 0
            for num in cpu_info:
                if num.isdigit():
                    cpu_total += float(num)
                cpu_free = float(cpu_info[4])
            cpu_u = str(round((1 - cpu_free / cpu_total) * 100 ,2)) + ‘%‘
            cpu_list = cpu_u.split()
            cpu_str = ‘‘.join(cpu_list)
            cpu_use.append(cpu_str)
    c1 = []
    cpu_l = ["cpu_number","cpu_use"]
    cpu_v = list(zip(cpu_number,cpu_use))
    cpu_len = len(cpu_v)
    for i in range(cpu_len):
        c1.append(dict(zip(cpu_l,cpu_v[i])))
    value =   {
                "ave_load":ave_load,
                "user_use":user_use,
                "sys_use":sys_use,
                "idle":idle,
                "iowait":iowait,
                "cpu_pre":cpu_pre,
                "logical_cpu":logical_cpu,
                "pyhsical_cpu":pyhsical_cpu,
                "logical_cpu_use":c1,
                "cpu_top10":c0
              }
    value_json = json.dumps(value)
    return value_json
print(‘‘)
print(cpuinfo())
#cpuinfo()
#获取内存信息
def meminfo():
    total_mem = str(round(psutil.virtual_memory().total / 1024 /1024/1024)) + ‘G‘
    use_mem = str(round(psutil.virtual_memory().used / 1024 /1024/1024)) + ‘G‘
    mem_percent = str(psutil.virtual_memory().percent) + ‘%‘
    free_mem = str(round(psutil.virtual_memory().free / 1024 /1024/1024)) + ‘G‘
    swap_mem = str(round(psutil.swap_memory().total / 1024 /1024/1024)) + "G"
    swap_use = str(round(psutil.swap_memory().used / 1024 /1024/1024 )) + ‘G‘
    swap_free = str(round(psutil.swap_memory().free / 1024 /1024/1024))  + ‘G‘
    swap_percent = str(psutil.swap_memory().percent) + ‘%‘
 #获取memory使用最高的前十行
    #top10 = len(os.popen(‘ps aux|grep -v PID|sort -rn -k +4|head -10‘).readlines())
#    dic1 = {"ave_load":ave_load,"user_use":user_use,"sys_use":sys_use,"idle":idle,"iowait":iowait,"cpu_pre":cpu_pre,"logical_cpu":logical_cpu,"pyhsical_cpu":pyhsical_cpu}
    l1,l2,l3,l4,l5,l6 = [],[],[],[],[],[]
    i = 0
    while i < 10:
        #user = os.popen(‘ps aux|grep -v PID|sort -rn -k +4|head‘).readlines().[info].split()[0]
        #pid = int(os.popen(‘ps aux|grep -v PID|sort -rn -k +4|head -10‘).readlines()[i].split()[1])
        try:
            info = psutil.Process(int(os.popen(‘ps aux|grep -v PID|sort -rn -k +4‘).readlines()[i].split()[1]))
            pid = info.pid
            user = info.username()
            process_name = info.name()
            mem_use = str(round(info.memory_percent(),2)) + ‘%‘
            status = info.status()
            l1.append(user)
            l2.append(pid)
            l3.append(mem_use)
            l4.append(process_name)
            l5.append(status)
            i += 1
        except:
            pass
        continue
    m0 = []
    l = ["user","pid","mem_use","process_name","status"]
    mem_value = list(zip(l1,l2,l3,l4,l5))
    mem_len = len(mem_value)
    for i in range(mem_len):
        m0.append(dict(zip(l,mem_value[i])))

    value =  {
               "total_mem":total_mem,
               "use_mem":use_mem,
               "free_mem":free_mem,
               "mem_percent":mem_percent,
               "swap_mem":swap_mem,
               "swap_use":swap_use,
               "swap_free":swap_free,
               "swap_percent":swap_percent,
               "mem_top10":m0
             }
    value_json = json.dumps(value)
    return value_json
print(‘‘)
print(meminfo())
#获取磁盘信息
def diskinfo():
    #print("\033[31mdisk_info:\033[0m")
    #print("disk%-10s total%-10s free%-10s used%-10s percent%-10s"%(‘‘,‘(G)‘,‘(G)‘,‘(G)‘,‘(%)‘))
    disk_num = int(‘‘.join(os.popen("ls /dev/sd[a-z]|wc -l").readlines()[0].split()))
    d1,d2,d3,d4,d5 = [],[],[],[],[]
    disk_total,disk_used,disk_free = 0,0,0
    disk_len = len(psutil.disk_partitions())
    for info in range(disk_len):
        disk = psutil.disk_partitions()[info][1]
        if len(disk) < 10:
            d1.append(disk)
            total = str(round(psutil.disk_usage(disk).total /1024/1024/1024)) + ‘G‘
            total_num = psutil.disk_usage(disk).total
            disk_total  += total_num
            free = str(round(psutil.disk_usage(disk).free /1024/1024/1024)) + ‘G‘
            disk_free += psutil.disk_usage(disk).free
            used = str(round(psutil.disk_usage(disk).used /1024/1024/1024)) + ‘G‘
            disk_used += psutil.disk_usage(disk).used
            percent = str(psutil.disk_usage(disk).percent) + ‘%‘
            d2.append(total)
            d3.append(free)
            d4.append(used)
            d5.append(percent)
    disk_used_percent = str(round(disk_used / disk_total * 100,2)) + ‘%‘
    disk_free_percent = str(round(disk_free / disk_total * 100,2)) + ‘%‘
    disk_total = str(round(disk_total /1024/1024/1024)) + "G"
    disk_free = str(round(disk_free /1024/1024/1024)) + "G"
    disk_used = str(round(disk_used /1024/1024/1024)) + "G"
    d0 = []
    d = ["mount","total","free","used","percent"]
    disk_value = list(zip(d1,d2,d3,d4,d5))
    disk_len = len(disk_value)
    for i in range(disk_len):
        d0.append(dict(zip(d,disk_value[i])))
    value =  {
               "disk":[
                        {"disk_num":disk_num},
                        {"disk_total":disk_total},
                        {"disk_free":disk_free},
                        {"disk_used":disk_used},
                        {"disk_used_percent":disk_used_percent},
                        {"disk_free_percent":disk_free_percent}
                      ],
               "partitions":d0
             }
    value_json = json.dumps(value)
    return value_json
print(‘‘)
print(diskinfo())
#diskinfo()

#获取网卡信息
def netinfo():
    net_item = list(psutil.net_if_addrs())
    n1,n2,n3,n4,n5,n6 = [],[],[],[],[],[]
    for net in net_item:
        if re.search(r‘bond.*|em.*|eno.*|^eth.*‘,net):
            network_card = net
            n1.append(network_card)
            ip = psutil.net_if_addrs()[net][0].address
            ip_len = len(ip.split(‘.‘))
            if ip_len == 4:
                ip =ip
            else:
                ip = ‘null‘
            n2.append(ip)
            recv_1,recv_2,send_1,send_2=0,0,0,0
            with open (‘/proc/net/dev‘,‘r‘) as f:
                net_info = f.readlines()[2:]
               # net_list = str(net_info).lower().split()
                net_len = len(net_info)
                for i in range(net_len):
                    net_n_info = net_info[i].split()
                    net_list = net_info[i].split(‘:‘)[0].strip(‘ ‘)
                    #print(net_list)
                    if net_list == net:
                        recv_1 = float(net_n_info[1])
                    #    print(recv_1)
                        send_1 = float(net_n_info[9])
            time.sleep(NETT)
            with open (‘/proc/net/dev‘,‘r‘) as f:
                net_info = f.readlines()[2:]
                #net_list = str(net_info).lower().split()
                net_len = len(net_info)
                for i in range(net_len):
                    net_n_info = net_info[i].split()
                    net_list = net_info[i].split(‘:‘)[0].strip(‘ ‘)
                    if net_list == net:
                        recv_2 = float(net_n_info[1])
                        #print(recv_2)
                        send_2 = float(net_n_info[9])
                received = str(round(recv_2/1024 - recv_1/1024,2)) + "kb/s"
                transmit = str(round(send_2/1024 - send_1/1024,2)) + "kb/s"
                n3.append(transmit)
                n4.append(received)
    #print(n3,n4)
            #网卡带宽
#            net_speed = list(os.popen("ethtool %s|grep -i speed"%(network_card)).readlines())[0].split()[1]
#            n5.append(net_speed)
            #网卡模式
#            net_duplex = list(os.popen("ethtool %s|grep -i Duplex"%(network_card)).readlines())[0].split()[1]
#            n6.append(net_duplex)
    #print (n1,n2,n3,n4)
    n0 = []
#    n = ["network_card","ip","transmit","received","net_speed","net_duplex"]
    n = ["network_card","ip","transmit","received"]
    net_value = list(zip(n1,n2,n3,n4))
    net_len = len(net_value)
    for i in range(net_len):
        n0.append(dict(zip(n,net_value[i])))
    value = {
             "network":n0
            }
    value_json = json.dumps(value)
    return value_json
print(‘‘)
print(netinfo())
#netinfo()
#获取TCP连接数
def tcpinfo():
    status_list = ["LISTEN","ESTABLISHED","TIME_WAIT","CLOSE_WAIT","LAST_ACK","SYN_SENT"]
    status_init = []
    net_conn =  psutil.net_connections()
    n1 = []
    for key in net_conn:
        status_init.append(key.status)
    for value in status_list:
        num = status_init.count(value)
        n1.append(num)
    value =    {
            "LISTEN":n1[0],
            "ESTABLISHED":n1[1],
            "TIME_WAIT":n1[2],
            "CLOSE_WAIT":n1[3],
            "LAST_ACK":n1[4],
            "SYN_SENT":n1[5]
           }
    value_json = json.dumps(value)
    return value_json
print(‘‘)
print(tcpinfo())

  注:该脚本也适用于python2,当然,需要安装psutil模块,或者已安装anaconda

原文地址:https://www.cnblogs.com/yjt1993/p/10083123.html

时间: 2024-12-21 21:32:57

python监控系统资源最终版(CPU,内存,磁盘等)的相关文章

java获取cpu,内存,磁盘等信息

原文:java获取cpu,内存,磁盘等信息 源代码下载地址:http://www.zuidaima.com/share/1550463331306496.htm package com.zuidaima.util; import java.io.File; import java.io.InputStreamReader; import java.io.LineNumberReader; import java.util.ArrayList; import java.util.List; imp

linux系统CPU,内存,磁盘,网络流量监控脚本

前序 1,#cat /proc/stat/ 信息包含了所有CPU活动的信息,该文件中的所有值都是从系统启动开始累积到当前时刻 2,#vmstat –s 或者#vmstat 虚拟内存统计 3, #cat /proc/loadavg 从系统启动开始到当前累积时刻 4, #uptime 系统运行多长时间 5, #mpstat CPU的一些统计信息 6, # 一,linux系统CPU,内存,磁盘,网络流量监控脚本 [作者:佚名来源:不详时间:2010-7-6 [我来说两句大中小] cme.sh网络流量监

linux系统CPU内存磁盘监控发送邮件脚本之二

#!/bin/bash export PATH export LANG=zh_CN.UTF-8 # # #top之后输入数字1,可以查看每颗CPU的情况. # # #先配置好mailx邮箱账号密码: #cat>/etc/mail.rc<<"EOF" #set sendcharsets=iso-8859-1,utf-8 #set [email protected] #set smtp=smtp.163.com #set [email protected] #set sm

监控cpu内存磁盘,并实现邮件报警

1 #!/bin/bash 2 #获取当前时间 3 now=`date -u -d"+8 hour" +'%Y-%m-%d %H:%M:%S'` 4 #cpu使用阈值 5 cpu_warn='5' 6 #mem空闲阈值 7 mem_warn='1' 8 #disk使用阈值 9 disk_warn='5' 10 #获取主机IP,下条命令也可以获取 11 hostip=$(ip addr | awk '/^[0-9]+: / {}; /inet.*global/ {print gensub

python监控系统资源

监控网卡流量 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 #!/usr/bin/python import re import os #get SNMP-MIB2 of the devices def getAllitems(host,oid):         sn1 = os.popen('snmpwalk -v 2c -c p

CPU,内存,磁盘,指令以及它们之间的关系

众所周知计算机的组成是由控制器.运算器.存储器.输入.输出设备五部分构成,而其中CPU就是由控制器.运算器.寄存器和时钟四部分构成,磁盘和内存就属于存储设备. CPU指令主要有以下四个: 1.加载:把一个字节或一个字从内存中赋值到寄存器,覆盖原来寄存器的内容 2.存储:把一个字节或一个字从寄存器复制到内存中的某个位置,覆盖原来内存上这个位置的内容 3.操作:将两个寄存器的内容赋值到ALU,ALU对这两个字进行算术操作,将结果存储在某个寄存器中,以覆盖寄存器中原来的值 4.跳转:从指令本身抽取一个

Shell采集系统cpu 内存 磁盘 网络信息

cpu信息采集 cpu使用率 采集算法 通过/proc/stat文件采集并计算CPU总使用率或者单个核使用率.以cpu0为例,算法如下: 1. cat /proc/stat | grep ‘cpu0’得到cpu0的信息 2. cpuTotal1=user+nice+system+idle+iowait+irq+softirq 3. cpuUsed1=user+nice+system+irq+softirq 4. sleep 30秒 5. 再次cat /proc/stat | grep 'cpu0

性能优化之基础资源cpu&amp;内存(JVM)

本章主要介绍计算机的一些基础资源以及操作系统处理后的一些基础资源. 主要包括 cpu 内存 磁盘 网络 线程 本章会介绍这些资源的一些原理,介绍如何查看资源的数量,使用情况,对性能和整体计算机执行的一些影响.本章很多内容都基于linux,不是特殊说明,就是针对linux的情况.可能在其它操作系统不一定适用. 另外还会对jvm之上的一些内容做特殊说明,因为大家很大的一些积累都在jvm之上,内部的系统基本也建立在jvm上. jvm 指令 jvm 内存使用 cpu cpu是计算机里最重要的资源,没有之

利用shell监控cpu、磁盘、内存使用率

利用shell监控cpu.磁盘.内存使用率,达到警报阈值发邮件进行通知 并配合任务计划,即可及时获取报警信息 #!/bin/bash ############################################## #Author: Liuzhengwei - [email protected] #QQ:1135960569 #Last modified: 2017-04-19 21:50 #Filename: jiankong.sh #Description:  #########