Date

Author: Thomas Severin

Date: 12.Nov.2020

I wrote a small Python script to be ready for the next Mars invasion. It is running all the time on my server. It checks only some system values like CPU and net usage and the availability of simutechno.net. If there is an overload or the address is not available, the program sends an email. This can be a protection against DDoS atacks. (Please not me, I'm one of the good guys!!)

Psutil is a small package with very useful system functions. All the needed functions to check the performance of my server are inside this package.

Smtplib is the Python package for sending emails and the package Urllib has some additional web functions.

.

# functions to get system values:


import psutil as ps
# functions to take a break:
from time import sleep
# package for email services:
import smtplib
import string
# package for web services:
from urllib.request import urlopen

errorMsg = ""
atack = 0
counter = 0
while atack < 4:
 sleep(4)

 counter = counter + 1
 # check the cpu usage
 if ps.cpu_percent(interval = 1) > 75:
     atack = atack + 1
     errorMsg = "High CPU usage ! "

 # check the net usage
 neti1 = ps.net_io_counters()[1]
 neto1 = ps.net_io_counters()[0]
 sleep(1)
 neti2 = ps.net_io_counters()[1]
 neto2 = ps.net_io_counters()[0]
 net = ((neti2+neto2) - (neti1+neto1))/2

 if net > 400000:
     atack = atack + 1
     errorMsg = "High Network traffic! "

 if counter > 20:
     atack = 0
     counter = 0
     # check the availability of the website
     try:
         if urlopen("http://simutechno.net").getcode() != 200:
             atack = 4
             errorMsg = "WebPage not available! "

     except:
         atack = 4
         errorMsg = "WebPage not available! "

if atack >= 4:
    # write a very important email if atack is higher then 3
    TO = "thomas.severin@simutechno.net"
    FROM = "server@simutechno.net"
    SUBJECT = "We are under atack!!"
    text = "Go and protect your server. Error Message: " + errorMsg
    BODY =  "\r\n".join((
         "From: %s" % FROM,
         "To: %s" % TO,
         "Subject: %s" % SUBJECT,
         "",
         text
         ))
    print("Body :",BODY)

    server = smtplib.SMTP('smtp.simutechno.net',587)
    server.starttls()
    error= server.login("T_Severin","passwort")
    print(error)
    error = server.sendmail(FROM, [TO], BODY)
    print(error)
    server.quit()

The function cpu_percentage in line is relative simple to use. The interval parameter is the time in seconds in which the function is measuring the CPU usage. The function ´network_io_counters´ is a tuple of all send bytes for the in and out direction. Therefore it is necessary to call the function with a small time delay and to calculate the difference to get the transmitted bytes. There is a while loop inside to be sure that there is really a problem. The loop is counting the issues.

With urlopen("http://simutechno.net").getcode() the program is checking the response of the website (status code 200 means the web server is available). This is included in a try-error handler. If the page is not available, urlopen send back an error message. In this case the atack counter is set directly to 4. Because this is a very bad issue...

I think the last part of the program is mostly self explaining. There is the emergency email prepared in the case of some not normal behaviours of the server with the smtplib package.

server = smtplib.SMTP('127.0.0.1') is my email server and

server.sendmail(FROM, [TO], BODY) is the command to send the email. Nearly so easy as outlook...

Bye!