Skip to content

Instantly share code, notes, and snippets.

@marcinhlybin
Created January 30, 2019 09:39
Show Gist options
  • Save marcinhlybin/6c997177f0f9354f7383c58957b3cfa9 to your computer and use it in GitHub Desktop.
Save marcinhlybin/6c997177f0f9354f7383c58957b3cfa9 to your computer and use it in GitHub Desktop.
Check dir size growth in time
#!/usr/bin/env python
import yaml
import os
import sys
from shell import shell
path = '/Users/ahes/Workspace'
growth_threshold_percent = 1
def run_du(path):
data = {}
du = shell('du -d1 ' + path)
for line in du.output():
size, file = line.split()
data[file] = size
return data
def get_size():
with open('du.yml', 'r') as f:
data = yaml.safe_load(f)
#print(data)
return data
if not os.path.isfile('du.yml'):
data = run_du(path)
with open('du.yml', 'w') as f:
yaml.dump(data, f)
print('Initial du.yml file saved')
sys.exit(0)
last_data = get_size()
current_data = run_du(path)
for file, size in current_data.items():
if file in last_data:
growth_percent = (int(size) - int(last_data[file])) / float(last_data[file]) * 100
if growth_percent >= growth_threshold_percent:
print('Size increase of {}% for file {}'.format(int(growth_percent), file))
else:
print('File {} not found in previous du run'.format(file))
@marcinhlybin
Copy link
Author

$ python growth.py                                                                                                                                                Wed Jan 30 10:40:43 2019
Size increase of 8496% for file /Users/ahes/Workspace/growth
Size increase of 41% for file /Users/ahes/Workspace

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