From 8e7087b41b42609837ed3484732f450c68e86edc Mon Sep 17 00:00:00 2001 From: Thomas Severin Date: Mon, 25 Apr 2022 10:05:30 +0200 Subject: [PATCH 1/3] Code refactoring and wording improvements. --- docs/scenarios/admin.rst | 40 +++++++++++++++++++--------------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/docs/scenarios/admin.rst b/docs/scenarios/admin.rst index 07d263945..236d08d2b 100644 --- a/docs/scenarios/admin.rst +++ b/docs/scenarios/admin.rst @@ -155,44 +155,42 @@ tests (net, CPU) fail, it will send an email. # Package for email services: import smtplib import string - MAX_NET_USAGE = 400000 + MAX_NET_USAGE = 400000 # bytes per seconds MAX_ATTACKS = 4 attack = 0 - counter = 0 while attack <= MAX_ATTACKS: sleep(4) - counter = counter + 1 - # Check the cpu usage - if cpu_percent(interval = 1) > 70: - attack = attack + 1 - # Check the net usage - neti1 = net_io_counters()[1] - neto1 = net_io_counters()[0] + + # Check the net usage wit named tuples + neti1 = net_io_counters().bytes_recv + neto1 = net_io_counters().bytes_sent sleep(1) - neti2 = net_io_counters()[1] - neto2 = net_io_counters()[0] + neti2 = net_io_counters().bytes_recv + neto2 = net_io_counters().bytes_sent + # Calculate the bytes per second net = ((neti2+neto2) - (neti1+neto1))/2 - if net > MAX_NET_USAGE: - attack = attack + 1 - if counter > 25: - attack = 0 - counter = 0 + + # Check the net and cpu usage + if (net > MAX_NET_USAGE) or (cpu_percent(interval = 1) > 70): + attack+=1 + elif attack > 1: + attack-=1 + # Write a very important email if attack is higher than 4 TO = "you@your_email.com" FROM = "webmaster@your_domain.com" SUBJECT = "Your domain is out of system resources!" 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.sendmail(FROM, [TO], BODY) server.quit() -A full terminal application like a widely extended top which is based on -psutil and with the ability of a client-server monitoring is -`glance `_. - +A full terminal application like a widely extended top is glance, which is based on psutil and has the ability for client-server monitoring. ******* Ansible From 69580b1179e64da6dca20215aeaf0257e044f369 Mon Sep 17 00:00:00 2001 From: Thomas S Date: Wed, 27 Apr 2022 18:34:21 +0200 Subject: [PATCH 2/3] Broken link to Glance --- docs/scenarios/admin.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/scenarios/admin.rst b/docs/scenarios/admin.rst index 236d08d2b..f70268fa9 100644 --- a/docs/scenarios/admin.rst +++ b/docs/scenarios/admin.rst @@ -190,7 +190,7 @@ tests (net, CPU) fail, it will send an email. server.quit() -A full terminal application like a widely extended top is glance, which is based on psutil and has the ability for client-server monitoring. +A full terminal application like a widely extended top is `glance `_, which is based on psutil and has the ability for client-server monitoring. ******* Ansible From 536b08fc804eb9af246a3e707aafb951a3cc14c1 Mon Sep 17 00:00:00 2001 From: Thomas S Date: Wed, 27 Apr 2022 18:34:46 +0200 Subject: [PATCH 3/3] Broken link to Glance --- docs/scenarios/admin.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/scenarios/admin.rst b/docs/scenarios/admin.rst index f70268fa9..73df990d7 100644 --- a/docs/scenarios/admin.rst +++ b/docs/scenarios/admin.rst @@ -190,7 +190,7 @@ tests (net, CPU) fail, it will send an email. server.quit() -A full terminal application like a widely extended top is `glance `_, which is based on psutil and has the ability for client-server monitoring. +A full terminal application like a widely extended top is `Glance `_, which is based on psutil and has the ability for client-server monitoring. ******* Ansible