Operations 3 min read

Bash Scripts for Real‑Time Network Traffic and Disk Usage Monitoring

This article provides two Bash scripts: one that continuously displays inbound and outbound traffic of a specified network interface, and another that remotely checks disk usage on up to 100 servers, issuing warnings when partitions exceed a defined threshold.

Practical DevOps Architecture
Practical DevOps Architecture
Practical DevOps Architecture
Bash Scripts for Real‑Time Network Traffic and Disk Usage Monitoring

This article presents two practical Bash scripts for system monitoring.

Script 1 – Real‑time network traffic monitoring

The script accepts a network interface name as an argument and continuously prints the inbound and outbound traffic rates in KB/s.

#!/bin/bash
NIC=$1
echo -e " In ------ Out"
while true; do
  OLD_IN=$(awk '$0~"'$NIC'"{print $2}' /proc/net/dev)
  OLD_OUT=$(awk '$0~"'$NIC'"{print $10}' /proc/net/dev)
  sleep 1
  NEW_IN=$(awk '$0~"'$NIC'"{print $2}' /proc/net/dev)
  NEW_OUT=$(awk '$0~"'$NIC'"{print $10}' /proc/net/dev)
  IN=$(printf "%.1f%s" "$((($NEW_IN-$OLD_IN)/1024))" "KB/s")
  OUT=$(printf "%.1f%s" "$((($NEW_OUT-$OLD_OUT)/1024))" "KB/s")
  echo "$IN $OUT"
  sleep 1
done

Script 2 – Disk usage monitoring for 100 servers

This script reads a host list file (host.info) containing IP, username, and SSH port, connects to each server, retrieves disk usage via df -h, and warns if any partition usage exceeds 80%.

#!/bin/bash
HOST_INFO=host.info
for IP in $(awk '/^[^#]/{print $1}' $HOST_INFO); do
  USER=$(awk -v ip=$IP 'ip==$1{print $2}' $HOST_INFO)
  PORT=$(awk -v ip=$IP 'ip==$1{print $3}' $HOST_INFO)
  TMP_FILE=/tmp/disk.tmp
  ssh -p $PORT $USER@$IP 'df -h' > $TMP_FILE
  USE_RATE_LIST=$(awk 'BEGIN{OFS="="}/^\/dev/{print $NF,int($5)}' $TMP_FILE)
  for USE_RATE in $USE_RATE_LIST; do
    PART_NAME=${USE_RATE%=*}
    USE_RATE=${USE_RATE#*=}
    if [ $USE_RATE -ge 80 ]; then
      echo "Warning: $PART_NAME Partition usage $USE_RATE%!"
    fi
  done
done

The article concludes with a list of related reading links for further exploration of monitoring, DevOps, and shell scripting topics.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxBashdisk
Practical DevOps Architecture
Written by

Practical DevOps Architecture

Hands‑on DevOps operations using Docker, K8s, Jenkins, and Ansible—empowering ops professionals to grow together through sharing, discussion, knowledge consolidation, and continuous improvement.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.