Skip to content

Instantly share code, notes, and snippets.

@jonhattan
Created October 16, 2013 11:04
Show Gist options
  • Save jonhattan/7006073 to your computer and use it in GitHub Desktop.
Save jonhattan/7006073 to your computer and use it in GitHub Desktop.
Script to check free disk space, and disk space consumption and print an atert. Quick instructions: place it in /root/scripts and configure a daily or weekly cron.
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# set the minimum disk space available to alert on (in GB).
minimum = 5
# disk space variation (in GB) threshhold. 'delta GB have been consumed between the last two checks'.
delta = 1
import commands
import pickle
try:
f = open('/root/scripts/diskfree.dat', 'r')
before = pickle.load(f)
f.close()
except IOError: # ie. file not found
before = None
r = commands.getstatusoutput('df')
if r[0] == 0:
lines = r[1].split('\n')
del lines[0]
for line in lines:
fs, size, used, available, percent, mp = line.split()
if mp != '/': continue
available = int(available)
# store the available amount for tomorrow
f = open('/root/scripts/diskfree.dat', 'w')
pickle.dump(available, f)
f.close()
if available < (1048576 * minimum):
print "Less tham %sGB of free space left!" % minimum
if before is not None:
variation = available - before
if variation > (1048576 * delta):
vgb = variation / 1048576.0
print "In the period between the last two checks, disk usage has grown in %sGB" %vgb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment