December 18, 2008

Follow-up on screen profiles

After reading Justin's proposal on screen profiles and Nicolas' answer on automatic screen launch, I've worked a bit on a profile for my company. I added a few things to it:
  • "hostname -f" after the release. %H provides "hostname -s" but I preferred a fqdn since we have many machines named the same.
  • load average (%l) after updates-available
  • number of users (taken from `uptime`)
  • uptime (taken from `uptime`)


with some nice colors. For thoses interested, here are the scripts :


$ cat load-average
#!/bin/sh

uptime | sed -e "s/.*load average: //" | tr -d " " | tr "," " "

$ cat nb-users
#!/bin/sh

uptime | sed -e "s/.*, *\(.* users\), .*/\1/"

$ cat release-short
#!/bin/sh

lsb_release -i -c -s | tr "\n" " "

$ cat uptime
#!/bin/sh

uptime | sed -e "s/.* up *\(.*\), *.* users, .*/\1/"



I added the following lines to the "common" profile :

backtick 105 5 5 /usr/share/screen-profiles/bin/load-average
backtick 106 10 10 /usr/share/screen-profiles/bin/nb-users
backtick 107 10 10 /usr/share/screen-profiles/bin/uptime
backtick 108 10 10 /bin/hostname -f
backtick 109 3600 3600 /usr/share/screen-profiles/bin/release-short



and this is my hardstatus line :

hardstatus string '%{+b Wr} (*) Hebex %{+b wk} %100`%{= Wk}|%{+b Gk}%108`%{= Wk}|%= |%{+b rW}%101`%{= Wk}|%{+b Ck}%l%{= Wk}|%{+b Mk}%106`%{= Wk}|%{+b bW}%107`%{= Wk}|%{+b gW}%103`%{= Wk}|%{= wk}%Y-%m-%d %c:%s'




Then I tried to connect from my Acer Aspire One and I realized that the line didn't fit on my screen (the 9" screen that is...). So I made a short profile for this purpose, by using %H instead of "hostname -f" and making a release-short function that only displays `lsb_release -i -c -s | tr "\n" " "`. I made this a new profile. Here is the hardstatus line :


hardstatus string '%{+b Wr} (*) Hebex %{+b wk}%109`%{= Wk}|%{+b Gk}%H%{= Wk} %=|%{+b rW}%101`%{= Wk}|%{+b Ck}%l%{= Wk}|%{+b Mk}%106`%{= Wk}|%{+b bW}%107`%{= Wk}|%{+b gW}%103`%{= Wk}|%{= wk}%Y-%m-%d %c:%s'



Then I wondered how I could load it automatically when I connect from my notebook. So I tweaked a bit Nicolas' lines and made a .screen_bashrc which I source in my .bashrc :



if [ "$PS1" ]; then
# Set screen-profile to short if we're on a notebook
# to normal otherwise
if [ "x${LC_NOTEBOOK}" = "xyes" ]; then
ln -sf /usr/share/screen-profiles/profiles/hebex-short.screenrc $HOME/.screenrc-profile
else
ln -sf /usr/share/screen-profiles/profiles/hebex.screenrc $HOME/.screenrc-profile
fi

if [ "$TERM" != "screen" ]; then
#screen -D -R
#Update 2008 Dec 16: -xRR is way better
screen -xRR
fi
fi



On my notebook, I added a "export LC_NOTEBOOK=yes" to my .bashrc, so whenever I connect to a machine using the notebook, the short screen profile is automatically selected instead of the normal one, and I can see all infos on my small screen :)

Note: I chose LC_NOTEBOOK because LC_* variables are usually listed in AcceptEnv in /etc/ssh/sshd_config to export language settings, so I was pretty sure that the variable would be exported fine.

2 comments:

SEJeff said...

Just because regexp work doesn't mean you should use them to solve every problem. It is overkill when you know the fields. Many people do this and it doesn't scale when you start thinking about much harder problems.

ie:

nb-users:
uptime | awk '{print $6 " users"}'

Raphaël said...

Thanks SEJeff for your comment. Actually, my first attempt used `uptime | cut -d',' -f3` which works even nicer than awk... But the reason I used sed was because I soon realized I needed it by testing on a few machines.

There is a time before your machine reaches one day of uptime, when the number of users connected is not $6 but $4.

For example, my laptop has been on for a bit more than 3 hours, and thus :

$ uptime
20:58:15 up 3:35, 1 user, load average: 0.79, 1.01, 1.06

$ uptime | cut -d"," -f3
load average: 0.91

$ uptime | awk '{print $6}'
load

Just because a solution is simpler doesn't mean it answers the problem fully.