• 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)

  • 相关阅读:
    C++ 静态断言 static_assert
    24.flink windows 窗口,彻底站起来。
    基于Xml方式的Bean的配置-Bean的作用范围scope配置
    .NET餐厅管理系统sql数据帮助类执行单条SQL(插入、更新、删除)
    【Hadoop大数据技术】——MapReduce分布式计算框架(学习笔记)
    多路径来源页面导航高亮以及面包屑导航修改
    ubuntu22 mysql8.0如何搭建主从复制?[亲自实践,步骤简洁]
    FANUC机器人在ROBOGUIDE中通过MODBUS POLL实现MODBUS TCP通信的具体方法
    Linux安装GitLab教程
    素数筛法及其优化策略
  • 原文地址:https://blog.csdn.net/xuezhangjun0121/article/details/127794202