Last active
September 28, 2015 09:58
-
-
Save admackin/1421493 to your computer and use it in GitHub Desktop.
Check free space and print error message if it is below a threshold
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from __future__ import division | |
import os | |
import sys | |
import optparse | |
""" | |
Designed for use with cron scripts, as in the default state it only prints output | |
when the free space it below the requested threshold. | |
""" | |
def main(): | |
parser = optparse.OptionParser() | |
parser.add_option("-r", dest='required_free', type="float", default=0.15, help= | |
"A floating point value for the proportion of free " | |
"space required [default: %default]") | |
parser.add_option("-p", dest='freepath', type="string", default='/', help= | |
"The path to check [default: %default]") | |
parser.add_option("-v", dest='verbose', action='store_true', default=False, help= | |
"Show free amount even when it is above the threshold") | |
options, _ = parser.parse_args() | |
check_free_proportion(options.freepath, options.required_free, options.verbose) | |
def check_free_proportion(freepath, req_free, verbose=False): | |
freeprop = get_free_proportion(freepath) | |
if freeprop < req_free: | |
print >> sys.stderr, "Path '%s' has only %0.1f%% free space remaining, which is less than the requested %0.1f%%" % (freepath, 100.0 * freeprop, 100.0 * req_free) | |
elif verbose: | |
print >> sys.stderr, "Path '%s' has %0.1f%% free space remaining, which is more than the requested %0.1f%%" % (freepath, 100.0 * freeprop, 100.0 * req_free) | |
def get_free_proportion(freepath): | |
statres = os.statvfs(freepath) | |
return statres.f_bavail / statres.f_blocks | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment