Skip to content

Instantly share code, notes, and snippets.

@aravindavk
Last active December 7, 2018 09:02
Show Gist options
  • Save aravindavk/1f02e2741ea2b1d3e567159eebf55889 to your computer and use it in GitHub Desktop.
Save aravindavk/1f02e2741ea2b1d3e567159eebf55889 to your computer and use it in GitHub Desktop.
# Collects the stats of PVC claims created in Kubernetes
# Install dependencies using `pip3 install kubernetes`
# Run this file and redirect the output to a file
# KUBECONFIG=<path_to_kube_config> python3 pv_claims_stats.py > data.txt
# For example, kubeconfig file will be available in the gcs/deploy directory
# KUBECONFIG=/root/gcs/deploy/kubeconfig python3 pv_claims_stats.py > data.txt
import time
from datetime import datetime
from kubernetes import client, config
config.load_kube_config()
core_v1_api = client.CoreV1Api()
def dump_stats():
pvcs = core_v1_api.list_namespaced_persistent_volume_claim("default")
pv_stats = {}
total = 0
ts = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
for pv in pvcs.items:
if pv_stats.get(pv.status.phase, None) is None:
pv_stats[pv.status.phase] = 0
pv_stats[pv.status.phase] += 1
total += 1
print("{ts},{name},{status},{volume},"
"{capacity},{accessmodes},{storageclassname}".format(
ts=ts,
name=pv.metadata.name,
status=pv.status.phase,
volume=pv.spec.volume_name,
capacity=pv.status.capacity["storage"] if pv.status.capacity is not None else "",
accessmodes=pv.status.access_modes,
storageclassname=pv.spec.storage_class_name
))
summary = "Summary: TS=%s\tTotal=%s" % (ts, total)
for k, v in pv_stats.items():
summary += "\t%s=%s" % (k, v)
print(summary)
if __name__ == "__main__":
while True:
dump_stats()
time.sleep(2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment