#/usr/bin/python
# -*- coding: UTF-8 -*-
from __future__ import print_function
from collections import namedtuple
Disk = namedtuple('Disk', 'major_number minor_number device_name'
' read_count red_merged_count red_sections'
' time_spent_reading write_count write_merged_count'
' write_sections time_spent_write io_requests'
' time_spent_doing_io weighted_time_spent_doing_io')
def get_disk_info(device):
"""
从/proc/diskstats中读取磁盘IO信息
$ cat /proc/diskstats
"""
with open("/proc/diskstats") as f:
for line in f:
if line.split()[2] == device:
print (*(line.split()))
print (line)
print (' '.join(line.split()))
return Disk(*(line.split()))
raise RuntimeError("device ({0}) not found !" .format(device))
def main():
disk_info = get_disk_info('sda1')
print(disk_info)
print("磁盘写次数:{0}".format(disk_info.write_count))
print("磁盘写字节数:{0}".format(long(disk_info.write_sections) * 512))
print("磁盘写延时:{0}".format(disk_info.time_spent_write))
if __name__ == '__main__':
main()
输出:
8 1 sda1 772 182 25142 231 14 4 48 21 0 203 252
8 1 sda1 772 182 25142 231 14 4 48 21 0 203 252
8 1 sda1 772 182 25142 231 14 4 48 21 0 203 252
Disk(major_number='8', minor_number='1', device_name='sda1', read_count='772', red_merged_count='182', red_sections='25142', time_spent_reading='231', write_count='14', write_merged_count='4', write_sections='48', time_spent_write='21', io_requests='0', time_spent_doing_io='203', weighted_time_spent_doing_io='252')
磁盘写次数:14
磁盘写字节数:24576
磁盘写延时:21
# -*- coding: UTF-8 -*-
from __future__ import print_function
from collections import namedtuple
Disk = namedtuple('Disk', 'major_number minor_number device_name'
' read_count red_merged_count red_sections'
' time_spent_reading write_count write_merged_count'
' write_sections time_spent_write io_requests'
' time_spent_doing_io weighted_time_spent_doing_io')
def get_disk_info(device):
"""
从/proc/diskstats中读取磁盘IO信息
$ cat /proc/diskstats
"""
with open("/proc/diskstats") as f:
for line in f:
if line.split()[2] == device:
print (*(line.split()))
print (line)
print (' '.join(line.split()))
return Disk(*(line.split()))
raise RuntimeError("device ({0}) not found !" .format(device))
def main():
disk_info = get_disk_info('sda1')
print(disk_info)
print("磁盘写次数:{0}".format(disk_info.write_count))
print("磁盘写字节数:{0}".format(long(disk_info.write_sections) * 512))
print("磁盘写延时:{0}".format(disk_info.time_spent_write))
if __name__ == '__main__':
main()
输出:
8 1 sda1 772 182 25142 231 14 4 48 21 0 203 252
8 1 sda1 772 182 25142 231 14 4 48 21 0 203 252
8 1 sda1 772 182 25142 231 14 4 48 21 0 203 252
Disk(major_number='8', minor_number='1', device_name='sda1', read_count='772', red_merged_count='182', red_sections='25142', time_spent_reading='231', write_count='14', write_merged_count='4', write_sections='48', time_spent_write='21', io_requests='0', time_spent_doing_io='203', weighted_time_spent_doing_io='252')
磁盘写次数:14
磁盘写字节数:24576
磁盘写延时:21