You are not logged in.

#1 2016-02-19 17:01:23

phonky
Member
Registered: 2008-12-19
Posts: 78

[SOLVED] i3,add network throughput 2 statusbar: how add script output

I want to monitor my network throughput.

I use i3.

In the wiki, I found this section:
https://wiki.archlinux.org/index.php/i3 … _statusbar

I already downloaded the script but haven't been able to get it's output into the bar!
I adapted the script to my needs (network interfaces).

The wiki docs say:

Now, just save the script in a suitable place (for example ~/.config/i3) and point your status program to it.

I am not sure what "point your status program to it" means here....
I want to keep the existing statusbar output and just add the network throughput info to it.

Last edited by phonky (2016-02-19 20:51:56)

Offline

#2 2016-02-19 18:05:25

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,426
Website

Re: [SOLVED] i3,add network throughput 2 statusbar: how add script output

Please paste the script, your status script and the output of `ip a`.


Moving to NC...


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#3 2016-02-19 18:12:14

phonky
Member
Registered: 2008-12-19
Posts: 78

Re: [SOLVED] i3,add network throughput 2 statusbar: how add script output

Here are (I think) everything you are requesting. Thanks for chiming in here.

ip a:

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: wlp1s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 5c:51:4f:72:6c:f3 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.60/24 brd 192.168.1.255 scope global dynamic wlp1s0
       valid_lft 549537sec preferred_lft 549537sec
    inet6 fe80::5e51:4fff:fe72:6cf3/64 scope link 
       valid_lft forever preferred_lft forever

How I run the i3bar command (standard config)

bar {
  #status_command sh -c 'i3status | i3status-netusage --interface=wlp1s0'
  status_command i3status 
  separator_symbol "::"

The script is the same script as linked from the wiki section.
I downloaded it and already adapted it to the results of `ip addr`

#!/bin/bash
# Public Domain
# (someone claimed the next lines would be useful for…
#  people. So here goes: © 2012 Stefan Breunig
#  stefan+measure-net-speed@mathphys.fsk.uni-heidelberg.de)


# path to store the old results in
path="/dev/shm/measure-net-speed"

# grabbing data for each adapter. 
# You can find the paths to your adapters using
#  find /sys/devices -name statistics
# If you have more (or less) than two adapters, simply adjust the script here
# and in the next block. 
#neth0="/sys/devices/pci0000:00/0000:00:19.0/net/eth0/statistics"
wlan0="/sys/devices/pci0000:00/0000:00:1c.0/0000:01:00.0/net/wlp1s0/statistics"
#read eth0_rx < "${eth0}/rx_bytes"
#read eth0_tx < "${eth0}/tx_bytes"
read wlan0_rx < "${wlan0}/rx_bytes"
read wlan0_tx < "${wlan0}/tx_bytes"

# get time and sum of rx/tx for combined display
time=$(date +%s)
#rx=$(( $eth0_rx + $wlan0_rx ))
#tx=$(( $eth0_tx + $wlan0_tx ))
rx=$(( $wlan0_rx ))
tx=$(( $wlan0_tx ))

# write current data if file does not exist. Do not exit, this will cause
# problems if this file is sourced instead of executed as another process.
if ! [[ -f "${path}" ]]; then
  echo "${time} ${rx} ${tx}" > "${path}"
  chmod 0666 "${path}"
fi

# read previous state and update data storage
read old < "${path}"
echo "${time} ${rx} ${tx}" > "${path}"

# parse old data and calc time passed
old=(${old//;/ })
time_diff=$(( $time - ${old[0]} ))

# sanity check: has a positive amount of time passed
if [[ "${time_diff}" -gt 0 ]]; then
  # calc bytes transferred, and their rate in byte/s
  rx_diff=$(( $rx - ${old[1]} ))
  tx_diff=$(( $tx - ${old[2]} ))
  rx_rate=$(( $rx_diff / $time_diff ))
  tx_rate=$(( $tx_diff / $time_diff ))
  # shift by 10 bytes to get KiB/s. If the value is larger than
  # 1024^2 = 1048576, then display MiB/s instead (simply cut off  
  # the last two digits of KiB/s). Since the values only give an  
  # rough estimate anyway, this improper rounding is negligible.

  # incoming
  rx_kib=$(( $rx_rate >> 10 ))
  if [[ "$rx_rate" -gt 1048576 ]]; then
    echo -n "${rx_kib:0:-3}.${rx_kib: -3:-2} M↓"
  else
    echo -n "${rx_kib} K↓"
  fi

  echo -n "  "

  # outgoing
  tx_kib=$(( $tx_rate >> 10 ))
  if [[ "$tx_rate" -gt 1048576 ]]; then
    echo -n "${tx_kib:0:-3}.${tx_kib: -3:-2} M↑"
  else
    echo -n "${tx_kib} K↑"
  fi
else
  echo -n " ? "
fi

Last edited by phonky (2016-02-19 18:15:11)

Offline

#4 2016-02-19 18:14:58

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,426
Website

Re: [SOLVED] i3,add network throughput 2 statusbar: how add script output

A couple of things: do you really put the script in /dev/shm? And you aren't caling it anywhere that I can see...


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#5 2016-02-19 18:43:13

phonky
Member
Registered: 2008-12-19
Posts: 78

Re: [SOLVED] i3,add network throughput 2 statusbar: how add script output

No!

I have my script in my scripts dir:

/home/me/run/i3netspeed.sh

Now I didn't change that path because I thought, what's the point of specifying the location of a file which should execute before looking into that path?
Maybe I  misunderstood it.


And yes, that is exactly my point, I don't know how to call it!
The wiki docs are a bit laconic, as I mentioned:

Now, just save the script in a suitable place (for example ~/.config/i3) and point your status program to it.

I understand I would need to somehow call the i3netspeed.sh script, and pass it's value to i3status.
This is exactly what I don't know how to do and where the docs are a bit short of detail smile

Offline

#6 2016-02-19 19:18:40

karlch
Member
Registered: 2015-06-14
Posts: 105

Re: [SOLVED] i3,add network throughput 2 statusbar: how add script output

Take a look at the end of "man i3status". It gives a nice example.

If you want to keep colour output, you will need to use a JSON wrapper. There are examples on the i3status github site.
I personally haven't tried any of these as I prefer using conky, but hope this helps smile

Offline

#7 2016-02-19 19:36:54

ajbibb
Member
Registered: 2012-02-12
Posts: 142

Re: [SOLVED] i3,add network throughput 2 statusbar: how add script output

I've not looked at the script you are using, but I start my home brew one this way (the status_comand line)

bar {
		font $bar_fnt
		colors {
			background	#222222
			statusline	#adbece 
			separator		#666666
			
			focused_workspace  #4c7899 #335c85 #adbece 
			active_workspace	 #333333 #5f676a #bdcbd8 
			inactive_workspace #333333 #222222 #888888 
			urgent_workspace   #2f343a #900000 #ffffff 
		}
    id                bar-1               
    status_command    ~/.config/i3status/i3_bar_1.sh
    mode              hide
    hidden_state      hide
    modifier          $mod
    position          bottom
		#tray_output       DVI-I-2
		workspace_buttons yes    
}

You should not need to worry about passing the script output to i3status.  If the script is written properly the script output is read and displayed by i3bar by using the status_command.

edit: changed i3status to i3bar in last line.

Last edited by ajbibb (2016-02-19 19:56:02)

Offline

#8 2016-02-19 20:33:55

phonky
Member
Registered: 2008-12-19
Posts: 78

Re: [SOLVED] i3,add network throughput 2 statusbar: how add script output

Hey! These are great contributions!!! Thank you so much!

Closing in, but not quite there.
I consulted the man page as suggested by @karlch, and looked at the examples in the linked github site.

I now have my own script to call i3 status, myi3status.sh:

  1 #!/bin/sh
  2   # shell script to prepend i3status with more stuff
  3 
  4 rate=""
  5 
  6 update_net_speed() {
  7     rate=`i3netspeed.sh`
  8 }
  9 
 10 i3status | while :
 11   do
 12     read line
 13     update_net_speed
 14     #taking the example from the man page produced a similar long string result:
 15     # echo "${rate} | $line" || exit 1 
 16     echo ",[{\"full_text\":\"${rate}\" },${line#,\[}" || exit 1  
 17   done
 18 

Which is called in .i3/config this way:

158 bar {
159   status_command ~/run/myi3status.sh
160   separator_symbol "::"
161 

But this produces a looong line on my status bar, it doesn't even fit in the width:

,"markup":"none","full_text":"W: (090% at Mokdad) 192.168.1.60"},{"name":"battery","instance":"/sys/class/power_supply/BAT0/uevent","markup":"none","full_text":"BAT 97.82%"},{"name":"load","markup":"none","full_text":"0.30"},{"name":"cpu_usage","markup":"none","full_text":"03%"},{"name":"time","markup":"none","full_text":"2016-02-19 15:28:45"}]

I guess it has to do with the mentioned JSON formatting?

Offline

#9 2016-02-19 20:48:26

phonky
Member
Registered: 2008-12-19
Posts: 78

Re: [SOLVED] i3,add network throughput 2 statusbar: how add script output

Never mind, I got it!!!
Many thanks to the community.

Here is the script which now runs correctly:

  1 #!/bin/sh
  2   # shell script to prepend i3status with more stuff
  3 
  4 netspeed=""
  5 
  6 update_net_speed() {
  7     netspeed=`i3netspeed.sh`
  8 }
  9 
 10 i3status | while :
 11   do
 12     read line
 13     update_net_speed
 14     net_output="[{ \"full_text\": \"${netspeed}\" },"
 15     echo "${line/[/$net_output}" || exit 1
 16   done
 17 

Offline

Board footer

Powered by FluxBB