-
-
Save eru123/a515565e4495fcd53e652b939b1fdfb4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# get script path | |
script_dir=$(dirname $(realpath $0)) | |
# script_dir=/home/jericho | |
# current month | |
# Ym=$(date +"%Y%m") | |
# DB PATH | |
# db_dir="$script_dir/mnet" | |
# mkdir -p $db_dir | |
# db_path="$db_dir/mnet-$Ym.db" | |
db_path="$script_dir/mnet.db" | |
if [ ! -f $db_path ]; then | |
echo "DB not found, creating..." | |
# create database | |
sqlite3 $db_path "CREATE TABLE IF NOT EXISTS network (id INTEGER PRIMARY KEY AUTOINCREMENT, con_if VARCHAR(255), con_in DOUBLE, con_out DOUBLE, con_ts DATETIME DEFAULT CURRENT_TIMESTAMP);" | |
sqlite3 $db_path "CREATE INDEX IF NOT EXISTS network_con_if_index ON network (con_if);" | |
echo "DB created" | |
fi | |
# Set the network interface to monitor | |
interface="eth0" | |
# Set the interval between measurements (in seconds) | |
interval=1 | |
# Loop forever | |
while true | |
# for i in $(seq 1 $max); | |
do | |
ts=$(date +"%Y-%m-%d %H:%M:%S") \ | |
&& bw=$(ifstat -i $interface -q 1 1 | tail -n 1 | awk '{print $1"-"$2}') \ | |
&& con_in=$(echo $bw | awk -F '-' '{print $1}') \ | |
&& con_out=$(echo $bw | awk -F '-' '{print $2}') \ | |
&& sqlite3 $db_path "INSERT INTO network (con_if, con_in, con_out, con_ts) VALUES ('$interface', '$con_in', '$con_out', '$ts');" \ | |
&& echo "$ts: $interface - $con_in/$con_out" & | |
sleep $interval | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment