device=’/dev/mapper/centos00-root’, mountpoint=’/’, fstype=’xfs’, opts=’rw,relatime,attr2,inode64,noquota’
device=’/dev/mapper/centos00-home’, mountpoint=’/home’, fstype=’xfs’, opts=’rw,relatime,attr2,inode64,noquota’
device=’/dev/sda2′, mountpoint=’/boot’, fstype=’xfs’, opts=’rw,relatime,attr2,inode64,noquota’

device=’/dev/mapper/data-data’, mountpoint=’/data’, fstype=’ext4′, opts=’rw,relatime,data=ordered’
device=’/dev/mapper/data-data’, mountpoint=’/data/docker/containers’, fstype=’ext4′, opts=’rw,relatime,data=ordered’
device=’/dev/mapper/data-data’, mountpoint=’/data/docker/overlay2′, fstype=’ext4′, opts=’rw,relatime,data=ordered’

思路

device重复的保留一个就可以了,这种都是docker之类的问题

#! /usr/bin/env python
from __future__ import division
try:
    import psutil

    disk={}
    for i in psutil.disk_partitions(all=False):
        if i[0] not in disk:
            disk[i[0]]=i[1]

    free=[]
    used=[]
    for key,value in disk.items():
        partDetail=psutil.disk_usage(value)
        #sdiskusage(total=42139451392, used=28426170368, free=11549126656, percent=71.1)
        free.append(partDetail.free)
        used.append(partDetail.used)

    pused=sum(used)/(sum(used)+sum(free))*100
    print(pused)
except Exception as e:
    pass

先过滤出设备挂载点去重,因为一个设备挂载在同一个点多次求和会有问题。
然后获得挂载点的值

使用率计算还有一个坑,就是used/total和他算出的不一样,实际应该是要用used/(used+free)

以前有问题的计算方式是


---用的下面这个--

#! /bin/bash
usage() {
total=$(df -m 2>> /dev/null | awk ‘NR>1 && /^\//{sum += $2};END {print sum}’)
avail=$(df -m 2>> /dev/null | awk ‘NR>1 && /^\//{sum += $4};END {print sum}’)
echo “$total $avail” | awk ‘{print 100-100*$2/$1}’
}

$1

———————————————————–

/bin/df -hT |awk '/over|sh/NR>1{print $1,"\t",$2,"\t",$3}

/bin/df -mT |awk '/^\//{print $1,"\t",$2,"\t",$3,"\t",$4}'

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注