A simple command line tool to monitor network workload of linux server.
Main logic:
- Use command
iftop
to capture network detail each 2 seconds - Parse command output and save history data to
sqlite3
filenetwork_detail.db
.
Develop enviroment:
- golang: 1.20.6
mod:
go mod tidy
Use following command to compile binary exec.
CGO_ENABLED=1 go build -a -ldflags '-extldflags "-static"' ./network_detail.go
Run cli with:
- replace with your network device name, example
ens160
./network_detail -i ens160
For long term running, we suggest to use systemd service
Create or edit file /usr/lib/systemd/system/network-detail.service
:
- replace with your upload path, example
/data/deploy/network_detail
[Unit]
Description=Monitor network detail
After=network.target
[Service]
ExecStart=/data/deploy/network_detail/network_detail
WorkingDirectory=/data/deploy/network_detail/
Restart=always
[Install]
WantedBy=multi-user.target
Apply changes and restart/status service:
systemctl daemon-reload
systemctl restart network-detail
systemctl enable network-detail
systemctl status network-detail
Just simple sql query to analysis.
Query detail:
SELECT * FROM network_detail ORDER BY time DESC;
Query by minute:
SELECT strftime('%Y-%m-%d %H:%M', time) AS minute, sum(send_bytes) as sum_send_bytes, sum(receive_bytes) as sum_receive_bytes
FROM network_detail
GROUP BY minute
ORDER BY minute DESC;
Filter with condition:
SELECT * FROM (
SELECT strftime('%Y-%m-%d %H:%M', time) AS minute, sum(send_bytes) as sum_send_bytes, sum(receive_bytes) as sum_receive_bytes
FROM network_detail
GROUP BY minute
ORDER BY minute DESC
)
WHERE sum_send_bytes > 1000000 or sum_receive_bytes > 1000000;