-
Notifications
You must be signed in to change notification settings - Fork 6
/
check_partition_space.py
executable file
·65 lines (55 loc) · 1.62 KB
/
check_partition_space.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
#!/usr/bin/env python2
"""
Author :Julio Sanz
Website :www.elarraydejota.com
Email :juliojosesb@gmail.com
Description :Python script to check space occupation of a partition and send mail
in case it surpasses a given value. It uses Google SMTP by default, change
as you need
Dependencies :This scrip has been written using Python 2.7.9
Usage :chk_partition_space.py [partition]
Example :chk_partition_space.py /home
License :GPLv3
"""
#
# IMPORTS
#
import os,sys,smtplib
#
# VARIABLES
#
partition=sys.argv[1]
alert_value=90 # Alert threshold in % of disk space occupation
server=os.uname()[1]
mail_to="yourmail@gmail.com"
mail_password="your_mail_password"
smtp_server="smtp.gmail.com"
#
# FUNCTIONS
#
def check_partition():
chk_partition = os.statvfs(partition)
total = chk_partition.f_blocks * chk_partition.f_frsize
used = (chk_partition.f_blocks - chk_partition.f_bfree) * chk_partition.f_frsize
percentage_used = (100 * used)/total
if percentage_used > alert_value:
send_mail(percentage_used)
def send_mail(percentage_used):
message = """From: From %s
To: To %s
Subject: Warning!! Percentaje usage of partition %s = %s %%
Please check affected partition and take action:
- Compress logs
- Delete data not needed
- Add more space to partition
""" % (server, mail_to, partition, percentage_used)
s = smtplib.SMTP(smtp_server, 587)
s.starttls()
s.login(mail_to, mail_password)
s.sendmail(server, mail_to, message)
s.quit()
#===============
# MAIN
#===============
if __name__ == '__main__':
check_partition()