Skip to content

Instantly share code, notes, and snippets.

@tbielawa
Last active March 28, 2017 16:36
Show Gist options
  • Save tbielawa/a24316f6aa83a1c841220e4cb71f94ac to your computer and use it in GitHub Desktop.
Save tbielawa/a24316f6aa83a1c841220e4cb71f94ac to your computer and use it in GitHub Desktop.
$ python ./parse_docker_info.py
Sizes as parsed using bitmath from 'docker info':
{'memtotal_mb': MB(15470.0), 'TotalMemory': GiB(15.11), 'DataSpaceAvailable': GB(43.01), 'DataSpaceTotal': GB(44.43)}
-----------------------
diskleft percent: 96.8039612874%
-----------------------
Sizes as from to_gigabytes:
memtotal_mb 15.47 GB
TotalMemory 16.2242389606 GB
DataSpaceAvailable 43.01 GB
DataSpaceTotal 44.43 GB
Sizes as from to_bytes:
memtotal_mb 15470000000
TotalMemory 16224238960.6
DataSpaceAvailable 43010000000.0
DataSpaceTotal 44430000000.0
#!/usr/bin/env python
"""
Demonstrates how bitmath could integrate into the preflight checks
Replaced code from the PR is maintained below but commented out.
The replacement code is on the following line.
"""
import bitmath
# recommended_diskspace = {
# "nodes": 15*10**9, => 15000000000 => GB(15.0)
# "masters": 40*10**9, => 40000000000 => GB(40.0)
# "etcd": 20*10**9, => => 20000000000 => GB(20.0)
# }
recommended_diskspace = {
"nodes": bitmath.GB(15),
"masters": bitmath.GB(40),
"etcd": bitmath.GB(20),
}
# Lines to read from the 'docker info' output
read_docker_info_lines = [
'Data Space Total',
'Data Space Available',
'Total Memory',
]
# Defined as a class var
minimum_docker_space_percent = 5.0
# comes from ansible facts
ansible_memtotal_mb = 15470
# record parsed sizes from the 'docker info' output
sizes = {}
with open('/tmp/docker-info.log', 'r') as fp:
for line in fp.readlines():
for info in read_docker_info_lines:
if line.strip().startswith(info):
line_parts = line.strip().split()
# 'docker info' gives the size as two words at the end of the line
info_size = "{} {}".format(line_parts[-2], line_parts[-1])
# label it using the docker info line without spaces included
sizes[info.replace(' ', '')] = bitmath.parse_string(info_size)
# Easy-mode parse the returned ansible memory var
sizes['memtotal_mb'] = bitmath.MB(ansible_memtotal_mb)
print """
Sizes as parsed using bitmath from 'docker info':"""
print sizes
print "-----------------------"
diskleft_percent = sizes['DataSpaceAvailable'] / sizes['DataSpaceTotal'] * 100
print "diskleft percent: {pct}%".format(pct=diskleft_percent)
print "-----------------------"
# fail if available diskspace is less than minimum docker threshold
if diskleft_percent < minimum_docker_space_percent:
msg = ("Amount of data space remaining ({remaining}%%) "
"for the docker storage driver is below minimum threshold ({min}%%)")
msg.format(remaining=diskleft_percent,
min=minimum_docker_space_percent)
print msg
# @staticmethod
# def to_gigabytes(total_bytes):
# ...
# return total_bytes / 10**9
print """
Sizes as from to_gigabytes:"""
for k, v in sizes.items():
print k, v.GB
# @staticmethod
# def to_bytes(strfmt):
# units = {
# ....
#
# segments = strfmt.lower().split(" ")
# if not units.get(segments[1]):
# return 0
#
# byte_size = float(segments[0]) * float(units.get(segments[1]))
# return byte_size
print """
Sizes as from to_bytes:"""
for k, v in sizes.items():
print k, v.bytes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment