获取服务器硬件详情(python)

一,获取服务器硬件详情

​ 旨为方便获取服务器详细信息

二, Tips

# python 3.7
socket
 psutil
platform
uuid

三, 代码示例

# _*_ coding: utf-8 _*_

import socket
import psutil
import platform
import uuid

class Server(object):
    def __init__(self):
        pass

    @property
    def system(self):
        hostname = socket.gethostname()
        ip = socket.gethostbyname(hostname)
        mac = uuid.UUID(int = uuid.getnode()).hex[-12:]
        mac = ":".join([mac[e:e + 2] for e in range(0, 11, 2)])
        os = platform.system()
        return {
            'hostname': hostname, # 主机名称
            'ip': ip, # IP地址
            'mac': mac, # MAC地址
            'os': os, # 操作系统
         }

    @property
    def cpu(self):
        return {
            'count': psutil.cpu_count(logical=False),  # 查看cpu物理个数
            'percent': str(psutil.cpu_percent(interval=2, percpu=False)) + '%'  # CPU的使用率(interval是获取2s内的cpu使用率波动)
        }

    # 内存
    @property
    def memory(self):
        total = round(psutil.virtual_memory().total / (1024.0 * 1024.0 * 1024.0), 2)  # 总物理内存(DDR)
        free = round(psutil.virtual_memory().free / (1024.0 * 1024.0 * 1024.0), 2)  # 剩余物理内存(DDR)
        percent = round((total - free) / total, 2)  # 物理内存使用率(DDR)
        return {
            'free': str(free) + 'G',
            'total': str(total) + 'G',
            'percent': str(percent * 100) + '%'
        }

    # 硬盘
    @property
    def disk(self):
        disk_usage = psutil.disk_usage('/')
        free = round(disk_usage.free / (1024.0 * 1024.0 * 1024.0), 2)
        total = round(disk_usage.total / (1024.0 * 1024.0 * 1024.0), 2)
        percent = round((total - free) / total, 2)
        return {
            'free': str(free) + 'G',
            'total': str(total) + 'G',
            'percent': str(percent * 100) + '%'
        }

if __name__ == '__main__':
    info = Server()
    print(info.system)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
编写服务器硬件状态巡检脚本可以使用Python的第三方库psutil来获取服务器硬件信息和状态。以下是一个简单的示例脚本: ```python import psutil # 获取CPU信息 cpu_percent = psutil.cpu_percent() cpu_count = psutil.cpu_count() cpu_freq = psutil.cpu_freq() # 获取内存信息 mem = psutil.virtual_memory() mem_total = mem.total mem_used = mem.used mem_percent = mem.percent # 获取磁盘信息 disk = psutil.disk_usage('/') disk_total = disk.total disk_used = disk.used disk_percent = disk.percent # 获取网络信息 net = psutil.net_io_counters() net_bytes_sent = net.bytes_sent net_bytes_recv = net.bytes_recv # 打印硬件状态信息 print("CPU使用率:{}%".format(cpu_percent)) print("CPU核心数:{}".format(cpu_count)) print("CPU频率:{}MHz".format(cpu_freq.current)) print("内存总量:{}B".format(mem_total)) print("内存使用量:{}B".format(mem_used)) print("内存使用率:{}%".format(mem_percent)) print("磁盘总量:{}B".format(disk_total)) print("磁盘使用量:{}B".format(disk_used)) print("磁盘使用率:{}%".format(disk_percent)) print("网络发送字节数:{}B".format(net_bytes_sent)) print("网络接收字节数:{}B".format(net_bytes_recv)) ``` 通过使用psutil库可以轻松地获取服务器的CPU使用率、内存状态、磁盘空间以及网络流量等信息,并对其进行监控和巡检。根据具体需求,我们可以进一步处理和记录这些信息,也可以设置定期巡检并发送邮件或生成报告等功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值