Hone logo
Hone
Problems

System Resource Monitoring with Python

Monitoring system resources like CPU usage, memory consumption, and disk I/O is crucial for maintaining application health and performance. This challenge asks you to implement a Python script that periodically collects and reports these key metrics, providing a basic foundation for system monitoring. This is useful for identifying bottlenecks, detecting anomalies, and proactively addressing potential issues.

Problem Description

You are tasked with creating a Python script that monitors system resources and logs the collected data. The script should:

  1. Collect Metrics: Gather CPU usage (as a percentage), memory usage (both total and used, in MB), and disk I/O (reads and writes, in MB) at regular intervals.
  2. Periodic Reporting: Report these metrics every interval seconds.
  3. Logging: Log the collected metrics to a file named monitoring_log.txt. Each log entry should include a timestamp and the collected metrics in a clear, readable format.
  4. Error Handling: Gracefully handle potential errors during metric collection (e.g., if a library is not installed or a system call fails). Log any errors encountered.

Key Requirements:

  • Use the psutil library for system resource monitoring. You'll need to install it: pip install psutil
  • The script should run continuously until manually stopped.
  • The logging format should be consistent and easy to parse.
  • The script should be well-structured and readable.

Expected Behavior:

The script should continuously run, collecting and logging system resource metrics at the specified interval. The monitoring_log.txt file should be updated with each log entry, containing the timestamp and the collected metrics. Error messages should be logged if any issues occur during metric collection.

Edge Cases to Consider:

  • What happens if psutil is not installed?
  • What happens if the logging file cannot be opened or written to?
  • How should you handle very high resource usage values to prevent log file bloat? (Consider formatting large numbers appropriately).
  • What happens if the interval is set to 0 or a negative number?

Examples

Example 1:

Input: interval = 5

Output: monitoring_log.txt will contain entries like:

2023-10-27 10:00:00 - CPU Usage: 15.2%, Memory Usage: Total: 8192 MB, Used: 4096 MB, Disk Reads: 1234 MB, Disk Writes: 5678 MB
2023-10-27 10:00:05 - CPU Usage: 18.7%, Memory Usage: Total: 8192 MB, Used: 4321 MB, Disk Reads: 1250 MB, Disk Writes: 5700 MB
...

Explanation: The script collects CPU, memory, and disk I/O metrics every 5 seconds and logs them to the file.

Example 2:

Input: interval = 10, psutil not installed

Output: monitoring_log.txt will contain an error message:

2023-10-27 10:00:00 - Error: psutil library not found. Please install it using 'pip install psutil'.

Explanation: The script detects that psutil is not installed and logs an error message. No further metrics are collected.

Example 3: (Edge Case)

Input: interval = -2

Output: monitoring_log.txt will contain an error message:

2023-10-27 10:00:00 - Error: Invalid interval. Interval must be a positive integer.

Explanation: The script detects an invalid interval and logs an error message.

Constraints

  • Interval: The interval value (in seconds) must be a positive integer.
  • psutil: The script must use the psutil library.
  • Logging: The log file (monitoring_log.txt) should be created in the same directory as the script.
  • Performance: The script should not consume excessive resources itself. The collection and logging process should be relatively lightweight. Avoid unnecessary loops or complex calculations.
  • Error Handling: All potential errors should be caught and logged gracefully.

Notes

  • Consider using the datetime module to format timestamps.
  • The psutil library provides various functions for collecting system resource metrics. Refer to its documentation for details: https://psutil.readthedocs.io/en/latest/
  • Think about how to structure your code to make it modular and easy to maintain. Consider creating separate functions for metric collection and logging.
  • The goal is to create a functional and robust monitoring script, not a highly optimized one. Focus on clarity and correctness.
  • Remember to handle the case where the log file already exists. You should append to it, not overwrite it.
Loading editor...
python