syspy  0.1
syspy windows system information tool with python
disk.py
Go to the documentation of this file.
1 __title__ = 'syspy'
2 __version__ = '0.1'
3 __author__ = 'Ali GOREN <goren.ali@yandex.com>'
4 __repo__ = 'https://github.com/aligoren/syspy'
5 __license__ = 'Apache v2.0 License'
6 
7 import psutil
8 import os
9 
10 class Disk:
11 
12  def __init__(self):
13  self.bytes2human
15  self.getDiskUsage
17 
18  def bytes2human(self,n):
19  # http://code.activestate.com/recipes/578019
20  # >>> bytes2human(10000)
21  # '9.8K'
22  # >>> bytes2human(100001221)
23  # '95.4M'
24  symbols = ('K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y')
25  prefix = {}
26  for i, s in enumerate(symbols):
27  prefix[s] = 1 << (i + 1) * 10
28  for s in reversed(symbols):
29  if n >= prefix[s]:
30  value = float(n) / prefix[s]
31  return '%.1f%s' % (value, s)
32  return "%sB" % n
33 
34  def getDiskPartitions(self):
35  """
36  Return all mounted disk partitions as a list of namedtuples
37  including device, mount point and filesystem type,
38  similarly to “df” command on UNIX. If all parameter
39  is False return physical devices only
40  (e.g. hard disks, cd-rom drives, USB keys)
41  and ignore all others (e.g. memory partitions such as /dev/shm).
42  Namedtuple’s fstype field is a string which varies depending on the platform.
43  On Linux it can be one of the values found in /proc/filesystems
44  (e.g. 'ext3' for an ext3 hard drive o 'iso9660' for the CD-ROM drive).
45  On Windows it is determined via GetDriveType and can be either
46  "removable", "fixed", "remote", "cdrom", "unmounted" or "ramdisk".
47  On OSX and FreeBSD it is retrieved via getfsstat(2).
48  """
49  for part in psutil.disk_partitions(all=False):
50  if os.name == 'nt':
51  if 'cdrom' in part.opts or part.fstype == '':
52  continue
53 
54  usage = psutil.disk_usage(part.mountpoint)
55  ret_pd = part.device
56  ret_ut = self.bytes2human(usage.total)
57  ret_usu = self.bytes2human(usage.used)
58  ret_ufree = self.bytes2human(usage.free)
59  ret_uper = int(usage.percent)
60  ret_prt = part.fstype
61  ret_pmoup = part.mountpoint
62 
63  return ret_pd, ret_ut, ret_usu, ret_ufree, ret_uper, ret_prt, ret_pmoup
64 
65  def getDiskUsage(self):
66  """
67  Return disk usage statistics about the given path as a namedtuple including total,
68  used and free space expressed in bytes, plus the percentage usage.
69  OSError is raised if path does not exist
70  """
71  dusage = list(psutil.disk_usage('/'))
72 
73  ret_total = self.bytes2human(dusage[0])
74  ret_used = self.bytes2human(dusage[1])
75  ret_free = self.bytes2human(dusage[2])
76  ret_percent = dusage[3]
77 
78  return ret_total, ret_used, ret_free, ret_percent
79 
80  def getDiskIoCounters(self):
81  """
82  Return system-wide disk I/O statistics as a namedtuple including the following fields:
83 
84  read_count: number of reads
85  write_count: number of writes
86  read_bytes: number of bytes read
87  write_bytes: number of bytes written
88  read_time: time spent reading from disk (in milliseconds)
89  write_time: time spent writing to disk (in milliseconds)
90  """
91  dioc = list(psutil.disk_io_counters())
92 
93  ret_read_count = dioc[0]
94  ret_write_count = dioc[1]
95  ret_read_bytes = dioc[2]
96  ret_write_bytes = dioc[3]
97  ret_read_time = dioc[4]
98  ret_write_time = dioc[5]
99 
100  return ret_read_count, ret_write_count, ret_read_bytes, ret_write_bytes, ret_read_time, ret_write_time
def getDiskIoCounters(self)
Definition: disk.py:80
def getDiskPartitions(self)
Definition: disk.py:34
def bytes2human(self, n)
Definition: disk.py:18
def __init__(self)
Definition: disk.py:12
def getDiskUsage(self)
Definition: disk.py:65