Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Generates Prometheus metrics for Docker thin pool usage
#!/usr/bin/env python
# Generates Prometheus metrics for Docker thin pool usage
import subprocess
import re
import shutil
METRIC_PREFIX = "docker_thinpool"
PROM_DIR = "/tmp"
def write_to_prom(metrics):
data = ""
for k, v in metrics.iteritems():
k = METRIC_PREFIX + "_" + k
data += "# HELP " + k + "\n"
data += "# TYPE " + k + " gauge\n"
data += k + " " + str(v) + "\n"
print data
promfile = PROM_DIR + '/' + METRIC_PREFIX
file = open(promfile + '.tmp', 'w')
shutil.move(promfile + '.tmp', promfile + '.prom')
def convert_to_bytes(n, w):
if 'MB' in w:
return int(float(n)*1000000)
elif 'GB' in w:
return int(float(n)*1000000000)
def extract_value(line):
n = re.findall(r'\d+\.?\d+', line)
w = re.findall(r'[a-zA-Z]+$', line)
return convert_to_bytes(n[0], w[0])
def get_docker_info():
return(subprocess.check_output(['docker', 'info']))
except subprocess.CalledProcessError as e:
print "Couldn't call docker info: " + str(e)
def get_metrics(docker_info):
metrics = {}
for l in docker_info.splitlines():
if "Data Space Used" in l:
metrics['data_space_used_bytes'] = extract_value(l)
elif "Data Space Total" in l:
metrics['data_space_total_bytes'] = extract_value(l)
elif "Thin Pool Minimum Free Space" in l:
metrics['minimum_free_space_bytes'] = extract_value(l)
return metrics
if __name__ == "__main__":
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment