Hone logo
Hone
Problems

Automated Metrics Collection and Reporting

This challenge focuses on building a Python script that automatically collects system metrics (CPU usage, memory usage, disk space) and generates a simple report. Automated metrics collection is crucial for monitoring system health, identifying performance bottlenecks, and proactively addressing potential issues. This exercise will help you practice data collection, processing, and basic reporting in Python.

Problem Description

You are tasked with creating a Python script that gathers system metrics and presents them in a readable format. The script should:

  1. Collect Metrics: Gather the following metrics:
    • CPU Usage: Percentage of CPU currently in use.
    • Memory Usage: Percentage of RAM currently in use.
    • Disk Space: Percentage of disk space used on the root partition ("/").
  2. Format Output: Present the collected metrics in a clear and concise format. The output should be a string containing the metric names and their corresponding values, separated by newlines.
  3. Error Handling: Gracefully handle potential errors during metric collection (e.g., if a command fails or a metric is unavailable). If an error occurs while collecting a specific metric, log an error message and continue collecting other metrics. Do not crash the entire script.
  4. Return a String: The function should return a single string containing the formatted metrics report.

Examples

Example 1:

Input: None (The script should run and collect metrics from the system)
Output:
CPU Usage: 15.2%
Memory Usage: 68.5%
Disk Space: 72.1%

Explanation: The script successfully collected and formatted the CPU, memory, and disk usage metrics. The percentages are illustrative and will vary based on the system.

Example 2:

Input: None (Simulating a scenario where disk space information is unavailable)
Output:
CPU Usage: 10.0%
Memory Usage: 75.3%
Disk Space: Error collecting disk space information.

Explanation: The script collected CPU and memory usage successfully. However, it encountered an error while collecting disk space information, logged the error, and continued to report the other metrics.

Example 3: (Edge Case - Low Memory)

Input: None (Simulating a system with very low memory)
Output:
CPU Usage: 5.1%
Memory Usage: 98.7%
Disk Space: 55.9%

Explanation: The script correctly handles a system with high memory usage and reports the percentage accurately.

Constraints

  • Operating System: The script should be compatible with Linux-based systems (using standard command-line tools). Windows compatibility is not required for this challenge.
  • Metric Accuracy: The accuracy of the collected metrics is not critical, but the values should be reasonably close to the actual system values.
  • Error Handling: The script must handle errors gracefully and continue execution even if some metrics cannot be collected.
  • Output Format: The output string must adhere to the specified format (metric name followed by a colon, value, and a newline).
  • Dependencies: You are allowed to use standard Python libraries (e.g., subprocess, psutil). Avoid external dependencies that require installation.
  • Performance: The script should collect and report metrics within a reasonable timeframe (e.g., less than 1 second).

Notes

  • You can use the subprocess module to execute shell commands and capture their output. Commands like top, free, and df are commonly used to retrieve system metrics on Linux.
  • Consider using psutil library for cross-platform metric collection. If you choose to use psutil, ensure you install it (pip install psutil).
  • The disk space metric should be calculated for the root partition ("/").
  • Focus on clarity and readability of your code. Include comments to explain your logic.
  • The error messages should be informative enough to help diagnose potential issues.
  • The percentages should be formatted to one decimal place.
  • The script should be designed to be easily extensible to collect additional metrics in the future.
Loading editor...
python