~ahasenack/landscape-client/landscape-client-1.5.5-0ubuntu0.9.04.0

« back to all changes in this revision

Viewing changes to landscape/sysinfo/disk.py

  • Committer: Bazaar Package Importer
  • Author(s): Rick Clark
  • Date: 2008-09-08 16:35:57 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080908163557-l3ixzj5dxz37wnw2
Tags: 1.0.18-0ubuntu1
New upstream release 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from __future__ import division
 
2
 
 
3
import os
 
4
 
 
5
from twisted.internet.defer import succeed
 
6
 
 
7
from landscape.lib.disk import get_mount_info, get_filesystem_for_path
 
8
 
 
9
 
 
10
def format_megabytes(megabytes):
 
11
    if megabytes >= 1024*1024:
 
12
        return "%.2fTB" % (megabytes/(1024*1024))
 
13
    elif megabytes >= 1024:
 
14
        return "%.2fGB" % (megabytes/1024)
 
15
    else:
 
16
        return "%dMB" % (megabytes)
 
17
 
 
18
 
 
19
def usage(info):
 
20
    total = info["total-space"]
 
21
    used = total - info["free-space"]
 
22
    return "%0.1f%% of %s" % ((used / total) * 100, format_megabytes(total))
 
23
 
 
24
class Disk(object):
 
25
 
 
26
    def __init__(self, mounts_file="/proc/mounts", statvfs=os.statvfs):
 
27
        self._mounts_file = mounts_file
 
28
        self._statvfs = statvfs
 
29
 
 
30
    def register(self, sysinfo):
 
31
        self._sysinfo = sysinfo
 
32
 
 
33
    def run(self):
 
34
        main_info = get_filesystem_for_path("/home", self._mounts_file,
 
35
                                            self._statvfs)
 
36
        total = main_info["total-space"]
 
37
        self._sysinfo.add_header("Usage of " + main_info["mount-point"],
 
38
                                 usage(main_info))
 
39
 
 
40
        seen_mounts = set()
 
41
        seen_devices = set()
 
42
        infos = list(get_mount_info(self._mounts_file, self._statvfs))
 
43
        infos.sort(key=lambda i: len(i["mount-point"]))
 
44
        for info in infos:
 
45
            total = info["total-space"]
 
46
 
 
47
            if info["mount-point"] in seen_mounts:
 
48
                continue
 
49
            seen_mounts.add(info["mount-point"])
 
50
            if info["device"] in seen_devices:
 
51
                continue
 
52
            seen_devices.add(info["device"])
 
53
            if info["filesystem"] in ("udf", "iso9660"):
 
54
                continue
 
55
            if total <= 0:
 
56
                # Some "virtual" filesystems have 0 total space. ignore them.
 
57
                continue
 
58
 
 
59
            used = ((total - info["free-space"]) / total) * 100
 
60
            if used >= 85:
 
61
                self._sysinfo.add_note("%s is using %s"
 
62
                                       % (info["mount-point"], usage(info)))
 
63
        return succeed(None)