koder aka kdanilov | 6e2ae79 | 2015-03-04 18:02:24 -0800 | [diff] [blame] | 1 | import os.path |
| 2 | |
| 3 | import psutil |
| 4 | |
| 5 | |
| 6 | def get_disk_by_mountpoint(mnt_point): |
| 7 | """ Return disk of mountpoint """ |
| 8 | diskparts = psutil.disk_partitions() |
| 9 | for item in diskparts: |
| 10 | if item.mountpoint == mnt_point: |
| 11 | return os.path.realpath(item.device) |
| 12 | |
| 13 | raise OSError("Can't define disk for {0!r}".format(mnt_point)) |
| 14 | |
| 15 | |
| 16 | def find_mount_point(path): |
| 17 | """ Find mount point by provided path """ |
| 18 | path = os.path.abspath(path) |
| 19 | while not os.path.ismount(path): |
| 20 | path = os.path.dirname(path) |
| 21 | return path |
| 22 | |
| 23 | |
| 24 | class DiskInfo(object): |
| 25 | def __init__(self, name, rd_cnt=0, wr_cnt=0, rd_bytes=0, |
| 26 | wr_bytes=0, rd_time=0, wr_time=0): |
| 27 | self.name = name |
| 28 | self.rd_cnt = rd_cnt |
| 29 | self.wr_cnt = wr_cnt |
| 30 | self.rd_bytes = rd_bytes |
| 31 | self.wr_bytes = wr_bytes |
| 32 | self.rd_time = rd_time |
| 33 | self.wr_time = wr_time |
| 34 | |
| 35 | def __str__(self): |
| 36 | message = 'DISK {0.name}: read count {0.rd_cnt}' + \ |
| 37 | ', write count {0.wr_cnt}' + \ |
| 38 | ', read bytes {0.rd_bytes}' + \ |
| 39 | ', write bytes {0.wr_bytes}' + \ |
| 40 | ', read time {0.rd_time}' + \ |
| 41 | ', write time {0.wr_time}' |
| 42 | return message.format(self) |
| 43 | |
| 44 | |
| 45 | def get_io_stats(path): |
| 46 | """ Return list of CEPHDiskInfo for all disks that used by CEPH on the |
| 47 | local node |
| 48 | """ |
| 49 | stat = psutil.disk_io_counters(perdisk=True) |
| 50 | disk = get_disk_by_mountpoint(find_mount_point(path)) |
| 51 | disk_base = os.path.basename(disk) |
| 52 | print disk_base |
| 53 | try: |
| 54 | return stat[disk_base] |
| 55 | except IndexError: |
| 56 | raise OSError("Disk {0} not found in stats".format(disk)) |