-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpybsdalert.py
46 lines (39 loc) · 1.43 KB
/
pybsdalert.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
'''Script that allows to monitor BSD system'''
import os
import smtplib
import argparse
import email
class BaseAlert():
'''Requires recv defined'''
def __init__(self,recv):
self.recv = recv
def send_alert(self,alert):
msg = email.message.EmailMessage()
msg['Subject'] = 'Alert from system'
msg['From'] = 'alert@' + os.uname().nodename
msg['To'] = self.recv
msg.set_content(alert)
with smtplib.SMTP(host='localhost') as mail:
mail.send_message(msg,msg['From'],self.recv)
class Disks(BaseAlert):
'''Alerts in case there is a drive limit reached'''
def __init__(self,recv,limit):
BaseAlert.__init__(self,recv)
self.limit = limit
def max_drive_limit(self):
msg = ''
for line in os.popen('df -P'):
p = line.split()[4]
p = p[:-1]
if p.isdigit() and self.limit < int(p):
msg += str(line)
if msg:
msg = 'Disk space low for:\n' + msg
self.send_alert(msg)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Monitors BSD system and emails alerts on triggers')
parser.add_argument('receiver', help='email of receiver')
parser.add_argument('limit', type=int, help='integer limit of which above disk space will be alerted')
args = parser.parse_args()
disk = Disks(args.receiver, args.limit)
disk.max_drive_limit()