-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiskspace.py
42 lines (40 loc) · 1.58 KB
/
diskspace.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
# The following script is is monitor space on / dir
# Author/Maintainer: Sambit Kumar Nayak & Krishna Chaitanya
# Date: 02nd June 2020
# + hostname will add the hostname automatically into the subject
import socket
import subprocess
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
threshold = 85
partition = "/"
hostname = socket.gethostname()
def report_via_email(s):
print(s)
msg = MIMEMultipart()
msg["Subject"] = "Low disk space warning on " + hostname
sender = "[email protected]"
recipient = ["[email protected]"]
msg["From"] = sender
msg["To"] = ', '.join(recipient)
part = MIMEText(s,'html')
msg.attach(part)
with smtplib.SMTP("yoursmtpprovider #for example smtp.elasticemail.com", 2525) as server:
server.ehlo()
server.starttls()
server.login("[email protected]","your smtp password")
server.sendmail(sender,recipient,msg.as_string())
def check_once():
df = subprocess.Popen(["df","-h"], stdout=subprocess.PIPE)
s = ""
for line in df.stdout:
s+=str(line.decode('utf-8'))+'<br>'
splitline = line.decode().split()
df = subprocess.Popen(["df","-h"], stdout=subprocess.PIPE)
for line in df.stdout:
splitline = line.decode().split()
if splitline[5] == partition:
if int(splitline[4][:-1]) > threshold:
report_via_email(s)
check_once()