Last active
December 11, 2018 17:51
-
-
Save mauricionr/7e42086283b21467bfc1ad3bed01f1ee to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# python ./kubepodmetric.py -w -s {{POD_PREFIX}} | |
import sys | |
import os | |
import re | |
import time | |
import json | |
import argparse | |
def getHpa(data, podName): | |
for item in data: | |
name = str(item['metadata']['name'].encode('utf8')) | |
if name.find(podName) > -1: | |
if item['status']['currentReplicas'] > 0: | |
return float(item['spec']['targetCPUUtilizationPercentage']) | |
parser = argparse.ArgumentParser(description='Fetch some useful pod Kubernetes statistics ') | |
parser.add_argument('podPrefix', metavar='POD_PREFIX_NAME', type=str, | |
help='Pod name prefix that should be measured') | |
parser.add_argument('-s', dest='sumarized', | |
default=False, action='store_true', | |
help='Summarizes pod metrics') | |
parser.add_argument('-w', dest='watch', | |
default=False, action='store_true', | |
help='Watch Kubernetes to change state') | |
args = parser.parse_args() | |
pod_prefix = args.podPrefix | |
watch = args.watch | |
summarized = args.sumarized | |
command = 'kubectl top pods -n production | grep %s' % pod_prefix | |
hpaCluster = getHpa(json.loads(os.popen('kubectl get hpa -o=json -n production').read())['items'], pod_prefix) | |
cpusL = 0.0 | |
cpusR = 0.0 | |
memsL = 0.0 | |
memsR = 0.0 | |
nameColLength = 30 | |
loop = True | |
describeOutput = False | |
app_name = False | |
try: | |
if summarized: | |
print('APPNAME%s\tCPU\tMEMORY\tQTY\tHPA\tLimCPU\tLimMEM\tLimHPA' % (' ' * (nameColLength - 7))) | |
while loop: | |
output = os.popen(command).read() | |
if summarized: | |
versions = [] | |
output = output.strip().split("\n") | |
lines = len(output) | |
cpu = 0.0 | |
mem = 0.0 | |
cpuMax = 0.0 | |
memMax = 0.0 | |
for line in output: | |
cols = (' '.join(line.split())).split(' ') | |
pod_name = cols[0] | |
pod_name_splitted = pod_name.split('-') | |
if not app_name: | |
app_name = pod_name_splitted[0] | |
if app_name != pod_name_splitted[0]: | |
raise BaseException('Different apps were found with informed prefix. Prefix: "%s" Apps found: "%s" and "%s"' % (pod_prefix, app_name, pod_name_splitted[0])) | |
if pod_name_splitted[1] not in versions: | |
versions.append(pod_name_splitted[1]) | |
if not describeOutput: | |
describeOutput = True | |
os.popen('kubectl describe pods -n production %s > /tmp/kubepodmetric_describe' % cols[0]) | |
cpus = os.popen('cat /tmp/kubepodmetric_describe | grep cpu | tail -n3').read().strip().split("\n") | |
mems = os.popen('cat /tmp/kubepodmetric_describe | grep memory | tail -n3').read().strip().split("\n") | |
os.unlink('/tmp/kubepodmetric_describe') | |
for line in cpus: | |
if line == '' or line[0] != ' ': | |
continue | |
line = line.replace(' ','') | |
# if line == 'cpu:100m': | |
# continue | |
if cpusL == 0.0: | |
cpusL = eval(line.split(':')[1].replace('m', '*0.01')) | |
# 4 | |
if cpusR == 0.0: | |
cpusR = eval(line.split(':')[1].replace('m', '*0.01')) | |
for line in mems: | |
if line == '' or line[0] != ' ': | |
continue | |
line = line.replace(' ','') | |
if memsL == 0.0: | |
memsL = eval(line.split(':')[1].replace('Mi','').replace('Gi', '*1000')) | |
continue | |
if memsR == 0.0 and memsL > 0: | |
memsR = eval(line.split(':')[1].replace('Mi','').replace('Gi', '*1000')) | |
if len(cols) is 3: | |
cpuI = float(eval(cols[1].replace('m', '*0.01'))) | |
memI = float(eval(cols[2].replace('Mi','').replace('Gi', '*1000'))) | |
cpu += cpuI | |
mem += memI | |
if cpuI > cpuMax: | |
cpuMax= cpuI | |
if memI > memMax: | |
memMax = memI | |
cpu /= lines | |
mem /= lines | |
pHpa = '-' | |
pCpu = '-' | |
pMem = '-' | |
if hpaCluster is None or cpusR == 0: | |
hpa = '-' | |
else: | |
hpa = cpu/cpusR | |
hpa = int(hpa * 100) | |
pHpa = int(100*hpa/hpaCluster) | |
if cpusL > 0: | |
pCpu = int(100 * cpuMax / cpusL) | |
if memsL > 0: | |
pMem = int(100 * memMax / memsL) | |
cpu *= 100 | |
versions = ' (%s)' % ','.join(versions) | |
app_name_spacing = ' ' * (nameColLength - len(app_name) - len(versions)) | |
print('%s%s%s\t%sm\t%sMi\t%s\t%s%%\t%s%%\t%s%%\t%s%%' % (app_name, versions, app_name_spacing, int(cpu), int(mem), lines, hpa, pCpu, pMem, pHpa)) | |
else: | |
print(output) | |
if not watch: | |
loop = False | |
else: | |
time.sleep(3) | |
except KeyboardInterrupt: | |
pass | |
except: | |
raise |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment