Get disk usage for all the containers with python script
2017-01-16
194 words
1 min read
With my increasing love for python, here is my attempt to get the disk usage of all the containers on some host. Well, since the requirements vary for everyone, so this script is far from complete.
import docker import json # We will connect to 192.168.122.1 for docker daemon. If that is not the case, # then change the below. client = docker.DockerClient(base_url="tcp://192.168.122.1:4243") # Get list of all containers. cls=client.containers.list() stats={} # And now we will iterate over that list to get stats for all the containers. for val in cls: print (val.name) stats[val.name] = val.stats(stream=False) # Get the disk usage for root and /tmp from containers with docker.exec stats[val.name]['df-root'] = ( str(val.exec_run(r'df -kh --output="size,used,avail,pcent" /', stream=False).splitlines()[1]).replace("'","").split()[1:] ) stats[val.name]['df-tmp'] = ( str((val.exec_run(r'df -kh --output="size,used,avail,pcent" /tmp ', stream=False).splitlines()[1:]+[''])[0]).replace("'","").split()[1:] ) # Now if you want, we have dict of all the data and we can process the # way we like it, for example create a html table for disk usage only. print ('<table>') for st in stats: print ('<tr>') print ("<td>Root-%s</td>"%(st)) for i in stats[st]['df-root']: print ('<td>%s</td>'%(i) ) print ('</tr>') print ('<tr>') print ("<td>tmp-%s</td>"%(st)) for i in stats[st]['df-tmp']: print ('<td>%s</td>'%(i) ) print ('</tr>') print ('</table>')
Related Articles:
- 2017/01/09 ansible with docker dynamic inventory
- 2016/07/04 Python script to manage virtual machines with python API for libvirt.
- 2016/12/05 5 Apps You need to Have in Your Phone While Touring India
- 2016/08/01 supernova – manage multiple openstack environment
- 2015/06/29 Easily monitor and archive your system log reports.
Authored By Amit Agarwal
Amit Agarwal, Linux and Photography are my hobbies.Creative Commons Attribution 4.0 International License.