栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

上报服务器的内存和CPU

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

上报服务器的内存和CPU

读取服务器的内存和CPU

psutil (python system and process utilities) 是一个跨平台的第三方库,能够轻松实现获取系统运行的进程和系统利用率(包扩CPU、内存、磁盘、网络等)信息。它主要用于系统监控、分析、限制系统资源和进程的管理。它实现了同等命令行工具提供的功能,如ps、top、lsof、netstat、ifconfig、who、df、kill、free、nice、ionice、iostat、iotop、uptime、pidof、tty、taskset、pmap等。目前支持的操作系统包括:Linux,Windows,macOS,AIX等等。同时支持python2与python3版本,当前最新版本为5.4.8。

安装 psutil

pip install psutil
编写python 脚本

SvrIndicator.py

import os,sys
import psutil
import urllib.request

class SvrIndicator:
    
    url = ""
    code = ""

    def __init__(self):
        if sys.argv.__len__() == 3:
            try:
                ## 参数 1 机器编码,比如 svr112
                ## 参数 2 上传地址, 比如 http://demo.com/write.php

                print(sys.argv[1])
                self.code = sys.argv[1]
                self.url = sys.argv[2]
            except:
                print("error")
        else:
            print("no init arg")
    
    """
    将服务器信息上传到互联网
    """
    def upload(self):
        if self.url :
            try:
                print("开始上传")
                cpu, memory, total, free = self.get_ecs_cpu_and_memory()
                print('cpu', cpu)
                print('memory', memory)
                print('total: {} GB'.format(total))
                print('free: {} GB'.format(free))
                q = "?code={code}&cpu={cpu}&memo={memo}&memo_free={memo_free}&memo_total={memo_total}".format(**{
                    "code":self.code,
                    "cpu":cpu,
                    "memo":memory,
                    "memo_free":free,
                    "memo_total":total,
                })
                self.url = self.url + q
                print(self.url)
                # GET
                resp = urllib.request.urlopen(self.url)
                print(resp.status)
                print(resp.read().decode("UTF-8"))

            except:
                print("net error")



        else:
            print("无法确定上传地址")


    """
        读取服务器CPU和内存
    """
    def get_ecs_cpu_and_memory(self):
        data = psutil.virtual_memory()
        total = data.total  # 总内存,单位为byte
        total = round(total / 1024 / 1024 / 1024, 2)  # 转换成GB
        free = data.available  # 可用内存
        free = round(free / 1024 / 1024 / 1024, 2)  # 转换成GB
        memory = "%0.2f" % (int(round(data.percent))) + ""  # 内存使用情况
        cpu = "%0.2f" % psutil.cpu_percent(interval=1) + ""  # CPU占用情况
        return cpu, memory, total, free



if __name__ == "__main__":
    si = SvrIndicator()
    si.upload()
打包代码为exe 2.1 安装pyinstaller

安装指令:

pip install pyinstaller
2.2 cmd 切换到打包python脚本 SvrIndicator.py 目录 2.3 打包文件
pyinstaller -F SvrIndicator.py

子目录 dist 会生成一个 SvrIndicator.exe 文件

编写批处理脚本
SvrIndicator.exe code123 http://demo.com/write.php
设置为定时任务即可 参考

python对服务器的cpu和内存的监控

Python之psutil库的使用

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/350662.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号