-
Notifications
You must be signed in to change notification settings - Fork 0
/
disc-sweeper.py
137 lines (105 loc) · 4.36 KB
/
disc-sweeper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import smtplib
import socket
import os
import subprocess
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
# PATH_TO_BE_MONITOR is the path to be monitored, e.g. "/"
# If the current percentage of free space under {PATH_TO_BE_MONITOR} is lower than {MIN_FREE_PERCENT},
# then a disk sweep will be performed and the contacts in the {TO_EMAILS} will get warned
PATH_TO_BE_MONITOR = "/path/to/be/monitored"
# PATHS_CAN_BE_CLEAN contains the paths could be swept
# These paths are typically paths to logs and tmp images you want to clean after some days
PATHS_CAN_BE_CLEAN = ["/path/can/be/deleted/1", "/path/can/be/deleted/2"]
# EXPIRY_DAYS is the expiry time of files to be swept.
# The files under {PATH_TO_BE_MONITOR} with modified-date more than {EXPIRY_DAYS} days ago will be deleted
EXPIRY_DAYS = 30
# TO_EMAILS is the list of person you want to notify upon disk-full
TO_EMAILS = ["[email protected]", "[email protected]"]
# TOOL_NAME is the name of this script, which will be shown as the 'From' bar in the warn email
TOOL_NAME = "system-monitor"
# MIN_FREE_PERCENT is the minimum percentage of free spaces required on the specified disk.
# see the description of PATH_TO_BE_MONITOR
MIN_FREE_PERCENT = 10
# PATH_LOGS is the path to the logs
# will be created if not exists
PATH_LOGS = "/data/disc-sweeper"
# EMAIL_SMTP_SERVER is your SMTP server
EMAIL_SMTP_SERVER = 'smtp.163.com'
# EMAIL_ACCOUNT is your account to be used by this script for sending the email
EMAIL_ACCOUNT = "[email protected]"
# EMAIL_PASSWD is the password of your email account
# if you use Gmail, plz enable 'https://myaccount.google.com/lesssecureapps'
# if you use 163 to send your email, plz set the 'authorization code' first, and use it as your password
EMAIL_PASSWD = "your-password"
def sendToEmail(fromaddr, toaddrs, subject, body):
account = EMAIL_ACCOUNT
password = EMAIL_PASSWD
# fill the msg body
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = ', '.join(toaddrs)
msg['Subject'] = subject
msg.attach(MIMEText(body, 'plain'))
text = msg.as_string()
# login your smtp server using your account
server = smtplib.SMTP_SSL(EMAIL_SMTP_SERVER, 587, timeout=15)
server.login(account, password)
# send the email and logout
server.sendmail(account, toaddrs, text)
server.quit()
def notify_disc_full(freePercent):
toaddr = TO_EMAILS
fromaddr = TOOL_NAME
hostname = socket.gethostname()
hostip = socket.gethostbyname(hostname)
subject = "Warn: Disk of {mon_path} on {host} [{ip}] is nearly full! Only {free}% free now".format(
mon_path=PATH_TO_BE_MONITOR,
host = hostname,
ip = hostip,
free = freePercent
)
df_res = subprocess.check_output(['df', '-h', PATH_TO_BE_MONITOR])
du_res = subprocess.check_output(['du', '-h', '-d', '1', PATH_TO_BE_MONITOR])
# customize your email body here
body = """
The free disk space on {mon_path} is lower than {min_free_percent}% !
A disc auto-clean process will be performed on following paths:
{clean_path}
======================================
df -h {mon_path}
{df}
du -hd 1 {mon_path}
{du}
======================================
//NOTE:
DO NOT REPLY!
This is an email sent by a script automatically
System adminstrators:
""".format(
df = df_res,
du = du_res,
clean_path = '\n'.join(PATHS_CAN_BE_CLEAN),
mon_path=PATH_TO_BE_MONITOR,
min_free_percent = MIN_FREE_PERCENT
)
sendToEmail(fromaddr, toaddr, subject, body)
def cleanDisc(path, mtime):
subprocess.call(["mkdir", "-p", PATH_LOGS])
f = open(PATH_LOGS + "sweep-" + datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S") +".log", "w")
findCmd = ["find"] + path + ["-mindepth", "1", "-mtime", "+"+str(mtime), "-delete", "-print"]
subprocess.call(findCmd, stdout=f, stderr=f)
f.close()
def getFreeDiscPercentage(path):
s = os.statvfs(path)
total_disc = s.f_frsize * s.f_blocks
avail_disc = s.f_frsize * s.f_bavail
return round((float(avail_disc)/total_disc)*100, 2)
## program entrance
freeSpacePercent = getFreeDiscPercentage(PATH_TO_BE_MONITOR)
if freeSpacePercent < MIN_FREE_PERCENT:
print "disc is nearly full! At least", MIN_FREE_PERCENT, "% free spaces is need."
notify_disc_full(freeSpacePercent)
cleanDisc(PATHS_CAN_BE_CLEAN, EXPIRY_DAYS)