Add Logging to the System Monitor

LinuxLinuxBeginner
Practice Now

Introduction

In this challenge, you will enhance the provided Linux system monitor script by adding a logging feature. The goal is to record resource usage (CPU, memory, and disk) into a log file after every monitoring cycle. This task will test your ability to modify and extend a shell script to include file-based output.

The base script is provided for you. You need to add the logging feature to it.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("Linux")) -.-> linux/BasicSystemCommandsGroup(["Basic System Commands"]) linux(("Linux")) -.-> linux/BasicFileOperationsGroup(["Basic File Operations"]) linux(("Linux")) -.-> linux/SystemInformationandMonitoringGroup(["System Information and Monitoring"]) linux/BasicSystemCommandsGroup -.-> linux/echo("Text Display") linux/BasicFileOperationsGroup -.-> linux/cat("File Concatenating") linux/SystemInformationandMonitoringGroup -.-> linux/date("Date/Time Displaying") subgraph Lab Skills linux/echo -.-> lab-299447{{"Add Logging to the System Monitor"}} linux/cat -.-> lab-299447{{"Add Logging to the System Monitor"}} linux/date -.-> lab-299447{{"Add Logging to the System Monitor"}} end

Add Logging to the System Monitor

Tasks

  • Modify the script /home/labex/project/system_monitor.sh to append the resource usage (CPU, memory, and disk) to a log file named resource_usage.log in the same directory. Each entry should include the current date and time.

Requirements

  • The log file must be named resource_usage.log and located in /home/labex/project.
  • Each log entry must include the date, time, and resource usage percentages for CPU, memory, and disk.
  • The logging must be implemented after the resource usage values are calculated in each monitoring cycle.

Hints

  1. Use the date command to get the current date and time, such as date '+%Y-%m-%d %H:%M:%S'. Explore the man date page to learn how to format the output.
  2. Remember to use the >> operator to append content to a file. Avoid overwriting the file each time.
  3. Combine the resource usage values into a single string before appending to the log file. Use variables and string interpolation for this purpose.
  4. Ensure the log file is written in the same directory as the script, using an absolute or relative path like /home/labex/project/resource_usage.log.

Examples

After completing the task, running /home/labex/project/system_monitor.sh for a few seconds should produce a resource_usage.log file with entries similar to:

cat /home/labex/project/resource_usage.log
2024-12-04 14:00:01 CPU: 15% Memory: 45% Disk: 62%
2024-12-04 14:00:03 CPU: 18% Memory: 46% Disk: 62%
2024-12-04 14:00:05 CPU: 20% Memory: 47% Disk: 62%

To exit the script gracefully, you can press Ctrl+C to send an interrupt signal.

System Monitor Log Example
โœจ Check Solution and Practice

Summary

In this challenge, you added a logging feature to the Linux system monitor script. This feature records resource usage into a log file, allowing users to track resource consumption over time. This task reinforced your understanding of file operations and date commands in Bash scripting.