Skip to content

Instantly share code, notes, and snippets.

@SietsevanderMolen
Last active May 15, 2023 17:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SietsevanderMolen/e7f594f209dfaa3596907e427b657e30 to your computer and use it in GitHub Desktop.
Save SietsevanderMolen/e7f594f209dfaa3596907e427b657e30 to your computer and use it in GitHub Desktop.
Feeds i3bar with current Qubes system status
#!/bin/bash
# By @minad and @SietsevanderMolen
WIFI_VM="sys-net"
json() {
if [[ -n "$3" ]]; then
echo -n "{\"name\":\"$1\",\"color\":\"$3\",\"full_text\":\"$2\"},"
else
echo -n "{\"name\":\"$1\",\"full_text\":\"$2\"},"
fi
}
status_net() {
local net=$(qvm-run $WIFI_VM -p 'iwconfig; ifconfig' 2>/dev/null)
local ssid=$(echo "$net" | perl -ne 'print $1 if /ESSID:"(.*)"/')
if [[ -n $ssid ]]; then
local quality=$(echo "$net" | perl -ne 'print "$1 " if /Quality=([^ ]+)/')
json wifi "W: $quality$ssid"
fi
local ip=$(echo "$net" | perl -ne 'if (/^[w|e]/../^$/) { print $1 if /inet ([^ ]+)/ }')
[[ -n $ip ]] && json ip "I: $ip"
}
status_time() {
local time=$(date '+%F %T')
echo -n "{\"name\":\"time\",\"full_text\":\"$time\"}" # last entry
}
status_bat() {
local bat_now=$(cat /sys/class/power_supply/BAT0/energy_now 2>/dev/null)
local bat_full=$(cat /sys/class/power_supply/BAT0/energy_full_design 2>/dev/null)
if [[ -n "$bat_full" ]]; then
local bat=$((100*bat_now/bat_full))
local ac=''
local color='#00ff00'
if [[ $(cat /sys/class/power_supply/AC/online) == '1' ]]; then
ac=' AC'
elif ((bat < 25)); then
color='#ff0000'
elif ((bat < 50)); then
color='#ffff00'
fi
json bat "B: $bat%$ac" "$color"
fi
}
status_load() {
local load=$(uptime)
load=${load/#*load average: }
load=${load%,*,*}
json load "$load"
}
status_qubes() {
local qubes=$(qvm-ls 2>/dev/null | grep ' \* ' | wc -l)
json qubes "$qubes Q"
}
status_disk() {
local disk=`df -h / | tail -n 1 | awk '{print $4}'`
json disk "D: $disk"
}
main() {
echo '{"version":1}'
echo '['
echo '[]'
local n
for ((n=0; ; ++n)); do
if (( n % 10 == 0 )); then
local qubes=$(status_qubes)
local net=$(status_net)
local disk=$(status_disk)
local bat=$(status_bat)
local load=$(status_load)
fi
local time=$(status_time)
echo ",[$qubes$net$disk$bat$load$time]"
sleep 1
done
}
main
@one7two99
Copy link

one7two99 commented Oct 3, 2017

Hello, can you add some more information what the changes are for you've made at the status_net part ?
Maybe also interesting for you:
I've also switched to i3wm and added my /usr/bin/qubes-i3status to get additional battery runtime information like the current discharge rate, to monitor VM usage and battery impact:

status_bat() {
local my_bat=/sys/class/power_supply/BAT0
# calculate current battery discharge rate (hint: could be adapted for AC)
local bat_rate=$(bc <<< "scale=1;cat $my_bat/current_nowcat $my_bat/voltage_now/10^12")"W"
# calculate how empty/full your battery is
local bat=$((100
cat $my_bat/charge_now/cat $my_bat/charge_full_design))
local ac=''
local color=''
if [[ $(cat /sys/class/power_supply/AC/online) == '1' ]]; then
ac=' AC'
elif ((bat < 25)); then
color='#ff0000' # red
elif ((bat < 50)); then
color='#ff6600' # orange
fi
json bat "Bat: $bat_rate $bat%$ac" "$color"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment