Skip to content

Instantly share code, notes, and snippets.

@fredsig
Created October 15, 2018 11:13
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save fredsig/570d178fa328151608d4de28807618a0 to your computer and use it in GitHub Desktop.
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')
file.write(data)
file.close()
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)
else:
exit(1)
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():
try:
return(subprocess.check_output(['docker', 'info']))
except subprocess.CalledProcessError as e:
print "Couldn't call docker info: " + str(e)
exit(1)
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__":
write_to_prom(get_metrics(get_docker_info()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment