<h2>UPDATE</h2>
<p>Regarding space, I realized I can mount the root path '/' into the container and use this piece of code to get my stats:</p>
<pre><code>import shutil

total, used, free = shutil.disk_usage(&quot;/&quot;)

print(&quot;Total: %d GiB&quot; % (total // (2**30)))
print(&quot;Used: %d GiB&quot; % (used // (2**30)))
print(&quot;Free: %d GiB&quot; % (free // (2**30)))
</code></pre>
<p>Still Looking for a way to do it through Kubernetes itself, though.</p>
<h2>Original Question</h2>
<p>I'm building a service that part of its function is to monitor system resources for my kubernetes cluster (not specific pods - the entire machine the node runs on)
And I realized that <code>kubectl top node</code> is a good way to get that information (excluding storage).</p>
<p>Is there a way to get this information using the kubernetes python package?</p>
<p>I've tried to find a solution in the <a href="https://github.com/kubernetes-client/python/blob/master/kubernetes/README.md" rel="nofollow noreferrer">package documentation</a> and realized that action is not there. (maybe under a different name? I couldn't find it)<br />
Also found <a href="https://github.com/kubernetes-client/python/issues/435" rel="nofollow noreferrer">this issue</a> on Github which partially solves my problem but I'm still looking for something simpler.</p>
<p><em><strong>My question: How can I check for system resources like Memory, storage, and CPU usage for my kubernetes node through the Kubernetes Python package?</strong></em></p>

<p>Resource usage metrics, such as pod or node CPU and memory usage, are available in Kubernetes through the Metrics API.<br />
You can access the <a href="https://github.com/kubernetes/community/blob/master/contributors/design-proposals/instrumentation/resource-metrics-api.md" rel="nofollow noreferrer">Metrics API</a> using <code>kubectl get --raw</code> command, e.g.:</p>
<pre><code>kubectl get --raw /apis/metrics.k8s.io/v1beta1/nodes
</code></pre>
<p>More examples can be found in the <a href="https://github.com/feiskyer/kubernetes-handbook/blob/master/en/addons/metrics.md#metrics-api" rel="nofollow noreferrer">Metrics API documentation</a>.</p>
<hr />
<p>Using <a href="https://github.com/kubernetes-client/python/#kubernetes-python-client" rel="nofollow noreferrer">Kubernetes Python Client</a> you should be able to check memory and cpu usage of Kubernetes nodes, just like with the <code>kubectl top nodes</code> command:<br />
<strong>NOTE:</strong> Memory is in <code>kilobytes</code> but can easily be converted to <code>megabytes</code>.</p>
<pre><code>#!/usr/bin/python
# Script name: usage_info.py

from kubernetes import client, config

config.load_kube_config()

api = client.CustomObjectsApi()
k8s_nodes = api.list_cluster_custom_object(&quot;metrics.k8s.io&quot;, &quot;v1beta1&quot;, &quot;nodes&quot;)


for stats in k8s_nodes['items']:
    print(&quot;Node Name: %s\tCPU: %s\tMemory: %s&quot; % (stats['metadata']['name'], stats['usage']['cpu'], stats['usage']['memory']))
</code></pre>
<p>And sample output:</p>
<pre><code>$ ./usage_info.py
Node Name: node01     CPU: 82845225n  Memory: 707408Ki
Node Name: node02     CPU: 99717207n  Memory: 613892Ki
Node Name: node03     CPU: 74841362n  Memory: 625316Ki
</code></pre>
<p>In terms of storage usage, I think it should be checked in a different way as this information isn't available at the <code>/apis/metrics.k8s.io/v1beta1/nodes</code> endpoint.</p>
