Exploring the datacheck
Bash Script: Network Data Monitoring
In this article, we'll explore the purpose, structure, and functionality of a Bash script called datacheck
. This script is designed to monitor network statistics using vnstat
, a popular network traffic monitoring tool, and provides flexibility in how the data is presented.
Purpose of the Script
The primary purpose of the datacheck
script is to retrieve and display network data usage for specified network interfaces. It provides an easy way to monitor traffic over various time periods (daily, monthly, yearly, or for a custom number of days) across different network interfaces on the system.
Functions and Features
Let's break down the core functionality of the script and see how each section works.
1. Setting the Default Interface
The script begins by defining a default network interface:
default_interface="enp0s25"
This default interface is enp0s25
, which can be replaced or extended to include additional network interfaces, depending on user input.
2. Help Message Display
One of the essential parts of a command-line tool is to provide help or usage information to the user. This script includes a display_help
function that outputs the usage details:
display_help() {
echo "Usage: $0 [OPTION] [DAYS]"
echo "Options:"
echo " -m: Display monthly statistics"
echo " -d: Display daily statistics"
echo " -y: Display yearly statistics"
echo " -t <DAYS>: Display statistics for the last <DAYS> days"
echo " -a: Show data for all interfaces"
echo " -h: Display this help message"
}
The user can access this help message by running the script with the -h
option, which makes it clear what options are available and how to use the script.
3. Parsing User Input
The script uses the getopts
command to parse command-line options:
while getopts ":mdyht:a" opt; do
case ${opt} in
m ) option="-m" ;;
d ) option="-d" ;;
y ) option="-y" ;;
t ) days=$OPTARG ;;
a ) all_interfaces=true ;;
h ) display_help; exit 0 ;;
\? ) echo "Invalid option: $OPTARG"; display_help; exit 1 ;;
esac
done
This allows the script to handle different arguments, such as:
-m
: Display monthly data.-d
: Display daily data.-y
: Display yearly data.-t <DAYS>
: Display data for the last specified number of days.-a
: Show data for all interfaces.-h
: Display help information.
4. Handling All Interfaces
If the user specifies the -a
option, the script adjusts to retrieve data for all available network interfaces:
ifaces=$(ls /sys/class/net | grep -v lo)
interfaces="${ifaces[@]}"
This command lists all network interfaces in the system, excluding the lo
(loopback) interface, and assigns them to the interfaces
variable. The script will then loop through each interface, retrieving its statistics.
5. Handling Custom Time Periods
If the user specifies a custom number of days using the -t
option, the script modifies the vnstat
command accordingly:
if [ -n "$days" ]; then
option+="-t $days"
fi
This allows the script to execute a vnstat
query for the last <DAYS>
days, giving more granular control over the time period for which data is displayed.
6. Running vnstat
and Handling Output
The script loops through each specified network interface and runs the vnstat
command:
for interface in $interfaces; do
vnstat_output=$(vnstat -i "$interface" $option 2>&1)
Here, it captures the output of the vnstat
command, which retrieves traffic data for the specified interface and time period.
The script checks if the vnstat
command returns any errors:
if [[ "$vnstat_output" == *"Error"* ]]; then
no_data_interfaces+=("$interface")
else
echo "Interface: $interface"
echo "$vnstat_output"
fi
If vnstat
fails (e.g., no data available for a particular interface), the interface is added to the no_data_interfaces
array. Otherwise, it prints the output for that interface.
7. Displaying Interfaces with No Data
Finally, if there are any interfaces for which no data is available, the script informs the user:
if [ ${#no_data_interfaces[@]} -gt 0 ]; then
echo "No data available for the following interfaces: ${no_data_interfaces[@]}"
fi
This ensures the user is aware of any issues with specific interfaces.
8. Example Output for usage:*
Interface: enp0s25
Database updated: 2024-09-20 14:00:00
enp0s25 since 2024-04-15
rx: 20.52 TiB tx: 8.25 TiB total: 28.78 TiB
monthly
rx | tx | total | avg. rate
------------------------+-------------+-------------+---------------
2024-08 2.62 TiB | 1.58 TiB | 4.20 TiB | 13.80 Mbit/s
2024-09 2.09 TiB | 902.77 GiB | 2.98 TiB | 15.47 Mbit/s
------------------------+-------------+-------------+---------------
estimated 3.21 TiB | 1.35 TiB | 4.56 TiB |
daily
rx | tx | total | avg. rate
------------------------+-------------+-------------+---------------
yesterday 72.81 GiB | 35.78 GiB | 108.59 GiB | 10.80 Mbit/s
today 22.34 GiB | 14.42 GiB | 36.76 GiB | 6.27 Mbit/s
------------------------+-------------+-------------+---------------
estimated 38.30 GiB | 24.72 GiB | 63.02 GiB |
Summary
The datacheck
script is a powerful tool for monitoring network usage across different time periods and interfaces. It provides flexibility with options for displaying daily, monthly, yearly, or custom time-frame data, and can be extended to monitor all interfaces on the system. Here's a quick recap of the script's key features:
- Customizable Timeframes: Options to display daily, monthly, yearly, or custom periods of data.
- Multiple Interfaces: Ability to monitor one or all available network interfaces.
- Error Handling: Proper feedback if no data is available for a specific interface.
- User-Friendly: Includes help instructions and clear usage options.
This script serves as an efficient solution for system administrators or users needing to monitor network traffic usage quickly and easily across various interfaces.