Skip to content

Instantly share code, notes, and snippets.

@portante
Last active August 14, 2019 07:31
Show Gist options
  • Save portante/cdd063bdb7db6dae421abd675df6a002 to your computer and use it in GitHub Desktop.
Save portante/cdd063bdb7db6dae421abd675df6a002 to your computer and use it in GitHub Desktop.
Format output from "oc get pods -o json" or "oc adm manage-node --list-pods --output=json" into a list with container, pod, namespace, cpu, memory, host
#!/usr/bin/env python
import sys
import os
import json
if sys.argv[1] == '-':
pods = json.load(sys.stdin)
else:
with open(sys.argv[1], "r") as fp:
pods = json.load(fp)
assert pods['kind'] == 'List'
assert pods['apiVersion'] == 'v1'
#print json.dumps(pods, indent=4, sort_keys=True)
keys = [ 'cn', 'pn', 'nn', 'cpu', 'mem', 'hn' ]
headers = { 'cn': "CONTAINER", 'pn': "POD", 'nn': "NAMESPACE", 'cpu': "CPU lim/req", 'mem': "MEMORY lim/req", 'hn': "HOSTNAME" }
widths = { 'cn': len(headers['cn']), 'pn': len(headers['pn']), 'nn': len(headers['nn']), 'cpu': len(headers['cpu']), 'mem': len(headers['mem']), 'hn': len(headers['hn']) }
containers = []
for pod in pods['items']:
pod_name = pod['metadata']['name']
widths['pn'] = max(len(pod_name), widths['pn'])
namespace_name = pod['metadata']['namespace']
widths['nn'] = max(len(namespace_name), widths['nn'])
host_name = pod['spec']['nodeName']
widths['hn'] = max(len(host_name), widths['hn'])
for container in pod['spec']['containers']:
try:
req_cpu = container["resources"]["requests"]["cpu"]
except Exception:
req_cpu = "-"
try:
req_mem = container["resources"]["requests"]["memory"]
except Exception:
req_mem = "-"
try:
lim_cpu = container["resources"]["limits"]["cpu"]
except Exception:
lim_cpu = "-"
try:
lim_mem = container["resources"]["limits"]["memory"]
except Exception:
lim_mem = "-"
cpu = "%s/%s" % (lim_cpu, req_cpu)
widths['cpu'] = max(len(cpu), widths['cpu'])
mem = "%s/%s" % (lim_mem, req_mem)
widths['mem'] = max(len(mem), widths['mem'])
container_name = container['name']
widths['cn'] = max(len(container_name), widths['cn'])
containers.append({ 'cn': container_name, 'pn': pod_name, 'nn': namespace_name, 'cpu': cpu, 'mem': mem, 'hn': host_name })
for k in keys:
sys.stdout.write("%-*s " % (widths[k], headers[k]))
sys.stdout.write("\n")
for container in containers:
for k in keys:
sys.stdout.write("%-*s " % (widths[k], container[k]))
sys.stdout.write("\n")
@portante
Copy link
Author

portante commented Nov 7, 2017

For example output:

$ ./format-pods.py pods.json
CONTAINER             POD                         NAMESPACE       CPU lim/req MEMORY lim/req HOSTNAME
registry              docker-registry-4-1prbf     default         -/100m      -/256Mi        host-0.example.com
oso-rhel7-zagg-web    oso-rhel7-zagg-web-10-jzjq4 default         1/1         1512Mi/1512Mi  host-0.example.com
registry-console      registry-console-3-mfm7l    default         -/-         -/-            host-0.example.com
router                router-5-p7wns              default         -/100m      -/256Mi        host-0.example.com
elasticsearch         logging-es-e0lzlfm8-4-758qg logging         -/1         11Gi/11Gi      host-0.example.com
fluentd-elasticsearch logging-fluentd-p82g6       logging         -/100m      512Mi/512Mi    host-0.example.com
hawkular-cassandra-1  hawkular-cassandra-1-nn319  openshift-infra -/-         2G/1G          host-0.example.com
hawkular-metrics      hawkular-metrics-p08vh      openshift-infra -/100m      3G/2G          host-0.example.com
heapster              heapster-hg0l0              openshift-infra -/-         3750M/937500k  host-0.example.com
registry              docker-registry-4-c8619     default         -/100m      -/256Mi        host-1.example.com
oso-rhel7-zagg-web    oso-rhel7-zagg-web-10-dqpgf default         1/1         1512Mi/1512Mi  host-1.example.com
router                router-5-l2hh1              default         -/100m      -/256Mi        host-1.example.com
curator               logging-curator-3-9qb4c     logging         -/100m      512Mi/512Mi    host-1.example.com
elasticsearch         logging-es-w5uss58w-4-w76pz logging         -/1         11Gi/11Gi      host-1.example.com
fluentd-elasticsearch logging-fluentd-t349h       logging         -/100m      512Mi/512Mi    host-1.example.com
hawkular-cassandra-2  hawkular-cassandra-2-cwrtg  openshift-infra -/-         2G/1G          host-1.example.com

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment