summaryrefslogtreecommitdiff
path: root/cloudinit/util.py
diff options
context:
space:
mode:
authorScott Moser <smoser@ubuntu.com>2014-09-30 16:01:00 -0400
committerScott Moser <smoser@ubuntu.com>2014-09-30 16:01:00 -0400
commite76fea0e88ecc361eb7e301c0916a89cb4505c24 (patch)
treeebb80791984ae8ef844f3b4cb1522ce32fcc2888 /cloudinit/util.py
parenta429725ede3395a7e9c21b864a82e2c092657ae2 (diff)
downloadvyos-cloud-init-e76fea0e88ecc361eb7e301c0916a89cb4505c24.tar.gz
vyos-cloud-init-e76fea0e88ecc361eb7e301c0916a89cb4505c24.zip
add code for setting up swap file
Diffstat (limited to 'cloudinit/util.py')
-rw-r--r--cloudinit/util.py21
1 files changed, 21 insertions, 0 deletions
diff --git a/cloudinit/util.py b/cloudinit/util.py
index 76e91951..349d0155 100644
--- a/cloudinit/util.py
+++ b/cloudinit/util.py
@@ -1957,3 +1957,24 @@ def pathprefix2dict(base, required=None, optional=None, delim=os.path.sep):
raise ValueError("Missing required files: %s", ','.join(missing))
return ret
+
+
+def read_meminfo(meminfo="/proc/meminfo", raw=False):
+ # read a /proc/meminfo style file and return
+ # a dict with 'total', 'free', and 'available'
+ mpliers = {'kB': 2**10, 'mB': 2 ** 20, 'B': 1, 'gB': 2 ** 30}
+ kmap = {'MemTotal:': 'total', 'MemFree:': 'free',
+ 'MemAvailable:': 'available'}
+ ret = {}
+ for line in load_file(meminfo).splitlines():
+ try:
+ key, value, unit = line.split()
+ except ValueError:
+ key, value = line.split()
+ unit = 'B'
+ if raw:
+ ret[key] = int(value) * mpliers[unit]
+ elif key in kmap:
+ ret[kmap[key]] = int(value) * mpliers[unit]
+
+ return ret