Debian Codegraph @

contrib/monitor_igd_traffic.sh

raw symbols SCIP
1 #!/usr/bin/envscip-shell filesystem . . `/usr/bin/env`. bash
2
3 # Example script to monitor traffic count (bytes in/bytes out) on my IGD
4 # Requires:
5 # - jq (install by: sudo apt-get install jq)
6 # - async_upnp_client (install by: pip install async_pnp_client)
7
8 set -e
9
10 if [scip-shell system . . `[`. "${1}" = "" ]; then
11 echoscip-shell system . . echo. "Usage: ${0} url-to-device-description"
12 exit 1
13 fi
14
15 # we want thousands separator
16 export LC_NUMERIC=en_US.UTF-8
17
18 UPNP_DEVICE_DESC=${1}
19 UPNP_ACTION_RECEIVED=WANCIFC/GetTotalBytesReceived
20 UPNP_ACTION_SENT=WANCIFC/GetTotalBytesSent
21 JQ_QUERY_RECEIVED=.out_parameters.NewTotalBytesReceived
22 JQ_QUERY_SENT=.out_parameters.NewTotalBytesSent
23 SLEEP_TIME=1
24
25 function get_bytes_receivedscip-shell shell shell-project 0.0.0 get_bytes_received(). {
26 echoscip-shell system . . echo. $(upnp-client --device ${UPNP_DEVICE_DESC} call-action ${UPNP_ACTION_RECEIVED} | jqscip-shell system . . jq. "${JQ_QUERY_RECEIVED}")
27 }
28
29 function get_bytes_sentscip-shell shell shell-project 0.0.0 get_bytes_sent(). {
30 echoscip-shell system . . echo. $(upnp-client --device ${UPNP_DEVICE_DESC} call-action ${UPNP_ACTION_SENT} | jqscip-shell system . . jq. "${JQ_QUERY_SENT}")
31 }
32
33 # print header
34 printfscip-shell system . . printf. "%-*s %*s %*s\n" 24 "Timestamp" 16 "Received" 16 "Sent"
35
36 BYTES_RECEIVED=$(get_bytes_receivedscip-shell shell shell-project 0.0.0 get_bytes_received().)
37 BYTES_SENT=$(get_bytes_sentscip-shell shell shell-project 0.0.0 get_bytes_sent().)
38 while [scip-shell system . . `[`. true ]; do
39 sleepscip-shell system . . sleep. ${SLEEP_TIME}
40
41 PREV_BYTES_RECEIVED=${BYTES_RECEIVED}
42 PREV_BYTES_SENT=${BYTES_SENT}
43
44 BYTES_RECEIVED=$(get_bytes_receivedscip-shell shell shell-project 0.0.0 get_bytes_received().)
45 BYTES_SENT=$(get_bytes_sentscip-shell shell shell-project 0.0.0 get_bytes_sent().)
46
47 DIFF_BYTES_RECEIVED=$((${BYTES_RECEIVED} - ${PREV_BYTES_RECEIVED}))
48 DIFF_BYTES_SENT=$((${BYTES_SENT} - ${PREV_BYTES_SENT}))
49
50 DATE=$(datescip-shell system . . date. "+%Y-%m-%d %H:%M:%S")
51 printfscip-shell system . . printf. "%-*s %'*.0f %'*.0f\n" 24 "${DATE}" 16 "${DIFF_BYTES_RECEIVED}" 16 "${DIFF_BYTES_SENT}"
52 done