-
Notifications
You must be signed in to change notification settings - Fork 2
/
filesystemalert_v2.py
executable file
·140 lines (125 loc) · 4.46 KB
/
filesystemalert_v2.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
138
139
140
#!/usr/bin/python
# @Author: Aldo Sotolongo
# @Date: 2016-05-25T14:22:59-04:00
# @Email: aldenso@gmail.com
# @Last modified by: Aldo Sotolongo
# @Last modified time: 2016-09-02T23:55:09-04:00
# File: filesystemalert_v2.py
# Description: This script is useful to set filesystem alerts based on
# defined thresholds, also you can exclude fs from the alerts
from subprocess import Popen, PIPE
from email.MIMEText import MIMEText
import smtplib
import syslog
fromaddr = "root@localhost"
toaddrs = "aldo@localhost"
temporalfile = "/tmp/fsalert.txt"
# toaddrs=["aldo@localhost","aldo@aldoca.com","aldenso@gmail.com"]
# If you want to exclude some FS set then in the variable excludefs as a list
excludefs = None
# excludefs=["/boot","/fsdump"]
# Set thresholds fs
yellowfs = 85
orangefs = 90
redfs = 95
# Set thresholds inodes
yellowinode = 85
orangeinode = 90
redinode = 95
# FS in alert, the first element is the header
fsalert = ["Alert type\tFileSystem Space\tUsage\n"]
# FS in alert, the first element is the header
inodealert = ["Alert type\tFileSystem Inodes\tUsage\n"]
filesystems = set()
cmd = "lsblk"
p = Popen(cmd, stdout=PIPE, shell=True)
output, error = p.communicate()
for line in output.splitlines():
if len(line.split()) == 7:
filesystems.add(line.split()[6])
for i in filesystems.copy():
if i == '[SWAP]' or i == 'MOUNTPOINT':
filesystems.remove(i)
def checkusagefs(filesystems):
print("### FS check ###")
if excludefs:
for e in excludefs:
filesystems.remove(e)
for fs in filesystems:
cmd = "df {} | tail -1".format(fs)
p = Popen(cmd, stdout=PIPE, shell=True)
output, error = p.communicate()
usage = int(output.split()[4][:-1])
if usage >= yellowfs and usage < orangefs:
fsalert.append("yellow FS ALERT\t{0}\t\t{1}\n".format(fs, usage))
elif usage >= orangefs and usage < redfs:
fsalert.append("orange FS ALERT\t{0}\t\t{1}\n".format(fs, usage))
elif usage >= redfs:
fsalert.append("red FS ALERT\t{0}\t\t{1}\n".format(fs, usage))
else:
print("{}:normal usage".format(fs))
def checkusageinode(filesystems):
print("### Inode check ###")
if excludefs:
for e in excludefs:
filesystems.remove(e)
for fs in filesystems:
cmd = "df -i {} | tail -1".format(fs)
p = Popen(cmd, stdout=PIPE, shell=True)
output, error = p.communicate()
usage = int(output.split()[4][:-1])
if usage >= yellowinode and usage < orangeinode:
inodealert.append("yellow Inode ALERT\t{0}\t\t{1}\n"
.format(fs, usage))
elif usage >= orangeinode and usage < redinode:
inodealert.append("orange Inode ALERT\t{0}\t\t{1}\n"
.format(fs, usage))
elif usage >= redinode:
inodealert.append("red Inode ALERT\t{0}\t\t{1}\n"
.format(fs, usage))
else:
print("{}:normal usage".format(fs))
def sendmail(fsalert=None, inodealert=None):
with open(temporalfile, "wb") as file:
if fsalert is not None:
fsalert.sort()
for entry in fsalert:
file.write(entry)
if inodealert is not None:
inodealert.sort()
for entry in inodealert:
file.write(entry)
fp = open(temporalfile, "rb")
msg = MIMEText(fp.read())
msg['From'] = fromaddr
msg['To'] = toaddrs
msg['Subject'] = "FileSystem Alert"
fp.close()
server = smtplib.SMTP('localhost')
# server.set_debuglevel(1)
server.sendmail(fromaddr, toaddrs, msg.as_string())
server.quit()
def main():
checkusagefs(filesystems)
checkusageinode(filesystems)
if len(fsalert) > 1 and len(inodealert) > 1:
sendmail(fsalert, inodealert)
for entry in fsalert:
print(entry)
for entry in inodealert:
print(entry)
syslog.syslog("File System Alert, mail sent to admins")
elif len(fsalert) > 1 and len(inodealert) == 1:
sendmail(fsalert, None)
for entry in fsalert:
print(entry)
syslog.syslog("File System Alert, mail sent to admins")
elif len(fsalert) == 1 and len(inodealert) > 1:
sendmail(None, inodealert)
for entry in inodealert:
print(entry)
syslog.syslog("File System Alert, mail sent to admins")
else:
print("Nothing to report")
if __name__ == "__main__":
main()