mirror of
https://github.com/kennethreitz/python-guide.git
synced 2026-06-05 14:50:19 +00:00
Merge pull request #1114 from tommy3001/master
Code refactoring and wording improvements for psutils.
This commit is contained in:
+19
-21
@@ -155,44 +155,42 @@ tests (net, CPU) fail, it will send an email.
|
|||||||
# Package for email services:
|
# Package for email services:
|
||||||
import smtplib
|
import smtplib
|
||||||
import string
|
import string
|
||||||
MAX_NET_USAGE = 400000
|
MAX_NET_USAGE = 400000 # bytes per seconds
|
||||||
MAX_ATTACKS = 4
|
MAX_ATTACKS = 4
|
||||||
attack = 0
|
attack = 0
|
||||||
counter = 0
|
|
||||||
while attack <= MAX_ATTACKS:
|
while attack <= MAX_ATTACKS:
|
||||||
sleep(4)
|
sleep(4)
|
||||||
counter = counter + 1
|
|
||||||
# Check the cpu usage
|
# Check the net usage wit named tuples
|
||||||
if cpu_percent(interval = 1) > 70:
|
neti1 = net_io_counters().bytes_recv
|
||||||
attack = attack + 1
|
neto1 = net_io_counters().bytes_sent
|
||||||
# Check the net usage
|
|
||||||
neti1 = net_io_counters()[1]
|
|
||||||
neto1 = net_io_counters()[0]
|
|
||||||
sleep(1)
|
sleep(1)
|
||||||
neti2 = net_io_counters()[1]
|
neti2 = net_io_counters().bytes_recv
|
||||||
neto2 = net_io_counters()[0]
|
neto2 = net_io_counters().bytes_sent
|
||||||
|
|
||||||
# Calculate the bytes per second
|
# Calculate the bytes per second
|
||||||
net = ((neti2+neto2) - (neti1+neto1))/2
|
net = ((neti2+neto2) - (neti1+neto1))/2
|
||||||
if net > MAX_NET_USAGE:
|
|
||||||
attack = attack + 1
|
# Check the net and cpu usage
|
||||||
if counter > 25:
|
if (net > MAX_NET_USAGE) or (cpu_percent(interval = 1) > 70):
|
||||||
attack = 0
|
attack+=1
|
||||||
counter = 0
|
elif attack > 1:
|
||||||
|
attack-=1
|
||||||
|
|
||||||
# Write a very important email if attack is higher than 4
|
# Write a very important email if attack is higher than 4
|
||||||
TO = "you@your_email.com"
|
TO = "you@your_email.com"
|
||||||
FROM = "webmaster@your_domain.com"
|
FROM = "webmaster@your_domain.com"
|
||||||
SUBJECT = "Your domain is out of system resources!"
|
SUBJECT = "Your domain is out of system resources!"
|
||||||
text = "Go and fix your server!"
|
text = "Go and fix your server!"
|
||||||
BODY = string.join(("From: %s" %FROM,"To: %s" %TO,"Subject: %s" %SUBJECT, "",text), "\r\n")
|
string="\r\n"
|
||||||
|
BODY = string.join(("From: %s" %FROM,"To: %s" %TO,
|
||||||
|
"Subject: %s" %SUBJECT, "",text))
|
||||||
server = smtplib.SMTP('127.0.0.1')
|
server = smtplib.SMTP('127.0.0.1')
|
||||||
server.sendmail(FROM, [TO], BODY)
|
server.sendmail(FROM, [TO], BODY)
|
||||||
server.quit()
|
server.quit()
|
||||||
|
|
||||||
|
|
||||||
A full terminal application like a widely extended top which is based on
|
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.
|
||||||
psutil and with the ability of a client-server monitoring is
|
|
||||||
`glance <https://github.com/nicolargo/glances/>`_.
|
|
||||||
|
|
||||||
|
|
||||||
*******
|
*******
|
||||||
Ansible
|
Ansible
|
||||||
|
|||||||
Reference in New Issue
Block a user