• python psutil模块获取系统磁盘|CPU|内存Memory|时区TimeZone等信息


    1. # -*- coding:utf-8 -*-
    2. import subprocess
    3. import psutil
    4. class System(object):
    5. """
    6. 获取系统信息
    7. """
    8. def __init__(self):
    9. self.sys = {}
    10. def get_time_zone(self):
    11. """
    12. 获取系统时区
    13. """
    14. time_content = subprocess.Popen(
    15. "timedatectl status | grep Time",
    16. shell=True,
    17. stdout=subprocess.PIPE
    18. )
    19. content, _ = time_content.communicate()
    20. if content:
    21. time_zone = content.strip().split(": ")[-1]
    22. time_zone = time_zone.strip()
    23. else:
    24. time_zone = ""
    25. self.sys['time_zone'] = time_zone
    26. def _cpu(self):
    27. """
    28. 获取系统cpu信息
    29. """
    30. self.sys["cpu"] = {}
    31. self.sys["cpu"]["cpu_percent"] = psutil.cpu_percent()
    32. cpus = subprocess.Popen(
    33. "top -bn 1 | grep -v 'grep' | grep '%Cpu(s):'",
    34. shell=True,
    35. stdout=subprocess.PIPE,
    36. stderr=subprocess.PIPE,
    37. )
    38. if cpus.returncode is None:
    39. cpus = cpus.stdout.read().split()
    40. else:
    41. cpus = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    42. self.sys["cpu"]["user"] = cpus[1]
    43. self.sys["cpu"]["system"] = cpus[3]
    44. self.sys["cpu"]["idle"] = cpus[7]
    45. self.sys["cpu"]["cpus"] = psutil.cpu_percent(percpu=True)
    46. def _disk(self):
    47. """
    48. 获取磁盘信息
    49. """
    50. total_disk, used_total = 0, 0
    51. for i in psutil.disk_partitions():
    52. mountpoint = i.mountpoint
    53. if "home" in mountpoint and "docker" in mountpoint:
    54. continue
    55. used = psutil.disk_usage(i.mountpoint).used
    56. used_total += used
    57. mountpoint_list = mountpoint.split("/")
    58. if len(mountpoint_list) == 2:
    59. total_disk += psutil.disk_usage(i.mountpoint).total
    60. disk_remain = total_disk - used_total
    61. self.sys['disk'] = {}
    62. self.sys['disk']['total'] = total_disk
    63. self.sys['disk']['used'] = used_total
    64. self.sys['disk']['remain'] = disk_remain
    65. def _memory(self):
    66. """
    67. 获取内存信息
    68. """
    69. self.sys["memory"] = {}
    70. self.sys["memory"]["total"] = psutil.virtual_memory().total
    71. self.sys["memory"]["percent"] = psutil.virtual_memory().percent
    72. self.sys["memory"]["used"] = psutil.virtual_memory().used
    73. self.sys["memory"]["free"] = psutil.virtual_memory().free
    74. self.sys["memory"]["cached"] = (
    75. psutil.virtual_memory().buffers + psutil.virtual_memory().cached
    76. )
    77. self.sys["memory"]["swap"] = psutil.swap_memory().used
    78. def _traffic(self):
    79. """
    80. 获取所有网卡的流入和流出流量的总和
    81. """
    82. counters = psutil.net_io_counters(" ")
    83. self.sys["traffic"] = {"send": 0, "recv": 0}
    84. for counter in counters:
    85. if "veth" in counter or "lo" in counter or "docker" in counter:
    86. continue
    87. self.sys["traffic"]["send"] += counters[counter].bytes_sent
    88. self.sys["traffic"]["recv"] += counters[counter].bytes_recv
    89. if __name__ == "__main__":
    90. system = System()
    91. system.get_time_zone()
    92. system._disk()
    93. system._memory()
    94. system._traffic()
    95. print(system.sys)

  • 相关阅读:
    Redis离线安装(内网)
    数据仓库工具箱-第三章-零售业务
    多因素认证 (MFA) 是防止数据泄露的最佳安全实践方法
    【ArcGIS微课1000例】0051:Geodatabase子类型操作全解
    剑指offer专项突击版第30天
    提升创意设计水平:十个必备的平面设计素材网站
    养生产品如何进行线上推广?产品线上推广的渠道有哪些?
    Python 编程基础 | 第五章-类与对象 | 5.1、定义类
    第五届“传智杯”全国大学生计算机大赛(练习赛)题解
    uniapp h5实现Excel、Word、PDF文件在线预览,而不是跳转下载,也不需要下载
  • 原文地址:https://blog.csdn.net/xuezhangjun0121/article/details/127794202