Created
April 17, 2012 08:58
-
-
Save yyuu/2404720 to your computer and use it in GitHub Desktop.
mon monitor plugin to free blocks/i-node on block devices
This file contains hidden or 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 with_statement | |
| import logging | |
| import optparse | |
| import os | |
| import sys | |
| def main(args): | |
| parser = optparse.OptionParser("usage %prog [OPTIONS]", add_help_option=False) | |
| parser.add_option('--help', action='help', help='show this message and exit') | |
| parser.add_option('-g', '--greater-than', '--should-be-greater-than', type='int', default=None, dest='gt', help='threshold') | |
| parser.add_option('-l', '--less-than', '--should-be-less-than', type='int', default=None, dest='lt', help='threshold') | |
| parser.add_option('-d', '--directory', default=[], action='append', dest='directories', help='directory') | |
| parser.add_option('-v', '--verbose', default=False, action='store_true', dest='verbose', help='show verbose messages') | |
| (options, args) = parser.parse_args(args) | |
| if options.verbose: | |
| logging.basicConfig(level=logging.DEBUG) | |
| directories = options.directories if 0 < len(options.directories) else [ '/' ] | |
| f = 0 | |
| for fs_file in directories: | |
| r = os.statvfs(fs_file) | |
| block_usage_pct = round(100.0 - (float(r.f_bavail) / float(r.f_blocks) * 100)) | |
| inode_usage_pct = round(100.0 - (float(r.f_favail) / float(r.f_files) * 100)) | |
| logging.debug("%s: block_usage_pct => %d, inode_usage_pct => %d" % (fs_file, block_usage_pct, inode_usage_pct)) | |
| if options.lt: | |
| if options.lt < block_usage_pct: | |
| logging.error("Block usage on %s is too big. (%d%% used)" % (fs_file, block_usage_pct)) | |
| f += 1 | |
| if options.lt < inode_usage_pct: | |
| logging.error("i-node usage on %s is too big. (%d%% used)" % (fs_file, inode_usage_pct)) | |
| f += 1 | |
| if options.gt: | |
| if block_usage_pct <= options.gt: | |
| logging.error("Block usage on %s is too small. (%d%% used)" % (fs_file, block_usage_pct)) | |
| f += 1 | |
| if inode_usage_pct <= options.gt: | |
| logging.error("i-node usage on %s is too small. (%d%% used)" % (fs_file, inode_usage_pct)) | |
| f += 1 | |
| sys.exit(0 if f == 0 else 1) | |
| if __name__ == "__main__": | |
| main(sys.argv) | |
| # vim:set ft=python : |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment