SiKing

January 30, 2009

Desktop system monitor

Filed under: linux — SiKing @ 11:50 pm
Tags:

So far I have been using the canned GNOME system monitor, but I decided that I want a little more eye-candy, some more information, basically just more. Googling around a bit, I first looked at Screenlets. After spending a day with it I decided against it, because my desktop ended up way too cluttered once I got everything that I wanted on there – I am keeping it around, however, for another project that I have in mind.

Eventually I found Conky. The nicest thing – in my mind – about it is that it is old-school: the whole thing is configured from a .*rc file, and that uses bash-like syntax.

I am not much of an artist; here is a screenshot of my Conky.
Conky screenshot

I am more of a coder; here is my entire .conkyrc.

alignment bottom_left
border_margin 0
border_width 0
color1 dark slate grey
color2 steel blue
default_color slate grey
double_buffer yes
draw_borders no
draw_graph_borders no
draw_outline no
draw_shades no
font -*-helvetica-medium-r-normal-*-11-*-*-*-*-*-*-*
gap_x 20
gap_y 30
minimum_size 300
own_window yes
own_window_hints undecorated,below,sticky,skip_taskbar,skip_pager
own_window_transparent yes
own_window_type normal
pad_percents 2
short_units yes
total_run_times 0
use_xft no

TEXT
${font -*-helvetica-bold-r-normal-*-14-*-*-*-*-*-*-*}${color1}
${alignc}${pre_exec id -un}@${nodename} ${machine}
${if_existing /etc/issue}${alignc}${pre_exec sed {'s/ [@\][[:alpha:]]//g; /^[[:blank:]]*$/ d'} /etc/issue}${endif}
${alignc}${sysname} ${kernel}
${alignc}${uptime} uptime
${color}${font}${hr 2}
${color}CPU ${cpu cpu0}% ${freq}MHz ${ibm_temps 0}°C
${color}FAN ${ibm_fan}RPM
${color}MEM ${memperc}% ${color2}${membar}
${color}SWAP ${swapperc}% ${color2}${swapbar}
${color}sda2 ${fs_free_perc /}% ${color2}${fs_bar /}
${color}sda3 ${fs_free_perc /home}% ${color2}${fs_bar /home}
${color}diskIO ${diskio sda}
${color}${diskiograph sda 30 0000ff ff0000}
${hr 2}
${if_gw}${if_up eth1}
${alignc}WiFi: ${addr eth1}
${alignc}${wireless_essid eth1} ${wireless_link_qual_perc eth1}%
UP ${upspeed eth1}kB/s${alignr}DOWN ${downspeed eth1}kB/s
${upspeedgraph eth1 30,150 ffff00 00ff00}${downspeedgraph eth1 30,150 ffff00 00ff00}
${totalup eth1}${alignr}${totaldown eth1}
${else}
${alignc}Ether: ${addr eth0}
UP ${upspeed eth0}kB/s${alignr}DOWN ${downspeed eth0}kB/s
${upspeedgraph eth0 30,150 ffff00 00ff00}${downspeedgraph eth0 30,150 ffff00 00ff00}
${totalup eth0}${alignr}${totaldown eth0}
${endif}
${alignc}GW: ${gw_ip}
${else}No network
${endif}

Note the ATI symbol is from my desktop wallpaper (the X300 black). If you do not care about how things work as long as they do, then you can stop reading now.

Conky basic tutorial

Following is a discussion of how I built up my Conky config file; I am hoping it will serve as a basic tutorial for anyone who is just starting out.

First thing, I surfed the Internet to see what others have done, and started planning out what I would like to have. Then I got down into the guts of the thing. My Conky basically has three blocks: system stats, hardware stats, and network stats.

The config file has two sections: a configuration section which describes the behaviour of Conky, and a variables section which describes the output – what you will see on the screen – of Conky. The configuration section is allowed to have comments anywhere and only lines that do not start with the # character will actually be used. The variables section starts with the configuration command “TEXT“, and is read literally. This means that you cannot have any comments in there, as they will be treated as text to be displayed.

As a first step I dumped everything from the Conky man page into a new config file (yes, this is really old school 😀 ) 'man -P cat conky > .conkyrc'. Open that up in an editor (vi of course!), delete everything above the line “CONFIGURATION SETTINGS”, and everything starting from line “EXAMPLES” down. Comment out (append the # character in front of) everything else. You now have a blank configuration file that contains all the instructions.

System stats block

This block is displayed with the following variables (code?):

${font -*-helvetica-bold-r-normal-*-14-*-*-*-*-*-*-*}${color1}
${alignc}${pre_exec id -un}@${nodename} ${machine}
${if_existing /etc/issue}${alignc}${pre_exec sed {'s/ [@\][[:alpha:]]//g; /^[[:blank:]]*$/ d'} /etc/issue}${endif}
${alignc}${sysname} ${kernel}
${alignc}${uptime} uptime
${color}${font}${hr 2}

Although messing around with fonts is actually the last thing I did, I will discuss it first. The configuration command font -*-helvetica-medium-r-normal-*-11-*-*-*-*-*-*-* sets the default font. For the system stats block I wanted to use a bigger font, so on the first line of the variables section I just raised the point size and bolded the font. Every line after this will use this new font, until you get to a blank ${font} variable (on the last line of this block), which resets it back to the default. One more thing worth mentioning, is that when I ran conky from the command line, it complained that it could not load the default font. However, it did display the way that I wanted so I just ignored this error.

I also changed the colour of this block with ${color1}; this is actually a variable which is defined in the configuration section with color1 dark slate grey. The colour works similarly to the font, in that it will be in effect until a blank ${color} resets it back to default. The default is defined in the configuration section with default_color slate grey. All these predefined colour names come from your system, and they can be found in /etc/X11/rgb.txt.

Every line in this block starts with the variable ${alignc} which centres all text following it up to the end of that line only.

One the first line that actually displays something, I wanted the login name @ machine name and the machine architecture. Conky provides variables for the machine name and the machine architecture, but surprisingly not the login name. You can get the login name from the system with id -un. Conky has several different ways of running system commands. The most popular is ${exec command} which will be ran periodically, and ${pre_exec command} which will be ran only once. Since the user name does not change, running it only once is good enough.

On the next line I wanted to have the name of the distribution that I am running. Again Conky does not provide a canned way of doing this, and in fact there is no standard way of doing this in the Linux world! I would expect my solution to work on only about 1/3 of all the distros out there. The idea is that there is a file (/etc/issue) that sometimes holds the name of the distro, along with some additional characters – see 'man issue' for more information about this file. I strip of the extra information and any blank lines with the sed command, and again this needs to be executed only once ${pre_exec ...}. The whole thing is wrapped in an if statement, that tests for the existence of this file. If you need to adjust this for your particular distro – good luck. 😈

Next two lines show the name of the operating system, the kernel version, and the uptime. All these Conky provides canned variables for. The whole thing is closed with a horizontal line of 2 pixel thickness.

Hardware stats block

${color}CPU ${cpu cpu0}% ${freq}MHz ${ibm_temps 0}°C
${color}FAN ${ibm_fan}RPM
${color}MEM ${memperc}% ${color2}${membar}
${color}SWAP ${swapperc}% ${color2}${swapbar}
${color}sda2 ${fs_free_perc /}% ${color2}${fs_bar /}
${color}sda3 ${fs_free_perc /home}% ${color2}${fs_bar /home}
${color}diskIO ${diskio sda}
${color}${diskiograph sda 30 0000ff ff0000}
${hr 2}

In this block I was changing the colours quite a lot, and so I reset every single line with ${color} whether it needed it or not just to keep things legible, and changed the colour on the line as needed. Again, ${color2} is a variable that is defined in the configuration block with color2 steel blue.

The two variables ${ibm_temps 0} (temperature of the CPU) and ${ibm_fan} (speed of the fan) are architecture dependant, but Conky provides plenty of other canned variables to get this information from other architectures. You will have to consult the manpage.

The degree Celsius took only a little while. There are a lot of suggestions on the Internet, that involve all sorts of hacks, some of which work and some of which don’t and all of which are unnecessary. I just picked a plain font that already contains this symbol. Done!

The rest of the information, hopefully, is self-explanatory, except perhaps for the last graph. The ${diskiograph (device) (height),(width) (gradient colour 1) (gradient colour 2) (scale)} has a lot of options. The device is the Linux naming convention of disks (hda, hdb, sda, sdb, whatever). The height and width are both in pixels. I use only the height, and by having no width, the graph will take up the maximum available width – more on this below. The first gradient colour is what the graph will show on the right side, the second colour is what will be on the left side, and the graph will step between the two in the middle. Unfortunately, I could not get the second colour to “take” – see the screenshot above – no idea if this is a bug or what. Last is the scale of the graph; from the available documentation I could not figure out what exactly this does, so I left it blank and let Conky pick something for me.

Network stats block

This block has the most complicated logic. There are actually two enclosed if statements. The outermost is:

${if_gw}
... network stuff ...
${alignc}GW: ${gw_ip}
${else}No network
${endif}

The only way in Conky to find out if the network is actually connected to anything, is by detecting if there is a gateway, using ${if_gw}. For the moment I will skip the middle “network stuff”. At the end Conky prints, centred, the gateway IP address. If there is no gateway ${else}, then you will get the plain text “No network” at the bottom of Conky. And lastly ${endif} ends the if statement.

Now for the network stuff. As a first attempt I had something like:

${if_up eth0}
... wired network stuff ...
${endif}
${if_up eth1}
... wireless network stuff ...
${endif}

The way that the GNOME network manager works, is that in order to turn on wireless (eth1 in my case), it must turn on all networking first including the ethernet card (eth0 in my case). Using the above, when wireless was on, I would see both the wired and the wireless stuff. So I had to change the logic to:

${if_up eth1}
${alignc}WiFi: ${addr eth1}
${alignc}${wireless_essid eth1} ${wireless_link_qual_perc eth1}%
UP ${upspeed eth1}kB/s${alignr}DOWN ${downspeed eth1}kB/s
${upspeedgraph eth1 30,150 ffff00 00ff00}${downspeedgraph eth1 30,150 ffff00 00ff00}
${totalup eth1}${alignr}${totaldown eth1}
${else}
${alignc}Ether: ${addr eth0}
UP ${upspeed eth0}kB/s${alignr}DOWN ${downspeed eth0}kB/s
${upspeedgraph eth0 30,150 ffff00 00ff00}${downspeedgraph eth0 30,150 ffff00 00ff00}
${totalup eth0}${alignr}${totaldown eth0}
${endif}

Basically, I switched the two around, and combined them into just one if statement: if wireless is on ${if_up eth1}, display wireless stuff, ${else} assume wired is on; end the whole thing with ${endif}. If wired is not on, there will be no gateway, which is already handled by the previously discussed if statement.

The information displayed is almost the same in both the wired and wireless case. First centred line is the device’s IP address. For wireless I have one extra line showing the wireless SSID and the percentage of the quality of the signal, again all centred. Next I show the up speed on the left side of the window, and the down speed on the right side ${alignr} of the window. Next line shows two graphs, back to back: history of the transmission speed, upload first and download second. I force the graphs to be 30 pixels high and 150 wide each. The combined width is, therefore, 300 pixels. This is the widest item in my entire Conky display, and everything else is formated according to this … except when there is no network at all. In the case there is no network, I force the width with the configuration statement minimum_size 300. Lastly, the graphs go from green 00ff00 on the right to yellow ffff00 on the left. Again this variable has an optional scale, just like ${diskiograph ...} above, which I chose not to use for the same reasons as above. Lastly I show the total upload bytes on the left and total download bytes on the right under the graphs.

One thing that may not be clear, is that in both cases there is also the same number of blank lines to maintain the same formatting. In the case of wireless, you will get a blank line right after the ${if_up eth1}, which if you look at my code way above is on the same line as the ${if_gw}. If the wired network is on, the ${if_up eth1} will NOT produce a blank line, but the ${else} will. In both cases, each of the two ${endif} will also produce a blank line. In order to see these in your own code, put any (unique) text on any line that does not have text, and test it that way.

Starting Conky automatically

Get into your session manager (or wherever you manage automatically started programs in your distro), and start Conky with 'conky --daemonize'. This will force it to run in the background. Oddly enough, if I used the background yes configuration command, this messed up my Nautilus.

All the above stuff took me approximately one day to figure out. The following took me four! First suggestion that I can give you, is to take the documentation with a serious grain of salt. From my experimentation, the various configuration commands do not do what they say they should do.

In the Conky FAQ, there is something about turning on the double buffering in your Xorg. For me, this makes no difference whatsoever.

All the important stuff is among the double_buffer and the four own_window* configuration commands. I tried all sots of combinations, until I ended up with:

double_buffer yes
own_window yes
own_window_hints undecorated,below,sticky,skip_taskbar,skip_pager
own_window_transparent yes
own_window_type normal

However, every once in a while, with no warning or an explanation, Conky stops working! In those cases, I usually change the window type to anything else, restart Conky. Change it back restart Xorg. Repeat as necessary; after a few times, it starts behaving again. 😯

The rest of the configuration commands are just for dressing and are completely optional.

Advertisements

Create a free website or blog at WordPress.com.

%d bloggers like this: