如何用Python实现查看服务器硬件信息
Admin 2022-08-22 群英技术资讯 845 次浏览
很多朋友都对“如何用Python实现查看服务器硬件信息”的内容比较感兴趣,对此小编整理了相关的知识分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获,那么感兴趣的朋友就继续往下看吧!有时候,我们在项目中需要实时监控服务器的硬件信息,那么python该如何获取呢,废话不多说,直接撸上代码。
import os
import platform
import re
from datetime import datetime
import time
import psutil
'''获取服务器信息'''
def get_server_information():
# 主机名称
hostname = platform.node()
# 系统版本
system_version = platform.platform()
# python版本
python_version = platform.python_version()
# 逻辑cpu数量
cpu_count = psutil.cpu_count()
# cup使用率
cpus_percent = psutil.cpu_percent(interval=0.1)
# 内存
memory_information = psutil.virtual_memory()
# 内存使用率
memory_usage = memory_information.percent
memory_used = str(round(memory_information.used / 1024 / 1024))
memory_total = str(round(memory_information.total / 1024 / 1024))
memory_free = str(round(memory_information.free / 1024 / 1024))
# 磁盘信息
disk_partitions_list = []
# 判断是否在容器中
if not os.path.exists('/.dockerenv'):
disk_partitions = psutil.disk_partitions()
for i in disk_partitions:
a = psutil.disk_usage(i.device)
disk_partitions_dict = {
'device-系统盘位置': i.device,
'fstype-硬盘类型': i.fstype,
'total-总容量': str(round(a.total / 1024 / 1024)),
'used-已用硬盘': str(round(a.used / 1024 / 1024)),
'free-空闲硬盘': str(round(a.free / 1024 / 1024)),
'percent已用比例': a.percent
}
disk_partitions_list.append(disk_partitions_dict)
# 开机时间
boot_time = datetime.fromtimestamp(psutil.boot_time()).replace(microsecond=0)
up_time = datetime.now().replace(microsecond=0) - boot_time
up_time_list = re.split(r':', str(up_time))
up_time_format = " {} 小时{} 分钟{} 秒".format(up_time_list[0], up_time_list[1], up_time_list[2])
# 当前时间
time_now = time.strftime('%H:%M:%S ', time.localtime(time.time()))
data = {}
data["hostname-主机名称"] = hostname
data["system_version-系统版本"] = system_version
data["python_version-python版本"] = python_version
data["cpus_percent-cup使用率"] = cpus_percent
data["已用内容-memory_usage"] = memory_usage
data["cpu_count-CPU核数"] = cpu_count
data["memory_used-已用内存"] = memory_used
data["memory_total-总内存容量"] = memory_total
data["memory_free-空闲内存"] = memory_free
data["boot_time-服务器时间"] = boot_time
data["up_time_format-服务器运行时间"] = up_time_format
data["disk_partitions_list"] = disk_partitions_list
data["time_now"] = time_now
return data
print(get_server_information())
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
Python内置函数-classmethod()。classmethod 修饰符对应的函数不需要实例化,不需要 self 参数,但第一个参数需要是表示自身类的 cls 参数,可以来调用类的属性,类的方法,实例化对象等。
这篇文章主要介绍了教你怎么用Python实现多路径迷宫,文中有非常详细的代码示例,对正在学习python的小伙伴们有非常好的帮助,需要的朋友可以参考下
为什么要用numpy? Python中提供了list容器,可以当作数组使用。但列表中的元素可以是任何对象,因此列表中保存的是对象的指针,这样一来,为了保存一个简单的列表[1,2,3]。就需要三个指针和三个整数对象。对于数值运算来说,这种结构显然不够高效。
这篇文章主要给大家介绍了关于如何利用Python开发一个简单的猜数字游戏的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Python具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
这篇文章主要为大家介绍了python神经网络特征金字塔FPN原理的解释,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008