nanyang-system-developers

Monitoring and Logging

Overview

Monitoring and logging are important for keeping systems, applications, and services running smoothly. Think of monitoring as checking the vital signs of a system, like a doctor checking a patient’s health. Logging is like keeping a diary of everything that happens in the system.

Why Monitoring and Logging are Necessary

Monitoring

Monitoring is important because:

  1. Early Detection of Issues: It helps find problems before they become big issues.
  2. Performance Optimization: You can make the system run better by analyzing performance data.
  3. Security: Monitoring can spot unusual activities that might be security threats.
  4. Compliance: Ensures systems follow rules and regulations.

Logging

Logging is also important because:

  1. Debugging: Logs help find and fix problems.
  2. Audit Trails: Logs keep a record of events for checking and compliance.
  3. Security: Logs help investigate security incidents.
  4. Performance Analysis: Logs show how the system is performing and how users are interacting with it.

Consequences of Not Implementing Monitoring and Logging

Without monitoring and logging, you might face:

Principles for Determining the Level of Monitoring and Logging Required

  1. Criticality of the System: More important systems need more monitoring and logging.
  2. User Impact: Systems with many users need detailed monitoring and logging.
  3. Regulatory Requirements: Industry rules might require specific monitoring and logging.
  4. Resource Availability: Balance monitoring and logging with available resources and budget.

Implementing Monitoring and Logging in a Python Program

Here is a simple example of how to monitor and log in a Python program using the psutil library to monitor system performance and the logging module to log events:

import psutil
import time
import logging

# Configure logging
logging.basicConfig(filename='system_monitor.log', level=logging.INFO, format='%(asctime)s - %(message)s')

def monitor_system():
    while True:
        cpu_usage = psutil.cpu_percent(interval=1)
        memory_info = psutil.virtual_memory()
        logging.info(f"CPU Usage: {cpu_usage}%")
        logging.info(f"Memory Usage: {memory_info.percent}%")
        time.sleep(5)

if __name__ == "__main__":
    monitor_system()

Implementing Monitoring and Logging in a Web Application

For a web application, you might use a monitoring tool like Prometheus along with Grafana for visualization, and the logging module for logging. Here is a basic example using Flask, Prometheus, and logging:

from flask import Flask
from prometheus_client import start_http_server, Summary
import logging

# Configure logging
logging.basicConfig(filename='webapp.log', level=logging.INFO, format='%(asctime)s - %(message)s')

app = Flask(__name__)
REQUEST_TIME = Summary('request_processing_seconds', 'Time spent processing request')

@app.route('/')
@REQUEST_TIME.time()
def hello():
    logging.info("Hello endpoint was called")
    return "Hello, World!"

if __name__ == "__main__":
    start_http_server(8000)
    app.run(port=5000)

In this example, Prometheus collects metrics on the request processing time, which can then be visualized using Grafana, while logging records each request to the “Hello” endpoint.

Monitoring and logging are essential practices that help keep systems reliable, performing well, and secure. By implementing good monitoring and logging strategies, you can manage and maintain your systems effectively.