A Shell Script to Monitor Network, Disk Usage, Uptime, Load Average and RAM


In a Linux environment, it's essential to keep track of system resources such as network usage, disk usage, uptime, load average, and RAM usage. This information helps system administrators detect potential issues and improve system performance. However, monitoring these metrics manually can be time-consuming and tedious. Therefore, in this article, we will discuss how to use a shell script to monitor these system resources.

What is a Shell Script?

A shell script is a program that runs on a command-line interface and automates repetitive tasks. It is a series of commands written in a scripting language, such as Bash, that can be executed in Linux terminal.

Creating a Shell Script

To create a shell script, we first need to create a file with a .sh extension. For example, let's create a file called "sysmon.sh" using nano editor −

nano sysmon.sh

This will open nano editor, where we can write our shell script. Once we have written our script, we can save and close editor by pressing "Ctrl + X," then "Y," and finally "Enter."

The next step is to make script executable by changing its permission with following command −

chmod +x sysmon.sh

Now that our script is executable let's move on to monitoring system resources.

Monitoring Network Usage

To monitor network usage, we can use "iftop" command-line utility. This tool displays real-time network usage by monitoring network connections and displaying bandwidth usage per connection. To use iftop in our script, we need to install it using following command −

sudo apt-get install iftop

After installing iftop, we can use it in our script to monitor network usage with following command −

iftop -t -s 2

This command displays network usage every two seconds in real-time, as shown below −

22:46:25     0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
22:46:27     0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
22:46:29     0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
22:46:31     0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
22:46:33     0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00

Monitoring Disk Usage

To monitor disk usage, we can use "df" command-line utility. This tool displays file system disk space usage by partition. To use "df" command in our script, we can use the following command −

df -h

This command displays disk usage in human-readable format, as shown below −

Filesystem      Size  Used Avail Use% Mounted on
udev            3.9G     0  3.9G   0% /dev
tmpfs           798M  1.1M  797M   1% /run
/dev/sda1        93G  9.9G   78G  12% /
tmpfs           3.9G     0  3.9G   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           3.9G     0  3.9G   0% /sys/fs/cgroup
/dev/loop1       56M   56M     0 100% /snap/core18/2128
/dev/loop2       33M   33M     0 100% /snap/snapd/14788

Monitoring Uptime

To monitor system uptime, we can use "uptime" command-line utility. This tool displays current time, system uptime, number of users, and system load average over last 1, 5, and 15 minutes. To use "uptime" command in our script, we can use following command −

uptime

This command displays system uptime and load average, as shown below −

22:56:16 up 15 days,  8:43,  1 user,  load average: 0.00, 0.01, 0.05

Monitoring Load Average

To monitor system load average, we can use "top" command-line utility. This tool displays real-time system resource usage, including CPU usage, memory usage, and system load average. To use "top" command in our script, we can use following command −

top -bn1 | grep load | awk '{printf "%.2f
", $(NF-2)}'

This command displays 1-minute load average, as shown below −

0.00

Monitoring RAM Usage

To monitor RAM usage, we can use "free" command-line utility. This tool displays total, used, and free memory available on system. To use "free" command in our script, we can use following command −

free -m | awk 'NR==2{printf "%.2f%%
", $3*100/$2 }'

This command displays percentage of used RAM, as shown below −

35.00%

Combining Commands into a Script

Now that we have commands to monitor network usage, disk usage, uptime, load average, and RAM usage, we can combine them into a shell script. Our final script will look like following −

#!/bin/bash
echo "Network Usage:"
sudo iftop -t -s 2
echo "Disk Usage:"
df -h
echo "Uptime:"
uptime
echo "Load Average:"
top -bn1 | grep load | awk '{printf "%.2f
", $(NF-2)}' echo "RAM Usage:" free -m | awk 'NR==2{printf "%.2f%%
", $3*100/$2 }'

This script first displays network usage, then disk usage, system uptime, load average, and RAM usage. To run this script, we need to navigate to the directory where we saved file and execute it with following command −

./sysmon.sh

This will run script and display system resource metrics in terminal.

Furthermore, script can be scheduled to run automatically at regular intervals using a cron job. A cron job is a scheduled task that runs at specified intervals and can be used to automate repetitive tasks. To schedule script to run every 10 minutes, we can add following entry to crontab file −

*/10 * * * * /path/to/sysmon.sh

This entry will run script every 10 minutes and display system resource metrics in terminal. We can also redirect output to a file for further analysis or send it to an email address using mail command.

Conclusion

Monitoring system resources is essential for ensuring smooth functioning of a Linux environment. In this article, we discussed how to use a shell script to monitor network usage, disk usage, uptime, load average, and RAM usage in Linux. By combining relevant commands, we can automate monitoring process and save time for system administrators.

Additionally, it's worth mentioning that this script can be further customized to suit specific monitoring needs. For example, network usage can be modified to only show traffic from a specific interface or IP address. Similarly, disk usage can be filtered to show only a specific partition or directory.

In conclusion, shell scripting is a powerful tool for monitoring system resources in Linux environments. With right combination of commands, system administrators can automate monitoring process and save time. script can be further customized and scheduled to run automatically using a cron job, making it an indispensable tool for system administrators.

Updated on: 20-Apr-2023

632 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements