Posted on
Advanced

Memory and resource monitoring through scripting

Author
  • User
    Linux Bash
    Posts by this author
    Posts by this author

A Guide to Memory and Resource Monitoring Through Bash Scripting

When managing Linux servers or desktops, monitoring system resources such as memory usage is crucial for performance tuning and troubleshooting. One of the practical ways to achieve this is through Bash scripting, which allows you to automate the monitoring tasks and get insights about your system's health. In this guide, we will explore how you can create scripts to monitor memory and other resources, as well as provide operating instructions for the most commonly used Linux package managers: apt, dnf, and zypper.

Prerequisites

Before we dive into scripting, you need to have some utilities installed on your system. Most Linux distributions come with these tools pre-installed, but it's good to check and install them if they're missing.

1. Installing Necessary Tools

For Ubuntu/Debian (apt):

Open your terminal and run the following commands:

sudo apt update
sudo apt install free vmstat sysstat

For Fedora (dnf):

In Fedora, use this set of commands:

sudo dnf makecache
sudo dnf install procps-ng sysstat

For openSUSE (zypper):

For openSUSE, the commands are slightly different:

sudo zypper refresh
sudo zypper install procps sysstat

These tools provide valuable commands like free, vmstat, and iostat, which are essential for monitoring.

Bash Scripting for Monitoring

Script to Monitor Memory Usage

Here’s a simple Bash script that uses the free command to monitor memory usage and logs it:

#!/bin/bash

LOG_DIR=/var/log/memory_monitor
mkdir -p ${LOG_DIR}

while true; do
    timestamp=$(date +"%Y-%m-%d %H:%M:%S")
    memory_usage=$(free -m | awk 'NR==2{printf "Memory Usage: %s/%sMB (%.2f%%)\n", $3,$2,$3*100/$2 }')

    echo "${timestamp} ${memory_usage}" >> ${LOG_DIR}/memory.log
    sleep 60  # Log every minute
done

Explanation:

  • This script logs the memory usage every minute.

  • Memory usage is extracted using free -m, which displays memory data in megabytes.

  • The awk command is used to format the output.

  • Logs are stored in /var/log/memory_monitor/memory.log.

Monitoring CPU and I/O Statistics

For a more comprehensive resource monitoring, including CPU and I/O statistics, consider the following snippet using iostat:

#!/bin/bash

LOG_DIR=/var/log/resource_monitor
mkdir -p ${LOG_DIR}

while true; do
    timestamp=$(date +"%Y-%m-%d %H:%M:%S")
    cpu_io_stats=$(iostat -c -d)

    echo "Timestamp: ${timestamp}" >> ${LOG_DIR}/resources.log
    echo "${cpu_io_stats}" >> ${LOG_DIR}/resources.log
    sleep 60  # Log every minute
done

Explanation:

  • iostat -c -d provides both CPU and I/O statistics.

  • Data is logged every minute to /var/log/resource_monitor/resources.log.

Conclusion

Bash scripting provides a flexible and powerful way to monitor and log system resources on Linux. By leveraging simple scripts and scheduled tasks (like cron jobs), you can keep an eye on the key performance metrics of your Linux system. Whether you're managing a server or just keeping tabs on your personal computer, these scripts can be customised and extended to fit various monitoring needs.

Remember to adjust the scripts according to what's most important for your setup, and keep your system's performance finely-tuned and under control.