I would like to find out the amount of bandwidth my USB webcam occupies while I'm using it. I would prefer a command line solution.
3 Answers
Use usbtop, it gives a nice overview of what devices are using how much bandwidth:
Bus ID 1 (USB bus number 1) To device From device
Device ID 1 : 0.00 kb/s 0.00 kb/s
Device ID 2 : 0.00 kb/s 0.00 kb/s
Bus ID 2 (USB bus number 2) To device From device
Device ID 1 : 0.00 kb/s 0.00 kb/s
Device ID 4 : 141.73 kb/s 13777.68 kb/s
Device ID 5 : 9.98 kb/s 11.24 kb/s
Device ID 6 : 0.00 kb/s 0.00 kb/s
Device ID 7 : 0.00 kb/s 0.00 kb/s
Device ID 8 : 141.71 kb/s 15257.26 kb/s
Install and run it like so:
$ sudo apt install git cmake g++ libboost-dev libpcap-dev libboost-thread-dev libboost-system-dev
$ git clone https://github.com/aguinet/usbtop.git
$ cd usbtop
$ cmake .
$ make
$ sudo src/usbtop
-
I installed
libboost1.62-all-dev
on my ubuntu server and the cmake step succeeds, but the make step failes with undefined references toboost::system::system_category()
for example. What else is needed to install?– feedc0deFeb 20, 2018 at 12:20 -
@danbru1211 that's strange.. it should pull in
libboost1.62-system-dev
. Which Ubuntu version are you using? (BTW I've added the dependencies to my answer)– sebasFeb 21, 2018 at 14:48
I think this can be done with wireshark.
When it is plug a USB device, in syslog it appear a message like
Feb 7 21:35:42 Computer kernel: [ 1237.639216] usb 2-1.1.4: new high-speed USB device number 8 using ehci_hcd
With this information, we know the device was plugged in bus 2 with device number 8.
The fire up wireshark
$ sudo wireshark
It will appear a list of devices... choose the one with same bus id you plug the device, in this case, "USB bus number 2" and start the capture.
In menu, choose "Statistics" then "IO Graphs".
Then in the graphs options, you can create a filter with just de device you want.
For example:
(usb.bus_id == 2) && (usb.device_address == 8)
In "X Axis" and "Y Axis" ajust the values to have a graphic with 'normal' values, for example Tick Interval: 1 sec, Unit: Bits/tick, and with that the graphic should be in Bits/sec.
-
1For this to work properly, the usbmon kernel module should be loaded first - wiki.wireshark.org/CaptureSetup/USB– guigouzJan 24, 2015 at 16:37
I've wrote a pair of shell scripts to get the throughput from a USB device. If someone what to use it, you can find it in this post.
getUsb.sh
#!/bin/bash
COUNTER=0;
# first get USB devices
IFS=$'\n'
USBDEVICES=$( lsusb | grep -v "0000:0000" | grep -iv "hub" )
CHOOSED_DEVICE=$(zenity --list --width=700 --height=500 --title "Connected USB devices" --column="Devices" ${USBDEVICES[@]})
unset IFS
echo ${CHOOSED_DEVICE}
echo ${CHOOSED_DEVICE} | cut -d: -f 1 | read
BUS=`echo ${CHOOSED_DEVICE} | cut -d: -f 1 | cut -d\ -f 2`
DEVICE=`echo ${CHOOSED_DEVICE} | cut -d: -f 1 | cut -d\ -f 4`
let BUS=$BUS+0
echo $BUS
echo $DEVICE
# create data to pipe
let totalIN=0;
let totalOUT=0;
echo "usbmon -i ${BUS} | grep "C Bo:${BUS}:${DEVICE}" ";
usbmon -i ${BUS} | grep "C B" | grep "${BUS}:${DEVICE}" | while read garb1 garb2 garb3 status garb5 value finalGarb; do
if [[ $status =~ "Bo" ]]; then
let totalIN=$totalIN+$value
echo $totalIN > /tmp/counterUsbIN
elif [[ $status =~ "Bi" ]]; then
let totalOUT=$totalOUT+$value
echo $totalOUT > /tmp/counterUsbOUT
else
echo "discarded"
continue;
fi
done
speedUsb.sh
#!/bin/bash -i
if [ $1 ]; then
SLEEP=$1;
else
SLEEP=1;
fi
PREV_VALUE_IN=`cat /tmp/counterUsbIN`
PREV_VALUE_OUT=`cat /tmp/counterUsbOUT`
LINECOUNT=$(tput lines);
while [ 1 ]; do
if [ $LINECOUNT -ge $(tput lines) ]; then
printf "%7s %7s \n" "IN" "OUT"
LINECOUNT=2;
else
(( LINECOUNT++ ))
fi
sleep $SLEEP
LAST_VALUE_IN=`cat /tmp/counterUsbIN`
LAST_VALUE_OUT=`cat /tmp/counterUsbOUT`
let VALUE_IN=${LAST_VALUE_IN}-${PREV_VALUE_IN}
let PREV_VALUE_IN=${LAST_VALUE_IN}
let VALUE_OUT=${LAST_VALUE_OUT}-${PREV_VALUE_OUT}
let PREV_VALUE_OUT=${LAST_VALUE_OUT}
mbytesSecIn=`echo "scale = 3; ${VALUE_IN}/${SLEEP}/1024/1024" | bc`
mbytesSecOut=`echo "scale = 3; ${VALUE_OUT}/${SLEEP}/1024/1024" | bc`
mbitsSecIn=`echo "scale = 3; ${VALUE_IN}*8/${SLEEP}/1024/1024" | bc`
mbitsSecOut=`echo "scale = 3; ${VALUE_OUT}*8/${SLEEP}/1024/1024" | bc`
printf "%7.3f %7.3f Mbytes/s %7.3f %7.3f Mbits/s\n" ${mbytesSecIn} ${mbytesSecOut} ${mbitsSecIn} ${mbitsSecOut}
done
-
Hi @Pipe. I tried to follow your blog but could not figure out how to build the
usbmon
userspace program from github.com/kkaempf/usbmon . It builds ausbdump
program, not ausbmon
program as your scripts are expecting. Can you clarify how you got the dependencies set up?– LucasFeb 18, 2016 at 23:19 -
Hi Lucas, those scripts where a quick hack and I never try to make it run without root account. It is not very clear in the post, but getUsb.sh must be run as root.– PipeFeb 22, 2016 at 10:39