Network Monitoring Tool

Monitoring System Blog Post: Building a Python-based Host Monitoring System

Note: This project is a test implementation and lacks certain security features, such as password encryption. Please do not use it for production purposes without proper security enhancements.

Monitoring the availability and performance of hosts is crucial for ensuring the stability and reliability of applications and services. In this blog post, we will explore how to build a Python-based host monitoring system using SQLite for data storage and various Python libraries for host status checks.

Introduction

As applications grow and become more complex, it becomes essential to keep an eye on the health of the hosts they rely on. Monitoring systems help us identify issues proactively and respond promptly to any downtime or performance degradation. We will build a lightweight host monitoring system that checks the status of hosts using both ICMP and HTTPS protocols.

Technologies Used

To develop this monitoring system, we will leverage the following technologies and libraries:

  • Python: A versatile programming language known for its simplicity and ease of use.

  • SQLite: A lightweight and serverless database management system that will store the host and log data.

  • ping3: A Python library used for ICMP ping checks.

  • requests: A popular library for making HTTP requests and validating HTTPS host availability.

  • Regular Expressions (Regex): To validate user inputs and hostnames.

User Registration and Input Validation

To ensure secure access and proper data handling, the monitoring system begins with user registration. User input validation is crucial to prevent erroneous data from entering the database. Using Python's re module, we implement validation for full names, email addresses, usernames, and passwords. The following code snippet showcases the input validation:

# Code snippet for User class input validation

# ...

    @staticmethod
    def is_valid_fullname(fullname):
        return re.match(r"^[A-Za-z ]{3,}$", fullname)

    @staticmethod
    def is_valid_email(email):
        return re.match(r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$", email)

    @staticmethod
    def is_valid_username(username):
        return re.match(r"^[A-Za-z0-9_]{4,}$", username)

    @staticmethod
    def is_valid_password(password):
        return re.match(r"^.{6,}$", password)

# ...

Host Management and Input Validation

Once registered, users can add and manage hosts in the monitoring system. To ensure data consistency, we validate host details such as type and hostname before inserting them into the database. For example, when adding an HTTPS host, we verify that the hostname starts with "https://" using the is_hostname_valid_https method:

# Code snippet for Hostname class input validation

# ...

    def add_host(self, user):
        if not self.is_type_valid(self.type):
            print("Check Type must be HTTPS or ICMP!!!")
            return

        if self.type == "HTTPS":
            if not self.is_hostname_valid_https(self.hostname):
                print("Hostname must start with https://")
                return

        # ... code to insert the host's details into the database ...

    @staticmethod
    def is_hostname_valid_https(hostname):
        return re.match(r"^https://", hostname)

# ...

Ping Check and HTTPS Check

The heart of our monitoring system lies in its ability to continuously check the status of hosts. We accomplish this with a background thread that performs ICMP and HTTPS checks. For ICMP hosts, we use the ping3.ping function to get the response time, while for HTTPS hosts, we employ the requests.get method to make HTTP requests. The response status code and response time are then recorded in the database:

# Code snippet for Hostname class ping and HTTPS checks

# ...

    @staticmethod
    def ping_check():
        while True:
            # ... code to retrieve hostnames from the database ...

            for id, hostname, type in zip(ids, hostnames, types):
                if type == "ICMP":
                    try:
                        response_time = ping3.ping(hostname)
                        # ... code to record response time and status in the database ...
                    except OSError as e:
                        # ... code to handle ping failure ...

                elif type == "HTTPS":
                    try:
                        response = requests.get(hostname)
                        if response.status_code == 200:
                            # ... code to record response time and status in the database ...
                        else:
                            # ... code to handle HTTP request failure ...
                    except requests.exceptions.RequestException as e:
                        # ... code to handle request exception ...

# ...

Conclusion

In this blog post, we explored the development of a Python-based host monitoring system using SQLite for data storage and various Python libraries for status checks. By leveraging user input validation, host management, and ping/HTTPS checks, we have created a lightweight yet powerful monitoring system.

Host monitoring is an essential practice to ensure the smooth operation of applications and services. By building our monitoring system in Python, we have demonstrated how to implement critical features while keeping the code concise and easy to understand.

In future iterations, additional features such as alert notifications and extended logging could be incorporated to enhance the monitoring system's capabilities. Remember, the key to effective monitoring lies in continuous improvement and adaptability to meet the changing needs of your applications.

You can find the complete source code and contribute to its development on my GitHub repository: Monitoring System GitHub Repository

Happy coding!