linux 磁盘 IO监控

2019-04-17 16:33:07 浏览数 (1)

示例:点击 -> 性能监控

主机IO监控:

固定时间段监控/proc/vmstat 以下字段 和前一个时间段相减,再除以时间间隔 得到io。

代码语言:javascript复制
[root@wangzi teg]# cat /proc/vmstat|grep pgpg
pgpgin 2094314778
pgpgout 265255600

进程级IO监控通过:

固定时间段监控/proc/$pid/io 以下字段 和前一个时间段相减,再除以时间间隔 得到io。

代码语言:javascript复制
[root@wangzi teg]# cat /proc/18826/io|grep -E '^read*|^write*'
read_bytes: 618811392
write_bytes: 102821888

python代码:

代码语言:javascript复制
#!/usr/bin/env python
# coding=utf-8
# author: brownwang
# mail: 277215243@qq.com
# datetime:2019/3/31 1:03 PM
# web: https://www.bthlt.com


def write_disk_io():
    disk_in=0
    disk_out=0
    with open('/proc/vmstat', 'r') as file:
        for line in file.readlines():
            if 'pgpgin' in line:
                disk_in=int(line.split()[1])/1024
            if 'pgpgout' in line:
                disk_out=int(line.split()[1])/1024
    select_sql="""select `in`,`out` from monitor_disk_io order by id desc limit 1"""
    result=cursorQuery(select_sql,[])
    add_disk_in=disk_in-int(result[0][0])
    add_disk_out=disk_out-int(result[0][1])
    insert_sql="""insert into `monitor_disk_io` (`in`,`out`,`add_in`,`add_out`,`flow_time`) values ({0},{1},{2},{3},'{4}')""".format(disk_in,disk_out,add_disk_in,add_disk_out,now_zero)
    cursorUpdate(insert_sql,[])

0 人点赞