Python os.statvfs() Examples

The following are 30 code examples of os.statvfs(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module os , or try the search function .
Example #1
Source File: rpiMonitor.py    From BerePi with BSD 2-Clause "Simplified" License 7 votes vote down vote up
def disk_usage(path, diskUsage='all'):
    st = os.statvfs(path)
    free = st.f_bavail * st.f_frsize
    total = st.f_blocks * st.f_frsize
    used = (st.f_blocks - st.f_bfree) * st.f_frsize

    if diskUsage == 'free':
        diskUsage = free
    elif diskUsage == 'total':
        diskUsage = total
    elif diskUsage == 'used':
        diskUsage = used
    elif diskUsage == 'all':
        diskUsage = free, total, used
    else:
        diskUsage = None

    return diskUsage

## funtion get_hostname : return raspberry pi hostname info 
Example #2
Source File: statsd-agent.py    From dino with Apache License 2.0 7 votes vote down vote up
def disk():
    c = statsd.StatsClient(STATSD_HOST, 8125, prefix=PREFIX + 'system.disk')
    while True:
        for path, label in PATHS:
            disk_usage = psutil.disk_usage(path)

            st = os.statvfs(path)
            total_inode = st.f_files
            free_inode = st.f_ffree
            inode_percentage = int(100*(float(total_inode - free_inode) / total_inode))

            c.gauge('%s.inodes.percent' % label, inode_percentage)
            c.gauge('%s.total' % label, disk_usage.total)
            c.gauge('%s.used' % label, disk_usage.used)
            c.gauge('%s.free' % label, disk_usage.free)
            c.gauge('%s.percent' % label, disk_usage.percent)
        time.sleep(GRANULARITY) 
Example #3
Source File: _psposix.py    From NoobSec-Toolkit with GNU General Public License v2.0 6 votes vote down vote up
def disk_usage(path):
    """Return disk usage associated with path."""
    try:
        st = os.statvfs(path)
    except UnicodeEncodeError:
        if not PY3 and isinstance(path, unicode):
            # this is a bug with os.statvfs() and unicode on
            # Python 2, see:
            # - https://github.com/giampaolo/psutil/issues/416
            # - http://bugs.python.org/issue18695
            try:
                path = path.encode(sys.getfilesystemencoding())
            except UnicodeEncodeError:
                pass
            st = os.statvfs(path)
        else:
            raise
    free = (st.f_bavail * st.f_frsize)
    total = (st.f_blocks * st.f_frsize)
    used = (st.f_blocks - st.f_bfree) * st.f_frsize
    percent = usage_percent(used, total, _round=1)
    # NB: the percentage is -5% than what shown by df due to
    # reserved blocks that we are currently not considering:
    # http://goo.gl/sWGbH
    return sdiskusage(total, used, free, percent) 
Example #4
Source File: inner.py    From atomic-reactor with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def _update(data):
        try:
            st = os.statvfs("/")
        except Exception as e:
            return e  # just for tests; we don't really need return value

        mb = 1000 ** 2  # sadly storage is generally expressed in decimal units
        new_data = dict(
            mb_free=st.f_bfree * st.f_frsize // mb,
            mb_total=st.f_blocks * st.f_frsize // mb,
            mb_used=(st.f_blocks - st.f_bfree) * st.f_frsize // mb,
            inodes_free=st.f_ffree,
            inodes_total=st.f_files,
            inodes_used=st.f_files - st.f_ffree,
        )
        for key in ["mb_total", "mb_used", "inodes_total", "inodes_used"]:
            data[key] = max(new_data[key], data.get(key, 0))
        for key in ["mb_free", "inodes_free"]:
            data[key] = min(new_data[key], data.get(key, float("inf")))

        return new_data 
Example #5
Source File: BaseApp.py    From p2ptv-pi with MIT License 6 votes vote down vote up
def get_disk_info(self, path):
        try:
            folder = os.path.dirname(path)
            if sys.platform == 'win32':
                free_bytes, total_bytes, _ = win32file.GetDiskFreeSpaceEx(folder)
                used_bytes = total_bytes - free_bytes
            else:
                st = os.statvfs(folder)
                free_bytes = st.f_bavail * st.f_frsize
                total_bytes = st.f_blocks * st.f_frsize
                used_bytes = (st.f_blocks - st.f_bfree) * st.f_frsize
            return (total_bytes, free_bytes, used_bytes)
        except:
            if DEBUG:
                log('baseapp::get_disk_info: cannot get disk info: path', path)
            return (None, None, None) 
Example #6
Source File: linux_fs.py    From igcollect with MIT License 6 votes vote down vote up
def main():
    args = parse_args()
    mountpoints = []
    with open('/proc/mounts', 'r') as fp:
        for line in fp:
            a, mountpoint, fstype, a = line.split(' ', 3)
            if fstype in ['ext2', 'ext3', 'ext4', 'xfs', 'zfs']:
                mountpoints.append(mountpoint)

    template = args.prefix + '.{}.{} {} ' + str(int(time()))
    for mp in mountpoints:
        stat = os.statvfs(mp)
        used = stat.f_frsize * stat.f_blocks - stat.f_bfree * stat.f_bsize
        size = stat.f_frsize * stat.f_blocks

        if mp == '/':
            mp = 'rootfs'
        mp = mp.replace('/', '_').lstrip('_')

        print(template.format(mp, 'used', used))
        print(template.format(mp, 'size', size)) 
Example #7
Source File: pickletester.py    From oss-ftp with MIT License 6 votes vote down vote up
def test_structseq(self):
        import time
        import os

        t = time.localtime()
        for proto in protocols:
            s = self.dumps(t, proto)
            u = self.loads(s)
            self.assertEqual(t, u)
            if hasattr(os, "stat"):
                t = os.stat(os.curdir)
                s = self.dumps(t, proto)
                u = self.loads(s)
                self.assertEqual(t, u)
            if hasattr(os, "statvfs"):
                t = os.statvfs(os.curdir)
                s = self.dumps(t, proto)
                u = self.loads(s)
                self.assertEqual(t, u)

    # Tests for protocol 2 
Example #8
Source File: pickletester.py    From Fluid-Designer with GNU General Public License v3.0 6 votes vote down vote up
def test_structseq(self):
        import time
        import os

        t = time.localtime()
        for proto in protocols:
            s = self.dumps(t, proto)
            u = self.loads(s)
            self.assert_is_copy(t, u)
            if hasattr(os, "stat"):
                t = os.stat(os.curdir)
                s = self.dumps(t, proto)
                u = self.loads(s)
                self.assert_is_copy(t, u)
            if hasattr(os, "statvfs"):
                t = os.statvfs(os.curdir)
                s = self.dumps(t, proto)
                u = self.loads(s)
                self.assert_is_copy(t, u) 
Example #9
Source File: pickletester.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def test_structseq(self):
        import time
        import os

        t = time.localtime()
        for proto in protocols:
            s = self.dumps(t, proto)
            u = self.loads(s)
            self.assertEqual(t, u)
            if hasattr(os, "stat"):
                t = os.stat(os.curdir)
                s = self.dumps(t, proto)
                u = self.loads(s)
                self.assertEqual(t, u)
            if hasattr(os, "statvfs"):
                t = os.statvfs(os.curdir)
                s = self.dumps(t, proto)
                u = self.loads(s)
                self.assertEqual(t, u)

    # Tests for protocol 2 
Example #10
Source File: reach_tools.py    From rtkbase with GNU Affero General Public License v3.0 6 votes vote down vote up
def getFreeSpace(logs_path):
    space = os.statvfs(os.path.expanduser("~"))
    free = space.f_bavail * space.f_frsize / 1024000
    total = space.f_blocks * space.f_frsize / 1024000

    used_by_logs = getLogsSize(logs_path)
    total_for_logs = free + used_by_logs
    percentage = (float(used_by_logs)/float(total_for_logs)) * 100
    total_for_logs_gb = float(total_for_logs) / 1024.0

    result = {
        "used": "{0:.0f}".format(used_by_logs),
        "total": "{0:.1f}".format(total_for_logs_gb),
        "percentage": "{0:.0f}".format(percentage)
    }

    print("Returning sizes!")
    print(result)

    return result 
Example #11
Source File: pickletester.py    From BinderFilter with MIT License 6 votes vote down vote up
def test_structseq(self):
        import time
        import os

        t = time.localtime()
        for proto in protocols:
            s = self.dumps(t, proto)
            u = self.loads(s)
            self.assertEqual(t, u)
            if hasattr(os, "stat"):
                t = os.stat(os.curdir)
                s = self.dumps(t, proto)
                u = self.loads(s)
                self.assertEqual(t, u)
            if hasattr(os, "statvfs"):
                t = os.statvfs(os.curdir)
                s = self.dumps(t, proto)
                u = self.loads(s)
                self.assertEqual(t, u)

    # Tests for protocol 2 
Example #12
Source File: _psposix.py    From NoobSec-Toolkit with GNU General Public License v2.0 6 votes vote down vote up
def disk_usage(path):
    """Return disk usage associated with path."""
    try:
        st = os.statvfs(path)
    except UnicodeEncodeError:
        if not PY3 and isinstance(path, unicode):
            # this is a bug with os.statvfs() and unicode on
            # Python 2, see:
            # - https://github.com/giampaolo/psutil/issues/416
            # - http://bugs.python.org/issue18695
            try:
                path = path.encode(sys.getfilesystemencoding())
            except UnicodeEncodeError:
                pass
            st = os.statvfs(path)
        else:
            raise
    free = (st.f_bavail * st.f_frsize)
    total = (st.f_blocks * st.f_frsize)
    used = (st.f_blocks - st.f_bfree) * st.f_frsize
    percent = usage_percent(used, total, _round=1)
    # NB: the percentage is -5% than what shown by df due to
    # reserved blocks that we are currently not considering:
    # http://goo.gl/sWGbH
    return sdiskusage(total, used, free, percent) 
Example #13
Source File: _psposix.py    From NoobSec-Toolkit with GNU General Public License v2.0 6 votes vote down vote up
def disk_usage(path):
    """Return disk usage associated with path."""
    try:
        st = os.statvfs(path)
    except UnicodeEncodeError:
        if not PY3 and isinstance(path, unicode):
            # this is a bug with os.statvfs() and unicode on
            # Python 2, see:
            # - https://github.com/giampaolo/psutil/issues/416
            # - http://bugs.python.org/issue18695
            try:
                path = path.encode(sys.getfilesystemencoding())
            except UnicodeEncodeError:
                pass
            st = os.statvfs(path)
        else:
            raise
    free = (st.f_bavail * st.f_frsize)
    total = (st.f_blocks * st.f_frsize)
    used = (st.f_blocks - st.f_bfree) * st.f_frsize
    percent = usage_percent(used, total, _round=1)
    # NB: the percentage is -5% than what shown by df due to
    # reserved blocks that we are currently not considering:
    # http://goo.gl/sWGbH
    return sdiskusage(total, used, free, percent) 
Example #14
Source File: _psposix.py    From NoobSec-Toolkit with GNU General Public License v2.0 6 votes vote down vote up
def disk_usage(path):
    """Return disk usage associated with path."""
    try:
        st = os.statvfs(path)
    except UnicodeEncodeError:
        if not PY3 and isinstance(path, unicode):
            # this is a bug with os.statvfs() and unicode on
            # Python 2, see:
            # - https://github.com/giampaolo/psutil/issues/416
            # - http://bugs.python.org/issue18695
            try:
                path = path.encode(sys.getfilesystemencoding())
            except UnicodeEncodeError:
                pass
            st = os.statvfs(path)
        else:
            raise
    free = (st.f_bavail * st.f_frsize)
    total = (st.f_blocks * st.f_frsize)
    used = (st.f_blocks - st.f_bfree) * st.f_frsize
    percent = usage_percent(used, total, _round=1)
    # NB: the percentage is -5% than what shown by df due to
    # reserved blocks that we are currently not considering:
    # http://goo.gl/sWGbH
    return sdiskusage(total, used, free, percent) 
Example #15
Source File: metrics.py    From lainonlife with MIT License 6 votes vote down vote up
def disk_metrics():
    """Get the disk usage, in bytes."""

    def add_usage(ms, dus, dname):
        try:
            for i, val in enumerate(dus):
                if val.decode("utf-8") == dname:
                    ms[dname] = int(dus[i - 1])
        except Exception:
            pass

    # Overall disk usage
    statinfo = os.statvfs("/")
    metrics = {"used": statinfo.f_frsize * (statinfo.f_blocks - statinfo.f_bfree)}

    # Per-directory disk usage
    dirs = ["/home", "/nix", "/srv", "/tmp", "/var"]
    argv = ["du", "-s", "-b"]
    argv.extend(dirs)  # why doesn't python have an expression variant of this!?
    dus = subprocess.check_output(argv).split()
    for dname in dirs:
        add_usage(metrics, dus, dname)

    return metrics 
Example #16
Source File: system.py    From iopipe-python with Apache License 2.0 6 votes vote down vote up
def read_disk():
    """
    Returns disk usage for /tmp

    :returns: Disk usage (total, used, percentage used)
    :rtype dict
    """
    s = os.statvfs("/tmp")
    return {
        # This should report as 500MB, if not may need to be hardcoded
        # https://aws.amazon.com/lambda/faqs/
        "totalMiB": (s.f_blocks * s.f_bsize) / MB_FACTOR,
        "usedMiB": ((s.f_blocks - s.f_bfree) * s.f_frsize) / MB_FACTOR,
        "usedPercentage": round(
            (((s.f_blocks - s.f_bfree) * s.f_frsize) / (s.f_blocks * s.f_bsize)) * 100,
            2,
        ),
    } 
Example #17
Source File: fsutil.py    From pykit with MIT License 6 votes vote down vote up
def get_path_usage(path):

    space_st = os.statvfs(path)

    # f_bavail: without blocks reserved for super users
    # f_bfree:  with    blocks reserved for super users
    avail = space_st.f_frsize * space_st.f_bavail

    capa = space_st.f_frsize * space_st.f_blocks
    used = capa - avail

    return {
        'total': capa,
        'used': used,
        'available': avail,
        'percent': float(used) / capa,
    } 
Example #18
Source File: passthrough.py    From python-fuse-sample with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def statfs(self, path):
        full_path = self._full_path(path)
        stv = os.statvfs(full_path)
        return dict((key, getattr(stv, key)) for key in ('f_bavail', 'f_bfree',
            'f_blocks', 'f_bsize', 'f_favail', 'f_ffree', 'f_files', 'f_flag',
            'f_frsize', 'f_namemax')) 
Example #19
Source File: visual_cortex.py    From rpi_ai with MIT License 5 votes vote down vote up
def getFreeSpace():
	st = os.statvfs(".")
	du = st.f_bavail * st.f_frsize
	return du 
Example #20
Source File: heap.py    From Imogen with MIT License 5 votes vote down vote up
def _choose_dir(self, size):
            # Choose a non-storage backed directory if possible,
            # to improve performance
            for d in self._dir_candidates:
                st = os.statvfs(d)
                if st.f_bavail * st.f_frsize >= size:  # enough free space?
                    return d
            return util.get_temp_dir() 
Example #21
Source File: file.py    From MCVirt with GNU General Public License v2.0 5 votes vote down vote up
def get_free_space(self):
        """Return the free space in megabytes."""
        # Obtain statvfs object
        statvfs = os.statvfs(self.get_location())

        # Calculate free space in bytes by multiplying number
        # of available blocks (free for non-superuser) and the
        # block size
        free_space_b = statvfs.f_bavail * statvfs.f_frsize

        return free_space_b 
Example #22
Source File: shutil.py    From scylla with Apache License 2.0 5 votes vote down vote up
def disk_usage(path):
        """Return disk usage statistics about the given path.

        Returned value is a named tuple with attributes 'total', 'used' and
        'free', which are the amount of total, used and free space, in bytes.
        """
        st = os.statvfs(path)
        free = st.f_bavail * st.f_frsize
        total = st.f_blocks * st.f_frsize
        used = (st.f_blocks - st.f_bfree) * st.f_frsize
        return _ntuple_diskusage(total, used, free) 
Example #23
Source File: helpers.py    From see with Apache License 2.0 5 votes vote down vote up
def verify_checksum(path, checksum):
    hash_md5 = hashlib.md5()
    block_size = os.statvfs(path).f_bsize
    with open(path, 'rb') as f:
        while True:
            chunk = f.read(block_size)
            if not chunk:
                break
            hash_md5.update(chunk)
    return hash_md5.hexdigest() == checksum 
Example #24
Source File: shutil.py    From GraphicDesignPatternByPython with MIT License 5 votes vote down vote up
def disk_usage(path):
        """Return disk usage statistics about the given path.

        Returned value is a named tuple with attributes 'total', 'used' and
        'free', which are the amount of total, used and free space, in bytes.
        """
        st = os.statvfs(path)
        free = st.f_bavail * st.f_frsize
        total = st.f_blocks * st.f_frsize
        used = (st.f_blocks - st.f_bfree) * st.f_frsize
        return _ntuple_diskusage(total, used, free) 
Example #25
Source File: data.py    From r_e_c_u_r with GNU General Public License v3.0 5 votes vote down vote up
def _get_mb_free_diskspace(path):
        st = os.statvfs(path)
        return st.f_bavail * st.f_frsize / 1024 / 1024 
Example #26
Source File: WindowsServer.py    From pycopia with Apache License 2.0 5 votes vote down vote up
def statvfs(self, path):
        path = cygwin2nt(path)
        return os.statvfs(path)

    # fd ops ruturn the file descript as handle (of course) 
Example #27
Source File: PosixServer.py    From pycopia with Apache License 2.0 5 votes vote down vote up
def statvfs(self, path):
        return os.statvfs(path)

    # fd ops ruturn the file descript as handle (of course) 
Example #28
Source File: test_fsutil.py    From pykit with MIT License 5 votes vote down vote up
def test_get_path_usage(self):

        path = '/'
        rst = fsutil.get_path_usage(path)

        # check against os.statvfs.
        st = os.statvfs(path)

        dd(humannum.humannum(rst))

        # space is changing..

        self.assertAlmostEqual(st.f_frsize * st.f_bavail, rst['available'], delta=4 * 1024**2)
        self.assertAlmostEqual(st.f_frsize * st.f_blocks, rst['total'], delta=4 * 1024**2)
        self.assertAlmostEqual(st.f_frsize * (st.f_blocks - st.f_bavail), rst['used'], delta=4 * 1024**2)
        self.assertAlmostEqual((st.f_blocks - st.f_bavail) * 100 / st.f_blocks,
                               int(rst['percent'] * 100), delta=4 * 1024**2)

        if st.f_bfree > st.f_bavail:
            self.assertLess(rst['available'], st.f_frsize * st.f_bfree)
        else:
            dd('st.f_bfree == st.f_bavail')

        # check against df

        rc, out, err = proc.shell_script('df -m / | tail -n1')
        # Filesystem 1M-blocks   Used Available Capacity  iused    ifree %iused  Mounted on
        # /dev/disk1    475828 328021    147556    69% 84037441 37774557   69%   /
        dd('space of "/" from df')
        total_mb = out.strip().split()[1]

        # "123.8M", keep int only
        rst_total_mb = humannum.humannum(rst['total'], unit=humannum.M)
        rst_total_mb = rst_total_mb.split('.')[0].split('M')[0]

        dd('total MB from df:', total_mb, 'result total MB:', rst_total_mb)

        self.assertAlmostEqual(int(total_mb), int(rst_total_mb), delta=4) 
Example #29
Source File: fsutil.py    From pykit with MIT License 5 votes vote down vote up
def get_path_inode_usage(path):

    inode_st = os.statvfs(path)

    available = inode_st.f_favail
    total = inode_st.f_files
    used = total - available

    return {
        'total': total,
        'used': used,
        'available': available,
        'percent': float(used) / total,
    } 
Example #30
Source File: storage_file.py    From qubes-core-admin with GNU Lesser General Public License v2.1 5 votes vote down vote up
def test_004_usage(self):
        pool = self.app.get_pool(self.POOL_NAME)
        with self.assertNotRaises(NotImplementedError):
            usage = pool.usage
        statvfs = os.statvfs(self.POOL_DIR)
        self.assertEqual(usage,
            statvfs.f_frsize * (statvfs.f_blocks - statvfs.f_bfree))