Created
September 18, 2017 03:43
-
-
Save diggzhang/5cf493da07994901174292c664c684db to your computer and use it in GitHub Desktop.
使用python监控磁盘容量
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
# coding:utf-8 | |
""" | |
Monitor on the target disk | |
If disk usage out of limit bounds | |
Execute the cleanup operation | |
""" | |
import os | |
import psutil | |
DISK_USAGE_LIMT = 70 | |
DIST_DISK = "/Backup2/" | |
CLEAN_TEMP_PATH = "/Backup2/v4_events_httplogs_daily_backup/*.7z" | |
def disk_cleaner(): | |
global CLEAN_TEMP_PATH | |
cmd = "ls -al {}".format(CLEAN_TEMP_PATH) | |
cmd_flag = os.system(cmd) | |
cmd2 = "rm {}".format(CLEAN_TEMP_PATH) | |
cmd2_flag = os.system(cmd2) | |
print("Cleanup status {} {}".format(cmd_flag, cmd2_flag)) | |
def monitor_disk(): | |
global DISK_USAGE_LIMT | |
global DIST_DISK | |
disk_percent = psutil.disk_usage(DIST_DISK).percent | |
if disk_percent > DISK_USAGE_LIMT: | |
print("Disk space usage: {}%, processing to cleanup... ".format(disk_percent)) | |
disk_cleaner() | |
else: | |
print("Disk space usage: {}%".format(disk_percent)) | |
monitor_disk() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment