Python psutil.disk_usage() Examples

The following are 30 code examples of psutil.disk_usage(). 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 psutil , or try the search function .
Example #1
Source File: system_status.py    From Paradrop with Apache License 2.0 12 votes vote down vote up
def getSystemInfo(cls):
        system = {
            'boot_time': psutil.boot_time(),
            'cpu_count': psutil.cpu_count(),
            'cpu_stats': psutil.cpu_stats().__dict__,
            'cpu_times': [k.__dict__ for k in psutil.cpu_times(percpu=True)],
            'disk_io_counters': psutil.disk_io_counters().__dict__,
            'disk_usage': [],
            'net_io_counters': psutil.net_io_counters().__dict__,
            'swap_memory': psutil.swap_memory().__dict__,
            'virtual_memory': psutil.virtual_memory().__dict__
        }

        partitions = psutil.disk_partitions()
        for p in partitions:
            if p.mountpoint in cls.INCLUDED_PARTITIONS:
                usage = psutil.disk_usage(p.mountpoint)
                system['disk_usage'].append({
                    'mountpoint': p.mountpoint,
                    'total': usage.total,
                    'used': usage.used
                })

        return system 
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: resource_info_alert.py    From ahenk with GNU Lesser General Public License v3.0 7 votes vote down vote up
def gather_resource_usage(self, task_id):
        # Memory usage
        memory_usage = psutil.virtual_memory()
        self.logger.debug("Memory usage: {0}".format(memory_usage))
        # Disk usage
        disk_usage = psutil.disk_usage('/')
        self.logger.debug("Disk usage: {0}".format(disk_usage))
        # CPU usage
        cpu_percentage = psutil.cpu_percent(interval=1)
        self.logger.debug("CPU percentage: {0}".format(cpu_percentage))

        data = {'memoryUsage': str(memory_usage), 'diskUsage': str(disk_usage), 'cpuPercentage': str(cpu_percentage)}
        command = 'python3 /opt/ahenk/ahenkd.py send -t {0} -m {1} -s'.format(task_id, json.dumps(str(data)))
        result_code, p_out, p_err = self.execute(command)
        if result_code != 0:
            self.logger.error("Error occurred while sending message: " + str(p_err)) 
Example #4
Source File: test_linux.py    From psutil with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_against_df(self):
        # test psutil.disk_usage() and psutil.disk_partitions()
        # against "df -a"
        def df(path):
            out = sh('df -P -B 1 "%s"' % path).strip()
            lines = out.split('\n')
            lines.pop(0)
            line = lines.pop(0)
            dev, total, used, free = line.split()[:4]
            if dev == 'none':
                dev = ''
            total, used, free = int(total), int(used), int(free)
            return dev, total, used, free

        for part in psutil.disk_partitions(all=False):
            usage = psutil.disk_usage(part.mountpoint)
            dev, total, used, free = df(part.mountpoint)
            self.assertEqual(usage.total, total)
            self.assertAlmostEqual(usage.free, free,
                                   delta=TOLERANCE_DISK_USAGE)
            self.assertAlmostEqual(usage.used, used,
                                   delta=TOLERANCE_DISK_USAGE) 
Example #5
Source File: launcher.py    From minemeld-core with Apache License 2.0 6 votes vote down vote up
def _check_disk_space(num_nodes):
    free_disk_per_node = int(os.environ.get(
        'MM_DISK_SPACE_PER_NODE',
        10*1024  # default: 10MB per node
    ))
    needed_disk = free_disk_per_node*num_nodes*1024
    free_disk = psutil.disk_usage('.').free

    LOG.debug('Disk space - needed: {} available: {}'.format(needed_disk, free_disk))

    if free_disk <= needed_disk:
        LOG.critical(
            ('Not enough space left on the device, available: {} needed: {}'
             ' - please delete traces, logs and old engine versions and restart').format(
             free_disk, needed_disk
            )
        )
        return None

    return free_disk 
Example #6
Source File: resource_info_alert.py    From ahenk with GNU Lesser General Public License v3.0 6 votes vote down vote up
def gather_resource_usage(self, task_id):
        # Memory usage
        memory_usage = psutil.virtual_memory()
        self.logger.debug("Memory usage: {0}".format(memory_usage))
        # Disk usage
        disk_usage = psutil.disk_usage('/')
        self.logger.debug("Disk usage: {0}".format(disk_usage))
        # CPU usage
        cpu_percentage = psutil.cpu_percent(interval=1)
        self.logger.debug("CPU percentage: {0}".format(cpu_percentage))

        data = {'memoryUsage': str(memory_usage), 'diskUsage': str(disk_usage), 'cpuPercentage': str(cpu_percentage)}
        command = 'python3 /opt/ahenk/ahenkd.py send -t {0} -m {1} -s'.format(task_id, json.dumps(str(data)))
        result_code, p_out, p_err = self.execute(command)
        if result_code != 0:
            self.logger.error("Error occurred while sending message: " + str(p_err)) 
Example #7
Source File: writer.py    From minemeld-core with Apache License 2.0 6 votes vote down vote up
def _run(self):
        while True:
            perc_used = psutil.disk_usage('.').percent

            if perc_used >= self._threshold:
                if not self._low_disk.is_set():
                    self._low_disk.set()
                    LOG.critical(
                        'Disk space used above threshold ({}%), writing disabled'.format(self._threshold)
                    )

            else:
                if self._low_disk.is_set():
                    self._low_disk.clear()
                    LOG.info('Disk space used below threshold, writing restored')

            gevent.sleep(60) 
Example #8
Source File: server.py    From MoePhoto with Apache License 2.0 6 votes vote down vote up
def getSystemInfo(info):
  import readgpu
  cuda, cudnn = readgpu.getCudaVersion()
  info.update({
    'cpu_count_phy': psutil.cpu_count(logical=False),
    'cpu_count_log': psutil.cpu_count(logical=True),
    'cpu_freq': psutil.cpu_freq().max,
    'disk_total': psutil.disk_usage(cwd).total // 2**20,
    'mem_total': psutil.virtual_memory().total // 2**20,
    'python': readgpu.getPythonVersion(),
    'torch': readgpu.getTorchVersion(),
    'cuda': cuda,
    'cudnn': cudnn,
    'gpus': readgpu.getGPUProperties()
  })
  readgpu.uninstall()
  del readgpu
  return info 
Example #9
Source File: statusapi.py    From minemeld-core with Apache License 2.0 6 votes vote down vote up
def get_system_status():
    data_path = config.get('MINEMELD_LOCAL_PATH', None)
    if data_path is None:
        jsonify(error={'message': 'MINEMELD_LOCAL_PATH not set'}), 500

    res = {}
    res['cpu'] = psutil.cpu_percent(interval=1, percpu=True)
    res['memory'] = psutil.virtual_memory().percent
    res['swap'] = psutil.swap_memory().percent
    res['disk'] = psutil.disk_usage(data_path).percent
    res['sns'] = SNS_AVAILABLE

    return jsonify(result=res, timestamp=int(time.time() * 1000)) 
Example #10
Source File: disk_utils.py    From agentless-system-crawler with Apache License 2.0 6 votes vote down vote up
def crawl_disk_partitions():
    partitions = []
    for partition in psutil.disk_partitions(all=True):
        try:
            pdiskusage = psutil.disk_usage(partition.mountpoint)
            partitions.append((partition.mountpoint, DiskFeature(
                partition.device,
                100.0 - pdiskusage.percent,
                partition.fstype,
                partition.mountpoint,
                partition.opts,
                pdiskusage.total,
            ), 'disk'))
        except OSError:
            continue
    return partitions 
Example #11
Source File: system_module.py    From stephanie-va with MIT License 6 votes vote down vote up
def tell_system_status(self):
        import psutil
        import platform
        import datetime

        os, name, version, _, _, _ = platform.uname()
        version = version.split('-')[0]
        cores = psutil.cpu_count()
        cpu_percent = psutil.cpu_percent()
        memory_percent = psutil.virtual_memory()[2]
        disk_percent = psutil.disk_usage('/')[3]
        boot_time = datetime.datetime.fromtimestamp(psutil.boot_time())
        running_since = boot_time.strftime("%A %d. %B %Y")
        response = "I am currently running on %s version %s.  " % (os, version)
        response += "This system is named %s and has %s CPU cores.  " % (name, cores)
        response += "Current disk_percent is %s percent.  " % disk_percent
        response += "Current CPU utilization is %s percent.  " % cpu_percent
        response += "Current memory utilization is %s percent. " % memory_percent
        response += "it's running since %s." % running_since
        return response 
Example #12
Source File: _resource_monitor.py    From colabtools with Apache License 2.0 6 votes vote down vote up
def get_disk_usage(path=None):
  """Reports total disk usage.

  Args:
    path: path at which disk to be measured is mounted

  Returns:
    A dict of the form {
      usage: int,
      limit: int,
    }
  """
  if not path:
    path = '/'
  usage = 0
  limit = 0
  if psutil is not None:
    disk_usage = psutil.disk_usage(path)
    usage = disk_usage.used
    limit = disk_usage.total
  return {'usage': usage, 'limit': limit} 
Example #13
Source File: simple_monitor.py    From You-are-Pythonista with GNU General Public License v3.0 6 votes vote down vote up
def log_generate():
    '''
    生成日志格式
    '''
    # 获取cpu百分比
    cpu_percent = psutil.cpu_percent()
    # 查看系统内存
    mem = psutil.virtual_memory()
    # 计算内存占用百分比
    mem_percent = int(mem.used / mem.total * 100)
    # 获取系统分区信息
    disk_partition = psutil.disk_partitions()
    # 计算系统磁盘占用百分比
    disk_percent = {mount_point : '{} %'.format(psutil.disk_usage('{}'.format(mount_point))[3]) \
                    for mount_point in ( dp.mountpoint for dp in disk_partition )}

    return {get_ip_address():{'cpu': '{} %'.format(cpu_percent), \
                              'mem': '{} %'.format(mem_percent), \
                              'disk':disk_percent, \
                              'time':int(time.time())}} 
Example #14
Source File: useful_tools.py    From persepolis with GNU General Public License v3.0 6 votes vote down vote up
def freeSpace(dir):
    try:
        import psutil
    except:
        if logger_availability:
            logger.sendToLog("psutil in not installed!", "ERROR")

        return None

    try:
        dir_space = psutil.disk_usage(dir)
        free_space = dir_space.free
        return int(free_space)

    except Exception as e:
        # log in to the log file
        if logger_availability:
            logger.sendToLog("persepolis couldn't find free space value:\n" + str(e), "ERROR")

        return None 
Example #15
Source File: test_misc.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_serialization(self):
        def check(ret):
            if json is not None:
                json.loads(json.dumps(ret))
            a = pickle.dumps(ret)
            b = pickle.loads(a)
            self.assertEqual(ret, b)

        check(psutil.Process().as_dict())
        check(psutil.virtual_memory())
        check(psutil.swap_memory())
        check(psutil.cpu_times())
        check(psutil.cpu_times_percent(interval=0))
        check(psutil.net_io_counters())
        if LINUX and not os.path.exists('/proc/diskstats'):
            pass
        else:
            if not APPVEYOR:
                check(psutil.disk_io_counters())
        check(psutil.disk_partitions())
        check(psutil.disk_usage(os.getcwd()))
        check(psutil.users()) 
Example #16
Source File: windows_system_metrics.py    From scalyr-agent-2 with Apache License 2.0 6 votes vote down vote up
def partion_disk_usage(sub_metric):
    mountpoints_initialized = [0]
    mountpoints = []

    def gather_metric():
        if mountpoints_initialized[0] == 0:
            for p in psutil.disk_partitions():
                # Only add to list of mountpoints if fstype is
                # specified.  This prevents reading from empty drives
                # such as cd-roms, card readers etc.
                if p.fstype:
                    mountpoints.append(p.mountpoint)
            mountpoints_initialized[0] = 1

        for mountpoint in mountpoints:
            try:
                diskusage = psutil.disk_usage(mountpoint)
                yield getattr(diskusage, sub_metric), {"partition": mountpoint}
            except OSError:
                # Certain partitions, like a CD/DVD drive, are expected to fail
                pass

    gather_metric.__doc__ = "TODO"
    return gather_metric 
Example #17
Source File: hard_drives.py    From node-launcher with MIT License 6 votes vote down vote up
def should_prune(input_directory: str) -> bool:
        directory = os.path.realpath(input_directory)
        try:
            total, used, free, percent = psutil.disk_usage(directory)
        except:
            log.warning(
                'should_prune_disk_usage',
                input_directory=input_directory,
                directory=directory,
                exc_info=True
            )
            return False
        free_gb = math.floor(free / GIGABYTE)
        should_prune = free_gb < AUTOPRUNE_GB
        log.info(
            'should_prune',
            input_directory=input_directory,
            total=total,
            used=used,
            free=free,
            percent=percent,
            free_gb=free_gb,
            should_prune=should_prune
        )
        return should_prune 
Example #18
Source File: hard_drives.py    From node-launcher with MIT License 6 votes vote down vote up
def get_gb(path: str) -> int:
        try:
            capacity, used, free, percent = psutil.disk_usage(path)
        except:
            log.warning(
                'get_gb',
                path=path,
                exc_info=True
            )
            return 0

        free_gb = math.floor(free / GIGABYTE)
        log.info(
            'get_gb',
            path=path,
            capacity=capacity,
            used=used,
            free=free,
            percent=percent,
            free_gb=free_gb
        )
        return free_gb 
Example #19
Source File: test_linux.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_disk_partitions_and_usage(self):
        # test psutil.disk_usage() and psutil.disk_partitions()
        # against "df -a"
        def df(path):
            out = sh('df -P -B 1 "%s"' % path).strip()
            lines = out.split('\n')
            lines.pop(0)
            line = lines.pop(0)
            dev, total, used, free = line.split()[:4]
            if dev == 'none':
                dev = ''
            total, used, free = int(total), int(used), int(free)
            return dev, total, used, free

        for part in psutil.disk_partitions(all=False):
            usage = psutil.disk_usage(part.mountpoint)
            dev, total, used, free = df(part.mountpoint)
            self.assertEqual(usage.total, total)
            # 10 MB tollerance
            if abs(usage.free - free) > 10 * 1024 * 1024:
                self.fail("psutil=%s, df=%s" % (usage.free, free))
            if abs(usage.used - used) > 10 * 1024 * 1024:
                self.fail("psutil=%s, df=%s" % (usage.used, used)) 
Example #20
Source File: disk_status.py    From marsnake with GNU General Public License v3.0 6 votes vote down vote up
def disk_status(response):
    #name    STATS   USED    MOUNT   FILESYSTEM
    for item in psutil.disk_partitions():
        device, mountpoint, fstype, opts = item
        
        try:
            total, used, free, percent = psutil.disk_usage(mountpoint)
            
            response["disk"][device] = {
                "fstype" : fstype,
                "total" : common.size_human_readable(total),
                "percent" : percent,
                "mountpoint" : mountpoint,
                "opts" : opts
            }
        except Exception:
            pass 
Example #21
Source File: test_misc.py    From psutil with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_serialization(self):
        def check(ret):
            if json is not None:
                json.loads(json.dumps(ret))
            a = pickle.dumps(ret)
            b = pickle.loads(a)
            self.assertEqual(ret, b)

        check(psutil.Process().as_dict())
        check(psutil.virtual_memory())
        check(psutil.swap_memory())
        check(psutil.cpu_times())
        check(psutil.cpu_times_percent(interval=0))
        check(psutil.net_io_counters())
        if LINUX and not os.path.exists('/proc/diskstats'):
            pass
        else:
            if not APPVEYOR:
                check(psutil.disk_io_counters())
        check(psutil.disk_partitions())
        check(psutil.disk_usage(os.getcwd()))
        check(psutil.users()) 
Example #22
Source File: utilities.py    From cot with MIT License 6 votes vote down vote up
def available_bytes_at_path(path):
    """Get the available disk space in a given directory.

    Args:
      path (str): Directory path to check.

    Returns:
      int: Available space, in bytes

    Raises:
      OSError: if the specified path does not exist or is not readable.
    """
    if not os.path.exists(path):
        raise OSError(errno.ENOENT, os.strerror(errno.ENOENT), path)
    if not os.path.isdir(path):
        raise OSError(errno.ENOTDIR, os.strerror(errno.ENOTDIR), path)
    available = disk_usage(path).free
    logger.debug("There appears to be %s available at %s",
                 pretty_bytes(available), path)
    return available 
Example #23
Source File: test_system.py    From psutil with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_disk_usage(self):
        usage = psutil.disk_usage(os.getcwd())
        self.assertEqual(usage._fields, ('total', 'used', 'free', 'percent'))

        assert usage.total > 0, usage
        assert usage.used > 0, usage
        assert usage.free > 0, usage
        assert usage.total > usage.used, usage
        assert usage.total > usage.free, usage
        assert 0 <= usage.percent <= 100, usage.percent
        if hasattr(shutil, 'disk_usage'):
            # py >= 3.3, see: http://bugs.python.org/issue12442
            shutil_usage = shutil.disk_usage(os.getcwd())
            tolerance = 5 * 1024 * 1024  # 5MB
            self.assertEqual(usage.total, shutil_usage.total)
            self.assertAlmostEqual(usage.free, shutil_usage.free,
                                   delta=tolerance)
            self.assertAlmostEqual(usage.used, shutil_usage.used,
                                   delta=tolerance)

        # if path does not exist OSError ENOENT is expected across
        # all platforms
        fname = self.get_testfn()
        with self.assertRaises(FileNotFoundError):
            psutil.disk_usage(fname) 
Example #24
Source File: script_ex.py    From heartbeats with MIT License 6 votes vote down vote up
def get_disk_info():
    disk_info = []
    for part in psutil.disk_partitions(all=False):
        if os.name == 'nt':
            if 'cdrom' in part.opts or part.fstype == '':
                # skip cd-rom drives with no disk in it; they may raise
                # ENOENT, pop-up a Windows GUI error for a non-ready
                # partition or just hang.
                continue
        usage = psutil.disk_usage(part.mountpoint)
        disk_info.append({
            'device':  part.device,
            'total':  usage.total,
            'used': usage.used,
            'free': usage.free,
            'percent': usage.percent,
            'fstype': part.fstype,
            'mountpoint': part.mountpoint
        })
    return disk_info 
Example #25
Source File: script.py    From heartbeats with MIT License 6 votes vote down vote up
def get_disk_info():
    disk_info = []
    for part in psutil.disk_partitions(all=False):
        if os.name == 'nt':
            if 'cdrom' in part.opts or part.fstype == '':
                # skip cd-rom drives with no disk in it; they may raise
                # ENOENT, pop-up a Windows GUI error for a non-ready
                # partition or just hang.
                continue
        usage = psutil.disk_usage(part.mountpoint)
        disk_info.append({
            'device':  part.device,
            'total':  usage.total,
            'used': usage.used,
            'free': usage.free,
            'percent': usage.percent,
            'fstype': part.fstype,
            'mountpoint': part.mountpoint
        })
    return disk_info 
Example #26
Source File: disk_usage.py    From psutil with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def main():
    templ = "%-17s %8s %8s %8s %5s%% %9s  %s"
    print(templ % ("Device", "Total", "Used", "Free", "Use ", "Type",
                   "Mount"))
    for part in psutil.disk_partitions(all=False):
        if os.name == 'nt':
            if 'cdrom' in part.opts or part.fstype == '':
                # skip cd-rom drives with no disk in it; they may raise
                # ENOENT, pop-up a Windows GUI error for a non-ready
                # partition or just hang.
                continue
        usage = psutil.disk_usage(part.mountpoint)
        print(templ % (
            part.device,
            bytes2human(usage.total),
            bytes2human(usage.used),
            bytes2human(usage.free),
            int(usage.percent),
            part.fstype,
            part.mountpoint)) 
Example #27
Source File: system_status.py    From Paradrop with Apache License 2.0 6 votes vote down vote up
def __init__(self):
        self.timestamp = time.time()
        self.cpu_load = []
        self.mem = dict(total = 0,
                        available = 0,
                        free = 0,
                        cached = 0,
                        buffers = 0)
        self.disk_partitions = {}
        self.network = {}

        partitions = psutil.disk_partitions()
        for p in partitions:
            if p.mountpoint in self.INCLUDED_PARTITIONS:
                usage = psutil.disk_usage(p.mountpoint)
                self.disk_partitions[p.mountpoint] = {
                    'total': usage.total,
                    'used': usage.used
                } 
Example #28
Source File: system.py    From ahenk with GNU Lesser General Public License v3.0 5 votes vote down vote up
def used():
                return int(int(psutil.disk_usage('/')[1]) / (1024 * 1024)) 
Example #29
Source File: baseDriver.py    From BioQueue with Apache License 2.0 5 votes vote down vote up
def get_init_resource():
    cpu = cpu_count() * 100
    psy_mem = psutil.virtual_memory()
    vrt_mem = psutil.swap_memory()
    disk = list(psutil.disk_usage(get_config("env", "workspace")))[0]
    return cpu, psy_mem.total, disk, vrt_mem.total + psy_mem.total 
Example #30
Source File: system.py    From ahenk with GNU Lesser General Public License v3.0 5 votes vote down vote up
def percent():
                return psutil.disk_usage('/')[3]