@@ -155,44 +155,42 @@ tests (net, CPU) fail, it will send an email.
155
155
# Package for email services:
156
156
import smtplib
157
157
import string
158
- MAX_NET_USAGE = 400000
158
+ MAX_NET_USAGE = 400000 # bytes per seconds
159
159
MAX_ATTACKS = 4
160
160
attack = 0
161
- counter = 0
162
161
while attack <= MAX_ATTACKS :
163
162
sleep(4 )
164
- counter = counter + 1
165
- # Check the cpu usage
166
- if cpu_percent(interval = 1 ) > 70 :
167
- attack = attack + 1
168
- # Check the net usage
169
- neti1 = net_io_counters()[1 ]
170
- neto1 = net_io_counters()[0 ]
163
+
164
+ # Check the net usage wit named tuples
165
+ neti1 = net_io_counters().bytes_recv
166
+ neto1 = net_io_counters().bytes_sent
171
167
sleep(1 )
172
- neti2 = net_io_counters()[1 ]
173
- neto2 = net_io_counters()[0 ]
168
+ neti2 = net_io_counters().bytes_recv
169
+ neto2 = net_io_counters().bytes_sent
170
+
174
171
# Calculate the bytes per second
175
172
net = ((neti2+ neto2) - (neti1+ neto1))/ 2
176
- if net > MAX_NET_USAGE :
177
- attack = attack + 1
178
- if counter > 25 :
179
- attack = 0
180
- counter = 0
173
+
174
+ # Check the net and cpu usage
175
+ if (net > MAX_NET_USAGE ) or (cpu_percent(interval = 1 ) > 70 ):
176
+ attack+= 1
177
+ elif attack > 1 :
178
+ attack-= 1
179
+
181
180
# Write a very important email if attack is higher than 4
182
181
TO = " you@your_email.com"
183
182
FROM = " webmaster@your_domain.com"
184
183
SUBJECT = " Your domain is out of system resources!"
185
184
text = " Go and fix your server!"
186
- BODY = string.join((" From: %s " % FROM ," To: %s " % TO ," Subject: %s " % SUBJECT , " " ,text), " \r\n " )
185
+ string= " \r\n "
186
+ BODY = string.join((" From: %s " % FROM ," To: %s " % TO ,
187
+ " Subject: %s " % SUBJECT , " " ,text))
187
188
server = smtplib.SMTP(' 127.0.0.1' )
188
189
server.sendmail(FROM , [TO ], BODY )
189
190
server.quit()
190
191
191
192
192
- A full terminal application like a widely extended top which is based on
193
- psutil and with the ability of a client-server monitoring is
194
- `glance <https://github.com/nicolargo/glances/ >`_.
195
-
193
+ A full terminal application like a widely extended top is `Glance <https://github.com/nicolargo/glances/ >`_, which is based on psutil and has the ability for client-server monitoring.
196
194
197
195
*******
198
196
Ansible
0 commit comments